gloo 7.1.3 → 7.3

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.
Files changed (109) hide show
  1. checksums.yaml +4 -4
  2. data/Rakefile +23 -0
  3. data/docs/application.md +65 -4
  4. data/docs/development.md +10 -0
  5. data/docs/iterators.md +66 -1
  6. data/docs/language_objects.md +34 -22
  7. data/docs/language_scripting.md +23 -2
  8. data/docs/language_syntax.md +86 -3
  9. data/docs/objects.md +6 -6
  10. data/docs/operators.md +36 -2
  11. data/docs/plugins.md +26 -0
  12. data/docs/testing.md +35 -9
  13. data/docs/verbs.md +31 -3
  14. data/lib/VERSION +1 -1
  15. data/lib/VERSION_NOTES +23 -0
  16. data/lib/dependencies.rb +4 -1
  17. data/lib/gloo/app/engine.rb +151 -23
  18. data/lib/gloo/app/prompt.rb +8 -0
  19. data/lib/gloo/convert/string_to_decimal.rb +3 -6
  20. data/lib/gloo/convert/string_to_integer.rb +3 -2
  21. data/lib/gloo/core/dictionary.rb +23 -12
  22. data/lib/gloo/core/obj.rb +77 -23
  23. data/lib/gloo/core/op.rb +99 -0
  24. data/lib/gloo/core/parser.rb +15 -0
  25. data/lib/gloo/core/tokens.rb +18 -0
  26. data/lib/gloo/core/verb.rb +130 -1
  27. data/lib/gloo/exec/dispatch.rb +7 -3
  28. data/lib/gloo/exec/runaway_recursion.rb +34 -0
  29. data/lib/gloo/exec/runner.rb +46 -0
  30. data/lib/gloo/exec/script.rb +16 -2
  31. data/lib/gloo/expr/expression.rb +77 -6
  32. data/lib/gloo/expr/l_decimal.rb +18 -2
  33. data/lib/gloo/expr/l_integer.rb +5 -2
  34. data/lib/gloo/expr/op_div.rb +4 -0
  35. data/lib/gloo/expr/op_eq.rb +4 -0
  36. data/lib/gloo/expr/op_gt.rb +4 -0
  37. data/lib/gloo/expr/op_gteq.rb +4 -0
  38. data/lib/gloo/expr/op_ineq.rb +4 -0
  39. data/lib/gloo/expr/op_lt.rb +4 -0
  40. data/lib/gloo/expr/op_lteq.rb +4 -0
  41. data/lib/gloo/expr/op_minus.rb +4 -0
  42. data/lib/gloo/expr/op_mult.rb +3 -7
  43. data/lib/gloo/expr/op_plus.rb +8 -1
  44. data/lib/gloo/objs/basic/alias.rb +6 -1
  45. data/lib/gloo/objs/basic/boolean.rb +15 -6
  46. data/lib/gloo/objs/basic/decimal.rb +25 -3
  47. data/lib/gloo/objs/basic/script.rb +3 -24
  48. data/lib/gloo/objs/basic/string_msgs.rb +50 -68
  49. data/lib/gloo/objs/ctrl/until.rb +219 -0
  50. data/lib/gloo/persist/declaration_ledger.rb +9 -9
  51. data/lib/gloo/persist/declaration_writer.rb +208 -0
  52. data/lib/gloo/persist/disc_mech.rb +70 -39
  53. data/lib/gloo/persist/file_loader.rb +125 -66
  54. data/lib/gloo/persist/file_saver.rb +130 -198
  55. data/lib/gloo/persist/file_storage.rb +15 -4
  56. data/lib/gloo/persist/file_style.rb +89 -0
  57. data/lib/gloo/persist/line_splitter.rb +16 -1
  58. data/lib/gloo/persist/persist_man.rb +112 -30
  59. data/lib/gloo/persist/save_index.rb +211 -0
  60. data/lib/gloo/persist/script_body_collector.rb +16 -21
  61. data/lib/gloo/persist/source/blank_node.rb +10 -3
  62. data/lib/gloo/persist/source/comment_node.rb +10 -2
  63. data/lib/gloo/persist/source/directive_node.rb +10 -2
  64. data/lib/gloo/persist/source/obj_node.rb +174 -9
  65. data/lib/gloo/persist/source/source_doc.rb +54 -0
  66. data/lib/gloo/persist/subtree_extractor.rb +9 -37
  67. data/lib/gloo/persist/trivia_buffer.rb +7 -7
  68. data/lib/gloo/plugin/callback.rb +54 -10
  69. data/lib/gloo/plugin/ext_manager.rb +44 -0
  70. data/lib/gloo/plugin/lib_manager.rb +65 -1
  71. data/lib/gloo/shell/runner.rb +36 -9
  72. data/lib/gloo/verbs/break.rb +6 -4
  73. data/lib/gloo/verbs/check.rb +8 -2
  74. data/lib/gloo/verbs/create.rb +13 -4
  75. data/lib/gloo/verbs/if.rb +33 -10
  76. data/lib/gloo/verbs/invoke.rb +6 -1
  77. data/lib/gloo/verbs/list.rb +9 -2
  78. data/lib/gloo/verbs/load.rb +31 -22
  79. data/lib/gloo/verbs/move.rb +50 -5
  80. data/lib/gloo/verbs/put.rb +9 -2
  81. data/lib/gloo/verbs/reload.rb +2 -1
  82. data/lib/gloo/verbs/run.rb +8 -1
  83. data/lib/gloo/verbs/save.rb +30 -4
  84. data/lib/gloo/verbs/tell.rb +9 -2
  85. data/lib/gloo/verbs/unless.rb +34 -10
  86. data/lib/gloo/verbs/unload.rb +81 -16
  87. data/test.gloo/app/exit_status.test.gloo +34 -0
  88. data/test.gloo/app/fixtures/exit_after_error.gloo +9 -0
  89. data/test.gloo/ctrl/until.test.gloo +100 -0
  90. data/test.gloo/lang/computed_paths.test.gloo +98 -0
  91. data/test.gloo/lang/declaration.test.gloo +36 -0
  92. data/test.gloo/lang/fixtures/mixed_indent.gloo +5 -0
  93. data/test.gloo/lang/literal.test.gloo +43 -0
  94. data/test.gloo/lang/naming.test.gloo +44 -6
  95. data/test.gloo/lang/ops.test.gloo +193 -0
  96. data/test.gloo/lang/runaway_recursion.test.gloo +44 -0
  97. data/test.gloo/objs/decimal.test.gloo +23 -0
  98. data/test.gloo/objs/string.test.gloo +30 -0
  99. data/test.gloo/verbs/create.test.gloo +6 -21
  100. data/test.gloo/verbs/fixtures/load_beside.gloo +6 -0
  101. data/test.gloo/verbs/fixtures/lossless_text.gloo +7 -0
  102. data/test.gloo/verbs/fixtures/lossless_text_saved.gloo +9 -0
  103. data/test.gloo/verbs/if.test.gloo +41 -0
  104. data/test.gloo/verbs/load.test.gloo +22 -3
  105. data/test.gloo/verbs/move.test.gloo +39 -0
  106. data/test.gloo/verbs/save.test.gloo +239 -0
  107. data/test.gloo/verbs/unless.test.gloo +40 -0
  108. data/test.gloo/verbs/unload.test.gloo +61 -3
  109. metadata +15 -1
@@ -0,0 +1,219 @@
1
+ # Author:: Eric Crane (mailto:eric.crane@mac.com)
2
+ # Copyright:: Copyright (c) 2026 Eric Crane. All rights reserved.
3
+ #
4
+ # A looping construct...do something until a condition is true.
5
+ #
6
+
7
+ module Gloo
8
+ module Objs
9
+ class Until < Gloo::Core::Obj
10
+
11
+ KEYWORD = 'until'.freeze
12
+ KEYWORD_SHORT = 'until'.freeze
13
+ DO = 'do'.freeze
14
+ DONE = 'done'.freeze
15
+ INDEX = 'index'.freeze
16
+ MAX = 'max'.freeze
17
+ NO_DONE_ERR = 'Could not run until loop %s: it has no done script.'.freeze
18
+ MAX_ERR = "Could not finish until loop %s: done wasn't true after %s passes.".freeze
19
+
20
+ #
21
+ # The name of the object type.
22
+ #
23
+ def self.typename
24
+ return KEYWORD
25
+ end
26
+
27
+ #
28
+ # The short name of the object type.
29
+ #
30
+ def self.short_typename
31
+ return KEYWORD_SHORT
32
+ end
33
+
34
+ #
35
+ # The most passes to run, or nil for no limit. A missing max, or
36
+ # one that isn't a positive number, is no limit.
37
+ #
38
+ def max
39
+ value = find_child_value( MAX )
40
+ return nil unless value.is_a?( ::Integer ) && value.positive?
41
+
42
+ return value
43
+ end
44
+
45
+ #
46
+ # Set the index of the current pass, if there is an index child.
47
+ #
48
+ def set_index( index )
49
+ o = find_child INDEX
50
+ return unless o
51
+
52
+ o.set_value index
53
+ end
54
+
55
+ #
56
+ # Run the do script once. Returns true if a break loop in it
57
+ # ended the run, so the loop should stop. A plain break ends only
58
+ # the do script, and the loop goes on.
59
+ #
60
+ def run_do
61
+ o = find_child DO
62
+ return false unless o
63
+
64
+ Gloo::Exec::Dispatch.message( @engine, 'run', o )
65
+ return o.respond_to?( :broke_loop? ) && o.broke_loop?
66
+ end
67
+
68
+ #
69
+ # Run the done script, and say whether the loop is done: its
70
+ # result (in it) by the [bool] rule. Nil if anything in it
71
+ # reported an error: the answer can't be known, so the loop stops.
72
+ #
73
+ def run_done( done )
74
+ errors_before = @engine.run_error_count
75
+ Gloo::Exec::Dispatch.message( @engine, 'run', done )
76
+ return nil if @engine.run_error_count > errors_before
77
+
78
+ return Gloo::Objs::Boolean.to_bool( @engine, @engine.heap.it.value )
79
+ end
80
+
81
+ # ---------------------------------------------------------------------
82
+ # Children
83
+ # ---------------------------------------------------------------------
84
+
85
+ #
86
+ # Does this object have children to add when an object
87
+ # is created in interactive mode?
88
+ # This does not apply during obj load, etc.
89
+ #
90
+ def add_children_on_create?
91
+ return true
92
+ end
93
+
94
+ #
95
+ # Add children to this object: just the do and done scripts.
96
+ # index and max are optional.
97
+ #
98
+ def add_default_children
99
+ fac = @engine.factory
100
+ fac.create_script DO, '', self
101
+ fac.create_script DONE, '', self
102
+ end
103
+
104
+ # ---------------------------------------------------------------------
105
+ # Messages
106
+ # ---------------------------------------------------------------------
107
+
108
+ #
109
+ # Get a list of message names that this object receives.
110
+ #
111
+ def self.messages
112
+ return super + [ 'run' ]
113
+ end
114
+
115
+ #
116
+ # Run the loop: do, then done, until done is true. Stops early on
117
+ # break loop in do, or an error in done; with a max, reaching it
118
+ # is an error.
119
+ #
120
+ def msg_run
121
+ done = find_child DONE
122
+ return fail_with( format( NO_DONE_ERR, pn ) ) if blank_script?( done )
123
+
124
+ limit = max
125
+ pass = 0
126
+ loop do
127
+ set_index pass
128
+ return if run_do
129
+
130
+ finished = run_done( done )
131
+ # nil: done reported an error, already reported.
132
+ return @engine.heap.it.set_to( false ) if finished.nil?
133
+ return if finished
134
+
135
+ pass += 1
136
+ return fail_with( format( MAX_ERR, pn, limit ) ) if limit && pass >= limit
137
+ end
138
+ end
139
+
140
+ # ---------------------------------------------------------------------
141
+ # Private functions
142
+ # ---------------------------------------------------------------------
143
+
144
+ private
145
+
146
+ #
147
+ # Is the script missing, or without a line to run?
148
+ #
149
+ def blank_script?( script )
150
+ return true if script.nil?
151
+
152
+ lines = Array( script.value )
153
+ return lines.all? { |line| line.to_s.strip.empty? }
154
+ end
155
+
156
+ #
157
+ # Report that the loop couldn't run or finish; it is false.
158
+ #
159
+ def fail_with( msg )
160
+ @engine.err msg
161
+ @engine.heap.it.set_to false
162
+ end
163
+
164
+ # ---------------------------------------------------------------------
165
+ # Object Documentation
166
+ # ---------------------------------------------------------------------
167
+
168
+ #
169
+ # Get the object's documentation data.
170
+ #
171
+ def self.doc_data
172
+ {
173
+ :name => KEYWORD,
174
+ :shortcut => KEYWORD_SHORT,
175
+ :description => 'Run a script again and again until a condition ' \
176
+ 'is true. Each pass runs do, then done; the loop ends when ' \
177
+ "done's result is true. do always runs at least once.",
178
+ :children => [
179
+ 'do (script) — The action to perform on each pass.',
180
+ "done (script) — Required. Run after each pass; the loop ends when its result (it) is true. Usually one line: done [script] : eval ^^.x > 100. True and false follow the [bool] rule (see doc language_syntax, What Counts as True).",
181
+ 'index (integer) — Optional. The current pass, counting from 0.',
182
+ 'max (integer) — Optional. The most passes to run. Without it (or with 0) there is no limit, and a loop whose done never comes true runs until stopped.'
183
+ ],
184
+ :messages => [
185
+ 'run — Run the loop until done is true.'
186
+ ],
187
+ :errors => [
188
+ "#{format( NO_DONE_ERR, '{path}' )} — There is no done script, or it is blank. Nothing runs, and it is false.",
189
+ "#{format( MAX_ERR, '{path}', '{max}' )} — max passes ran and done never came true. it is false.",
190
+ 'Any error in done ends the loop: whether it is done can\'t be known. it is false. An error in do doesn\'t; the next pass may work.'
191
+ ],
192
+ :notes => 'break in do skips the rest of the pass; break loop ends the loop (see doc iterators, Skipping an Item or Ending a Loop). done is checked after each pass, so do always runs at least once.',
193
+ :examples => <<~EXAMPLES.strip
194
+ count [can] :
195
+ x [int] : 0
196
+ up [until] :
197
+ do [script] : tell count.x to inc
198
+ done [script] : eval count.x > 100
199
+ on_load [script] :
200
+ tell count.up to run
201
+ show count.x
202
+
203
+ retry [can] :
204
+ tries [int] : 0
205
+ pick [until] :
206
+ max [int] : 5
207
+ index [int] :
208
+ do [script] : tell retry.tries to inc
209
+ done [script] : eval retry.tries = 3
210
+ on_load [script] :
211
+ tell retry.pick to run
212
+ show retry.pick.index
213
+ EXAMPLES
214
+ }
215
+ end
216
+
217
+ end
218
+ end
219
+ end
@@ -3,8 +3,8 @@
3
3
  #
4
4
  # Tracks what a single file declares as it loads: its top-level
5
5
  # objects (for save resolution), and every object it created -- so a
6
- # re-declaration of a name an earlier file already used can be
7
- # flagged, since in that case the second value is silently dropped.
6
+ # re-declaration of a name already used, in an earlier file or earlier
7
+ # in this one, can be flagged when its value is ignored.
8
8
  #
9
9
 
10
10
  module Gloo
@@ -39,19 +39,19 @@ module Gloo
39
39
 
40
40
  #
41
41
  # `prior` already existed when this file declared `name` with
42
- # `value`. If another file created it and the values differ, the
43
- # new value is being ignored -- warn so it isn't a surprise. A
44
- # duplicate name inside one file is left alone (a documented
45
- # "first one wins"). The warning's location (this file and line)
46
- # comes from the load.
42
+ # `value`: the line names the same object, and its value is
43
+ # ignored (the first declaration wins). When the ignored value is
44
+ # different, warn so it isn't a surprise -- whether the first
45
+ # declaration was in another file or earlier in this one. The
46
+ # warning's location (this file and line) comes from the load.
47
47
  #
48
48
  def clash( prior, name, value )
49
- return if @mine.key?( prior )
50
49
  return if value.nil? || value.to_s.strip.empty?
51
50
  return if prior.value.to_s == value.to_s
52
51
 
52
+ where = @mine.key?( prior ) ? 'above' : 'in another loaded file'
53
53
  @engine.warn(
54
- "'#{name}' is already declared in another loaded file with a " \
54
+ "'#{name}' is already declared #{where} with a " \
55
55
  "different value (#{prior.value.inspect} kept, #{value.inspect} ignored)."
56
56
  )
57
57
  end
@@ -0,0 +1,208 @@
1
+ # Author:: Eric Crane (mailto:eric.crane@mac.com)
2
+ # Copyright:: Copyright (c) 2026 Eric Crane. All rights reserved.
3
+ #
4
+ # Writes the text a save generates: a changed declaration (its head
5
+ # kept as written, only the value written again) or a declaration for
6
+ # a new object, in the file's style (see FileStyle).
7
+ #
8
+ # A value is written so it loads back the same: on the line, as a
9
+ # BEGIN/END block (exactly as it is -- a block's lines are the value),
10
+ # or as a script body. A value with no exact spelling is written as
11
+ # the closest one, with a warning.
12
+ #
13
+
14
+ module Gloo
15
+ module Persist
16
+ class DeclarationWriter
17
+
18
+ BEGIN_BLOCK = 'BEGIN'.freeze
19
+ END_BLOCK = 'END'.freeze
20
+ SEP = ' : '.freeze
21
+ NOT_EXACT = "Could not save '%s' exactly: %s.".freeze
22
+ NO_FINAL_NEWLINE = 'a block value always ends with a newline'.freeze
23
+ END_LINE = 'a line that is just END ends the block early'.freeze
24
+ NOT_UTF8 = 'it is not valid UTF-8'.freeze
25
+
26
+ #
27
+ # Set up a writer for a file with the given style.
28
+ #
29
+ def initialize( engine, style )
30
+ @engine = engine
31
+ @style = style
32
+ end
33
+
34
+ #
35
+ # The new text of a declaration whose object's value changed: its
36
+ # head as written, then the value.
37
+ #
38
+ def changed_text( node )
39
+ obj = node.obj
40
+ return body_text( node.head, node.raw_tail, obj, body_indent( node ), trailing_blanks( node ) ) if body?( obj, node.block_style == :body )
41
+
42
+ return value_text( node.head, separator( node ), obj, node.raw_indent, node.block_style == :begin_end )
43
+ end
44
+
45
+ #
46
+ # A source node for a new object (without its children), at the
47
+ # given indentation, written with the given name (its own, or a
48
+ # dotted path for a shorthand line).
49
+ #
50
+ def new_node( obj, indent, name = obj.name )
51
+ spelled = @style.type_for( obj )
52
+ head = spelled ? "#{indent}#{name} [#{spelled}]" : "#{indent}#{name}"
53
+ node = Source::ObjNode.new( :name => name, :raw_type => spelled || obj.type_display )
54
+ node.raw_indent = indent
55
+ node.head = head
56
+ node.obj = obj
57
+ node.raw = if body?( obj, false )
58
+ body_text( head, ' :', obj, "#{indent}#{@style.unit}", '' )
59
+ else
60
+ value_text( head, SEP, obj, indent, false )
61
+ end
62
+ node.block_style = block_style_of( node.raw )
63
+ node.raw_tail = node.raw.scrub.lines.first.chomp.delete_prefix( head )
64
+ return node
65
+ end
66
+
67
+ private
68
+
69
+ # ---------------------------------------------------------------------
70
+ # Script bodies
71
+ # ---------------------------------------------------------------------
72
+
73
+ #
74
+ # Is the value written as a script body: a multi-line value (a
75
+ # script) whose value is a list of lines, or that was a body?
76
+ #
77
+ def body?( obj, was_body )
78
+ return false unless obj.multiline_value?
79
+
80
+ return was_body || obj.value_is_array?
81
+ end
82
+
83
+ #
84
+ # A script body: the head and tail (eg. ' :'), then each command
85
+ # on its own line at the body's indentation, then any blank lines
86
+ # that followed the body as written.
87
+ #
88
+ def body_text( head, tail, obj, indent, trailing )
89
+ lines = script_lines( obj )
90
+ str = "#{head}#{tail.rstrip}\n"
91
+ lines.each { |line| str << "#{indent}#{line}\n" }
92
+ return "#{str}#{trailing}"
93
+ end
94
+
95
+ #
96
+ # A script's command lines, whatever form its value is in.
97
+ #
98
+ def script_lines( obj )
99
+ return obj.value if obj.value_is_array?
100
+ return [] if obj.value.to_s.strip.empty?
101
+
102
+ return [ obj.value.to_s.strip ]
103
+ end
104
+
105
+ #
106
+ # The indentation of a body as written: its first non-blank line's,
107
+ # or one step in from the declaration.
108
+ #
109
+ def body_indent( node )
110
+ line = node.raw.lines.drop( 1 ).find { |l| !l.strip.empty? }
111
+ return line[ /\A[ \t]*/ ] if line
112
+
113
+ return "#{node.raw_indent}#{@style.unit}"
114
+ end
115
+
116
+ #
117
+ # The blank lines at the end of a body as written, kept when the
118
+ # body is written again.
119
+ #
120
+ def trailing_blanks( node )
121
+ lines = node.raw.lines.drop( 1 )
122
+ blanks = []
123
+ blanks.unshift( lines.pop ) while lines.any? && lines.last.strip.empty?
124
+ return blanks.join
125
+ end
126
+
127
+ # ---------------------------------------------------------------------
128
+ # Values on the line or in a BEGIN/END block
129
+ # ---------------------------------------------------------------------
130
+
131
+ #
132
+ # The declaration with its value: on the line if it can be written
133
+ # there, otherwise as a BEGIN/END block. A value that was a block
134
+ # stays one if it still ends with a newline.
135
+ #
136
+ def value_text( head, sep, obj, indent, was_block )
137
+ value = obj.value_display.to_s
138
+ warn_not_exact( obj, NOT_UTF8 ) unless value.valid_encoding?
139
+ return block_text( head, sep, obj, value, indent ) if block?( value, was_block )
140
+ return "#{head}#{sep.rstrip}\n" if value.empty?
141
+
142
+ return "#{head}#{sep}#{value}\n"
143
+ end
144
+
145
+ #
146
+ # Does the value need (or keep) a BEGIN/END block? A line break
147
+ # can't go on the line, and a value ending in a backslash or in
148
+ # BEGIN would be read as a continuation or a block start.
149
+ #
150
+ def block?( value, was_block )
151
+ return true if was_block && value.end_with?( "\n" )
152
+
153
+ clean = value.scrub
154
+ return clean.include?( "\n" ) || clean.end_with?( '\\' ) || clean.strip.end_with?( BEGIN_BLOCK )
155
+ end
156
+
157
+ #
158
+ # A BEGIN/END block: the value's lines exactly as they are, and END
159
+ # lined up with the declaration.
160
+ #
161
+ def block_text( head, sep, obj, value, indent )
162
+ content = value
163
+ unless content.end_with?( "\n" )
164
+ warn_not_exact( obj, NO_FINAL_NEWLINE )
165
+ content = "#{content}\n"
166
+ end
167
+ warn_not_exact( obj, END_LINE ) if content.scrub.lines.any? { |l| l.strip == END_BLOCK }
168
+
169
+ return "#{head}#{sep}#{BEGIN_BLOCK}\n#{content}#{indent}#{END_BLOCK}\n"
170
+ end
171
+
172
+ #
173
+ # What goes between a declaration's head and its value, as written:
174
+ # usually ' : '.
175
+ #
176
+ def separator( node )
177
+ tail = node.raw_tail.to_s
178
+ return tail.sub( /#{BEGIN_BLOCK}\s*\z/o, '' ) if node.block_style == :begin_end
179
+
180
+ value = node.raw_value.to_s
181
+ return tail[ 0, tail.length - value.length ] if !value.empty? && tail.end_with?( value )
182
+
183
+ trimmed = tail.rstrip
184
+ return trimmed.end_with?( ':' ) ? "#{trimmed} " : SEP
185
+ end
186
+
187
+ #
188
+ # The block style of a declaration written from scratch.
189
+ #
190
+ def block_style_of( text )
191
+ text = text.scrub
192
+ first = text.lines.first.to_s
193
+ return :begin_end if first.rstrip.end_with?( BEGIN_BLOCK ) && text.lines.size > 1
194
+ return :body if text.lines.size > 1
195
+
196
+ return :inline
197
+ end
198
+
199
+ #
200
+ # Warn that a value is saved as the closest thing gloo can write.
201
+ #
202
+ def warn_not_exact( obj, why )
203
+ @engine.warn format( NOT_EXACT, obj.pn, why )
204
+ end
205
+
206
+ end
207
+ end
208
+ end
@@ -30,28 +30,42 @@ module Gloo
30
30
  #
31
31
  # Get all the gloo files in the folder (partial path). The folder
32
32
  # can end with the start of a file name, as a prefix (folder/a).
33
+ # A relative folder is looked for in each of the given folders in
34
+ # turn (see default_folders); the first that has matching files
35
+ # wins.
33
36
  #
34
- def get_all_files_in( folder )
35
- dir = folder_for_files( folder )
36
- return Dir.glob( "#{dir}*.gloo" )
37
+ def get_all_files_in( folder, folders = nil )
38
+ return Dir.glob( "#{absolute_folder( folder )}*.gloo" ) if absolute?( folder )
39
+
40
+ ( folders || default_folders ).each do |base|
41
+ files = Dir.glob( "#{File.join( base, folder )}*.gloo" )
42
+ return files.map { |f| File.expand_path( f ) } unless files.empty?
43
+ end
44
+ return []
37
45
  end
38
46
 
39
47
  #
40
- # Where to look for a folder of files: an absolute or ~ folder as
41
- # given (~ expanded), anything else in the project folder, or if it
42
- # isn't there, in the gloo home.
48
+ # An absolute or ~ folder, with ~ expanded.
43
49
  #
44
- def folder_for_files( folder )
50
+ def absolute_folder( folder )
45
51
  return folder.sub( '~', Dir.home ) if folder.start_with?( '~' )
46
- return folder if File.absolute_path?( folder )
47
52
 
48
- dir = File.join( @engine.settings.project_path, folder )
49
- return dir if Dir.exist?( dir )
53
+ return folder
54
+ end
55
+
56
+ #
57
+ # Is the name a full path, or one starting with ~?
58
+ #
59
+ def absolute?( name )
60
+ return name.start_with?( '~' ) || File.absolute_path?( name )
61
+ end
50
62
 
51
- @log.debug "Folder does not exist in project: #{folder}"
52
- dir = File.join( @engine.settings.user_root, folder )
53
- @log.debug "Looking in gloo home? found == #{Dir.exist?( dir )}"
54
- return dir
63
+ #
64
+ # Where a relative name is looked for when no folders are given:
65
+ # the project folder, then the gloo home.
66
+ #
67
+ def default_folders
68
+ return [ @engine.settings.project_path, @engine.settings.user_root ]
55
69
  end
56
70
 
57
71
  #
@@ -75,59 +89,76 @@ module Gloo
75
89
 
76
90
  #
77
91
  # Expand a single file path. The .gloo extension is optional: it's
78
- # added when the name doesn't end with it, for a full or ~ path
79
- # and for a name in the project or gloo home.
92
+ # added when the name doesn't end with it.
80
93
  #
81
- def expand( name )
94
+ # A full or ~ path is used as given. A relative name is looked for
95
+ # in each of the given folders in turn (the load verb's order, see
96
+ # PersistMan#search_folders). With no folders given (a file named
97
+ # on the command line), it's tried as given, relative to the
98
+ # working folder, and then in the project folder and gloo home.
99
+ #
100
+ def expand( name, folders = nil )
101
+ full_name = name.end_with?( file_ext ) ? name : "#{name}#{file_ext}"
102
+ return expand_in( full_name, folders ) if folders && !absolute?( name )
103
+
82
104
  # Try the name as given: a full path, or relative to the working
83
105
  # folder (eg. gloo hello_world.gloo).
84
106
  ext_path = File.expand_path( name )
85
107
  return [ ext_path ] if self.valid?( ext_path )
86
108
 
87
- full_name = name.end_with?( file_ext ) ? name : "#{name}#{file_ext}"
88
-
89
109
  # A full or ~ path without the extension.
90
- if name.start_with?( '~' ) || File.absolute_path?( name )
110
+ if absolute?( name )
91
111
  ext_path = File.expand_path( full_name )
92
112
  return [ ext_path ] if self.valid?( ext_path )
93
113
  end
94
114
 
95
- # Try in projects
96
- pn = File.join( @engine.settings.project_path, full_name )
97
- return [ pn ] if self.valid?( pn )
98
-
99
- # Try in user root
100
- pn = File.join( @engine.settings.user_root, full_name )
101
- return [ pn ] if self.valid?( pn )
115
+ return expand_in( full_name, default_folders )
116
+ end
102
117
 
118
+ #
119
+ # The file in the first of the folders that has it, as a one-item
120
+ # list; nil if none does.
121
+ #
122
+ def expand_in( full_name, folders )
123
+ folders.each do |base|
124
+ pn = File.expand_path( File.join( base, full_name ) )
125
+ return [ pn ] if self.valid?( pn )
126
+ end
103
127
  return nil
104
128
  end
105
129
 
106
130
  #
107
- # Read in the contents of a single file.
131
+ # Read in the contents of a single file, as UTF-8 whatever the
132
+ # locale: a .gloo file is UTF-8 (under LANG=C, a plain read would
133
+ # take every non-ASCII character as an invalid byte). Bytes that
134
+ # aren't valid UTF-8 are kept as they are; the loader warns about
135
+ # them.
108
136
  #
109
137
  def read( file )
110
- return File.read( file )
138
+ return File.binread( file ).force_encoding( Encoding::UTF_8 )
111
139
  end
112
140
 
113
141
  #
114
142
  # Resolve a path to save a new (not-yet-existing) file to, given
115
- # a name or relative path -- same convention as expand (relative
116
- # to the project root, .gloo appended if missing), but without
117
- # requiring the file to already exist.
143
+ # a name or relative path, with .gloo appended if missing. Unlike
144
+ # expand, the file doesn't have to exist. A full path is used as
145
+ # given and a ~ path is expanded. A relative name goes in the
146
+ # given folder (the first place a load would look, see
147
+ # PersistMan#save_folder), or the project folder if none is given.
118
148
  #
119
- def resolve_save_path( name )
149
+ def resolve_save_path( name, folder = nil )
120
150
  full_name = name.end_with?( file_ext ) ? name : "#{name}#{file_ext}"
121
- return full_name if full_name.start_with?( '/' )
151
+ return File.expand_path( absolute_folder( full_name ) ) if absolute?( full_name )
122
152
 
123
- return File.join( @engine.settings.project_path, full_name )
153
+ return File.expand_path( File.join( folder || @engine.settings.project_path, full_name ) )
124
154
  end
125
155
 
126
- #
127
- # Write data to the file.
128
- #
156
+ #
157
+ # Write data to the file, byte for byte (no conversion for the
158
+ # locale).
159
+ #
129
160
  def write( pn, data )
130
- File.write( pn, data )
161
+ File.binwrite( pn, data )
131
162
  end
132
163
 
133
164
  end