fsorg 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/fsorg.rb +24 -40
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ddcf3941d7eb4d9fc41fff221b3b178121564c52a8bfe29aabc2b30a8e0e70d7
|
4
|
+
data.tar.gz: a7b50828bdccb1a2afc8ed7911fec2c5ef6f4ebe846d86be774438c7480c8e10
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: de9ff6a647a0640cac2cc1d7ea297834b70cc9eab702f2be37565acf340cfdaaa9d35353edd6a86979131c18686f348840d7716c597d7516613f1be5e937553f
|
7
|
+
data.tar.gz: 238a5c6b2d33689d66675ea1204b0babd74da5513471c8addd1f90263e8073680c464211d16b69bde824d11e49d7e6aa48a35cf6eb3af4f7b78653a8e4754450
|
data/lib/fsorg.rb
CHANGED
@@ -21,7 +21,6 @@ class Fsorg
|
|
21
21
|
@document = document
|
22
22
|
@current_line = 0
|
23
23
|
@current_depth = -1
|
24
|
-
@to_write = [] # [ { :path, :content, :permissions } ]
|
25
24
|
end
|
26
25
|
|
27
26
|
def from_relative_to_root(path)
|
@@ -47,7 +46,6 @@ class Fsorg
|
|
47
46
|
# TODO process_see
|
48
47
|
process_for
|
49
48
|
process_if
|
50
|
-
store_writes
|
51
49
|
turn_puts_into_runs
|
52
50
|
ask_missing_variables
|
53
51
|
process_root
|
@@ -134,7 +132,7 @@ class Fsorg
|
|
134
132
|
filepath = @document_path.parent.join(line.sub /^INCLUDE /, "")
|
135
133
|
included_raw = File.new(filepath).read.strip
|
136
134
|
included_fsorg = Fsorg.new(@root_directory, @data, included_raw, filepath)
|
137
|
-
included_fsorg.preprocess
|
135
|
+
included_fsorg.preprocess
|
138
136
|
@data = included_fsorg.data.merge @data
|
139
137
|
output += included_fsorg.document.lines(chomp: true)
|
140
138
|
else
|
@@ -147,25 +145,7 @@ class Fsorg
|
|
147
145
|
|
148
146
|
def store_writes
|
149
147
|
output = []
|
150
|
-
|
151
|
-
inside_write_directive = -> { !current.keys.empty? }
|
152
|
-
|
153
|
-
@document.lines(chomp: true).each_with_index do |line, index|
|
154
|
-
@current_line = index + 1
|
155
|
-
if inside_write_directive.()
|
156
|
-
if line.strip == "}"
|
157
|
-
@to_write << current
|
158
|
-
current = {}
|
159
|
-
else
|
160
|
-
current[:content] += line + "\n"
|
161
|
-
end
|
162
|
-
elsif /^WRITE(\s+INTO)?\s+(?<destination>.+?)(?:\s+MODE\s+(?<permissions>.+?))?\s*\{$/.match line.strip
|
163
|
-
current = $~.named_captures.transform_keys(&:to_sym)
|
164
|
-
current[:content] = ""
|
165
|
-
else
|
166
|
-
output << line
|
167
|
-
end
|
168
|
-
end
|
148
|
+
|
169
149
|
|
170
150
|
@document = output.join "\n"
|
171
151
|
end
|
@@ -292,7 +272,7 @@ class Fsorg
|
|
292
272
|
end
|
293
273
|
|
294
274
|
def ask_missing_variables
|
295
|
-
|
275
|
+
@document.scan /\{\{(?<variable>[^}]+?)\}\}/ do |variable|
|
296
276
|
unless @data.include? variable[0].to_sym
|
297
277
|
@data[variable[0].to_sym] = :ask
|
298
278
|
end
|
@@ -338,16 +318,12 @@ class Fsorg
|
|
338
318
|
@data[:HERE]
|
339
319
|
end
|
340
320
|
|
341
|
-
def execute_writes(dry_run, quiet)
|
342
|
-
@to_write.each do |future_file|
|
343
|
-
do_write future_file, dry_run, quiet
|
344
|
-
end
|
345
|
-
end
|
346
|
-
|
347
321
|
def walk(dry_run, quiet, verbose)
|
348
322
|
current_path = [@root_directory]
|
349
|
-
current_path_as_pathname = -> { current_path.reduce(Pathname.new "") { |path, fragment| path.join fragment } }
|
350
323
|
@data[:HERE] = @root_directory
|
324
|
+
file_to_write = {}
|
325
|
+
inside_write_directive = -> { !file_to_write.keys.empty? }
|
326
|
+
current_path_as_pathname = -> { current_path.reduce(Pathname.new "") { |path, fragment| path.join fragment } }
|
351
327
|
|
352
328
|
if verbose
|
353
329
|
puts "Data is #{@data.except :HERE}".light_black
|
@@ -361,7 +337,17 @@ class Fsorg
|
|
361
337
|
line = line.gsub "{{#{key}}}", value.to_s
|
362
338
|
end
|
363
339
|
|
364
|
-
if
|
340
|
+
if inside_write_directive.()
|
341
|
+
if line.strip == "}"
|
342
|
+
do_write file_to_write, dry_run, quiet
|
343
|
+
file_to_write = {}
|
344
|
+
else
|
345
|
+
file_to_write[:content] += line + "\n"
|
346
|
+
end
|
347
|
+
elsif /^WRITE(\s+INTO)?\s+(?<destination>.+?)(?:\s+MODE\s+(?<permissions>.+?))?\s*\{$/.match line.strip
|
348
|
+
file_to_write = $~.named_captures.transform_keys(&:to_sym)
|
349
|
+
file_to_write[:content] = ""
|
350
|
+
elsif /^(?<leaf>.+?)\s+\{/ =~ line.strip
|
365
351
|
current_path << leaf
|
366
352
|
@data[:HERE] = current_path_as_pathname.()
|
367
353
|
if verbose
|
@@ -384,11 +370,6 @@ class Fsorg
|
|
384
370
|
do_run command, current_location, environment, dry_run, quiet, verbose
|
385
371
|
end
|
386
372
|
end
|
387
|
-
|
388
|
-
@current_depth = 0
|
389
|
-
# TODO do writes alongside other operations
|
390
|
-
puts ("Writing files " + "─" * 40).light_black
|
391
|
-
execute_writes dry_run, quiet
|
392
373
|
end
|
393
374
|
|
394
375
|
def do_mkpath(path, dry_run, quiet)
|
@@ -401,14 +382,17 @@ class Fsorg
|
|
401
382
|
end
|
402
383
|
|
403
384
|
def do_write(future_file, dry_run, quiet)
|
404
|
-
|
405
|
-
|
385
|
+
indentation = " " * @current_depth
|
386
|
+
dest = from_relative_to_root(current_location) / future_file[:destination]
|
387
|
+
unless dest.parent.relative_path_from(@root_directory).to_s == "."
|
388
|
+
do_mkpath dest.parent, dry_run, quiet
|
389
|
+
end
|
406
390
|
|
407
391
|
unless quiet
|
408
|
-
puts "> ".cyan.bold + dest.relative_path_from(@root_directory).to_s + (future_file[:permissions] ? " mode #{future_file[:permissions]}".yellow : "")
|
392
|
+
puts "#{indentation}> ".cyan.bold + dest.relative_path_from(@root_directory).to_s + (future_file[:permissions] ? " mode #{future_file[:permissions]}".yellow : "")
|
409
393
|
end
|
410
394
|
unless dry_run
|
411
|
-
dest.write replace_data future_file[:content]
|
395
|
+
dest.write deindent replace_data future_file[:content]
|
412
396
|
# Not using dest.chmod as the syntax for permissions is more than just integers,
|
413
397
|
# and matches in fact the exact syntax of chmod's argument, per the manpage, chmod(1) (line "Each MODE is of the form…")
|
414
398
|
`chmod #{future_file[:permissions]} #{dest}` if future_file[:permissions]
|