Module: Advance
- Defined in:
- lib/advance.rb,
lib/advance/version.rb
Constant Summary collapse
- RESET =
"\e[0m"- BOLD =
"\e[1m"- ITALIC =
"\e[3m"- UNDERLINE =
"\e[4m"- CYAN =
"\e[36m"- GRAY =
"\e[37m"- GREEN =
"\e[32m"- MAGENTA =
"\e[35m"- RED =
"\e[31m"- WHITE =
"\e[1;37m"- YELLOW =
"\e[33m"- VERSION =
"0.1.0"
Instance Method Summary collapse
- #add_dir_to_path(dir) ⇒ Object
- #do_command(command, feedback = true) ⇒ Object
- #ensure_bin_on_path ⇒ Object
- #file_path_template(dir_path, files) ⇒ Object
- #multi(label, command) ⇒ Object
- #previous_dir_path ⇒ Object
- #previous_file_path ⇒ Object
- #single(label, command) ⇒ Object
- #step(label) ⇒ Object
- #step_dir_prefix(step_no) ⇒ Object
- #work_in_sub_dir(dir_name, existing_message = nil) ⇒ Object
Instance Method Details
#add_dir_to_path(dir) ⇒ Object
146 147 148 149 150 151 152 |
# File 'lib/advance.rb', line 146 def add_dir_to_path(dir) bin_dir = File.(dir) path = ENV["PATH"] return if path.include?(bin_dir) ENV["PATH"] = [path, bin_dir].join(":") end |
#do_command(command, feedback = true) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/advance.rb', line 21 def do_command(command, feedback = true) puts "#{YELLOW}#{command}#{RESET} " if feedback start_time = Time.now stdout, stderr, status = Open3.capture3(command) elapsed_time = Time.now - start_time File.open("log", "w") do |f| f.puts "%%% command: >#{command}<" f.puts "%%% returned status: >#{status}<" f.puts "%%% elapsed time: #{elapsed_time} seconds" f.puts "%%% stdout:" f.puts stdout f.puts "%%% stderr:" f.puts stderr end if !status.success? raise "step #{$step} #{label} failed with #{status}" end end |
#ensure_bin_on_path ⇒ Object
138 139 140 141 142 143 144 |
# File 'lib/advance.rb', line 138 def ensure_bin_on_path advance_path = File.dirname(__FILE__) add_dir_to_path(advance_path) caller_path = File.dirname(caller[0].split(/:/).first) add_dir_to_path(caller_path) end |
#file_path_template(dir_path, files) ⇒ Object
98 99 100 101 102 103 104 105 106 |
# File 'lib/advance.rb', line 98 def file_path_template(dir_path, files) file = files.first file_path = File.join(dir_path, file) if File.directory?(file_path) File.join(dir_path, "{file}", "{file}") else File.join(dir_path, "{file}") end end |
#multi(label, command) ⇒ Object
68 69 70 71 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 |
# File 'lib/advance.rb', line 68 def multi(label, command) no_feedback = false step(label) do # previous_dir_path = File.expand_path(previous_dir_path) files = Dir.entries(previous_dir_path).reject { |f| f =~ %r{^(\.\.?|log)$} } file_path_template = file_path_template(previous_dir_path, files) last_progress = "" progress_proc = ->(index, max_index) do latest_progress = sprintf("%3i%", index.to_f / max_index * 100) puts latest_progress if last_progress != latest_progress last_progress = latest_progress end TeamEffort.work(files, $cores, progress_proc: progress_proc) do |file| begin previous_file_path = file_path_template.gsub("{file}", file) command.gsub!("{file_path}", previous_file_path) unless $step == 1 command.gsub!("{file}", file) unless $step == 1 puts "#{YELLOW}#{command}#{RESET}" dir_name = file work_in_sub_dir(dir_name) do do_command command, no_feedback end rescue puts "%%%% error while processing #{file}" raise end end end end |
#previous_dir_path ⇒ Object
40 41 42 43 44 45 46 47 48 |
# File 'lib/advance.rb', line 40 def previous_dir_path relative_path = case $step when 1 ".." else File.join("..", Dir.entries("..").find { |d| d =~ /^#{step_dir_prefix($step - 1)}/ }) end File.(relative_path) end |
#previous_file_path ⇒ Object
50 51 52 53 54 |
# File 'lib/advance.rb', line 50 def previous_file_path dir_entries = Dir.glob(File.join(previous_dir_path, "*")) dir_entries_clean = dir_entries.reject { |f| File.directory?(f) || f =~ %r{^\.\.?|log} } dir_entries_clean.first end |
#single(label, command) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/advance.rb', line 56 def single(label, command) step(label) do if command =~ /\{previous_file\}/ command.gsub!("{previous_file}", previous_file_path) end if command =~ /\{previous_dir\}/ command.gsub!("{previous_dir}", previous_dir_path) end do_command command end end |
#step(label) ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/advance.rb', line 126 def step(label) $step ||= 0 $step += 1 dir_name = "#{step_dir_prefix($step)}_#{label}" $previous_dir = File.join(FileUtils.pwd, dir_name) puts "#{CYAN}step #{$step} #{label}#{WHITE}... #{RESET}" work_in_sub_dir(dir_name, "#{GREEN}OK#{RESET}") do yield end end |
#step_dir_prefix(step_no) ⇒ Object
122 123 124 |
# File 'lib/advance.rb', line 122 def step_dir_prefix(step_no) "step_%03d" % [step_no] end |
#work_in_sub_dir(dir_name, existing_message = nil) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/advance.rb', line 108 def work_in_sub_dir(dir_name, = nil) return if Dir.exist? dir_name tmp_dir_name = "tmp_#{dir_name}" FileUtils.rm_rf tmp_dir_name FileUtils.mkdir_p tmp_dir_name FileUtils.cd tmp_dir_name yield FileUtils.cd ".." FileUtils.mv tmp_dir_name, dir_name end |