Skip to content

Commit fe85c6d

Browse files
committed
- Add transofrm directive, to create argv shortcuts
1 parent 71be640 commit fe85c6d

File tree

11 files changed

+91
-19
lines changed

11 files changed

+91
-19
lines changed

examples/transform/commands.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
--help
2+
sayhi
3+
s

examples/transform/runfile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import 'server'
2+
3+
# Create command shortcuts
4+
transform 'sayhi', 'hello "new runfile user"'
5+
transform 's', 'server start'
6+
7+
usage 'hello [NAME]'
8+
help 'Say hello'
9+
action 'hello' do |args|
10+
name = args['NAME'] || 'You...'
11+
say "Hello g`#{name}`"
12+
end

examples/transform/server.runfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
action :start do
2+
puts 'server is starting...'
3+
end

lib/runfile/concerns/dsl.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ def required_contexts
5656
@required_contexts ||= {}
5757
end
5858

59+
def transform(from, to)
60+
transforms[from] = to
61+
end
62+
5963
def shortcut(name)
6064
current_action.shortcut = name.to_s
6165
end
@@ -106,12 +110,16 @@ def helper_blocks
106110
@helper_blocks ||= []
107111
end
108112

113+
def imports
114+
@imports ||= {}
115+
end
116+
109117
def options
110118
@options ||= {}
111119
end
112120

113-
def imports
114-
@imports ||= {}
121+
def transforms
122+
@transforms ||= {}
115123
end
116124

117125
private

lib/runfile/userfile.rb

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ def code
2424
@code ||= File.read(path)
2525
end
2626

27+
def commands
28+
actions.values.select(&:help)
29+
end
30+
2731
def eval_code
2832
return if evaluated?
2933

@@ -39,6 +43,18 @@ def full_name
3943
id.join ' '
4044
end
4145

46+
def guests
47+
@guests ||= begin
48+
result = imports.map do |glob, context|
49+
Dir.glob("#{glob}.runfile").sort.map do |guest_path|
50+
Userfile.new guest_path, context: context, host: self
51+
end
52+
end
53+
54+
result.flatten
55+
end
56+
end
57+
4258
def id
4359
if host
4460
(host.id + [name]).compact
@@ -61,32 +77,23 @@ def rootfile?
6177

6278
def run(argv = [])
6379
eval_code
80+
argv = transform_argv argv if argv.any?
81+
6482
found_guest = find_guest argv
83+
6584
if found_guest
6685
found_guest.run argv
6786
else
6887
run_local argv
6988
end
7089
end
7190

72-
def commands
73-
actions.values.select(&:help)
74-
end
75-
76-
def guests
77-
@guests ||= begin
78-
result = imports.map do |glob, context|
79-
Dir.glob("#{glob}.runfile").sort.map do |guest_path|
80-
Userfile.new guest_path, context: context, host: self
81-
end
82-
end
91+
private
8392

84-
result.flatten
85-
end
93+
def docopt
94+
@docopt ||= render 'userfile', context: self
8695
end
8796

88-
private
89-
9097
def find_action(args)
9198
acts = actions.values
9299
acts.find { |a| args[a.name] } ||
@@ -121,8 +128,12 @@ def run_local(argv)
121128
exit_code if exit_code.is_a? Integer
122129
end
123130

124-
def docopt
125-
@docopt ||= render 'userfile', context: self
131+
def transform_argv(argv)
132+
transforms.each do |from, to|
133+
return Shellwords.split(to) + argv[1..] if from == argv[0]
134+
end
135+
136+
argv
126137
end
127138
end
128139
end

lib/runfile/views/userfile.gtx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,15 @@ if env_vars.any?
8282
end
8383
end
8484

85+
if transforms.any?
86+
> Shortcuts:
87+
maxlen = transforms.keys.map(&:size).max
88+
transforms.each do |from, to|
89+
= " nb`#{from.ljust maxlen}` #{to}"
90+
end
91+
>
92+
end
93+
8594
if examples.any?
8695
> Examples:
8796
examples.each do |text|

spec/approvals/initiator/examples

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
nesting
1414
require-context
1515
shortcut
16+
transform
1617

1718
Run run example EXAMPLE with any of these examples to copy the files
1819
to the current directory.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Usage:
2+
run hello [NAME]
3+
run server
4+
run [COMMAND] (--help | -h)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Usage:
2+
run hello [NAME]
3+
run server
4+
run [COMMAND] (--help | -h)
5+
6+
Commands:
7+
nb`hello`
8+
Say hello
9+
10+
nb`server`
11+
Run nu`run server --help` for more information
12+
13+
Options:
14+
--help, -h
15+
Show this message
16+
17+
Shortcuts:
18+
nb`sayhi` hello "new runfile user"
19+
nb`s ` server start
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
server is starting...
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello new runfile user

0 commit comments

Comments
 (0)