foobara-files-generator 0.1.4 → 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8cf98ecb24a6c06ed9168b0dbf7c14a4d6aa19b7a0365684634a295de86fed57
4
- data.tar.gz: 2626b68d7005938cc276c044523857154aee0fbde4a4ce1717df847b7dc35cbe
3
+ metadata.gz: 73585897f289a970418fd4ad54b69c95dbda81c429b63ffdee6e8ad67e5da181
4
+ data.tar.gz: b3e10646b9e8968cf3b31b84d70a4683d5bbb068ee0804f60206a848883354e6
5
5
  SHA512:
6
- metadata.gz: 65e71db48ca1463143cfbab679dba25423992018fddc5fe19858d2d7ccd8f5bd355600e4bb47727ac45e23c08a5b759b3e92908521422a6e07ac7c8b43356a52
7
- data.tar.gz: a51167675d456aa2686f283f1679c929471d28f233f8599e7fc6d0909f0a7a392d224166ebc094fe25ffc00f7cf75359ed7129979717e44ff7396b7795de89f5
6
+ metadata.gz: ac958d96e17998a4332593331522c5cb8ab536686aecaf3327d4cc778ae641223ae982fe3f6e57226919a777e7a6d6978bc48b9ece71086efbde4b3f92e74c32
7
+ data.tar.gz: 90ee2be41ef24ded533f2c39a6b75d716668d3dda3af247e8432cdcee178816161d0219fbaf6047be5d061f0b5372d469e1a0b480a437d0e892826f0db681602
data/CHANGELOG.md CHANGED
@@ -1,3 +1,12 @@
1
+ ## [0.2.0] - 2026-10-02
2
+
3
+ - Introduce a new foobara-generated.json format
4
+ - Support start_marker/end_marker for adding/removing sections from an existing file
5
+
6
+ ## [0.1.5] - 2026-09-20
7
+
8
+ - Don't fail if symlink already exists
9
+
1
10
  ## [0.1.4] - 2026-09-19
2
11
 
3
12
  - Support creating symlinks
@@ -1,3 +1,9 @@
1
1
  require "foobara/all"
2
2
 
3
+ module Foobara
4
+ class FilesGenerator
5
+ foobara_domain!
6
+ end
7
+ end
8
+
3
9
  Foobara::Util.require_directory "#{__dir__}/../../src"
data/src/generate.rb CHANGED
@@ -3,8 +3,7 @@ require "find"
3
3
  module Foobara
4
4
  module Generators
5
5
  class Generate < Foobara::Command
6
- # TODO: specify a better type?
7
- result :associative_array
6
+ result :generate_result
8
7
 
9
8
  attr_accessor :element_to_generate
10
9
 
@@ -64,8 +63,14 @@ module Foobara
64
63
  return unless element_to_generate.applicable?
65
64
  # simplecov:enable
66
65
 
67
- paths_to_source_code[Util.array(element_to_generate.target_path).join("/")] =
68
- element_to_generate.generate(elements_to_generate)
66
+ path = Util.array(element_to_generate.target_path).join("/")
67
+ generated_code = element_to_generate.generate(elements_to_generate)
68
+
69
+ if generated_code.is_a?(::String)
70
+ generated_code = ::Foobara::FilesGenerator::Types::GeneratedFile.new(content: generated_code)
71
+ end
72
+
73
+ paths_to_source_code[path] = generated_code
69
74
  end
70
75
 
71
76
  def include_non_templated_files
@@ -79,7 +84,10 @@ module Foobara
79
84
 
80
85
  relative_path = file_path.relative_path_from(templates_dir_pathname)
81
86
 
82
- paths_to_source_code[relative_path.to_s] = File.read(file_path)
87
+ content = File.read(file_path)
88
+
89
+ paths_to_source_code[relative_path.to_s] =
90
+ ::Foobara::FilesGenerator::Types::GeneratedFile.new(content:)
83
91
  end
84
92
  end
85
93
 
@@ -0,0 +1,14 @@
1
+ require_relative "generated_file"
2
+
3
+ module Foobara
4
+ class FilesGenerator
5
+ module Types
6
+ GENERATE_RESULT = FilesGenerator.foobara_register_type(
7
+ [:Types, :generate_result],
8
+ :associative_array,
9
+ key_type_declaration: :string,
10
+ value_type_declaration: GeneratedFile
11
+ )
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,13 @@
1
+ module Foobara
2
+ class FilesGenerator
3
+ module Types
4
+ class GeneratedFile < Foobara::Model
5
+ attributes do
6
+ content :string, :required
7
+ start_marker :string, :allow_nil
8
+ end_marker :string, :allow_nil
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ module Foobara
2
+ class FilesGenerator
3
+ module Types
4
+ class GeneratedFileRecord < Foobara::Model
5
+ attributes do
6
+ file_path :string, :required
7
+ start_marker :string, :allow_nil
8
+ end_marker :string, :allow_nil
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -20,45 +20,102 @@ module Foobara
20
20
 
21
21
  attr_accessor :paths_to_source_code
22
22
 
23
+ def empty_generated_files_json(version = FilesGenerator::VERSION)
24
+ {
25
+ files: [],
26
+ metadata: {
27
+ files_generator: version
28
+ }
29
+ }
30
+ end
31
+
23
32
  def generate_generated_files_json
24
- paths_to_source_code[generated_files_json_filename] = "[\n#{
25
- paths_to_source_code.keys.sort.map { |k| " \"#{k}\"" }.join(",\n")
26
- }\n]\n"
33
+ generated_files_hash = empty_generated_files_json
34
+ files = generated_files_hash[:files]
35
+
36
+ paths_to_source_code.keys.sort.each do |file_path|
37
+ generated_file = paths_to_source_code[file_path]
38
+ file_info = { file_path: }
39
+
40
+ start_marker = generated_file.start_marker
41
+ file_info[:start_marker] = start_marker if start_marker
42
+
43
+ end_marker = generated_file.end_marker
44
+ file_info[:end_marker] = end_marker if end_marker
45
+
46
+ files << file_info
47
+ end
48
+
49
+ content = JSON.generate(generated_files_hash)
50
+
51
+ paths_to_source_code[generated_files_json_filename] = FilesGenerator::Types::GeneratedFile.new(content:)
27
52
  end
28
53
 
29
- def delete_old_files_if_needed
54
+ def old_generated_files_json_contents
30
55
  file_list_file = "#{output_directory}/#{generated_files_json_filename}"
31
56
 
32
- if File.exist?(file_list_file)
33
- # simplecov:disable
34
- file_list = JSON.parse(File.read(file_list_file))
57
+ return unless File.exist?(file_list_file)
35
58
 
36
- file_list.map do |file|
37
- Thread.new { FileUtils.rm_f("#{output_directory}/#{file}") }
38
- end.each(&:join)
39
- # simplecov:enable
59
+ contents = JSON.parse(File.read(file_list_file))
60
+
61
+ if contents.is_a?(::Array)
62
+ file_paths = contents
63
+
64
+ contents = empty_generated_files_json("0.1.5")
65
+ files = contents[:files]
66
+
67
+ file_paths.each do |file_path|
68
+ files << FilesGenerator::Types::GeneratedFileRecord.new(file_path:)
69
+ end
70
+ else
71
+ contents = Util.deep_symbolize_keys(contents)
72
+
73
+ contents[:files].map! do |generated_file_data|
74
+ FilesGenerator::Types::GeneratedFileRecord.new(**generated_file_data)
75
+ end
40
76
  end
77
+
78
+ contents
41
79
  end
42
80
 
43
- def write_all_files_to_disk
44
- # simplecov:disable
45
- if paths_to_source_code.key?(generated_files_json_filename)
46
- # simplecov:enable
47
- write_file_to_disk(generated_files_json_filename, paths_to_source_code[generated_files_json_filename])
48
- end
81
+ def delete_old_files_if_needed
82
+ generated_files_info = old_generated_files_json_contents
83
+
84
+ return unless generated_files_info
49
85
 
50
- paths_to_source_code.map do |path, contents|
51
- Thread.new { write_file_to_disk(path, contents) unless path == generated_files_json_filename }
86
+ generated_files_info[:files].map do |generated_file_record|
87
+ Thread.new do
88
+ file_path = File.join(output_directory, generated_file_record.file_path)
89
+
90
+ if generated_file_record.start_marker
91
+ remove_generated_section_from_file(generated_file_record, file_path)
92
+ else
93
+ FileUtils.rm_f file_path
94
+ end
95
+ end
96
+ end.each(&:join)
97
+ end
98
+
99
+ def write_all_files_to_disk
100
+ paths_to_source_code.map do |path, generated_file|
101
+ Thread.new do
102
+ content = generated_file.content
103
+ write_file_to_disk(path, content) # unless path == generated_files_json_filename
104
+ end
52
105
  end.each(&:join)
53
106
  end
54
107
 
55
108
  def write_file_to_disk(path, contents)
56
- path = "#{output_directory}/#{path}"
109
+ path = File.join(output_directory, path)
57
110
  FileUtils.mkdir_p(File.dirname(path))
58
111
 
59
112
  if contents.is_a?(FilesGenerator::Symlink)
60
113
  if FilesGenerator::Symlink.supported?
61
- File.symlink(contents, path)
114
+ # simplecov:disable
115
+ unless File.symlink?(path)
116
+ # simplecov:enable
117
+ File.symlink(contents, path)
118
+ end
62
119
  # simplecov:disable
63
120
  else
64
121
  # TODO: come up with a way to test this path
@@ -145,6 +202,39 @@ module Foobara
145
202
  def stats
146
203
  "Wrote #{paths_to_source_code.size} files to #{output_directory}"
147
204
  end
205
+
206
+ def remove_generated_section_from_file(generated_file_record, file_path)
207
+ # simplecov:disable
208
+ return unless File.exist?(file_path)
209
+ # simplecov:enable
210
+
211
+ start_marker = generated_file_record.start_marker
212
+ end_marker = generated_file_record.end_marker
213
+
214
+ contents = File.read(file_path)
215
+
216
+ start_index = contents.index(start_marker)
217
+
218
+ # simplecov:disable
219
+ return unless start_index
220
+ # simplecov:enable
221
+
222
+ end_index = contents[(start_index + start_marker.size)..].index(end_marker)
223
+
224
+ # simplecov:disable
225
+ return unless end_index
226
+ # simplecov:enable
227
+
228
+ end_index += start_index + start_marker.size + 1
229
+ end_index += end_marker.size
230
+
231
+ keep_first = contents[0...start_index]
232
+ keep_last = contents[end_index..]
233
+
234
+ contents = keep_first + keep_last
235
+
236
+ File.write(file_path, contents)
237
+ end
148
238
  end
149
239
  end
150
240
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: foobara-files-generator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Miles Georgi
@@ -45,6 +45,9 @@ files:
45
45
  - src/files_generator.rb
46
46
  - src/generate.rb
47
47
  - src/symlink.rb
48
+ - src/types/generate_result.rb
49
+ - src/types/generated_file.rb
50
+ - src/types/generated_file_record.rb
48
51
  - src/write_generated_files_to_disk.rb
49
52
  homepage: https://github.com/foobara/files-generator
50
53
  licenses: