Class: Foobara::Generators::WriteGeneratedFilesToDisk

Inherits:
Command
  • Object
show all
Defined in:
foobara-files-generator-0.0.7/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

Attributes included from CommandPatternImplementation::Concerns::Subcommands

#is_subcommand

Attributes included from CommandPatternImplementation::Concerns::Runtime

#exception, #outcome, #raw_result

Attributes included from CommandPatternImplementation::Concerns::Errors

#error_collection

Attributes included from CommandPatternImplementation::Concerns::Inputs

#inputs, #raw_inputs

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Command

install!, reset_all

Methods included from Concern

foobara_class_methods_module_for, foobara_concern?, included

Methods included from CommandPatternImplementation::Concerns::Reflection

#initialize

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

#load_entities, #load_records

Methods included from CommandPatternImplementation::Concerns::Transactions

#relevant_entity_classes

Methods included from NestedTransactionable

#auto_detect_current_transactions, #commit_transaction, #commit_transaction_if_open, #open_transaction, #opened_transactions, #relevant_entity_classes, relevant_entity_classes_for_type, #relevant_entity_classes_for_type, #rollback_transaction, #transactions, #use_transaction, with_needed_transactions_for_type

Methods included from CommandPatternImplementation::Concerns::StateMachine

#state_machine

Methods included from CommandPatternImplementation::Concerns::Runtime

#execute, #halt!, #run, #run!, #run_execute, #succeed, #success?, #validate, #validate_records

Methods included from CommandPatternImplementation::Concerns::Errors

#initialize

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

#inspect, truncating

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_codeObject

Returns the value of attribute paths_to_source_code.



20
21
22
# File 'foobara-files-generator-0.0.7/src/write_generated_files_to_disk.rb', line 20

def paths_to_source_code
  @paths_to_source_code
end

Class Method Details

.generator_keyObject



9
10
11
# File 'foobara-files-generator-0.0.7/src/write_generated_files_to_disk.rb', line 9

def generator_key
  nil
end

Instance Method Details

#delete_old_files_if_neededObject



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'foobara-files-generator-0.0.7/src/write_generated_files_to_disk.rb', line 28

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_jsonObject



22
23
24
25
26
# File 'foobara-files-generator-0.0.7/src/write_generated_files_to_disk.rb', line 22

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_filenameObject



58
59
60
61
62
63
64
65
66
67
68
69
# File 'foobara-files-generator-0.0.7/src/write_generated_files_to_disk.rb', line 58

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



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'foobara-files-generator-0.0.7/src/write_generated_files_to_disk.rb', line 108

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 CouldNotExecuteError, "could not #{cmd}\n#{stderr.read}"
    end
    # :nocov:
  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



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'foobara-files-generator-0.0.7/src/write_generated_files_to_disk.rb', line 72

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:
      message = "Could not #{cmd}\n#{stderr.read}"
      if raise_if_fails
        raise CouldNotExecuteError, message
      else
        warn "WARNING: #{message}"
      end
      # :nocov:
    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

#statsObject



130
131
132
# File 'foobara-files-generator-0.0.7/src/write_generated_files_to_disk.rb', line 130

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

#write_all_files_to_diskObject



42
43
44
45
46
47
48
49
50
# File 'foobara-files-generator-0.0.7/src/write_generated_files_to_disk.rb', line 42

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



52
53
54
55
56
# File 'foobara-files-generator-0.0.7/src/write_generated_files_to_disk.rb', line 52

def write_file_to_disk(path, contents)
  path = "#{output_directory}/#{path}"
  FileUtils.mkdir_p(File.dirname(path))
  File.write(path, contents)
end