class Baguette::Context class_property logfile_path = "/tmp/package-create.log" class_property no_logfile = false end class Do < Process # list of environment variables we want to have when building class_property environment = {} of String => String def self.require_cmd(cmd : String) unless Process.run("which", [ cmd ]).success? STDERR.puts "#{cmd} isn't installed" exit 1 end end def self.cd(chdir) Baguette::Log.detail "cd " + chdir.colorize(:light_magenta).to_s Dir.cd chdir end def self.cp(origin : String, destination : String) corig = origin.colorize(:light_magenta).to_s cdest = destination.colorize(:light_orange).to_s Baguette::Log.detail "cp #{corig} #{cdest}" FileUtils.cp origin, destination end def self.run(chdir, command, args) output = Process::Redirect::Inherit if Baguette::Context.no_logfile output = Process::Redirect::Close else # log sub-commands outputs Baguette::Log.info "logging command " + "#{command} #{args}".colorize(:light_magenta).to_s Baguette::Log.info "in " + Baguette::Context.logfile_path.colorize(:blue).mode(:bright).to_s File.open Baguette::Context.logfile_path, "a" do |file| file.puts "" file.puts "" file.puts "logging command $ #{command}" file.puts " parameters $ #{args}" end output = File.open Baguette::Context.logfile_path, "a" end c = Process.run command, args, chdir: chdir, output: output, error: output, env: @@environment case output when File output.close end c end def self.run(command, args) self.run nil, command, args end def self.run(command) self.run nil, command, nil end def self.sh(command) self.run nil, "sh", ["-x", "-e", "-c", command] end def self.captured_sh(command) output = IO::Memory.new child = Process.run "sh", ["-x", "-e", "-c", command], output: output {child, output} end # Log file moves during splits. def self.mv(f1 : String, f2 : String) self.run "mv", [ f1, f2 ] end # Log directory creations during splits. def self.mkdir_p(dir : String) self.run "mkdir", [ "-p", dir ] end end