aboutsummaryrefslogtreecommitdiff
path: root/tools/replace.nom
diff options
context:
space:
mode:
Diffstat (limited to 'tools/replace.nom')
-rwxr-xr-xtools/replace.nom55
1 files changed, 27 insertions, 28 deletions
diff --git a/tools/replace.nom b/tools/replace.nom
index 1ec8ecc..66ca690 100755
--- a/tools/replace.nom
+++ b/tools/replace.nom
@@ -9,36 +9,35 @@ use "lib/os.nom"
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-%args = (command line args)
-%inplace = (no)
-if (%args.1 is "-i"):
- %inplace = (yes)
- %args::remove index 1
+barf "Deprecated."
-if ((size of %args) < 3):
+if ((size of %args.extra_args) < 3):
say "Usage: nomsu tools/replace.nom [-i] tree_to_replace replacement files..."
lua> "os.exit(1)"
-%pattern = (parse (%args::remove index 1))
-%replacement = (parse (%args::remove index 1))
-for %path in %args:
- for %filename in (files for %path):
- unless (any [%filename::matches "%.nom$", %filename == "-", %filename == "stdin"]):
- do next %filename
- %tree = (parse (read file %filename) from %filename)
- %tree2 = (%tree with %pattern ~> %replacement)
- if (%tree2 == %tree):
- say "No changes in \%filename"
- do next %filename
-
- %text = "\
- ..#!/usr/bin/env nomsu -V\(%tree.version or (Nomsu version))
- \(%tree2 as nomsu)"
+%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
- when:
- %inplace:
- say "Replaced in \%filename"
- write %text to file %filename
-
- else:
- say %text
+ else:
+ say %text