mysh 0.2.7 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +407 -87
- data/lib/mysh.rb +14 -4
- data/lib/mysh/expression.rb +10 -7
- data/lib/mysh/external_ruby.rb +1 -1
- data/lib/mysh/handlebars.rb +29 -0
- data/lib/mysh/handlebars/string.rb +22 -0
- data/lib/mysh/internal/action.rb +5 -5
- data/lib/mysh/internal/action_pool.rb +1 -1
- data/lib/mysh/internal/actions/cd.rb +2 -1
- data/lib/mysh/internal/actions/help/hbar.txt +13 -12
- data/lib/mysh/internal/actions/help/history.txt +1 -1
- data/lib/mysh/internal/actions/help/kbd.txt +1 -0
- data/lib/mysh/internal/actions/help/sub_help.rb +4 -3
- data/lib/mysh/internal/actions/help/vars.txt +34 -0
- data/lib/mysh/internal/actions/pwd.rb +1 -1
- data/lib/mysh/internal/actions/show/env.rb +3 -1
- data/lib/mysh/internal/actions/show/ruby.rb +1 -1
- data/lib/mysh/internal/actions/type.rb +1 -1
- data/lib/mysh/internal/actions/vars.rb +58 -0
- data/lib/mysh/internal/decorate.rb +13 -28
- data/lib/mysh/pre_processor.rb +11 -0
- data/lib/mysh/quick.rb +1 -0
- data/lib/mysh/shell_variables.rb +29 -0
- data/lib/mysh/shell_variables/evaluate.rb +23 -0
- data/lib/mysh/shell_variables/globalize.rb +9 -0
- data/lib/mysh/shell_variables/shell_variable_keeper.rb +36 -0
- data/lib/mysh/shell_variables/shell_variable_store.rb +76 -0
- data/lib/mysh/user_input.rb +10 -7
- data/lib/mysh/version.rb +1 -1
- data/mysh.gemspec +1 -1
- data/samples/show.txt +1 -1
- data/tests/my_shell_tests.rb +78 -5
- metadata +14 -5
- data/lib/mysh/user_input/handlebars.rb +0 -46
@@ -0,0 +1,11 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
#* mysh/pre_processor.rb -- The mysh macro/handlebar preprocessor.
|
4
|
+
class String
|
5
|
+
|
6
|
+
#The mysh string pre-processor stack.
|
7
|
+
def preprocess(evaluator=$mysh_exec_host)
|
8
|
+
self.eval_variables.eval_handlebars(evaluator).eval_quoted_braces
|
9
|
+
end
|
10
|
+
|
11
|
+
end
|
data/lib/mysh/quick.rb
CHANGED
@@ -10,6 +10,7 @@ module Mysh
|
|
10
10
|
QUICK['='] = lambda {|str| $mysh_exec_host.execute(str) }
|
11
11
|
QUICK['?'] = lambda {|str| HELP_COMMAND.quick_parse_and_call(str) }
|
12
12
|
QUICK['@'] = lambda {|str| SHOW_COMMAND.quick_parse_and_call(str) }
|
13
|
+
QUICK['$'] = lambda {|str| VARS_COMMAND.call(str) }
|
13
14
|
|
14
15
|
#Try to execute the string as a quick command.
|
15
16
|
def self.try_execute_quick_command(str)
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
require_relative 'shell_variables/shell_variable_store'
|
4
|
+
require_relative 'shell_variables/shell_variable_keeper'
|
5
|
+
require_relative 'shell_variables/globalize'
|
6
|
+
require_relative 'shell_variables/evaluate'
|
7
|
+
|
8
|
+
#* mysh/shell_variables.rb -- Adds support for mysh scripting variables.
|
9
|
+
module Mysh
|
10
|
+
|
11
|
+
#Set up some essential entries.
|
12
|
+
MNV['$'.to_sym] = "$"
|
13
|
+
|
14
|
+
#Set up some default values.
|
15
|
+
MNV[:debug] = "false"
|
16
|
+
MNV[:prompt] = "mysh"
|
17
|
+
MNV[:post_prompt] = "$prompt"
|
18
|
+
MNV[:pre_prompt] = "$w"
|
19
|
+
|
20
|
+
MNV[:d] = "{{ Time.now.strftime(MNV[:date_fmt]) }}"
|
21
|
+
MNV[:h] = "{{ ENV['HOME'].decorate }}"
|
22
|
+
MNV[:t] = "{{ Time.now.strftime(MNV[:time_fmt]) }}"
|
23
|
+
MNV[:u] = "{{ ENV['USER'] }}"
|
24
|
+
MNV[:w] = "{{ Dir.pwd.decorate }}"
|
25
|
+
|
26
|
+
MNV[:date_fmt] = "%Y-%m-%d"
|
27
|
+
MNV[:time_fmt] = "%H:%M"
|
28
|
+
|
29
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
#Monkey patches for Mysh variables
|
4
|
+
class String
|
5
|
+
|
6
|
+
#Extract common mysh data from this string.
|
7
|
+
def extract_mysh_types
|
8
|
+
if self =~ /false|no|off/i
|
9
|
+
false
|
10
|
+
else
|
11
|
+
self
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
#Evaluate any variable substitutions in the input.
|
16
|
+
def eval_variables
|
17
|
+
self.gsub(/(\$\$)|(\$[a-z][a-z0-9_]*)/) do |str|
|
18
|
+
sym = str[1..-1].to_sym
|
19
|
+
MNV.key?(sym) ? MNV[sym].to_s : str
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
#* mysh/shell_variables/shell_variable_keeper.rb -- The keeper of mysh values.
|
4
|
+
module Mysh
|
5
|
+
|
6
|
+
#The keeper of mysh variable values.
|
7
|
+
class Keeper
|
8
|
+
|
9
|
+
#Set up this variable
|
10
|
+
def initialize(value="")
|
11
|
+
@value = value
|
12
|
+
end
|
13
|
+
|
14
|
+
#Get the value of this variable.
|
15
|
+
def get_value(loop_check={})
|
16
|
+
my_id = self.object_id
|
17
|
+
fail "Mysh variable looping error." if loop_check[my_id]
|
18
|
+
loop_check[my_id] = self
|
19
|
+
|
20
|
+
@value.preprocess
|
21
|
+
end
|
22
|
+
|
23
|
+
#Get the source code of this variable.
|
24
|
+
def get_source
|
25
|
+
@value
|
26
|
+
end
|
27
|
+
|
28
|
+
#Set the value of this variable.
|
29
|
+
def set_value(value)
|
30
|
+
@value = value
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
|
@@ -0,0 +1,76 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
#* mysh/shell_variables/shell_variable_store.rb -- Storage for mysh variables.
|
4
|
+
module Mysh
|
5
|
+
|
6
|
+
#The holder of mysh variables.
|
7
|
+
module MNV
|
8
|
+
#Set up the storage hash.
|
9
|
+
@store = Hash.new { |_hash, _key| Keeper.new }
|
10
|
+
|
11
|
+
#The shared loop checker for programmatic access.
|
12
|
+
@loop_check = nil
|
13
|
+
|
14
|
+
#Get the value of a variable.
|
15
|
+
#<br>Endemic Code Smells
|
16
|
+
#* :reek:TooManyStatements
|
17
|
+
def self.[](index)
|
18
|
+
keeper = get_keeper(index)
|
19
|
+
|
20
|
+
if @loop_check
|
21
|
+
keeper.get_value(@loop_check)
|
22
|
+
else
|
23
|
+
begin
|
24
|
+
keeper.get_value(@loop_check = {})
|
25
|
+
ensure
|
26
|
+
@loop_check = nil
|
27
|
+
end
|
28
|
+
end.extract_mysh_types
|
29
|
+
end
|
30
|
+
|
31
|
+
#Set the value of a variable.
|
32
|
+
def self.[]=(index, value)
|
33
|
+
unless value.empty?
|
34
|
+
keeper = get_keeper(index)
|
35
|
+
keeper.set_value(value)
|
36
|
+
set_keeper(index, keeper)
|
37
|
+
else
|
38
|
+
delete_keeper(index)
|
39
|
+
end
|
40
|
+
|
41
|
+
value
|
42
|
+
end
|
43
|
+
|
44
|
+
#Get the value keeper of a variable.
|
45
|
+
def self.get_keeper(index)
|
46
|
+
@store[index]
|
47
|
+
end
|
48
|
+
|
49
|
+
#Set the value keeper of a variable.
|
50
|
+
def self.set_keeper(index, keeper)
|
51
|
+
@store[index] = keeper
|
52
|
+
end
|
53
|
+
|
54
|
+
#Delete the value keeper of a variable.
|
55
|
+
def self.delete_keeper(index)
|
56
|
+
@store.delete(index)
|
57
|
+
end
|
58
|
+
|
59
|
+
#Get the source code of a variable.
|
60
|
+
def self.get_source(index)
|
61
|
+
@store[index].get_source
|
62
|
+
end
|
63
|
+
|
64
|
+
#Does this entry exist?
|
65
|
+
def self.key?(index)
|
66
|
+
@store.key?(index)
|
67
|
+
end
|
68
|
+
|
69
|
+
#Get all the keys
|
70
|
+
def self.keys
|
71
|
+
@store.keys
|
72
|
+
end
|
73
|
+
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
data/lib/mysh/user_input.rb
CHANGED
@@ -4,7 +4,6 @@ require 'mini_readline'
|
|
4
4
|
|
5
5
|
require_relative 'user_input/smart_source'
|
6
6
|
require_relative 'user_input/parse'
|
7
|
-
require_relative 'user_input/handlebars'
|
8
7
|
|
9
8
|
#* mysh/user_input.rb -- Get some text from the user.
|
10
9
|
module Mysh
|
@@ -15,10 +14,14 @@ module Mysh
|
|
15
14
|
end
|
16
15
|
|
17
16
|
#Get one command from the user.
|
18
|
-
|
19
|
-
|
17
|
+
#<br>Endemic Code Smells
|
18
|
+
#* :reek:TooManyStatements
|
19
|
+
def self.get_command
|
20
|
+
puts MNV[:pre_prompt] if MNV.key?(:pre_prompt)
|
21
|
+
|
22
|
+
initial_input = @input.readline(prompt: MNV[:prompt] + '>')
|
20
23
|
@input.instance_options[:initial] = ""
|
21
|
-
get_command_extra(initial_input
|
24
|
+
get_command_extra(initial_input)
|
22
25
|
|
23
26
|
rescue MiniReadlineEOI
|
24
27
|
@mysh_running = false
|
@@ -28,10 +31,10 @@ module Mysh
|
|
28
31
|
private
|
29
32
|
|
30
33
|
#Get any continuations of the inputs
|
31
|
-
def self.get_command_extra(str
|
34
|
+
def self.get_command_extra(str)
|
32
35
|
if /\\\s*$/ =~ str
|
33
|
-
parms = {prompt:
|
34
|
-
get_command_extra($PREMATCH + "\n" + @input.readline(parms)
|
36
|
+
parms = {prompt: MNV[:post_prompt] + '\\', prefix: str[0] }
|
37
|
+
get_command_extra($PREMATCH + "\n" + @input.readline(parms))
|
35
38
|
else
|
36
39
|
str
|
37
40
|
end
|
data/lib/mysh/version.rb
CHANGED
data/mysh.gemspec
CHANGED
@@ -30,7 +30,7 @@ Gem::Specification.new do |spec|
|
|
30
30
|
spec.add_development_dependency 'minitest_visible', ">= 0.1.2"
|
31
31
|
spec.add_development_dependency 'rdoc', "~> 4.0.1"
|
32
32
|
|
33
|
-
spec.add_runtime_dependency 'mini_readline', ">= 0.
|
33
|
+
spec.add_runtime_dependency 'mini_readline', ">= 0.7.2"
|
34
34
|
spec.add_runtime_dependency 'vls', ">= 0.4.1"
|
35
35
|
spec.add_runtime_dependency 'in_array', ">= 0.1.7"
|
36
36
|
end
|
data/samples/show.txt
CHANGED
data/tests/my_shell_tests.rb
CHANGED
@@ -24,6 +24,8 @@ class MyShellTester < Minitest::Test
|
|
24
24
|
assert_equal(Module, Mysh.class)
|
25
25
|
assert_equal(Class, Mysh::Action.class)
|
26
26
|
assert_equal(Class, Mysh::ActionPool.class)
|
27
|
+
assert_equal(Module, Mysh::MNV.class)
|
28
|
+
assert_equal(Class, Mysh::Keeper.class)
|
27
29
|
|
28
30
|
assert_equal(Mysh::ActionPool, Mysh::COMMANDS.class)
|
29
31
|
assert_equal(Mysh::ActionPool, Mysh::HELP.class)
|
@@ -46,14 +48,16 @@ class MyShellTester < Minitest::Test
|
|
46
48
|
end
|
47
49
|
|
48
50
|
def test_handlebars
|
51
|
+
Mysh.reset_host
|
52
|
+
|
49
53
|
assert_equal("ABC 123 DEF",
|
50
|
-
|
54
|
+
"ABC {{ (1..3).to_a.join }} DEF".eval_handlebars)
|
51
55
|
|
52
|
-
assert_equal("ABC",
|
53
|
-
assert_equal("",
|
56
|
+
assert_equal("ABC", "{{ 'ABC' }}".eval_handlebars)
|
57
|
+
assert_equal("", "{{ 'ABC' #}}".eval_handlebars)
|
54
58
|
|
55
|
-
assert_equal("{{ 'ABC' }}",
|
56
|
-
assert_equal("{{A}}",
|
59
|
+
assert_equal("{{ 'ABC' }}", "\\{\\{ 'ABC' \\}\\}".eval_quoted_braces)
|
60
|
+
assert_equal("{{A}}", "{{ '{'+'{A}'+'}' }}".eval_handlebars.eval_quoted_braces)
|
57
61
|
end
|
58
62
|
|
59
63
|
def test_command_parsing
|
@@ -122,4 +126,73 @@ class MyShellTester < Minitest::Test
|
|
122
126
|
|
123
127
|
end
|
124
128
|
|
129
|
+
def test_mysh_variables
|
130
|
+
Mysh.reset_host
|
131
|
+
|
132
|
+
assert_equal("", MNV[:test])
|
133
|
+
assert_equal("", MNV.get_source(:test))
|
134
|
+
refute(MNV.key?(:test), "MNV[:test] should not exist.")
|
135
|
+
|
136
|
+
MNV[:test] = "test 1 2 3"
|
137
|
+
assert_equal("test 1 2 3", MNV[:test])
|
138
|
+
assert_equal("test 1 2 3", MNV.get_source(:test))
|
139
|
+
assert(MNV.key?(:test), "MNV[:test] should exist.")
|
140
|
+
|
141
|
+
MNV[:test] = ""
|
142
|
+
assert_equal("", MNV[:test])
|
143
|
+
assert_equal("", MNV.get_source(:test))
|
144
|
+
refute(MNV.key?(:test), "MNV[:test] should not exist.")
|
145
|
+
|
146
|
+
Mysh.try_execute_command("$test = new value")
|
147
|
+
assert_equal("new value", MNV[:test])
|
148
|
+
assert_equal("new value", MNV.get_source(:test))
|
149
|
+
assert(MNV.key?(:test), "MNV[:test] should exist.")
|
150
|
+
|
151
|
+
Mysh.try_execute_command("$test =")
|
152
|
+
assert_equal("", MNV[:test])
|
153
|
+
assert_equal("", MNV.get_source(:test))
|
154
|
+
refute(MNV.key?(:test), "MNV[:test] should not exist.")
|
155
|
+
|
156
|
+
Mysh.try_execute_command("$test = true")
|
157
|
+
assert(MNV[:test])
|
158
|
+
assert_equal("true", MNV.get_source(:test))
|
159
|
+
assert(MNV.key?(:test), "MNV[:test] should exist.")
|
160
|
+
|
161
|
+
Mysh.try_execute_command("$test = off")
|
162
|
+
assert_equal(false, MNV[:test])
|
163
|
+
assert_equal("off", MNV.get_source(:test))
|
164
|
+
assert(MNV.key?(:test), "MNV[:test] should exist.")
|
165
|
+
|
166
|
+
Mysh.try_execute_command("$a = foo")
|
167
|
+
Mysh.try_execute_command("$b = bar")
|
168
|
+
Mysh.try_execute_command("$test = $a$b")
|
169
|
+
assert_equal("foobar", MNV[:test])
|
170
|
+
assert_equal("$a$b", MNV.get_source(:test))
|
171
|
+
|
172
|
+
Mysh.try_execute_command("$test = $$foo")
|
173
|
+
assert_equal("$foo", MNV[:test])
|
174
|
+
assert_equal("$$foo", MNV.get_source(:test))
|
175
|
+
|
176
|
+
Mysh.try_execute_command("$bad = $bad")
|
177
|
+
assert_raises { MNV[:bad] }
|
178
|
+
|
179
|
+
MNV[:test] = "{{(1..9).to_a.join}}"
|
180
|
+
assert_equal("123456789", MNV[:test])
|
181
|
+
assert_equal("{{(1..9).to_a.join}}", MNV.get_source(:test))
|
182
|
+
|
183
|
+
Mysh.try_execute_command("$test = $whats_all_this")
|
184
|
+
assert_equal("$whats_all_this", MNV[:test])
|
185
|
+
assert_equal("$whats_all_this", MNV.get_source(:test))
|
186
|
+
|
187
|
+
Mysh.try_execute_command("$bad = {{ MNV[:bad] }}")
|
188
|
+
assert_raises { MNV[:bad] }
|
189
|
+
assert_raises { MNV[:bad] } #Yes test this twice!
|
190
|
+
assert_raises { Mysh.try_execute_command("=$bad") }
|
191
|
+
assert_raises { Mysh.try_execute_command("=$bad") } #And this too!
|
192
|
+
|
193
|
+
Mysh.try_execute_command("$bad = OK")
|
194
|
+
assert_equal("OK", MNV[:bad])
|
195
|
+
assert_equal("OK", MNV[:bad]) #And this too!
|
196
|
+
end
|
197
|
+
|
125
198
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mysh
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Peter Camilleri
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-12-
|
11
|
+
date: 2016-12-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -100,14 +100,14 @@ dependencies:
|
|
100
100
|
requirements:
|
101
101
|
- - ">="
|
102
102
|
- !ruby/object:Gem::Version
|
103
|
-
version: 0.
|
103
|
+
version: 0.7.2
|
104
104
|
type: :runtime
|
105
105
|
prerelease: false
|
106
106
|
version_requirements: !ruby/object:Gem::Requirement
|
107
107
|
requirements:
|
108
108
|
- - ">="
|
109
109
|
- !ruby/object:Gem::Version
|
110
|
-
version: 0.
|
110
|
+
version: 0.7.2
|
111
111
|
- !ruby/object:Gem::Dependency
|
112
112
|
name: vls
|
113
113
|
requirement: !ruby/object:Gem::Requirement
|
@@ -154,6 +154,8 @@ files:
|
|
154
154
|
- lib/mysh/expression.rb
|
155
155
|
- lib/mysh/expression/lineage.rb
|
156
156
|
- lib/mysh/external_ruby.rb
|
157
|
+
- lib/mysh/handlebars.rb
|
158
|
+
- lib/mysh/handlebars/string.rb
|
157
159
|
- lib/mysh/internal.rb
|
158
160
|
- lib/mysh/internal/action.rb
|
159
161
|
- lib/mysh/internal/action_pool.rb
|
@@ -175,12 +177,14 @@ files:
|
|
175
177
|
- lib/mysh/internal/actions/help/ruby.txt
|
176
178
|
- lib/mysh/internal/actions/help/show.txt
|
177
179
|
- lib/mysh/internal/actions/help/sub_help.rb
|
180
|
+
- lib/mysh/internal/actions/help/vars.txt
|
178
181
|
- lib/mysh/internal/actions/history.rb
|
179
182
|
- lib/mysh/internal/actions/pwd.rb
|
180
183
|
- lib/mysh/internal/actions/show.rb
|
181
184
|
- lib/mysh/internal/actions/show/env.rb
|
182
185
|
- lib/mysh/internal/actions/show/ruby.rb
|
183
186
|
- lib/mysh/internal/actions/type.rb
|
187
|
+
- lib/mysh/internal/actions/vars.rb
|
184
188
|
- lib/mysh/internal/actions/vls.rb
|
185
189
|
- lib/mysh/internal/decorate.rb
|
186
190
|
- lib/mysh/internal/format.rb
|
@@ -191,9 +195,14 @@ files:
|
|
191
195
|
- lib/mysh/internal/format/object.rb
|
192
196
|
- lib/mysh/internal/format/string.rb
|
193
197
|
- lib/mysh/internal/manage.rb
|
198
|
+
- lib/mysh/pre_processor.rb
|
194
199
|
- lib/mysh/quick.rb
|
200
|
+
- lib/mysh/shell_variables.rb
|
201
|
+
- lib/mysh/shell_variables/evaluate.rb
|
202
|
+
- lib/mysh/shell_variables/globalize.rb
|
203
|
+
- lib/mysh/shell_variables/shell_variable_keeper.rb
|
204
|
+
- lib/mysh/shell_variables/shell_variable_store.rb
|
195
205
|
- lib/mysh/user_input.rb
|
196
|
-
- lib/mysh/user_input/handlebars.rb
|
197
206
|
- lib/mysh/user_input/parse.rb
|
198
207
|
- lib/mysh/user_input/smart_source.rb
|
199
208
|
- lib/mysh/version.rb
|