aboutsummaryrefslogtreecommitdiff
path: root/tools/replace.nom
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2018-11-20 14:52:59 -0800
committerBruce Hill <bruce@bruce-hill.com>2018-11-20 14:54:40 -0800
commit2bbc035f5dcc3ecd62724b9d1de0e7e3ea902379 (patch)
tree34a83497f7570946b6252183b3e8fe0ce2010595 /tools/replace.nom
parentf30413853063483147d941ffccc4b663b71bc943 (diff)
Simplifying the filesystem code (no longer entangled with nomsupath) and
using that to simplify the tools. Now the tools directly take lists of file paths rather than things that might go through nomsupath or directories or get processed by filetype. Use your shell for globbing stuff like `nomsu tools/test.nom core/*.nom`
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