nomsu/tools/replace.nom

44 lines
1.4 KiB
Plaintext
Raw Normal View History

2018-11-19 17:37:37 -08:00
#!/usr/bin/env nomsu -V4.11.12.8
2018-08-29 15:12:01 -07:00
#
Tool to find and replace one tree with another.
2018-10-31 15:05:17 -07:00
nomsu tools/replace.nom [-i] tree_to_replace replacement file1 file2 directory1 ...
2018-08-29 15:12:01 -07:00
If "-i" is the first argument, replacements will be performed in-place. Otherwise, the
upgraded code will be printed.
2018-08-29 15:12:01 -07:00
use "lib/os.nom"
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
barf "Deprecated."
2018-08-29 15:12:01 -07:00
if ((size of %args.extra_args) < 3):
2018-08-29 15:12:01 -07:00
say "Usage: nomsu tools/replace.nom [-i] tree_to_replace replacement files..."
lua> "os.exit(1)"
%pattern = ((%args.extra_args.1) parsed)
%replacement = ((%args.extra_args.2) parsed)
for %filename in %args.extra_args at %i:
if (%i < 3): do next %i
%file = (read file %filename)
unless %file: barf "File does not exist: \%filename"
%nomsu = (NomsuCode from (Source %filename 1 (size of %file)) %file)
%tree = (%nomsu parsed)
# TODO: fix this to use variable substitution
%tree2 = (..)
%tree::map (..)
for %subtree:
if (%subtree == %pattern):
return %replacement
if (%tree2 == %tree):
say "No changes in \%filename"
do next %filename
%text = ((%tree2 as nomsu)::text)
when:
%args."-i":
say "Replaced in \%filename"
write %text to file %filename
2018-08-29 15:12:01 -07:00
else:
say %text