aboutsummaryrefslogtreecommitdiff
path: root/tools
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
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')
-rwxr-xr-xtools/autoformat.nom32
-rwxr-xr-xtools/find_action.nom60
-rwxr-xr-xtools/parse.nom19
-rwxr-xr-xtools/replace.nom55
-rwxr-xr-xtools/test.nom24
-rwxr-xr-xtools/upgrade.nom74
6 files changed, 107 insertions, 157 deletions
diff --git a/tools/autoformat.nom b/tools/autoformat.nom
index 96b732b..5661145 100755
--- a/tools/autoformat.nom
+++ b/tools/autoformat.nom
@@ -10,23 +10,17 @@ use "lib/os.nom"
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
%args = (command line args)
-%inplace = (no)
-if (%args.1 is "-i"):
- %inplace = (yes)
- %args::remove index 1
-
-for %path in %args:
- if (%path == "-"):
- %path = "stdin"
+for %filename in %args.extra_args:
+ %file = (read file %filename)
+ unless %file:
+ barf "File does not exist: \%filename"
+ %leading_indent = (%file::matching "[\n]*([ ]*)")
+ %code = (NomsuCode from (%Source %filename 1 (size of %file)) %file)
+ %tree = (%code parsed)
+ %formatted = "\
+ ..\%leading_indent\(((%tree as nomsu)::text)::with "\n" -> "\n\%leading_indent")"
- for %filename in (files for %path):
- unless ((%filename::matches "%.nom$") or (%filename == "stdin")):
- do next %filename
- %contents = (read file %filename)
- %code = (NomsuCode from (Source %filename 1 (size of %contents)) %contents)
- %tree = (%code parsed)
- %formatted = ((%tree as nomsu)::text)
- if %inplace:
- write %formatted to file %filename
- ..else:
- say %formatted inline
+ if %args."-i":
+ write %formatted to file %filename
+ ..else:
+ say %formatted inline
diff --git a/tools/find_action.nom b/tools/find_action.nom
index c6cbe36..6b85ad3 100755
--- a/tools/find_action.nom
+++ b/tools/find_action.nom
@@ -9,37 +9,33 @@ use "lib/consolecolor.nom"
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-%stub = (command line args).1
+%stub = (command line args).extra_args.1
say "Looking for stub: \%stub..."
-for % in 2 to (size of (command line args)):
- for %filename in (files for (command line args).%):
- if (%filename == "-"):
- %filename = "stdin"
-
- unless ((%filename == "stdin") or (%filename::matches "%.nom$")):
- do next %filename
-
- %file = (read file %filename)
- %code = (NomsuCode from (%Source %filename 1 (size of %file)) %file)
- try:
- %tree = (%code parsed)
- ..and if it barfs %msg:
- say (red "\%filename failed to parse:\n\%msg")
- %tree = (nil)
-
- unless %tree:
- do next %filename
+for % in 2 to (size of (command line args).extra_args):
+ %filename = (command line args).extra_args.%
+ %file = (read file %filename)
+ unless %file:
+ barf "File does not exist: \%filename"
+ %code = (NomsuCode from (%Source %filename 1 (size of %file)) %file)
+ try:
+ %tree = (%code parsed)
+ ..and if it barfs %msg:
+ say (red "\%filename failed to parse:\n\%msg")
+ %tree = (nil)
+
+ unless %tree:
+ do next %filename
+
+ %results = []
+ for %t in recursive %tree:
+ if ((%t is "Action" syntax tree) and (%t.stub is %stub)):
+ %line_num = (%file::line number at %t.source.start)
+ %results::add {..}
+ line: %line_num, text: "\(blue "\%filename:\%line_num:")\n\(yellow (source lines of %t))"
- %results = []
- for %t in recursive %tree:
- if ((%t is "Action" syntax tree) and (%t.stub is %stub)):
- %line_num = (%file::line number at %t.source.start)
- %results::add {..}
- line: %line_num, text: "\(blue "\%filename:\%line_num:")\n\(yellow (source lines of %t))"
-
- if (%t is syntax tree):
- for %sub in %t:
- recurse %t on %sub
- sort %results by % -> %.line
- for % in %results:
- say %.text
+ if (%t is syntax tree):
+ for %sub in %t:
+ recurse %t on %sub
+ sort %results by % -> %.line
+ for % in %results:
+ say %.text
diff --git a/tools/parse.nom b/tools/parse.nom
index 9e43c88..cb367ab 100755
--- a/tools/parse.nom
+++ b/tools/parse.nom
@@ -35,15 +35,10 @@ externally (print tree %t at indent %indent) means:
else:
say "\%indent \(quote %arg)"
-for %path in (command line args):
- for %filename in (files for %path):
- if (%filename == "-"):
- %filename = "stdin"
-
- unless ((%filename == "stdin") or (%filename::matches "%.nom$")):
- do next %filename
-
- %text = (read file %filename)
- %nomsu = (NomsuCode from (Source %filename 1 (size of %text)) %text)
- %tree = (%nomsu parsed)
- print tree %tree at indent ""
+for %filename in (command line args).extra_args:
+ %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)
+ print tree %tree at indent ""
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
diff --git a/tools/test.nom b/tools/test.nom
index bdbec54..52e548f 100755
--- a/tools/test.nom
+++ b/tools/test.nom
@@ -8,24 +8,10 @@ use "lib/consolecolor.nom"
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-%args = (command line args)
-if (%args.1 == "-v"):
- %args::remove index 1
- %verbose = (yes)
-
-%to_run = [..]
- :
- for %path in (command line args):
- for %filename in (files for %path):
- if (%filename == "-"):
- %filename = "stdin"
- if ((%filename::matches "%.nom$") or (%filename == "stdin")): add %filename
-
# Make sure all the files get run
-for %filename in %to_run:
- use %filename
+for %filename in (command line args).extra_args: use %filename
%tests = {: for %s = %t in (tests): add (=lua "Source:from_string(\%s)") = %t}
-for %filename in %to_run:
+for %filename in (command line args).extra_args:
%file_tests = []
for %src = %test in %tests:
if (%src.filename == %filename):
@@ -34,13 +20,13 @@ for %filename in %to_run:
unless (%file_tests is empty):
sort %file_tests by % -> %.source
lua> "io.write('[ .. ] ', \%filename); io.flush()"
- if %verbose: say ""
+ if (command line args)."-v": say ""
for % in %file_tests:
- if %verbose:
+ if (command line args)."-v":
say " \(yellow (%.test::with "\n" -> "\n "))"
run %.test
- if %verbose:
+ if (command line args)."-v":
say (green "PASS")
..else:
say "\r[\(green "PASS")"
diff --git a/tools/upgrade.nom b/tools/upgrade.nom
index 56d67a5..781e71b 100755
--- a/tools/upgrade.nom
+++ b/tools/upgrade.nom
@@ -7,56 +7,36 @@
use "compatibility"
use "lib/os.nom"
+use "lib/consolecolor.nom"
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
%args = (command line args)
-%inplace = (no)
-%start_version = (nil)
-%version = (Nomsu version)
-repeat:
- if %args.1 is:
- "-i":
- %inplace = (yes)
- %args::remove index 1
+%inplace = (%args."-i" or %args."--inplace")
+%start_version = %args."--upgrade-from"
+%version = (%args."--upgrade-to" or (Nomsu version))
+%test = (%args."-t" or %args."--test")
+for %filename in %args.extra_args:
+ %file = (read file %filename)
+ unless %file:
+ barf "File does not exist: \%filename"
+ %leading_indent = (%file::matching "[\n]*([ ]*)")
+ %code = (NomsuCode from (Source %filename 1 (size of %file)) %file)
+ %tree = (%code parsed)
+ %uptree = (..)
+ %tree upgraded from (%start_version or (%tree.version or (Nomsu version))) to \
+ ..%version
+ %text = "\%leading_indent\(((%uptree as nomsu)::text)::with "\n" -> "\n\%leading_indent")"
+ when:
+ %inplace:
+ say "Upgraded \%filename"
+ write %text to file %filename
- "-t":
- use "lib/consolecolor.nom"
- %test = (yes)
- %args::remove index 1
+ %test:
+ if (%uptree == %tree):
+ say (dim "\%filename will not be changed")
+ ..else:
+ say (bright "\%filename will be changed")
- "-V":
- %version = %args.2
- %args::remove index 1
- %args::remove index 1
-
- "-S":
- %start_version = %args.2
- %args::remove index 1
- %args::remove index 1
-
- else: stop
-
-for %path in %args:
- for %filename in (files for %path):
- unless (%filename::matches "%.nom$"): do next %filename
- %file = (read file %filename)
- %code = (NomsuCode from (%Source %filename 1 (size of %file)) %file)
- %tree = (%code parsed)
- %uptree = (..)
- %tree upgraded from (%start_version or (%tree.version or (Nomsu version))) to \
- ..%version
- %text = ((%uptree as nomsu)::text)
- when:
- %inplace:
- say "Upgraded \%filename"
- write %text to file %filename
-
- %test:
- if (%uptree == %tree):
- say (dim "\%filename will not be changed")
- ..else:
- say (bright "\%filename will be changed")
-
- else:
- say %text
+ else:
+ say %text inline