gloo-lang 0.9.0 → 0.9.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (193) hide show
  1. checksums.yaml +4 -4
  2. data/lib/VERSION +1 -1
  3. data/lib/dependencies.rb +2 -2
  4. data/lib/{gloo_lang.rb → gloo-lang.rb} +7 -9
  5. data/lib/gloo_lang/app/args.rb +112 -0
  6. data/lib/gloo_lang/app/engine.rb +230 -0
  7. data/lib/gloo_lang/app/help.rb +156 -0
  8. data/lib/gloo_lang/app/log.rb +81 -0
  9. data/lib/gloo_lang/app/mode.rb +27 -0
  10. data/lib/gloo_lang/app/settings.rb +186 -0
  11. data/lib/gloo_lang/convert/converter.rb +35 -0
  12. data/lib/gloo_lang/convert/string_to_datetime.rb +21 -0
  13. data/lib/gloo_lang/convert/string_to_decimal.rb +20 -0
  14. data/lib/gloo_lang/convert/string_to_integer.rb +20 -0
  15. data/lib/gloo_lang/core/baseo.rb +30 -0
  16. data/lib/gloo_lang/core/dictionary.rb +181 -0
  17. data/lib/gloo_lang/core/error.rb +61 -0
  18. data/lib/gloo_lang/core/event_manager.rb +44 -0
  19. data/lib/gloo_lang/core/factory.rb +210 -0
  20. data/lib/gloo_lang/core/gloo_system.rb +266 -0
  21. data/lib/gloo_lang/core/heap.rb +52 -0
  22. data/lib/gloo_lang/core/here.rb +36 -0
  23. data/lib/gloo_lang/core/it.rb +36 -0
  24. data/lib/gloo_lang/core/literal.rb +30 -0
  25. data/lib/gloo_lang/core/obj.rb +303 -0
  26. data/lib/gloo_lang/core/obj_finder.rb +30 -0
  27. data/lib/gloo_lang/core/op.rb +40 -0
  28. data/lib/gloo_lang/core/parser.rb +59 -0
  29. data/lib/gloo_lang/core/pn.rb +188 -0
  30. data/lib/gloo_lang/core/tokens.rb +165 -0
  31. data/lib/gloo_lang/core/verb.rb +86 -0
  32. data/lib/gloo_lang/exec/action.rb +48 -0
  33. data/lib/gloo_lang/exec/dispatch.rb +40 -0
  34. data/lib/gloo_lang/exec/exec_env.rb +74 -0
  35. data/lib/gloo_lang/exec/runner.rb +45 -0
  36. data/lib/gloo_lang/exec/script.rb +49 -0
  37. data/lib/gloo_lang/exec/stack.rb +78 -0
  38. data/lib/gloo_lang/expr/expression.rb +118 -0
  39. data/lib/gloo_lang/expr/l_boolean.rb +36 -0
  40. data/lib/gloo_lang/expr/l_decimal.rb +39 -0
  41. data/lib/gloo_lang/expr/l_integer.rb +37 -0
  42. data/lib/gloo_lang/expr/l_string.rb +58 -0
  43. data/lib/gloo_lang/expr/op_div.rb +22 -0
  44. data/lib/gloo_lang/expr/op_minus.rb +22 -0
  45. data/lib/gloo_lang/expr/op_mult.rb +22 -0
  46. data/lib/gloo_lang/expr/op_plus.rb +24 -0
  47. data/lib/gloo_lang/help/app/application.txt +22 -0
  48. data/lib/gloo_lang/help/app/configuration.txt +7 -0
  49. data/lib/gloo_lang/help/app/default_help.txt +14 -0
  50. data/lib/gloo_lang/help/app/logging.txt +16 -0
  51. data/lib/gloo_lang/help/core/color.txt +31 -0
  52. data/lib/gloo_lang/help/core/error.txt +33 -0
  53. data/lib/gloo_lang/help/core/events.txt +21 -0
  54. data/lib/gloo_lang/help/core/gloo_system.txt +57 -0
  55. data/lib/gloo_lang/help/core/here.txt +30 -0
  56. data/lib/gloo_lang/help/core/it.txt +23 -0
  57. data/lib/gloo_lang/help/core/ops.txt +16 -0
  58. data/lib/gloo_lang/help/core/pathname.txt +29 -0
  59. data/lib/gloo_lang/help/objs/basic/alias.txt +36 -0
  60. data/lib/gloo_lang/help/objs/basic/boolean.txt +28 -0
  61. data/lib/gloo_lang/help/objs/basic/container.txt +33 -0
  62. data/lib/gloo_lang/help/objs/basic/decimal.txt +28 -0
  63. data/lib/gloo_lang/help/objs/basic/integer.txt +27 -0
  64. data/lib/gloo_lang/help/objs/basic/script.txt +29 -0
  65. data/lib/gloo_lang/help/objs/basic/string.txt +28 -0
  66. data/lib/gloo_lang/help/objs/basic/text.txt +27 -0
  67. data/lib/gloo_lang/help/objs/basic/untyped.txt +22 -0
  68. data/lib/gloo_lang/help/objs/cli/banner.txt +49 -0
  69. data/lib/gloo_lang/help/objs/cli/bar.txt +37 -0
  70. data/lib/gloo_lang/help/objs/cli/colorize.txt +33 -0
  71. data/lib/gloo_lang/help/objs/cli/confirm.txt +26 -0
  72. data/lib/gloo_lang/help/objs/cli/menu.txt +44 -0
  73. data/lib/gloo_lang/help/objs/cli/menu_item.txt +26 -0
  74. data/lib/gloo_lang/help/objs/cli/pastel.txt +43 -0
  75. data/lib/gloo_lang/help/objs/cli/prompt.txt +27 -0
  76. data/lib/gloo_lang/help/objs/cli/select.txt +34 -0
  77. data/lib/gloo_lang/help/objs/ctrl/each.txt +48 -0
  78. data/lib/gloo_lang/help/objs/ctrl/repeat.txt +38 -0
  79. data/lib/gloo_lang/help/objs/data/markdown.txt +25 -0
  80. data/lib/gloo_lang/help/objs/data/mysql.txt +40 -0
  81. data/lib/gloo_lang/help/objs/data/query.txt +37 -0
  82. data/lib/gloo_lang/help/objs/data/sqlite.txt +26 -0
  83. data/lib/gloo_lang/help/objs/data/table.txt +46 -0
  84. data/lib/gloo_lang/help/objs/dev/git_repo.txt +36 -0
  85. data/lib/gloo_lang/help/objs/dev/stats.txt +36 -0
  86. data/lib/gloo_lang/help/objs/dt/date.txt +23 -0
  87. data/lib/gloo_lang/help/objs/dt/datetime.txt +24 -0
  88. data/lib/gloo_lang/help/objs/dt/time.txt +23 -0
  89. data/lib/gloo_lang/help/objs/ror/erb.txt +37 -0
  90. data/lib/gloo_lang/help/objs/ror/eval.txt +24 -0
  91. data/lib/gloo_lang/help/objs/snd/play.txt +23 -0
  92. data/lib/gloo_lang/help/objs/snd/say.txt +28 -0
  93. data/lib/gloo_lang/help/objs/system/file.txt +48 -0
  94. data/lib/gloo_lang/help/objs/system/ssh_exec.txt +30 -0
  95. data/lib/gloo_lang/help/objs/system/system.txt +32 -0
  96. data/lib/gloo_lang/help/objs/web/http_get.txt +35 -0
  97. data/lib/gloo_lang/help/objs/web/http_post.txt +34 -0
  98. data/lib/gloo_lang/help/objs/web/json.txt +34 -0
  99. data/lib/gloo_lang/help/objs/web/slack.txt +33 -0
  100. data/lib/gloo_lang/help/objs/web/teams.txt +30 -0
  101. data/lib/gloo_lang/help/objs/web/uri.txt +38 -0
  102. data/lib/gloo_lang/help/verbs/alert.txt +33 -0
  103. data/lib/gloo_lang/help/verbs/beep.txt +25 -0
  104. data/lib/gloo_lang/help/verbs/cls.txt +24 -0
  105. data/lib/gloo_lang/help/verbs/context.txt +43 -0
  106. data/lib/gloo_lang/help/verbs/create.txt +33 -0
  107. data/lib/gloo_lang/help/verbs/execute.txt +27 -0
  108. data/lib/gloo_lang/help/verbs/help.txt +34 -0
  109. data/lib/gloo_lang/help/verbs/if.txt +37 -0
  110. data/lib/gloo_lang/help/verbs/list.txt +34 -0
  111. data/lib/gloo_lang/help/verbs/load.txt +38 -0
  112. data/lib/gloo_lang/help/verbs/move.txt +42 -0
  113. data/lib/gloo_lang/help/verbs/put.txt +38 -0
  114. data/lib/gloo_lang/help/verbs/quit.txt +25 -0
  115. data/lib/gloo_lang/help/verbs/run.txt +41 -0
  116. data/lib/gloo_lang/help/verbs/save.txt +26 -0
  117. data/lib/gloo_lang/help/verbs/show.txt +30 -0
  118. data/lib/gloo_lang/help/verbs/tell.txt +34 -0
  119. data/lib/gloo_lang/help/verbs/unless.txt +38 -0
  120. data/lib/gloo_lang/help/verbs/version.txt +32 -0
  121. data/lib/gloo_lang/help/verbs/wait.txt +29 -0
  122. data/lib/gloo_lang/objs/basic/alias.rb +78 -0
  123. data/lib/gloo_lang/objs/basic/boolean.rb +120 -0
  124. data/lib/gloo_lang/objs/basic/container.rb +76 -0
  125. data/lib/gloo_lang/objs/basic/decimal.rb +76 -0
  126. data/lib/gloo_lang/objs/basic/integer.rb +73 -0
  127. data/lib/gloo_lang/objs/basic/script.rb +99 -0
  128. data/lib/gloo_lang/objs/basic/string.rb +77 -0
  129. data/lib/gloo_lang/objs/basic/text.rb +87 -0
  130. data/lib/gloo_lang/objs/basic/untyped.rb +41 -0
  131. data/lib/gloo_lang/objs/cli/banner.rb +108 -0
  132. data/lib/gloo_lang/objs/cli/bar.rb +133 -0
  133. data/lib/gloo_lang/objs/cli/colorize.rb +73 -0
  134. data/lib/gloo_lang/objs/cli/confirm.rb +96 -0
  135. data/lib/gloo_lang/objs/cli/menu.rb +206 -0
  136. data/lib/gloo_lang/objs/cli/menu_item.rb +95 -0
  137. data/lib/gloo_lang/objs/cli/pastel.rb +97 -0
  138. data/lib/gloo_lang/objs/cli/prompt.rb +110 -0
  139. data/lib/gloo_lang/objs/cli/select.rb +126 -0
  140. data/lib/gloo_lang/objs/ctrl/each.rb +279 -0
  141. data/lib/gloo_lang/objs/ctrl/repeat.rb +108 -0
  142. data/lib/gloo_lang/objs/data/markdown.rb +84 -0
  143. data/lib/gloo_lang/objs/data/mysql.rb +192 -0
  144. data/lib/gloo_lang/objs/data/query.rb +176 -0
  145. data/lib/gloo_lang/objs/data/sqlite.rb +159 -0
  146. data/lib/gloo_lang/objs/data/table.rb +140 -0
  147. data/lib/gloo_lang/objs/dev/git.rb +140 -0
  148. data/lib/gloo_lang/objs/dev/stats.rb +120 -0
  149. data/lib/gloo_lang/objs/dt/date.rb +50 -0
  150. data/lib/gloo_lang/objs/dt/datetime.rb +62 -0
  151. data/lib/gloo_lang/objs/dt/time.rb +50 -0
  152. data/lib/gloo_lang/objs/ror/erb.rb +116 -0
  153. data/lib/gloo_lang/objs/ror/eval.rb +107 -0
  154. data/lib/gloo_lang/objs/snd/play.rb +48 -0
  155. data/lib/gloo_lang/objs/snd/say.rb +98 -0
  156. data/lib/gloo_lang/objs/system/file_handle.rb +138 -0
  157. data/lib/gloo_lang/objs/system/ssh_exec.rb +126 -0
  158. data/lib/gloo_lang/objs/system/system.rb +136 -0
  159. data/lib/gloo_lang/objs/web/http_get.rb +159 -0
  160. data/lib/gloo_lang/objs/web/http_post.rb +183 -0
  161. data/lib/gloo_lang/objs/web/json.rb +135 -0
  162. data/lib/gloo_lang/objs/web/slack.rb +130 -0
  163. data/lib/gloo_lang/objs/web/teams.rb +117 -0
  164. data/lib/gloo_lang/objs/web/uri.rb +148 -0
  165. data/lib/gloo_lang/persist/file_loader.rb +191 -0
  166. data/lib/gloo_lang/persist/file_saver.rb +49 -0
  167. data/lib/gloo_lang/persist/file_storage.rb +45 -0
  168. data/lib/gloo_lang/persist/line_splitter.rb +81 -0
  169. data/lib/gloo_lang/persist/persist_man.rb +120 -0
  170. data/lib/gloo_lang/utils/format.rb +21 -0
  171. data/lib/gloo_lang/utils/stats.rb +205 -0
  172. data/lib/gloo_lang/utils/words.rb +19 -0
  173. data/lib/gloo_lang/verbs/alert.rb +79 -0
  174. data/lib/gloo_lang/verbs/beep.rb +40 -0
  175. data/lib/gloo_lang/verbs/cls.rb +37 -0
  176. data/lib/gloo_lang/verbs/context.rb +62 -0
  177. data/lib/gloo_lang/verbs/create.rb +68 -0
  178. data/lib/gloo_lang/verbs/execute.rb +56 -0
  179. data/lib/gloo_lang/verbs/help.rb +264 -0
  180. data/lib/gloo_lang/verbs/if.rb +92 -0
  181. data/lib/gloo_lang/verbs/list.rb +98 -0
  182. data/lib/gloo_lang/verbs/load.rb +45 -0
  183. data/lib/gloo_lang/verbs/move.rb +89 -0
  184. data/lib/gloo_lang/verbs/put.rb +94 -0
  185. data/lib/gloo_lang/verbs/quit.rb +40 -0
  186. data/lib/gloo_lang/verbs/run.rb +75 -0
  187. data/lib/gloo_lang/verbs/save.rb +39 -0
  188. data/lib/gloo_lang/verbs/show.rb +64 -0
  189. data/lib/gloo_lang/verbs/tell.rb +79 -0
  190. data/lib/gloo_lang/verbs/unless.rb +92 -0
  191. data/lib/gloo_lang/verbs/version.rb +37 -0
  192. data/lib/gloo_lang/verbs/wait.rb +42 -0
  193. metadata +191 -3
@@ -0,0 +1,95 @@
1
+ # Author:: Eric Crane (mailto:eric.crane@mac.com)
2
+ # Copyright:: Copyright (c) 2020 Eric Crane. All rights reserved.
3
+ #
4
+ # A CLI menu item. One element in a CLI menu.
5
+ #
6
+
7
+ module GlooLang
8
+ module Objs
9
+ class MenuItem < GlooLang::Core::Obj
10
+
11
+ KEYWORD = 'menu_item'.freeze
12
+ KEYWORD_SHORT = 'mitem'.freeze
13
+ SHORTCUT = 'shortcut'.freeze
14
+ DESCRIPTION = 'description'.freeze
15
+ DO = 'do'.freeze
16
+
17
+ #
18
+ # The name of the object type.
19
+ #
20
+ def self.typename
21
+ return KEYWORD
22
+ end
23
+
24
+ #
25
+ # The short name of the object type.
26
+ #
27
+ def self.short_typename
28
+ return KEYWORD_SHORT
29
+ end
30
+
31
+ #
32
+ # Get the value of the menu item shortcut.
33
+ # Returns nil if there is none.
34
+ #
35
+ def shortcut_value
36
+ o = find_child SHORTCUT
37
+ return self.name unless o
38
+
39
+ return o.value
40
+ end
41
+
42
+ #
43
+ # Get the action's description.
44
+ # Returns nil if there is none.
45
+ #
46
+ def description_value
47
+ o = find_child DESCRIPTION
48
+ return self.value unless o
49
+
50
+ return o.value
51
+ end
52
+
53
+ #
54
+ # Get the action's script.
55
+ # Returns nil if there is none.
56
+ #
57
+ def do_script
58
+ return find_child DO
59
+ end
60
+
61
+ # ---------------------------------------------------------------------
62
+ # Children
63
+ # ---------------------------------------------------------------------
64
+
65
+ # Does this object have children to add when an object
66
+ # is created in interactive mode?
67
+ # This does not apply during obj load, etc.
68
+ def add_children_on_create?
69
+ return true
70
+ end
71
+
72
+ # Add children to this object.
73
+ # This is used by containers to add children needed
74
+ # for default configurations.
75
+ def add_default_children
76
+ fac = $engine.factory
77
+ fac.create_string SHORTCUT, '', self
78
+ fac.create_string DESCRIPTION, '', self
79
+ fac.create_script DO, '', self
80
+ end
81
+
82
+ # ---------------------------------------------------------------------
83
+ # Messages
84
+ # ---------------------------------------------------------------------
85
+
86
+ #
87
+ # Get a list of message names that this object receives.
88
+ #
89
+ def self.messages
90
+ return super
91
+ end
92
+
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,97 @@
1
+ # Author:: Eric Crane (mailto:eric.crane@mac.com)
2
+ # Copyright:: Copyright (c) 2020 Eric Crane. All rights reserved.
3
+ #
4
+ # Show colorized output with the pastel gem.
5
+ #
6
+ require 'pastel'
7
+
8
+ module GlooLang
9
+ module Objs
10
+ class Pastel < GlooLang::Core::Obj
11
+
12
+ KEYWORD = 'pastel'.freeze
13
+ KEYWORD_SHORT = 'pastel'.freeze
14
+ TEXT = 'text'.freeze
15
+ COLOR = 'color'.freeze
16
+
17
+ #
18
+ # The name of the object type.
19
+ #
20
+ def self.typename
21
+ return KEYWORD
22
+ end
23
+
24
+ #
25
+ # The short name of the object type.
26
+ #
27
+ def self.short_typename
28
+ return KEYWORD_SHORT
29
+ end
30
+
31
+ #
32
+ # Get the text from the child object.
33
+ #
34
+ def text_value
35
+ o = find_child TEXT
36
+ return '' unless o
37
+
38
+ return o.value
39
+ end
40
+
41
+ #
42
+ # Get the color from the child object.
43
+ #
44
+ def color_value
45
+ o = find_child COLOR
46
+ return '' unless o
47
+
48
+ return o.value
49
+ end
50
+
51
+ # ---------------------------------------------------------------------
52
+ # Children
53
+ # ---------------------------------------------------------------------
54
+
55
+ #
56
+ # Does this object have children to add when an object
57
+ # is created in interactive mode?
58
+ # This does not apply during obj load, etc.
59
+ #
60
+ def add_children_on_create?
61
+ return true
62
+ end
63
+
64
+ #
65
+ # Add children to this object.
66
+ # This is used by containers to add children needed
67
+ # for default configurations.
68
+ #
69
+ def add_default_children
70
+ fac = $engine.factory
71
+ fac.create_string TEXT, '', self
72
+ fac.create_string COLOR, '', self
73
+ end
74
+
75
+ # ---------------------------------------------------------------------
76
+ # Messages
77
+ # ---------------------------------------------------------------------
78
+
79
+ #
80
+ # Get a list of message names that this object receives.
81
+ #
82
+ def self.messages
83
+ return super + %w[show]
84
+ end
85
+
86
+ #
87
+ # Show the banner bar
88
+ #
89
+ def msg_show
90
+ pastel = ::Pastel.new
91
+ c = self.color_value.split( ' ' ).map( &:to_sym )
92
+ puts pastel.decorate( self.text_value, *c )
93
+ end
94
+
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,110 @@
1
+ # Author:: Eric Crane (mailto:eric.crane@mac.com)
2
+ # Copyright:: Copyright (c) 2019 Eric Crane. All rights reserved.
3
+ #
4
+ # Show a CLI prompt and collect user input.
5
+ #
6
+
7
+ module GlooLang
8
+ module Objs
9
+ class Prompt < GlooLang::Core::Obj
10
+
11
+ KEYWORD = 'prompt'.freeze
12
+ KEYWORD_SHORT = 'ask'.freeze
13
+ PROMPT = 'prompt'.freeze
14
+ RESULT = 'result'.freeze
15
+
16
+ #
17
+ # The name of the object type.
18
+ #
19
+ def self.typename
20
+ return KEYWORD
21
+ end
22
+
23
+ #
24
+ # The short name of the object type.
25
+ #
26
+ def self.short_typename
27
+ return KEYWORD_SHORT
28
+ end
29
+
30
+ #
31
+ # Get the prompt from the child object.
32
+ # Returns nil if there is none.
33
+ #
34
+ def prompt_value
35
+ o = find_child PROMPT
36
+ return nil unless o
37
+
38
+ return o.value
39
+ end
40
+
41
+ #
42
+ # Set the result of the system call.
43
+ #
44
+ def set_result( data )
45
+ r = find_child RESULT
46
+ return nil unless r
47
+
48
+ r.set_value data
49
+ end
50
+
51
+ # ---------------------------------------------------------------------
52
+ # Children
53
+ # ---------------------------------------------------------------------
54
+
55
+ #
56
+ # Does this object have children to add when an object
57
+ # is created in interactive mode?
58
+ # This does not apply during obj load, etc.
59
+ #
60
+ def add_children_on_create?
61
+ return true
62
+ end
63
+
64
+ #
65
+ # Add children to this object.
66
+ # This is used by containers to add children needed
67
+ # for default configurations.
68
+ #
69
+ def add_default_children
70
+ fac = $engine.factory
71
+ fac.create_string PROMPT, '>', self
72
+ fac.create_string RESULT, nil, self
73
+ end
74
+
75
+ # ---------------------------------------------------------------------
76
+ # Messages
77
+ # ---------------------------------------------------------------------
78
+
79
+ #
80
+ # Get a list of message names that this object receives.
81
+ #
82
+ def self.messages
83
+ return super + %w[run multiline]
84
+ end
85
+
86
+ #
87
+ # Show a multiline prompt and get the user's input.
88
+ #
89
+ def msg_multiline
90
+ prompt = prompt_value
91
+ return unless prompt
92
+
93
+ result = $prompt.multiline( prompt )
94
+ set_result result.join
95
+ end
96
+
97
+ #
98
+ # Show the prompt and get the user's input.
99
+ #
100
+ def msg_run
101
+ prompt = prompt_value
102
+ return unless prompt
103
+
104
+ result = $prompt.ask( prompt )
105
+ set_result result
106
+ end
107
+
108
+ end
109
+ end
110
+ end
@@ -0,0 +1,126 @@
1
+ # Author:: Eric Crane (mailto:eric.crane@mac.com)
2
+ # Copyright:: Copyright (c) 2020 Eric Crane. All rights reserved.
3
+ #
4
+ # Show a CLI prompt and user selection from a list.
5
+ #
6
+
7
+ module GlooLang
8
+ module Objs
9
+ class Select < GlooLang::Core::Obj
10
+
11
+ KEYWORD = 'select'.freeze
12
+ KEYWORD_SHORT = 'sel'.freeze
13
+ PROMPT = 'prompt'.freeze
14
+ OPTIONS = 'options'.freeze
15
+ RESULT = 'result'.freeze
16
+
17
+ #
18
+ # The name of the object type.
19
+ #
20
+ def self.typename
21
+ return KEYWORD
22
+ end
23
+
24
+ #
25
+ # The short name of the object type.
26
+ #
27
+ def self.short_typename
28
+ return KEYWORD_SHORT
29
+ end
30
+
31
+ #
32
+ # Get the prompt from the child object.
33
+ # Returns nil if there is none.
34
+ #
35
+ def prompt_value
36
+ o = find_child PROMPT
37
+ return nil unless o
38
+
39
+ return o.value
40
+ end
41
+
42
+ #
43
+ # Get the list of options for selection.
44
+ #
45
+ def options
46
+ o = find_child OPTIONS
47
+ return [] unless o
48
+
49
+ return o.children.map( &:name )
50
+ end
51
+
52
+ #
53
+ # Get the value of the selected item.
54
+ #
55
+ def key_for_option( selected )
56
+ o = find_child OPTIONS
57
+ return nil unless o
58
+
59
+ o.children.each do |c|
60
+ return c.value if c.name == selected
61
+ end
62
+
63
+ return nil
64
+ end
65
+
66
+ #
67
+ # Set the result of the system call.
68
+ #
69
+ def set_result( data )
70
+ r = find_child RESULT
71
+ return nil unless r
72
+
73
+ r.set_value data
74
+ end
75
+
76
+ # ---------------------------------------------------------------------
77
+ # Children
78
+ # ---------------------------------------------------------------------
79
+
80
+ #
81
+ # Does this object have children to add when an object
82
+ # is created in interactive mode?
83
+ # This does not apply during obj load, etc.
84
+ #
85
+ def add_children_on_create?
86
+ return true
87
+ end
88
+
89
+ #
90
+ # Add children to this object.
91
+ # This is used by containers to add children needed
92
+ # for default configurations.
93
+ #
94
+ def add_default_children
95
+ fac = $engine.factory
96
+ fac.create_string PROMPT, '>', self
97
+ fac.create_can OPTIONS, self
98
+ fac.create_string RESULT, nil, self
99
+ end
100
+
101
+ # ---------------------------------------------------------------------
102
+ # Messages
103
+ # ---------------------------------------------------------------------
104
+
105
+ #
106
+ # Get a list of message names that this object receives.
107
+ #
108
+ def self.messages
109
+ return super + %w[run]
110
+ end
111
+
112
+ #
113
+ # Show the prompt and get the user's selection.
114
+ #
115
+ def msg_run
116
+ prompt = prompt_value
117
+ return unless prompt
118
+
119
+ per = GlooLang::App::Settings.page_size
120
+ result = $prompt.select( prompt, options, :per_page => per )
121
+ set_result self.key_for_option( result )
122
+ end
123
+
124
+ end
125
+ end
126
+ end
@@ -0,0 +1,279 @@
1
+ # Author:: Eric Crane (mailto:eric.crane@mac.com)
2
+ # Copyright:: Copyright (c) 2019 Eric Crane. All rights reserved.
3
+ #
4
+ # A looping construct...do something for each whatever in something.
5
+ # This object has several possible uses:
6
+ # - each word in a string
7
+ # - each line in a string
8
+ # - each file in a directory
9
+ # - each git repo in a directory
10
+ #
11
+
12
+ module GlooLang
13
+ module Objs
14
+ class Each < GlooLang::Core::Obj
15
+
16
+ KEYWORD = 'each'.freeze
17
+ KEYWORD_SHORT = 'each'.freeze
18
+ CHILD = 'child'.freeze
19
+ WORD = 'word'.freeze
20
+ LINE = 'line'.freeze
21
+ FILE = 'file'.freeze
22
+ REPO = 'repo'.freeze
23
+ IN = 'IN'.freeze
24
+ DO = 'do'.freeze
25
+
26
+ #
27
+ # The name of the object type.
28
+ #
29
+ def self.typename
30
+ return KEYWORD
31
+ end
32
+
33
+ #
34
+ # The short name of the object type.
35
+ #
36
+ def self.short_typename
37
+ return KEYWORD_SHORT
38
+ end
39
+
40
+ #
41
+ # Get the URI from the child object.
42
+ # Returns nil if there is none.
43
+ #
44
+ def in_value
45
+ o = find_child IN
46
+ return o ? o.value : nil
47
+ end
48
+
49
+ #
50
+ # Run the do script once.
51
+ #
52
+ def run_do
53
+ o = find_child DO
54
+ return unless o
55
+
56
+ GlooLang::Exec::Dispatch.message 'run', o
57
+ end
58
+
59
+ # ---------------------------------------------------------------------
60
+ # Children
61
+ # ---------------------------------------------------------------------
62
+
63
+ #
64
+ # Does this object have children to add when an object
65
+ # is created in interactive mode?
66
+ # This does not apply during obj load, etc.
67
+ #
68
+ def add_children_on_create?
69
+ return true
70
+ end
71
+
72
+ #
73
+ # Add children to this object.
74
+ # This is used by containers to add children needed
75
+ # for default configurations.
76
+ #
77
+ def add_default_children
78
+ fac = $engine.factory
79
+ fac.create_string WORD, '', self
80
+ fac.create_string IN, '', self
81
+ fac.create_script DO, '', self
82
+ end
83
+
84
+ # ---------------------------------------------------------------------
85
+ # Messages
86
+ # ---------------------------------------------------------------------
87
+
88
+ #
89
+ # Get a list of message names that this object receives.
90
+ #
91
+ def self.messages
92
+ return super + [ 'run' ]
93
+ end
94
+
95
+ # Run the system command.
96
+ def msg_run
97
+ if each_child?
98
+ run_each_child
99
+ elsif each_word?
100
+ run_each_word
101
+ elsif each_line?
102
+ run_each_line
103
+ elsif each_repo?
104
+ run_each_repo
105
+ end
106
+ end
107
+
108
+ # ---------------------------------------------------------------------
109
+ # Child Object
110
+ # ---------------------------------------------------------------------
111
+
112
+ #
113
+ # Is it set up to run for each word?
114
+ # If there is a child object by the name "word"
115
+ # then we will loop for each word in the string.
116
+ #
117
+ def each_child?
118
+ return true if contains_child? CHILD
119
+
120
+ return false
121
+ end
122
+
123
+ #
124
+ # Run for each word.
125
+ #
126
+ def run_each_child
127
+ o = find_child IN
128
+ return unless o
129
+
130
+ o = GlooLang::Objs::Alias.resolve_alias( o )
131
+ o.children.each do |child|
132
+ set_child child
133
+ run_do
134
+ end
135
+ end
136
+
137
+ #
138
+ # Set the child alias.
139
+ #
140
+ def set_child( obj )
141
+ o = find_child CHILD
142
+ return unless o
143
+
144
+ o.set_value obj.pn
145
+ end
146
+
147
+ # ---------------------------------------------------------------------
148
+ # Word
149
+ # ---------------------------------------------------------------------
150
+
151
+ #
152
+ # Is it set up to run for each word?
153
+ # If there is a child object by the name "word"
154
+ # then we will loop for each word in the string.
155
+ #
156
+ def each_word?
157
+ return true if find_child WORD
158
+
159
+ return false
160
+ end
161
+
162
+ #
163
+ # Run for each word.
164
+ #
165
+ def run_each_word
166
+ str = in_value
167
+ return unless str
168
+
169
+ str.split( ' ' ).each do |word|
170
+ set_word word
171
+ run_do
172
+ end
173
+ end
174
+
175
+ #
176
+ # Set the value of the word.
177
+ #
178
+ def set_word( word )
179
+ o = find_child WORD
180
+ return unless o
181
+
182
+ o.set_value word
183
+ end
184
+
185
+ # ---------------------------------------------------------------------
186
+ # Line
187
+ # ---------------------------------------------------------------------
188
+
189
+ #
190
+ # Is it set up to run for each line?
191
+ # If there is a child object by the name "line"
192
+ # then we will loop for each line in the string.
193
+ #
194
+ def each_line?
195
+ return true if find_child LINE
196
+
197
+ return false
198
+ end
199
+
200
+ #
201
+ # Run for each line.
202
+ #
203
+ def run_each_line
204
+ str = in_value
205
+ return unless str
206
+
207
+ str.each_line do |line|
208
+ set_line line
209
+ run_do
210
+ end
211
+ end
212
+
213
+ #
214
+ # Set the value of the word.
215
+ #
216
+ def set_line( line )
217
+ o = find_child LINE
218
+ return unless o
219
+
220
+ o.set_value line
221
+ end
222
+
223
+ # ---------------------------------------------------------------------
224
+ # Git Repo
225
+ # ---------------------------------------------------------------------
226
+
227
+ #
228
+ # Is it set up to run for each git repo?
229
+ # If there is a child object by the name "repo"
230
+ # then we will loop for each repo in the directory.
231
+ #
232
+ def each_repo?
233
+ return true if find_child REPO
234
+
235
+ return false
236
+ end
237
+
238
+ #
239
+ # Find all git projects in a path.
240
+ #
241
+ def find_all_git_projects( path )
242
+ path.children.collect do |f|
243
+ if f.directory? && ( File.basename( f ) == '.git' )
244
+ File.dirname( f )
245
+ elsif f.directory?
246
+ find_all_git_projects( f )
247
+ end
248
+ end.flatten.compact
249
+ end
250
+
251
+ #
252
+ # Run for each line.
253
+ #
254
+ def run_each_repo
255
+ path = in_value
256
+ return unless path
257
+
258
+ path = Pathname.new( path )
259
+ repos = find_all_git_projects( path )
260
+ repos.each do |o|
261
+ set_repo o
262
+ run_do
263
+ end
264
+ end
265
+
266
+ #
267
+ # Set the value of the repo.
268
+ # This is a path to the repo.
269
+ #
270
+ def set_repo( path )
271
+ o = find_child REPO
272
+ return unless o
273
+
274
+ o.set_value path
275
+ end
276
+
277
+ end
278
+ end
279
+ end