Dropped js replace tool.

This commit is contained in:
Brad Nelson
2023-12-22 20:46:23 -08:00
parent 3c0f249b71
commit c475681b0e
12 changed files with 105 additions and 205 deletions

View File

@ -97,7 +97,7 @@ rule mkdir
rule importation
description = importation
depfile = $out.dd
command = ../tools/importation.py $in $out -I . -I .. --name $name --header $header_mode --depsout $out.dd --set-version $version --set-revision $revision
command = ../tools/importation.py -i $in -o $out -I . -I .. --name $name --header $header_mode --depsout $out.dd -DVERSION=$version -DREVSION=$revision
build gen: mkdir

View File

@ -7,11 +7,10 @@ import sys
parser = argparse.ArgumentParser(
prog='importation',
description='Imports header / fs files')
parser.add_argument('input')
parser.add_argument('output')
parser.add_argument('-i', required=True)
parser.add_argument('-o', required=True)
parser.add_argument('-I', action='append')
parser.add_argument('--set-version')
parser.add_argument('--set-revision')
parser.add_argument('-D', action='append')
parser.add_argument('--depsout')
parser.add_argument('--no-out', action='store_true')
parser.add_argument('--keep-first-comment', action='store_true')
@ -19,6 +18,7 @@ parser.add_argument('--name')
parser.add_argument('--header')
args = parser.parse_args()
bases = args.I or []
replacements = args.D or []
results = []
imported = set([__file__])
@ -36,6 +36,7 @@ def Import(filename):
sfilename = os.path.join(os.path.dirname(filename), sfilename)
Import(sfilename)
elif (filename.endswith('.h') or
filename.endswith('.html') or
filename.endswith('.ino') or
filename.endswith('.cc') or
filename.endswith('.cpp') or
@ -54,14 +55,13 @@ def Import(filename):
results.append(line)
def Process():
Import(args.input)
Import(args.i)
# Conversion version tags.
output = []
for line in results:
if args.set_version:
line = line.replace('{{VERSION}}', args.set_version)
if args.set_revision:
line = line.replace('{{REVISION}}', args.set_revision)
for r in replacements:
name, value = r.split('=', 1)
line = line.replace('{{' + name + 'VERSION}}', value)
output.append(line)
# Drop comments.
comment1 = False
@ -85,11 +85,11 @@ def Process():
# Emit deps.
if args.depsout:
with open(args.depsout, 'w') as fh:
fh.write(args.output + ': ' +
fh.write(args.o + ': ' +
' '.join([os.path.relpath(i) for i in imported]) + '\n')
# Emit expanded file.
if not args.no_out:
with open(args.output, 'w') as fh:
with open(args.o, 'w') as fh:
if args.header == 'web':
fh.write('const ' + args.name + ' = `\n' +
'\n'.join(output) + '\n`;\n')

View File

@ -1,69 +0,0 @@
#! /usr/bin/env nodejs
// Copyright 2021 Bradley D. Nelson
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
var fs = require('fs');
function DropCopyright(source) {
var lines = source.split('\n');
var cleaned = [];
for (var i = 0; i < lines.length; ++i) {
if (lines[i] == '<!--') {
while (lines[i] != '-->') {
++i;
}
++i;
}
if (lines[i].search('Copyright') >= 0) {
while (lines[i] != '') {
++i;
}
} else {
cleaned.push(lines[i]);
}
}
return cleaned.join('\n').trim();
}
var source = fs.readFileSync(process.stdin.fd).toString();
var replacements = [];
for (var i = 2; i < process.argv.length; ++i) {
var item = process.argv[i];
var m = item.match(/^([^=]+)=@(.+)$/);
if (m) {
var content = DropCopyright(fs.readFileSync(m[2]).toString());
replacements.push(['{{' + m[1] + '}}', content]);
continue;
}
var m = item.match(/^([^=]+)=(.+)$/);
if (m) {
replacements.push(['{{' + m[1] + '}}', m[2]]);
continue;
}
throw 'Bad replacement ' + item;
}
for (;;) {
var old_source = source;
for (var i = 0; i < replacements.length; ++i) {
source = source.replace(
replacements[i][0],
function() { return replacements[i][1]});
}
if (old_source == source) {
break;
}
}
process.stdout.write(source);