foobara-files-generator 0.1.5 → 0.2.1

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: a3461e9456b77d42ef2b18982335d6dc249e4ab9d31aaa8021b53175994d1b6b
4
- data.tar.gz: ce0b2ffa4258580acf8bdba8de4e02472e93bdcdff9a84da2e3195096e675312
3
+ metadata.gz: 403cd73716460c07e8b8c6a7cef1a9757d4d2a376e6cbc905660975395f6d9af
4
+ data.tar.gz: 6ee34983b9f2aa3e6abde8480ea05fc22476b89cc624da63d69a3d995b776630
5
5
  SHA512:
6
- metadata.gz: 6f814407b5a575d59e94d54de20c793e50b9acb8a60c88ff08f0b7340bcad7c060df879da05925c3864e5282cac99de71daba7f2864ee967c0012581114d8308
7
- data.tar.gz: fba7d452ac12189ed7507217e3b2478dfb34204690ed4afdf3202163300e2b3440bbd9e334e9fa1f5f6cf2f408a27ef805ac251a7793f275fb736bf88b237b15
6
+ metadata.gz: 46d07a4dd416e7a58bf74e1694138030af5650f499a86aea31f501ef78b0682ecfb57ebe3e7c0b45a9c9bbb1341da84686873405ee974f3e613a62610d3eeb7f
7
+ data.tar.gz: 32e65214cfc83f8cb4d8bb00f11a35361a520af6ff38e8f54eceb805933799fcf1b347cf6b37dbb2a7592b96e01c3860c458c08d074fabf29b7ec114d64ee497
data/CHANGELOG.md CHANGED
@@ -1,3 +1,13 @@
1
+ ## [0.2.1] - 2026-10-03
2
+
3
+ - Support an array of generator keys
4
+ - Deprecate requiring "foobara/files_generator/bundler_actions"
5
+
6
+ ## [0.2.0] - 2026-10-02
7
+
8
+ - Introduce a new foobara-generated.json format
9
+ - Support start_marker/end_marker for adding/removing sections from an existing file
10
+
1
11
  ## [0.1.5] - 2026-09-20
2
12
 
3
13
  - Don't fail if symlink already exists
@@ -1,16 +1 @@
1
- require "bundler"
2
-
3
- module Foobara
4
- class FilesGenerator
5
- module BundlerActions
6
- def bundle_install
7
- puts "bundling..."
8
- cmd = "bundle install"
9
-
10
- Bundler.with_unbundled_env do
11
- run_cmd_and_return_output(cmd)
12
- end
13
- end
14
- end
15
- end
16
- end
1
+ warn 'DEPRECATED: do not explicitly load require "foobara/files_generator/bundler_actions"'
@@ -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"
@@ -0,0 +1,16 @@
1
+ require "bundler"
2
+
3
+ module Foobara
4
+ class FilesGenerator
5
+ module BundlerActions
6
+ def bundle_install
7
+ puts "bundling..."
8
+ cmd = "bundle install"
9
+
10
+ Bundler.with_unbundled_env do
11
+ run_cmd_and_return_output(cmd)
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -3,6 +3,7 @@ require "erb"
3
3
  module Foobara
4
4
  class FilesGenerator
5
5
  include TruncatedInspect
6
+ include BundlerActions
6
7
 
7
8
  class << self
8
9
  def manifest_to_generator_classes(_manifest)
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,42 +20,93 @@ 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|
86
+ generated_files_info[:files].map do |generated_file_record|
51
87
  Thread.new do
52
- write_file_to_disk(path, contents) unless path == generated_files_json_filename
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
53
104
  end
54
105
  end.each(&:join)
55
106
  end
56
107
 
57
108
  def write_file_to_disk(path, contents)
58
- path = "#{output_directory}/#{path}"
109
+ path = File.join(output_directory, path)
59
110
  FileUtils.mkdir_p(File.dirname(path))
60
111
 
61
112
  if contents.is_a?(FilesGenerator::Symlink)
@@ -80,10 +131,9 @@ module Foobara
80
131
  key = self.class.generator_key
81
132
 
82
133
  if key
83
- # TODO: test this path
84
- # simplecov:disable
134
+ key = key.first if key.is_a?(::Array)
135
+
85
136
  "#{key}-generator.json"
86
- # simplecov:enable
87
137
  else
88
138
  "foobara-generated.json"
89
139
  end
@@ -151,6 +201,39 @@ module Foobara
151
201
  def stats
152
202
  "Wrote #{paths_to_source_code.size} files to #{output_directory}"
153
203
  end
204
+
205
+ def remove_generated_section_from_file(generated_file_record, file_path)
206
+ # simplecov:disable
207
+ return unless File.exist?(file_path)
208
+ # simplecov:enable
209
+
210
+ start_marker = generated_file_record.start_marker
211
+ end_marker = generated_file_record.end_marker
212
+
213
+ contents = File.read(file_path)
214
+
215
+ start_index = contents.index(start_marker)
216
+
217
+ # simplecov:disable
218
+ return unless start_index
219
+ # simplecov:enable
220
+
221
+ end_index = contents[(start_index + start_marker.size)..].index(end_marker)
222
+
223
+ # simplecov:disable
224
+ return unless end_index
225
+ # simplecov:enable
226
+
227
+ end_index += start_index + start_marker.size + 1
228
+ end_index += end_marker.size
229
+
230
+ keep_first = contents[0...start_index]
231
+ keep_last = contents[end_index..]
232
+
233
+ contents = keep_first + keep_last
234
+
235
+ File.write(file_path, contents)
236
+ end
154
237
  end
155
238
  end
156
239
  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.5
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Miles Georgi
@@ -43,8 +43,12 @@ files:
43
43
  - lib/foobara/files_generator.rb
44
44
  - lib/foobara/files_generator/bundler_actions.rb
45
45
  - src/files_generator.rb
46
+ - src/files_generator/bundler_actions.rb
46
47
  - src/generate.rb
47
48
  - src/symlink.rb
49
+ - src/types/generate_result.rb
50
+ - src/types/generated_file.rb
51
+ - src/types/generated_file_record.rb
48
52
  - src/write_generated_files_to_disk.rb
49
53
  homepage: https://github.com/foobara/files-generator
50
54
  licenses: