rakelib/extensions.rb
author Jan Vrany <jan.vrany@fit.cvut.cz>
Sat, 11 Jun 2016 15:25:05 +0100
changeset 42 5e06187ba5a3
parent 41 e9fd2a661960
child 62 de0e8016c2d5
permissions -rw-r--r--
Oops, fixed glob-style expansion in `String#/` Due to a bad regexp, some files were rejected leading to missing sources for package stx:libscm/mercurial (and possibly others)

require 'rake'
require 'rbconfig'
require 'tsort'
require 'pathname'
require 'find'

class String
  rake_extension("/") do
    def / (arg)
      r = File.join(File.expand_path(self), arg.to_s())
      if arg.to_s.include? ?* or arg.to_s.include? ??
        r = Dir.glob(r)
        r.reject! { | f | (f =~ /\^.svn|^CVS|^\.hg|^\.git/i) != nil }
      end
      return r
    end
  end
end


(defined? VERBOSE) || (VERBOSE = nil)

class Object
  rake_extension("halt") do 
    def halt()
      begin
        require 'pry'      
      rescue LoadError 
        error("`pry` not installed, run `gem install pry` to install it")
      end
      begin
        require 'pry-byebug'
      rescue LoadError
        error("`pry-byebug` not installed, run `gem install pry-byebug` to install it")        
      end
      binding.pry
    end
  end  

  rake_extension("info") do 
    def info(message, details = nil)
      if (VERBOSE != nil)
        $stderr.puts "[INFO] #{message}"
        if (details) 
          $stderr.puts "      #{details}"
        end
      end
    end
  end  

  rake_extension("warn") do 
    def warn(message, details = nil)
      if (VERBOSE != nil)
        $stderr.puts "[WARN] #{message}"
        if (details) 
          $stderr.puts "      #{details}"
        end
      end
    end
  end  

  
  rake_extension("error") do
    def error(message)
      raise Exception.new(message)
    end
  end 

  rake_extension("error_unsupported_platform") do
    def error_unsupported_platform()
      error("Unsupported platform (#{RbConfig::CONFIG['host_os']})")
    end
  end 

  rake_extension("win32?") do
    def win32?      
      if win32_wine?
        return true
      end
      return (RbConfig::CONFIG['host_os'] =~ /mingw32/) != nil
    end
  end 

  rake_extension("win32_wine?") do
    def win32_wine?      
      return ENV['CROSSCOMPILE'] == 'wine'
    end
  end 

 
  rake_extension("unix?") do
    def unix?     
      if win32_wine?
        return false
      end      
      return (RbConfig::CONFIG['host_os'] =~ /linux|solaris/) != nil
    end
  end 
    
  rake_extension("linux?") do
    def linux?
      if win32_wine?
        return false
      end

      return (RbConfig::CONFIG['host_os'] =~ /linux/i) != nil
    end
  end 
  
  rake_extension("x86_64?") do
    def x86_64?
      return RbConfig::CONFIG['host_cpu'] == 'x86_64'
    end
  end 

  rake_extension("i386?") do
    def i386?
      return RbConfig::CONFIG['host_cpu'] == 'i386'
    end
  end 

  rake_extension("redefine") do
    def redefine(*args, &block)
      task_name, arg_names, deps = Rake.application.resolve_args(args)
      task = Rake.application.lookup(task_name)
      error "task #{task_name} not defined ant thus cannot be redefined" if not task
      info "Redefining task #{task.name}"
      task.clear()
      task.set_arg_names(arg_names)
      task.enhance(deps, &block)
    end
  end 

  rake_extension("clear") do
    def clear(*args, &block)
      if block_given? 
        error "Block has no meaning when clearing task prereqs"
      end
      task_name, arg_names, deps = Rake.application.resolve_args(args)
      deps = deps.collect { | each | each.to_s }
      task = Rake.application.lookup(task_name)
      return nil if not task
      info "Clearing dependencies of task #{task.name} (#{deps.join(", ")})"
      task.prerequisites.reject! { | each | deps.include? each.to_s  }    
      return task
    end
  end 
 
  class << self
    alias :__const_missing__ :const_missing
  end

  def self.const_missing(name)
    if ENV[name.to_s]
      return ENV[name.to_s]
    else
      return __const_missing__(name)
    end
  end
end

class Rake::Task
  class PrerequisiteSorter
    include TSort

    def initialize(task)
      @task = task
    end

   
    def each_prereq(task)
      task.prerequisites.each do | prereq |
        prereq_t = task.application.lookup(prereq, task.scope)
        if prereq_t
          yield prereq_t
        end
      end
    end

    def tsort_each_node(&block)
      each_prereq(@task, &block)      
    end

    def tsort_each_child(task, &block)      
      each_prereq(task, &block)      
    end
  end # class PrerequisiteSorter
    
  def all_prerequisites_sorted
    return PrerequisiteSorter.new(self).tsort.collect{ | t | t.name }
  end

end



module RakeFileUtils
  def make(args = '')
    if win32?
      #sh "make.exe -N -f bc.mak #{args}"
      if win32_wine?
        def make_objdir(directory)              
          if File.exist? directory / 'bmake.bat'
            #puts "Making objbc in #{directory}"      
            if not File.exist?(directory / OBJ_DIR)
              #puts "Made objbc in #{directory}"      
              mkdir directory / OBJ_DIR
            end            
            Dir.entries(directory).each do | each |              
              if each != '.' and each != '..' and File.directory?(directory / each)
                #puts "  recursing into #{directory / each}"
                make_objdir(directory / each)
              end
            end
          end
        end
        make_objdir('.')
        sh "wine cmd /c #{MAKE} #{args}"
      else 
        sh "#{MAKE} #{args}"
      end
    else
      sh "#{MAKE} #{args}"
    end
  end

  # Pretty much like sed. Replaces all occurences of `pattern` by `replacement` in given `file`.
  # If `inplace` is `true`, then the modified contents is written back to the file. Otherwise it
  # printed on `STDOUT`.
  def sed(pattern, replacement, file, inplace = false)
    contents = File.read(file)
    contents.gsub!(pattern, replacement)
    if inplace then
      cp file, "#{file}.bak"
      File.open(file, "w") { | f | f.puts contents }
    else
      STDOUT.puts contents
    end
  end
  
  # Create a compressed archive of `source`. Under Windows it creates
  # `.zip` archive, otherwise (on Linux) it creates `tar.bz2`. 
  # 
  # The archive is created in the same directory as the source and
  # has the same name unless explicitily specified by `archive:` option. 
  # If `remove: true` option is set to true, the original (`source`) directory is
  # removed after adding the archive. 
  #
  # As a side-effect, it generates a SHA256 checksum in file .sha256 unles
  # option `sha256: false` is given.
  # 
  # Examples: 
  #
  # Create `somedir.bar.bz2` on `/tmp` containg contants of `/tmp/somedir`:
  #
  #     zip '/tmp/somedir'
  #
  # Create `smalltalkx.bar.bz2` on `/tmp` containg contants of `/tmp/build_dir`
  # and remove `/tmp/build_dir` afterwards:
  # 
  #     zip '/tmp/build_dir', archive: 'smalltalkx', remove: true
  #  
  def zip(source, options = {})
    remove = options[:remove] || false
    archive = options[:archive] || nil
    sha256 = options[:sha256] || true
    suffix = win32? ? '.zip' : '.tar.bz2'
    if not archive then
      archive = "#{source}#{suffix}"
    else 
      if not archive.end_with? suffix then
        archive = "#{archive}#{suffix}"
      end
    end
    archive = File.expand_path(archive)
    chdir File.dirname(source) do
      if win32? then
        sh "zip -q -r #{remove ? '-T -m' : ''} \"#{archive}\" \"#{File.basename(source)}\""
      else 
        sh "tar cjf \"#{archive}\" #{remove ? '--remove-files' : ''} \"#{File.basename(source)}\""
      end
    end        
    if sha256 then
      require 'digest'
      File.open("#{archive}.sha256", "w") do | sum |
      	sum.write Digest::SHA256.file(archive).hexdigest
      end
    end
  end

  # Extract an (compressed) archive. 
  # 
  # Files are extracted to the directory that contains `archive` unless 
  # options `directory: "some/other/directory` is given. 
  # 
  # If file named `#{archive}.sha256` exists then SHA256 checksum is validated
  # against the one `.sha256` file. If differ, an `Exception` is raised.
  #
  # If option `remove: true` is given, then the archive is removed after
  # all files are extracted. 
  # 
  def unzip(archive, options = {})     
    directory = options[:directory] || File.dirname(archive)
    remove = options[:remove] || false
    archive = File.expand_path archive
    sha256 = "#{archive}.sha256"
    if File.exist? sha256 then
      require 'digest'
      actual = Digest::SHA256.file(archive).hexdigest
      expected = nil
      File.open(sha256) { | f | expected = f.read}
      if actual != expected then
        raise Exception.new("SHA256 checksum for #{archive} differ (actual #{actual}, expected #{expected}")
      end
    end

    chdir directory do      
      if archive.end_with? '.zip' then
        sh "unzip \"#{archive}\""
      elsif archive.end_with? '.tar.bz2' then
        sh "tar xjf \"#{archive}\""
      elsif archive.end_with? '.tar.gz' then
        sh "tar xzf \"#{archive}\""
      else
        raise Exception.new("Unknown archive type: #{File.basename(archive)}")        
      end       
    end
    if remove then
      rm_f archive
    end
  end
  

  # Like FileUtils.cp_r, but takes a filter proc that can return false 
  # to skip a file
  #
  # Note that if the filter rejects a subdirectory then everything within that
  # subdirectory is automatically skipped as well.
  #
  # Taken from http://www.ruby-forum.com/attachment/4467/filteredCopy.rb  
  # Both of these are modified from the implementations in fileutils.rb from 
  # Ruby 1.9.1p378  
  def cp_rx src, dest, options = {}, &filter
    fu_check_options options, OPT_TABLE['cp_r']
    fu_output_message "cp -r#{options[:preserve] ? 'p' : ''}#{options[:remove_destination] ? ' --remove-destination' : ''} #{[src,dest].flatten.join ' '}" if options[:verbose]
    return if options[:noop]
    fu_each_src_dest(src, dest) do |s, d|
        copy_entryx s, d, filter, options[:preserve], options[:dereference_root], options[:remove_destination]
    end
  end

  # Like FileUtils.copy_entry, but takes a filter proc that can return false to skip a file
  def copy_entryx(src, dest, filter, preserve = false, dereference_root = false, remove_destination = false)
    Entry_.new(src, nil, dereference_root).traverse do |ent|
        if filter.call(ent.path) then
          destent = Entry_.new(dest, ent.rel, false)
          File.unlink destent.path if remove_destination && File.file?(destent.path)
          ent.copy destent.path
          ent.copy_metadata destent.path if preserve
        end
    end
  end

  
  # * ruby implementation of find that follows symbolic directory links
  # * tested on ruby 1.9.3, ruby 2.0 and jruby on Fedora 20 linux
  # * you can use Find.prune
  # * detect symlinks to dirs by path "/" suffix; does nothing with files so `symlink?` method is working fine
  # * depth first order
  # * detects cycles and raises an error
  # * raises on broken links
  # * uses recursion in the `do_find` proc when directory links are met (takes a lot of nested links until SystemStackError, that's practically never)
  #
  # * use like: find_follow(".") {|f| puts f}
  #
  # Copyright (c) 2014 Red Hat inc
  #
  # Permission is hereby granted, free of charge, to any person obtaining a copy
  # of this software and associated documentation files (the "Software"), to deal
  # in the Software without restriction, including without limitation the rights
  # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  # copies of the Software, and to permit persons to whom the Software is
  # furnished to do so, subject to the following conditions:
  #
  # The above copyright notice and this permission notice shall be included in
  # all copies or substantial portions of the Software.
  #
  # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  # THE SOFTWARE.
  def find_follow(*paths)
    block_given? or return enum_for(__method__, *paths)

    link_cache = {}
    link_resolve = lambda { |path|
      # puts "++ link_resolve: #{path}" # trace
      if link_cache[path]
        return link_cache[path]
      else
        return link_cache[path] = Pathname.new(path).realpath.to_s
      end
    }
    # this lambda should cleanup `link_cache` from unnecessary entries
    link_cache_reset = lambda { |path|
      # puts "++ link_cache_reset: #{path}" # trace
      # puts link_cache.to_s # trace
      link_cache.select! do |k,v|
        path == k || k == "/" || path.start_with?(k + "/")
      end
      # puts link_cache.to_s # trace
    }
    link_is_recursive = lambda { |path|
      # puts "++ link_is_recursive: #{path}" # trace
      # the ckeck is useless if path is not a link but not our responsibility

      # we need to check full path for link cycles
      pn_initial = Pathname.new(path)
      unless pn_initial.absolute?
        # can we use `expand_path` here? Any issues with links?
        pn_initial = Pathname.new(File.join(Dir.pwd, path))
      end

      # clear unnecessary cache
      link_cache_reset.call(pn_initial.to_s)

      link_dst = link_resolve.call(pn_initial.to_s)

      pn_initial.ascend do |pn|
        if pn != pn_initial && link_dst == link_resolve.call(pn.to_s)
          return {:link => path, :dst => pn}
        end
      end

      return false
    }

    do_find = proc { |path|
      Find.find(path) do |path|
        if File.symlink?(path) && File.directory?(File.realpath(path))
          if path[-1] == "/"
            # probably hitting https://github.com/jruby/jruby/issues/1895
            yield(path.dup)
            Dir.new(path).each { |subpath|
              do_find.call(path + subpath) unless [".", ".."].include?(subpath)
            }
          elsif is_recursive = link_is_recursive.call(path)
            raise "cannot handle recursive links: #{is_recursive[:link]} => #{is_recursive[:dst]}"
          else
            do_find.call(path + "/")
          end
        else
          yield(path)
        end
      end
    }

    while path = paths.shift
      do_find.call(path)
    end
  end  

  # Taken from https://gist.github.com/akostadinov/fc688feba7669a4eb784
  # based on find_follow.rb : https://gist.github.com/akostadinov/05c2a976dc16ffee9cac
  # 
  # * use like: cp_r_dereference 'src', 'dst'
  #
  # Note: if directory `src` content is copied instead of the full dir. i.e. you end up
  #                                     with `dst/*` instead of `dst/basename(src)/*`
  # 
  # Copyright (c) 2014 Red Hat inc
  # 
  # Permission is hereby granted, free of charge, to any person obtaining a copy
  # of this software and associated documentation files (the "Software"), to deal
  # in the Software without restriction, including without limitation the rights
  # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  # copies of the Software, and to permit persons to whom the Software is
  # furnished to do so, subject to the following conditions:
  # 
  # The above copyright notice and this permission notice shall be included in
  # all copies or substantial portions of the Software.
  # 
  # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  # THE SOFTWARE.
  
  # copy recursively dereferencing symlinks
  def cp_r_dereference(src, dst)
    src_pn = Pathname.new(src)
    find_follow(src) do | path |
      relpath = Pathname.new(path).relative_path_from(src_pn).to_s
      dstpath = File.join(dst, relpath)

      if File.directory?(path) || ( File.symlink?(path) && File.directory?(File.realpath(path)) )
        FileUtils.mkdir_p(dstpath)
      else
        FileUtils.copy_file(path, dstpath)
      end
    end
  end

end