From fe85c6d52020d7d7d6915022b60e6105b20f8139 Mon Sep 17 00:00:00 2001 From: Danny Ben Shitrit Date: Fri, 27 Jan 2023 18:50:48 +0000 Subject: [PATCH] - Add transofrm directive, to create argv shortcuts --- examples/transform/commands.txt | 3 ++ examples/transform/runfile | 12 +++++ examples/transform/server.runfile | 3 ++ lib/runfile/concerns/dsl.rb | 12 ++++- lib/runfile/userfile.rb | 45 ++++++++++++------- lib/runfile/views/userfile.gtx | 9 ++++ spec/approvals/initiator/examples | 1 + spec/approvals/integration/transform/run | 4 ++ .../integration/transform/run --help | 19 ++++++++ spec/approvals/integration/transform/run s | 1 + .../approvals/integration/transform/run sayhi | 1 + 11 files changed, 91 insertions(+), 19 deletions(-) create mode 100644 examples/transform/commands.txt create mode 100644 examples/transform/runfile create mode 100644 examples/transform/server.runfile create mode 100644 spec/approvals/integration/transform/run create mode 100644 spec/approvals/integration/transform/run --help create mode 100644 spec/approvals/integration/transform/run s create mode 100644 spec/approvals/integration/transform/run sayhi diff --git a/examples/transform/commands.txt b/examples/transform/commands.txt new file mode 100644 index 0000000..9f60011 --- /dev/null +++ b/examples/transform/commands.txt @@ -0,0 +1,3 @@ +--help +sayhi +s \ No newline at end of file diff --git a/examples/transform/runfile b/examples/transform/runfile new file mode 100644 index 0000000..75611e1 --- /dev/null +++ b/examples/transform/runfile @@ -0,0 +1,12 @@ +import 'server' + +# Create command shortcuts +transform 'sayhi', 'hello "new runfile user"' +transform 's', 'server start' + +usage 'hello [NAME]' +help 'Say hello' +action 'hello' do |args| + name = args['NAME'] || 'You...' + say "Hello g`#{name}`" +end diff --git a/examples/transform/server.runfile b/examples/transform/server.runfile new file mode 100644 index 0000000..2f52ddb --- /dev/null +++ b/examples/transform/server.runfile @@ -0,0 +1,3 @@ +action :start do + puts 'server is starting...' +end \ No newline at end of file diff --git a/lib/runfile/concerns/dsl.rb b/lib/runfile/concerns/dsl.rb index ae27a55..eef97b8 100644 --- a/lib/runfile/concerns/dsl.rb +++ b/lib/runfile/concerns/dsl.rb @@ -56,6 +56,10 @@ def required_contexts @required_contexts ||= {} end + def transform(from, to) + transforms[from] = to + end + def shortcut(name) current_action.shortcut = name.to_s end @@ -106,12 +110,16 @@ def helper_blocks @helper_blocks ||= [] end + def imports + @imports ||= {} + end + def options @options ||= {} end - def imports - @imports ||= {} + def transforms + @transforms ||= {} end private diff --git a/lib/runfile/userfile.rb b/lib/runfile/userfile.rb index 793fc8d..07c2545 100644 --- a/lib/runfile/userfile.rb +++ b/lib/runfile/userfile.rb @@ -24,6 +24,10 @@ def code @code ||= File.read(path) end + def commands + actions.values.select(&:help) + end + def eval_code return if evaluated? @@ -39,6 +43,18 @@ def full_name id.join ' ' end + def guests + @guests ||= begin + result = imports.map do |glob, context| + Dir.glob("#{glob}.runfile").sort.map do |guest_path| + Userfile.new guest_path, context: context, host: self + end + end + + result.flatten + end + end + def id if host (host.id + [name]).compact @@ -61,7 +77,10 @@ def rootfile? def run(argv = []) eval_code + argv = transform_argv argv if argv.any? + found_guest = find_guest argv + if found_guest found_guest.run argv else @@ -69,24 +88,12 @@ def run(argv = []) end end - def commands - actions.values.select(&:help) - end - - def guests - @guests ||= begin - result = imports.map do |glob, context| - Dir.glob("#{glob}.runfile").sort.map do |guest_path| - Userfile.new guest_path, context: context, host: self - end - end + private - result.flatten - end + def docopt + @docopt ||= render 'userfile', context: self end - private - def find_action(args) acts = actions.values acts.find { |a| args[a.name] } || @@ -121,8 +128,12 @@ def run_local(argv) exit_code if exit_code.is_a? Integer end - def docopt - @docopt ||= render 'userfile', context: self + def transform_argv(argv) + transforms.each do |from, to| + return Shellwords.split(to) + argv[1..] if from == argv[0] + end + + argv end end end diff --git a/lib/runfile/views/userfile.gtx b/lib/runfile/views/userfile.gtx index b2a0ec9..10709c2 100644 --- a/lib/runfile/views/userfile.gtx +++ b/lib/runfile/views/userfile.gtx @@ -82,6 +82,15 @@ if env_vars.any? end end +if transforms.any? + > Shortcuts: + maxlen = transforms.keys.map(&:size).max + transforms.each do |from, to| + = " nb`#{from.ljust maxlen}` #{to}" + end + > +end + if examples.any? > Examples: examples.each do |text| diff --git a/spec/approvals/initiator/examples b/spec/approvals/initiator/examples index 6eea87c..9c7a26a 100644 --- a/spec/approvals/initiator/examples +++ b/spec/approvals/initiator/examples @@ -13,6 +13,7 @@ nesting require-context shortcut + transform Run run example EXAMPLE with any of these examples to copy the files to the current directory. diff --git a/spec/approvals/integration/transform/run b/spec/approvals/integration/transform/run new file mode 100644 index 0000000..3ab951d --- /dev/null +++ b/spec/approvals/integration/transform/run @@ -0,0 +1,4 @@ +Usage: + run hello [NAME] + run server + run [COMMAND] (--help | -h) \ No newline at end of file diff --git a/spec/approvals/integration/transform/run --help b/spec/approvals/integration/transform/run --help new file mode 100644 index 0000000..0e62d6d --- /dev/null +++ b/spec/approvals/integration/transform/run --help @@ -0,0 +1,19 @@ +Usage: + run hello [NAME] + run server + run [COMMAND] (--help | -h) + +Commands: + nb`hello` + Say hello + + nb`server` + Run nu`run server --help` for more information + +Options: + --help, -h + Show this message + +Shortcuts: + nb`sayhi` hello "new runfile user" + nb`s ` server start \ No newline at end of file diff --git a/spec/approvals/integration/transform/run s b/spec/approvals/integration/transform/run s new file mode 100644 index 0000000..035a184 --- /dev/null +++ b/spec/approvals/integration/transform/run s @@ -0,0 +1 @@ +server is starting... diff --git a/spec/approvals/integration/transform/run sayhi b/spec/approvals/integration/transform/run sayhi new file mode 100644 index 0000000..e5107f7 --- /dev/null +++ b/spec/approvals/integration/transform/run sayhi @@ -0,0 +1 @@ +Hello new runfile user