Skip to content

Commit

Permalink
- Add transofrm directive, to create argv shortcuts
Browse files Browse the repository at this point in the history
  • Loading branch information
DannyBen committed Jan 27, 2023
1 parent 71be640 commit fe85c6d
Show file tree
Hide file tree
Showing 11 changed files with 91 additions and 19 deletions.
3 changes: 3 additions & 0 deletions examples/transform/commands.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
--help
sayhi
s
12 changes: 12 additions & 0 deletions examples/transform/runfile
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions examples/transform/server.runfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
action :start do
puts 'server is starting...'
end
12 changes: 10 additions & 2 deletions lib/runfile/concerns/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
45 changes: 28 additions & 17 deletions lib/runfile/userfile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ def code
@code ||= File.read(path)
end

def commands
actions.values.select(&:help)
end

def eval_code
return if evaluated?

Expand All @@ -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
Expand All @@ -61,32 +77,23 @@ 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
run_local 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] } ||
Expand Down Expand Up @@ -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
9 changes: 9 additions & 0 deletions lib/runfile/views/userfile.gtx
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down
1 change: 1 addition & 0 deletions spec/approvals/initiator/examples
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 4 additions & 0 deletions spec/approvals/integration/transform/run
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Usage:
run hello [NAME]
run server
run [COMMAND] (--help | -h)
19 changes: 19 additions & 0 deletions spec/approvals/integration/transform/run --help
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions spec/approvals/integration/transform/run s
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
server is starting...
1 change: 1 addition & 0 deletions spec/approvals/integration/transform/run sayhi
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello new runfile user

0 comments on commit fe85c6d

Please sign in to comment.