mkvtoolnix/rake.d/helpers.rb

190 lines
5.0 KiB
Ruby
Raw Normal View History

# coding: utf-8
$use_tempfile_for_run = defined?(RUBY_PLATFORM) && /mingw/i.match(RUBY_PLATFORM)
require "tempfile"
2012-04-09 14:32:46 +00:00
require "fileutils"
$git_mutex = Mutex.new
2016-02-28 10:33:40 +00:00
$message_mutex = Mutex.new
def puts(message)
$message_mutex.synchronize {
$stdout.puts message
$stdout.flush
2016-02-28 10:33:40 +00:00
}
end
def error(message)
puts message
exit 1
end
def last_exit_code
$?.respond_to?(:exitstatus) ? $?.exitstatus : $?.to_i
end
def run cmdline, opts = {}
code = run_wrapper cmdline, opts
exit code if (code != 0) && !opts[:allow_failure].to_bool
end
def run_wrapper cmdline, opts = {}
cmdline = cmdline.gsub(/\n/, ' ').gsub(/^\s+/, '').gsub(/\s+$/, '').gsub(/\s+/, ' ')
code = nil
shell = ENV["RUBYSHELL"].blank? ? "c:/msys/bin/sh" : ENV["RUBYSHELL"]
puts cmdline unless opts[:dont_echo].to_bool
output = nil
if opts[:filter_output]
output = Tempfile.new("mkvtoolnix-rake-output")
cmdline += " > #{output.path} 2>&1"
end
if $use_tempfile_for_run
Tempfile.open("mkvtoolnix-rake-run") do |t|
t.puts cmdline
t.flush
system shell, t.path
code = last_exit_code
t.unlink
end
else
system cmdline
code = last_exit_code
end
code = opts[:filter_output].call code, output.readlines if opts[:filter_output]
return code
ensure
if output
output.close
output.unlink
end
end
def runq(msg, cmdline, options = {})
verbose = ENV['V'].to_bool
puts msg if !verbose
run cmdline, options.clone.merge(:dont_echo => !verbose)
end
2010-07-26 10:29:18 +00:00
def runq_git(msg, cmdline, options = {})
verbose = ENV['V'].to_bool
sub_cmd = cmdline.split(/\s+/)[0]
msg = " GIT #{sub_cmd.upcase} #{msg}"
puts msg if !verbose
$git_mutex.synchronize { run "git #{cmdline}", options.clone.merge(:dont_echo => !verbose) }
end
def ensure_dir dir
File.unlink(dir) if FileTest.exist?(dir) && !FileTest.directory?(dir)
Dir.mkdir(dir) if !FileTest.exist?(dir)
end
def create_dependency_dirs
[ $dependency_dir, $dependency_tmp_dir ].each { |dir| ensure_dir dir }
end
def dependency_output_name_for file_name
$dependency_tmp_dir + "/" + file_name.gsub(/[\/\.]/, '_') + '.d'
end
def handle_deps(target, exit_code, skip_abspath=false)
dep_file = dependency_output_name_for target
2010-07-26 10:29:18 +00:00
get_out = lambda do
File.unlink(dep_file) if FileTest.exist?(dep_file)
exit exit_code if 0 != exit_code
return
end
FileTest.exist?(dep_file) || get_out.call
create_dependency_dirs
2010-07-26 10:29:18 +00:00
File.open("#{$dependency_dir}/" + target.gsub(/[\/\.]/, '_') + '.dep', "w") do |out|
line = IO.readlines(dep_file).collect { |l| l.chomp }.join(" ").gsub(/\\/, ' ').gsub(/\s+/, ' ')
if /(.+?):\s*([^\s].*)/.match(line)
2010-07-26 10:29:18 +00:00
target = $1
sources = $2.gsub(/^\s+/, '').gsub(/\s+$/, '').split(/\s+/)
if skip_abspath
sources.delete_if { |entry| entry.start_with? '/' }
end
2012-04-26 13:58:35 +00:00
out.puts(([ target ] + sources).join("\n"))
2010-07-26 10:29:18 +00:00
end
end
get_out.call
rescue
2010-07-26 10:29:18 +00:00
get_out.call
end
def import_dependencies
return unless FileTest.directory? $dependency_dir
Dir.glob("#{$dependency_dir}/*.dep").each do |file_name|
lines = IO.readlines(file_name).collect(&:chomp)
target = lines.shift
file target => lines.select { |dep_name| File.exists? dep_name }
end
2010-07-26 10:29:18 +00:00
end
2010-07-27 16:52:12 +00:00
def arrayify(*args)
args.collect { |arg| arg.is_a?(Array) ? arg.to_a : arg }.flatten
2010-07-27 16:52:12 +00:00
end
def install_dir(*dirs)
arrayify(*dirs).each do |dir|
dir = c(dir) if dir.is_a? Symbol
run "#{c(:mkinstalldirs)} #{c(:DESTDIR)}#{dir}"
end
end
def install_program(destination, *files)
destination = c(destination) + '/' if destination.is_a? Symbol
arrayify(*files).each do |file|
run "#{c(:INSTALL_PROGRAM)} #{file} #{c(:DESTDIR)}#{destination}"
end
end
def install_data(destination, *files)
destination = c(destination) + '/' if destination.is_a? Symbol
arrayify(*files).each do |file|
run "#{c(:INSTALL_DATA)} #{file} #{c(:DESTDIR)}#{destination}"
end
end
2010-07-27 17:47:40 +00:00
def remove_files_by_patterns patterns
verbose = ENV['V'].to_bool
patterns.collect { |pattern| FileList[pattern].to_a }.flatten.uniq.select { |file_name| File.exists? file_name }.each do |file_name|
puts " rm #{file_name}" if verbose
File.unlink file_name
end
end
def read_files *file_names
Hash[ *file_names.flatten.collect { |file_name| [ file_name, IO.readlines(file_name).collect { |line| line.force_encoding "UTF-8" } ] }.flatten(1) ]
end
class Rake::Task
def investigate
result = "------------------------------\n"
result << "Investigating #{name}\n"
result << "class: #{self.class}\n"
result << "task needed: #{needed?}\n"
result << "timestamp: #{timestamp}\n"
result << "pre-requisites:\n"
prereqs = @prerequisites.collect { |name| Rake::Task[name] }
prereqs.sort! { |a,b| a.timestamp <=> b.timestamp }
result += prereqs.collect { |p| "--#{p.name} (#{p.timestamp})\n" }.join("")
latest_prereq = @prerequisites.collect{|n| Rake::Task[n].timestamp}.max
result << "latest-prerequisite time: #{latest_prereq}\n"
result << "................................\n\n"
return result
end
end