Class: Foobara::Generators::WriteGeneratedFilesToDisk

Inherits:
Command
  • Object
show all
Defined in:
foobara-files-generator-0.2.4/src/write_generated_files_to_disk.rb

Defined Under Namespace

Classes: CouldNotExecuteError

Constant Summary

Constants included from TruncatedInspect

TruncatedInspect::MAX_LENGTH

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Command

install!

Methods included from CommandPatternImplementation

install_state_machine_callback_methods

Methods included from TruncatedInspect

#inspect, truncating

Methods included from Concern

foobara_class_methods_module_for, foobara_concern?, included

Instance Attribute Details

#paths_to_source_code ⇒ Object

Returns the value of attribute paths_to_source_code.



22
23
24
# File 'src/write_generated_files_to_disk.rb', line 22

def paths_to_source_code
  @paths_to_source_code
end

Class Method Details

.generator_key ⇒ Object



11
12
13
# File 'src/write_generated_files_to_disk.rb', line 11

def generator_key
  nil
end

Instance Method Details

#delete_old_files_if_needed ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'src/write_generated_files_to_disk.rb', line 86

def delete_old_files_if_needed
  rotate_old_generated_files_json_if_needed

  generated_files_info = old_generated_files_json_contents

  return unless generated_files_info

  generated_files_info[:files].map do |generated_file_record|
    Thread.new do
      file_path = File.join(output_directory, generated_file_record.file_path)

      if generated_file_record.start_marker
        remove_generated_section_from_file(generated_file_record, file_path)
      else
        FileUtils.rm_f file_path
      end
    end
  end.each(&:join)
end

#empty_generated_files_json(version = FilesGenerator::VERSION) ⇒ Object



24
25
26
27
28
29
30
31
# File 'src/write_generated_files_to_disk.rb', line 24

def empty_generated_files_json(version = FilesGenerator::VERSION)
  {
    files: [],
    metadata: {
      files_generator: version
    }
  }
end

#generate_generated_files_json ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'src/write_generated_files_to_disk.rb', line 33

def generate_generated_files_json
  generated_files_hash = empty_generated_files_json
  files = generated_files_hash[:files]

  paths_to_source_code.keys.sort.each do |file_path|
    generated_file = paths_to_source_code[file_path]
    file_info = { file_path: }

    start_marker = generated_file.start_marker
    file_info[:start_marker] = start_marker if start_marker

    end_marker = generated_file.end_marker
    file_info[:end_marker] = end_marker if end_marker

    files << file_info
  end

  content = JSON.generate(generated_files_hash)

  paths_to_source_code[generated_files_json_filename] = FilesGenerator::Types::GeneratedFile.new(content:)
end

#generated_files_json_filename(key = self.class.generator_key) ⇒ Object



157
158
159
160
161
162
163
164
165
# File 'src/write_generated_files_to_disk.rb', line 157

def generated_files_json_filename(key = self.class.generator_key)
  if key
    key = key.first if key.is_a?(::Array)

    "#{key}-generator.json"
  else
    "foobara-generated.json"
  end
end

#old_generated_files_json_contents ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'src/write_generated_files_to_disk.rb', line 55

def old_generated_files_json_contents
  file_list_file = "#{output_directory}/#{generated_files_json_filename}"

  return unless File.exist?(file_list_file)

  contents = JSON.parse(File.read(file_list_file))

  if contents.is_a?(::Array)
    file_paths = contents

    contents = empty_generated_files_json("0.1.5")
    files = contents[:files]

    file_paths.each do |file_path|
      files << FilesGenerator::Types::GeneratedFileRecord.new(file_path:)
    end
  else
    contents = Util.deep_symbolize_keys(contents)

    contents[:files].map! do |generated_file_data|
      FilesGenerator::Types::GeneratedFileRecord.new(**generated_file_data)
    end
  end

  contents
end

#old_generated_files_path(key = self.class.generator_key) ⇒ Object



82
83
84
# File 'src/write_generated_files_to_disk.rb', line 82

def old_generated_files_path(key = self.class.generator_key)
  "#{output_directory}/#{generated_files_json_filename(key)}"
end

#remove_generated_section_from_file(generated_file_record, file_path) ⇒ Object



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'src/write_generated_files_to_disk.rb', line 230

def remove_generated_section_from_file(generated_file_record, file_path)
  # simplecov:disable
  return unless File.exist?(file_path)
  # simplecov:enable

  start_marker = generated_file_record.start_marker
  end_marker = generated_file_record.end_marker

  contents = File.read(file_path)

  start_index = contents.index(start_marker)

  # simplecov:disable
  return unless start_index
  # simplecov:enable

  end_index = contents[(start_index + start_marker.size)..].index(end_marker)

  # simplecov:disable
  return unless end_index
  # simplecov:enable

  end_index += start_index + start_marker.size
  end_index += end_marker.size

  keep_first = contents[0...start_index]
  keep_last = contents[end_index..]

  contents = keep_first + keep_last

  if contents.strip.empty?
    FileUtils.rm_f(file_path)
  else
    File.write(file_path, contents)
  end
end

#rotate_old_generated_files_json_if_needed ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'src/write_generated_files_to_disk.rb', line 106

def rotate_old_generated_files_json_if_needed
  keys = self.class.generator_key

  return unless keys.is_a?(::Array)

  key, *deprecated_keys = keys

  non_deprecated_path = old_generated_files_path(key)
  return if File.exist?(non_deprecated_path)

  deprecated_keys.each do |key|
    path = old_generated_files_path(key)

    if File.exist?(path)
      FileUtils.mv(path, non_deprecated_path)
      break
    end
  end
end

#run_cmd_and_return_output(cmd) ⇒ Object



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'src/write_generated_files_to_disk.rb', line 204

def run_cmd_and_return_output(cmd)
  retval = +""

  Open3.popen3(cmd) do |_stdin, stdout, stderr, wait_thr|
    loop do
      line = stdout.gets
      break unless line

      retval << line
    end

    exit_status = wait_thr.value
    unless exit_status.success?
      # simplecov:disable
      raise CouldNotExecuteError, "could not #{cmd}\n#{stderr.read}"
    end
    # simplecov:enable
  end
rescue Errno::ENOENT
  raise CouldNotExecuteError, "Could not run: #{cmd}\nMaybe it is not installed?"
end

#run_cmd_and_write_output(cmd, raise_if_fails: true) ⇒ Object

TODO: probably needs a better name



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'src/write_generated_files_to_disk.rb', line 168

def run_cmd_and_write_output(cmd, raise_if_fails: true)
  Open3.popen3(cmd) do |_stdin, stdout, stderr, wait_thr|
    loop do
      line = stdout.gets
      break unless line

      puts line
    end

    exit_status = wait_thr.value

    unless exit_status.success?
      # simplecov:disable
      message = "Could not #{cmd}\n#{stderr.read}"
      if raise_if_fails
        raise CouldNotExecuteError, message
      else
        warn "WARNING: #{message}"
      end
      # simplecov:enable
    end

    exit_status
  end
rescue Errno::ENOENT
  message = "Could not run: #{cmd}\nMaybe it is not installed?"

  if raise_if_fails
    raise CouldNotExecuteError, message
  else
    warn "WARNING: #{message}"
  end

  nil
end

#stats ⇒ Object



226
227
228
# File 'src/write_generated_files_to_disk.rb', line 226

def stats
  "Wrote #{paths_to_source_code.size} files to #{output_directory}"
end

#write_all_files_to_disk ⇒ Object



126
127
128
129
130
131
132
133
# File 'src/write_generated_files_to_disk.rb', line 126

def write_all_files_to_disk
  paths_to_source_code.map do |path, generated_file|
    Thread.new do
      content = generated_file.content
      write_file_to_disk(path, content) # unless path == generated_files_json_filename
    end
  end.each(&:join)
end

#write_file_to_disk(path, contents) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'src/write_generated_files_to_disk.rb', line 135

def write_file_to_disk(path, contents)
  path = File.join(output_directory, path)
  FileUtils.mkdir_p(File.dirname(path))

  if contents.is_a?(FilesGenerator::Symlink)
    if FilesGenerator::Symlink.supported?
      # simplecov:disable
      unless File.symlink?(path)
        # simplecov:enable
        File.symlink(contents, path)
      end
      # simplecov:disable
    else
      # TODO: come up with a way to test this path
      FileUtils.cp_r(contents, path)
      # simplecov:enable
    end
  else
    File.write(path, contents)
  end
end