Class: Foobara::Generators::WriteGeneratedFilesToDisk
- Defined in:
- foobara-files-generator-0.0.5/src/write_generated_files_to_disk.rb
Direct Known Subclasses
AutocrudGenerator::WriteAutocrudToDisk, CommandGenerator::WriteCommandToDisk, DomainGenerator::WriteDomainToDisk, DomainMapperGenerator::WriteDomainMapperToDisk, EmptyRubyProjectGenerator::WriteEmptyRubyProjectToDisk, OrganizationGenerator::WriteOrganizationToDisk, RackConnectorGenerator::WriteRackConnectorToDisk, RemoteImportsGenerator::WriteRemoteImportsToDisk, ResqueConnectorGenerator::WriteResqueConnectorToDisk, TypeGenerator::WriteTypeToDisk, RemoteGenerator::WriteTypescriptToDisk
Constant Summary
Constants included from TruncatedInspect
Instance Attribute Summary collapse
-
#paths_to_source_code ⇒ Object
Returns the value of attribute paths_to_source_code.
Attributes included from CommandPatternImplementation::Concerns::Subcommands
Attributes included from CommandPatternImplementation::Concerns::Runtime
Attributes included from CommandPatternImplementation::Concerns::Errors
Attributes included from CommandPatternImplementation::Concerns::Inputs
Class Method Summary collapse
Instance Method Summary collapse
- #delete_old_files_if_needed ⇒ Object
- #generate_generated_files_json ⇒ Object
- #generated_files_json_filename ⇒ Object
- #run_cmd_and_return_output(cmd) ⇒ Object
-
#run_cmd_and_write_output(cmd, raise_if_fails: true) ⇒ Object
TODO: probably needs a better name.
- #stats ⇒ Object
- #write_all_files_to_disk ⇒ Object
- #write_file_to_disk(path, contents) ⇒ Object
Methods inherited from Command
Methods included from Concern
foobara_class_methods_module_for, foobara_concern?, included
Methods included from CommandPatternImplementation::Concerns::Reflection
Methods included from CommandPatternImplementation::Concerns::DomainMappers
#domain_map, #domain_map!, #run_mapped_subcommand!
Methods included from CommandPatternImplementation::Concerns::Subcommands
#run_subcommand!, #subcommand?
Methods included from CommandPatternImplementation::Concerns::Entities
Methods included from CommandPatternImplementation::Concerns::Transactions
#auto_detect_current_transactions, #commit_transaction, #open_transaction, #opened_transactions, #relevant_entity_classes, #rollback_transaction, #transactions
Methods included from CommandPatternImplementation::Concerns::StateMachine
Methods included from CommandPatternImplementation::Concerns::Runtime
#execute, #halt!, #run, #run!, #run_execute, #succeed, #success?, #validate, #validate_records
Methods included from CommandPatternImplementation::Concerns::Errors
Methods included from CommandPatternImplementation::Concerns::Inputs
#cast_and_validate_inputs, #initialize, #method_missing, #respond_to_missing?, #respond_to_missing_for_inputs?
Methods included from TruncatedInspect
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Foobara::CommandPatternImplementation::Concerns::Inputs
Instance Attribute Details
#paths_to_source_code ⇒ Object
Returns the value of attribute paths_to_source_code.
18 19 20 |
# File 'foobara-files-generator-0.0.5/src/write_generated_files_to_disk.rb', line 18 def paths_to_source_code @paths_to_source_code end |
Class Method Details
.generator_key ⇒ Object
7 8 9 |
# File 'foobara-files-generator-0.0.5/src/write_generated_files_to_disk.rb', line 7 def generator_key nil end |
Instance Method Details
#delete_old_files_if_needed ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'foobara-files-generator-0.0.5/src/write_generated_files_to_disk.rb', line 26 def delete_old_files_if_needed file_list_file = "#{output_directory}/#{generated_files_json_filename}" if File.exist?(file_list_file) # :nocov: file_list = JSON.parse(File.read(file_list_file)) file_list.map do |file| Thread.new { FileUtils.rm("#{output_directory}/#{file}") } end.each(&:join) # :nocov: end end |
#generate_generated_files_json ⇒ Object
20 21 22 23 24 |
# File 'foobara-files-generator-0.0.5/src/write_generated_files_to_disk.rb', line 20 def generate_generated_files_json paths_to_source_code[generated_files_json_filename] = "[\n#{ paths_to_source_code.keys.sort.map { |k| " \"#{k}\"" }.join(",\n") }\n]\n" end |
#generated_files_json_filename ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'foobara-files-generator-0.0.5/src/write_generated_files_to_disk.rb', line 56 def generated_files_json_filename key = self.class.generator_key if key # TODO: test this path # :nocov: "#{key}-generator.json" # :nocov: else "foobara-generated.json" end end |
#run_cmd_and_return_output(cmd) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'foobara-files-generator-0.0.5/src/write_generated_files_to_disk.rb', line 92 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? # :nocov: raise "could not #{cmd}\n#{stderr.read}" end # :nocov: end retval end |
#run_cmd_and_write_output(cmd, raise_if_fails: true) ⇒ Object
TODO: probably needs a better name
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'foobara-files-generator-0.0.5/src/write_generated_files_to_disk.rb', line 70 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? # :nocov: if raise_if_fails raise "could not #{cmd}\n#{stderr.read}" else warn "WARNING: could not #{cmd}\n#{stderr.read}" end # :nocov: end end end |
#stats ⇒ Object
114 115 116 |
# File 'foobara-files-generator-0.0.5/src/write_generated_files_to_disk.rb', line 114 def stats "Wrote #{paths_to_source_code.size} files to #{output_directory}" end |
#write_all_files_to_disk ⇒ Object
40 41 42 43 44 45 46 47 48 |
# File 'foobara-files-generator-0.0.5/src/write_generated_files_to_disk.rb', line 40 def write_all_files_to_disk if paths_to_source_code.key?(generated_files_json_filename) write_file_to_disk(generated_files_json_filename, paths_to_source_code[generated_files_json_filename]) end paths_to_source_code.map do |path, contents| Thread.new { write_file_to_disk(path, contents) unless path == generated_files_json_filename } end.each(&:join) end |
#write_file_to_disk(path, contents) ⇒ Object
50 51 52 53 54 |
# File 'foobara-files-generator-0.0.5/src/write_generated_files_to_disk.rb', line 50 def write_file_to_disk(path, contents) path = "#{output_directory}/#{path}" FileUtils.mkdir_p(File.dirname(path)) File.write(path, contents) end |