Class: Foobara::Generators::WriteGeneratedFilesToDisk
- Inherits:
-
Command
- Object
- Command
- Foobara::Generators::WriteGeneratedFilesToDisk
show all
- Defined in:
- foobara-files-generator-0.1.2/src/write_generated_files_to_disk.rb
Defined Under Namespace
Classes: CouldNotExecuteError
Constant Summary
TruncatedInspect::MAX_LENGTH
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from Command
install!
#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.
21
22
23
|
# File 'src/write_generated_files_to_disk.rb', line 21
def paths_to_source_code
@paths_to_source_code
end
|
Class Method Details
.generator_key ⇒ Object
10
11
12
|
# File 'src/write_generated_files_to_disk.rb', line 10
def generator_key
nil
end
|
Instance Method Details
#delete_old_files_if_needed ⇒ Object
29
30
31
32
33
34
35
36
37
38
39
40
41
|
# File 'src/write_generated_files_to_disk.rb', line 29
def delete_old_files_if_needed
file_list_file = "#{output_directory}/#{generated_files_json_filename}"
if File.exist?(file_list_file)
file_list = JSON.parse(File.read(file_list_file))
file_list.map do |file|
Thread.new { FileUtils.rm_f("#{output_directory}/#{file}") }
end.each(&:join)
end
end
|
#generate_generated_files_json ⇒ Object
23
24
25
26
27
|
# File 'src/write_generated_files_to_disk.rb', line 23
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
59
60
61
62
63
64
65
66
67
68
69
70
|
# File 'src/write_generated_files_to_disk.rb', line 59
def generated_files_json_filename
key = self.class.generator_key
if key
"#{key}-generator.json"
else
"foobara-generated.json"
end
end
|
#run_cmd_and_return_output(cmd) ⇒ Object
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
# File 'src/write_generated_files_to_disk.rb', line 109
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?
raise CouldNotExecuteError, "could not #{cmd}\n#{stderr.read}"
end
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
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
107
|
# File 'src/write_generated_files_to_disk.rb', line 73
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?
message = "Could not #{cmd}\n#{stderr.read}"
if raise_if_fails
raise CouldNotExecuteError, message
else
warn "WARNING: #{message}"
end
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
131
132
133
|
# File 'src/write_generated_files_to_disk.rb', line 131
def stats
"Wrote #{paths_to_source_code.size} files to #{output_directory}"
end
|
#write_all_files_to_disk ⇒ Object
43
44
45
46
47
48
49
50
51
|
# File 'src/write_generated_files_to_disk.rb', line 43
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
53
54
55
56
57
|
# File 'src/write_generated_files_to_disk.rb', line 53
def write_file_to_disk(path, contents)
path = "#{output_directory}/#{path}"
FileUtils.mkdir_p(File.dirname(path))
File.write(path, contents)
end
|