rakelib/extensions.rb
author Jan Vrany <jan.vrany@fit.cvut.cz>
Thu, 24 Aug 2017 00:38:15 +0100
branchstx-8.0.0
changeset 172 b0b41978d5b1
parent 142 357ba05ad169
child 173 43ecb6a2bcbd
permissions -rw-r--r--
Rakefiles: modernize `zip()` and `unzip()` functions
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
     1
require 'rake'
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
     2
require 'rbconfig'
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
     3
require 'pathname'
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
     4
require 'find'
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
     5
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
     6
class String
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
     7
  rake_extension('/') do
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
     8
    def / (arg)
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
     9
      r = File.join(File.expand_path(self), arg.to_s)
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    10
      if arg.to_s.include? ?* or arg.to_s.include? ??
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    11
        r = Dir.glob(r)
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    12
        r.reject! {|f| (f =~ /\^.svn|^CVS|^\.hg|^\.git/i) != nil}
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    13
      end
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    14
      return r
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    15
    end
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    16
  end
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    17
end
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    18
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    19
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    20
(defined? VERBOSE) || (VERBOSE = nil)
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    21
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    22
class Object
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    23
  rake_extension('halt') do
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    24
    def halt
40
f75898c6595d Debugging support
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 9
diff changeset
    25
      begin
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    26
        require 'pry'
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    27
      rescue LoadError
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    28
        error('`pry` not installed, run `gem install pry` to install it')
40
f75898c6595d Debugging support
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 9
diff changeset
    29
      end
f75898c6595d Debugging support
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 9
diff changeset
    30
      begin
f75898c6595d Debugging support
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 9
diff changeset
    31
        require 'pry-byebug'
f75898c6595d Debugging support
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 9
diff changeset
    32
      rescue LoadError
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    33
        error('`pry-byebug` not installed, run `gem install pry-byebug` to install it')
40
f75898c6595d Debugging support
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 9
diff changeset
    34
      end
f75898c6595d Debugging support
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 9
diff changeset
    35
      binding.pry
f75898c6595d Debugging support
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 9
diff changeset
    36
    end
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    37
  end
40
f75898c6595d Debugging support
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 9
diff changeset
    38
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    39
  rake_extension('info') do
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    40
    def info(message, details = nil)
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    41
      unless VERBOSE.nil?
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    42
        $stderr.puts "[INFO] #{message}"
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    43
        $stderr.puts "      #{details}" if details
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    44
      end
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    45
    end
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    46
  end
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    47
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    48
  rake_extension('warn') do
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    49
    def warn(message, details = nil)
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    50
      unless VERBOSE.nil?
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    51
        $stderr.puts "[WARN] #{message}"
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    52
        $stderr.puts "      #{details}" if details
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    53
      end
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    54
    end
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    55
  end
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    56
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    57
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    58
  rake_extension('error') do
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    59
    def error(message)
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    60
      raise Exception.new(message)
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    61
    end
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    62
  end
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    63
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    64
  rake_extension('error_unsupported_platform') do
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    65
    def error_unsupported_platform
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    66
      error("Unsupported platform (#{RbConfig::CONFIG['host_os']})")
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    67
    end
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    68
  end
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    69
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    70
  rake_extension('win32?') do
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    71
    def win32?
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    72
      return true if win32_wine?
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    73
      return (RbConfig::CONFIG['host_os'] =~ /mingw32/) != nil
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    74
    end
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    75
  end
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    76
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    77
  rake_extension('win32_wine?') do
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    78
    def win32_wine?
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    79
      return ENV['CROSSCOMPILE'] == 'wine'
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    80
    end
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    81
  end
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    82
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    83
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    84
  rake_extension('unix?') do
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    85
    def unix?
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    86
      return false if win32_wine?
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    87
      return (RbConfig::CONFIG['host_os'] =~ /linux|solaris/) != nil
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    88
    end
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    89
  end
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    90
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    91
  rake_extension('linux?') do
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    92
    def linux?
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    93
      return false if win32_wine?
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    94
      return (RbConfig::CONFIG['host_os'] =~ /linux/i) != nil
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    95
    end
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    96
  end
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    97
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    98
  rake_extension('x86_64?') do
3
18c56730fabf Renamed `amd64?` to `x86_64?` for consistency
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 0
diff changeset
    99
    def x86_64?
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   100
      return RbConfig::CONFIG['host_cpu'] == 'x86_64'
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   101
    end
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   102
  end
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   103
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   104
  rake_extension('i386?') do
3
18c56730fabf Renamed `amd64?` to `x86_64?` for consistency
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 0
diff changeset
   105
    def i386?
18c56730fabf Renamed `amd64?` to `x86_64?` for consistency
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 0
diff changeset
   106
      return RbConfig::CONFIG['host_cpu'] == 'i386'
18c56730fabf Renamed `amd64?` to `x86_64?` for consistency
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 0
diff changeset
   107
    end
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   108
  end
3
18c56730fabf Renamed `amd64?` to `x86_64?` for consistency
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 0
diff changeset
   109
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   110
  rake_extension('redefine') do
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   111
    def redefine(*args, &block)
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   112
      task_name, arg_names, deps = Rake.application.resolve_args(args)
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   113
      task = Rake.application.lookup(task_name)
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   114
      error "task #{task_name} not defined ant thus cannot be redefined" unless task
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   115
      info "Redefining task #{task.name}"
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   116
      task.clear
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   117
      task.set_arg_names(arg_names)
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   118
      task.enhance(deps, &block)
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   119
    end
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   120
  end
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   121
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   122
  rake_extension('clear') do
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   123
    def clear(*args, &block)
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   124
      error 'Block has no meaning when clearing task prereqs' if block_given?
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   125
      task_name, arg_names, deps = Rake.application.resolve_args(args)
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   126
      deps = deps.collect {|each| each.to_s}
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   127
      task = Rake.application.lookup(task_name)
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   128
      return nil unless task
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   129
      info "Clearing dependencies of task #{task.name} (#{deps.join(", ")})"
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   130
      task.prerequisites.reject! {|each| deps.include? each.to_s}
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   131
      return task
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   132
    end
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   133
  end
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   134
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   135
  class << self
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   136
    alias :__const_missing__ :const_missing
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   137
  end
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   138
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   139
  def self.const_missing(name)
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   140
    ENV[name.to_s] ? (return ENV[name.to_s]) : (return __const_missing__(name))
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   141
  end
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   142
end
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   143
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   144
module RakeFileUtils
81
2a1efb99c83d Oops, fixed bug introduced in de0e8016c2d5
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 62
diff changeset
   145
  # Evaluates given command using a shell. If block is given
2a1efb99c83d Oops, fixed bug introduced in de0e8016c2d5
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 62
diff changeset
   146
  # then block is evaluate with status, stdout and stderr.
2a1efb99c83d Oops, fixed bug introduced in de0e8016c2d5
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 62
diff changeset
   147
  # If no block is given, return true if command is successfull
91
76f23122e86a Added option (env var) to tell rakefiles to retain stc and librun sources.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 86
diff changeset
   148
  # ot thrown an exception if command fails (its exit status is non-zero)
62
de0e8016c2d5 Cleanup: Cleaned up command executing code in `rakelib\scm.rb`.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 42
diff changeset
   149
  #
de0e8016c2d5 Cleanup: Cleaned up command executing code in `rakelib\scm.rb`.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 42
diff changeset
   150
  # if cwd: keyword is specified, then working directory is changed
de0e8016c2d5 Cleanup: Cleaned up command executing code in `rakelib\scm.rb`.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 42
diff changeset
   151
  # to specified directory before the command is run.  
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   152
  def sh(cmd, cwd: Dir.pwd)
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   153
    when_writing(cmd) do
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   154
      if block_given?
62
de0e8016c2d5 Cleanup: Cleaned up command executing code in `rakelib\scm.rb`.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 42
diff changeset
   155
        chdir(cwd) do
de0e8016c2d5 Cleanup: Cleaned up command executing code in `rakelib\scm.rb`.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 42
diff changeset
   156
          fu_output_message (cmd.kind_of?(Array) ? cmd.join(' ') : cmd)
de0e8016c2d5 Cleanup: Cleaned up command executing code in `rakelib\scm.rb`.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 42
diff changeset
   157
          stdout, stderr, status = Open3.capture3(*cmd)
de0e8016c2d5 Cleanup: Cleaned up command executing code in `rakelib\scm.rb`.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 42
diff changeset
   158
        end
de0e8016c2d5 Cleanup: Cleaned up command executing code in `rakelib\scm.rb`.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 42
diff changeset
   159
        case block.arity
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   160
          when 1
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   161
            yield status
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   162
          when 2
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   163
            yield status, stdout
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   164
          when 3
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   165
            yield status, stdout, stderr
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   166
          else
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   167
            raise Exception.new('invalid arity of given block')
62
de0e8016c2d5 Cleanup: Cleaned up command executing code in `rakelib\scm.rb`.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 42
diff changeset
   168
        end
de0e8016c2d5 Cleanup: Cleaned up command executing code in `rakelib\scm.rb`.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 42
diff changeset
   169
        return status.success?
de0e8016c2d5 Cleanup: Cleaned up command executing code in `rakelib\scm.rb`.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 42
diff changeset
   170
      else
81
2a1efb99c83d Oops, fixed bug introduced in de0e8016c2d5
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 62
diff changeset
   171
        success = false
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   172
        chdir(cwd) do
62
de0e8016c2d5 Cleanup: Cleaned up command executing code in `rakelib\scm.rb`.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 42
diff changeset
   173
          fu_output_message (cmd.kind_of?(Array) ? cmd.join(' ') : cmd)
de0e8016c2d5 Cleanup: Cleaned up command executing code in `rakelib\scm.rb`.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 42
diff changeset
   174
          system(*cmd)
81
2a1efb99c83d Oops, fixed bug introduced in de0e8016c2d5
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 62
diff changeset
   175
          success = $?.success?
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   176
        end
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   177
        raise Exception.new("command failed: #{cmd.kind_of?(Array) ? cmd.join(' ') : cmd}") unless success
62
de0e8016c2d5 Cleanup: Cleaned up command executing code in `rakelib\scm.rb`.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 42
diff changeset
   178
      end
de0e8016c2d5 Cleanup: Cleaned up command executing code in `rakelib\scm.rb`.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 42
diff changeset
   179
    end
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   180
    true
62
de0e8016c2d5 Cleanup: Cleaned up command executing code in `rakelib\scm.rb`.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 42
diff changeset
   181
  end
de0e8016c2d5 Cleanup: Cleaned up command executing code in `rakelib\scm.rb`.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 42
diff changeset
   182
86
f2a7a4378c22 Added task `workflow:merge' to ease merging code from eXept
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 81
diff changeset
   183
  # Cross-platform way of finding an executable in the $PATH.
f2a7a4378c22 Added task `workflow:merge' to ease merging code from eXept
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 81
diff changeset
   184
  #
f2a7a4378c22 Added task `workflow:merge' to ease merging code from eXept
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 81
diff changeset
   185
  #   which('ruby') #=> /usr/bin/ruby  
f2a7a4378c22 Added task `workflow:merge' to ease merging code from eXept
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 81
diff changeset
   186
  def which(cmd)
f2a7a4378c22 Added task `workflow:merge' to ease merging code from eXept
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 81
diff changeset
   187
    exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
f2a7a4378c22 Added task `workflow:merge' to ease merging code from eXept
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 81
diff changeset
   188
    ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   189
      exts.each {|ext|
86
f2a7a4378c22 Added task `workflow:merge' to ease merging code from eXept
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 81
diff changeset
   190
        exe = File.join(path, "#{cmd}#{ext}")
f2a7a4378c22 Added task `workflow:merge' to ease merging code from eXept
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 81
diff changeset
   191
        return exe if File.executable?(exe) && !File.directory?(exe)
f2a7a4378c22 Added task `workflow:merge' to ease merging code from eXept
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 81
diff changeset
   192
      }
f2a7a4378c22 Added task `workflow:merge' to ease merging code from eXept
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 81
diff changeset
   193
    end
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   194
    nil
86
f2a7a4378c22 Added task `workflow:merge' to ease merging code from eXept
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 81
diff changeset
   195
  end
f2a7a4378c22 Added task `workflow:merge' to ease merging code from eXept
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 81
diff changeset
   196
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   197
  def make(args = '')
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   198
    if win32?
117
f163911eb7bc Win32: Workarounds for bugs in eXept's Win32 makefiles as of 2017-01-13
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 94
diff changeset
   199
      # As of 2017-01-13, mingwmake no longer makes sure objdir exists,
f163911eb7bc Win32: Workarounds for bugs in eXept's Win32 makefiles as of 2017-01-13
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 94
diff changeset
   200
      # causing mingwmake.bat to fail. To workaround that, make sure
f163911eb7bc Win32: Workarounds for bugs in eXept's Win32 makefiles as of 2017-01-13
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 94
diff changeset
   201
      # objdir exists. 
f163911eb7bc Win32: Workarounds for bugs in eXept's Win32 makefiles as of 2017-01-13
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 94
diff changeset
   202
      # How could eXept mess up things so badly?
f163911eb7bc Win32: Workarounds for bugs in eXept's Win32 makefiles as of 2017-01-13
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 94
diff changeset
   203
      mkdir_p OBJ_DIR
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   204
      sh "#{MAKE} #{args}"
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   205
    else
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   206
      sh "#{MAKE} #{args}"
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   207
    end
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   208
  end
7
b6fe3a90f6e0 Added `zip()` & `unzip()` extensions to easily create / extract archives from build scripts.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 3
diff changeset
   209
35
9d3a72ddb471 Fixed 32bit Linux builds on 32bit Linux.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 9
diff changeset
   210
  # Pretty much like sed. Replaces all occurences of `pattern` by `replacement` in given `file`.
9d3a72ddb471 Fixed 32bit Linux builds on 32bit Linux.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 9
diff changeset
   211
  # If `inplace` is `true`, then the modified contents is written back to the file. Otherwise it
9d3a72ddb471 Fixed 32bit Linux builds on 32bit Linux.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 9
diff changeset
   212
  # printed on `STDOUT`.
9d3a72ddb471 Fixed 32bit Linux builds on 32bit Linux.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 9
diff changeset
   213
  def sed(pattern, replacement, file, inplace = false)
9d3a72ddb471 Fixed 32bit Linux builds on 32bit Linux.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 9
diff changeset
   214
    contents = File.read(file)
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   215
93
650412e81596 Automatically extract package dependencies from project definition file
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 86
diff changeset
   216
    # Spuriously it happens the file is somehow corrupted and not propetly
650412e81596 Automatically extract package dependencies from project definition file
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 86
diff changeset
   217
    # UTF8 encoded. This would result in error like
650412e81596 Automatically extract package dependencies from project definition file
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 86
diff changeset
   218
    #
650412e81596 Automatically extract package dependencies from project definition file
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 86
diff changeset
   219
    #    `gsub': invalid byte sequence in UTF-8 (ArgumentError)
650412e81596 Automatically extract package dependencies from project definition file
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 86
diff changeset
   220
    #
650412e81596 Automatically extract package dependencies from project definition file
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 86
diff changeset
   221
    # To prevent, replace all invalid character by $?. Not a safest 
650412e81596 Automatically extract package dependencies from project definition file
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 86
diff changeset
   222
    # approach, though. 
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   223
    contents = contents.encode('UTF-8', :invalid => :replace, :replace => '?').encode('UTF-8')
93
650412e81596 Automatically extract package dependencies from project definition file
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 86
diff changeset
   224
35
9d3a72ddb471 Fixed 32bit Linux builds on 32bit Linux.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 9
diff changeset
   225
    contents.gsub!(pattern, replacement)
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   226
    if inplace
35
9d3a72ddb471 Fixed 32bit Linux builds on 32bit Linux.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 9
diff changeset
   227
      cp file, "#{file}.bak"
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   228
      File.open(file, 'w') {|f| f.puts contents}
35
9d3a72ddb471 Fixed 32bit Linux builds on 32bit Linux.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 9
diff changeset
   229
    else
9d3a72ddb471 Fixed 32bit Linux builds on 32bit Linux.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 9
diff changeset
   230
      STDOUT.puts contents
9d3a72ddb471 Fixed 32bit Linux builds on 32bit Linux.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 9
diff changeset
   231
    end
9d3a72ddb471 Fixed 32bit Linux builds on 32bit Linux.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 9
diff changeset
   232
  end
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   233
7
b6fe3a90f6e0 Added `zip()` & `unzip()` extensions to easily create / extract archives from build scripts.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 3
diff changeset
   234
  # Create a compressed archive of `source`. Under Windows it creates
b6fe3a90f6e0 Added `zip()` & `unzip()` extensions to easily create / extract archives from build scripts.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 3
diff changeset
   235
  # `.zip` archive, otherwise (on Linux) it creates `tar.bz2`. 
b6fe3a90f6e0 Added `zip()` & `unzip()` extensions to easily create / extract archives from build scripts.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 3
diff changeset
   236
  # 
b6fe3a90f6e0 Added `zip()` & `unzip()` extensions to easily create / extract archives from build scripts.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 3
diff changeset
   237
  # The archive is created in the same directory as the source and
b6fe3a90f6e0 Added `zip()` & `unzip()` extensions to easily create / extract archives from build scripts.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 3
diff changeset
   238
  # has the same name unless explicitily specified by `archive:` option. 
b6fe3a90f6e0 Added `zip()` & `unzip()` extensions to easily create / extract archives from build scripts.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 3
diff changeset
   239
  # If `remove: true` option is set to true, the original (`source`) directory is
b6fe3a90f6e0 Added `zip()` & `unzip()` extensions to easily create / extract archives from build scripts.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 3
diff changeset
   240
  # removed after adding the archive. 
b6fe3a90f6e0 Added `zip()` & `unzip()` extensions to easily create / extract archives from build scripts.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 3
diff changeset
   241
  #
b6fe3a90f6e0 Added `zip()` & `unzip()` extensions to easily create / extract archives from build scripts.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 3
diff changeset
   242
  # As a side-effect, it generates a SHA256 checksum in file .sha256 unles
b6fe3a90f6e0 Added `zip()` & `unzip()` extensions to easily create / extract archives from build scripts.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 3
diff changeset
   243
  # option `sha256: false` is given.
b6fe3a90f6e0 Added `zip()` & `unzip()` extensions to easily create / extract archives from build scripts.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 3
diff changeset
   244
  # 
b6fe3a90f6e0 Added `zip()` & `unzip()` extensions to easily create / extract archives from build scripts.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 3
diff changeset
   245
  # Examples: 
b6fe3a90f6e0 Added `zip()` & `unzip()` extensions to easily create / extract archives from build scripts.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 3
diff changeset
   246
  #
172
b0b41978d5b1 Rakefiles: modernize `zip()` and `unzip()` functions
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 142
diff changeset
   247
  # Create `somedir.bar.bz2` in `/tmp` containg contants of `/tmp/somedir`:
7
b6fe3a90f6e0 Added `zip()` & `unzip()` extensions to easily create / extract archives from build scripts.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 3
diff changeset
   248
  #
b6fe3a90f6e0 Added `zip()` & `unzip()` extensions to easily create / extract archives from build scripts.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 3
diff changeset
   249
  #     zip '/tmp/somedir'
b6fe3a90f6e0 Added `zip()` & `unzip()` extensions to easily create / extract archives from build scripts.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 3
diff changeset
   250
  #
b6fe3a90f6e0 Added `zip()` & `unzip()` extensions to easily create / extract archives from build scripts.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 3
diff changeset
   251
  # Create `smalltalkx.bar.bz2` on `/tmp` containg contants of `/tmp/build_dir`
b6fe3a90f6e0 Added `zip()` & `unzip()` extensions to easily create / extract archives from build scripts.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 3
diff changeset
   252
  # and remove `/tmp/build_dir` afterwards:
b6fe3a90f6e0 Added `zip()` & `unzip()` extensions to easily create / extract archives from build scripts.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 3
diff changeset
   253
  # 
b6fe3a90f6e0 Added `zip()` & `unzip()` extensions to easily create / extract archives from build scripts.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 3
diff changeset
   254
  #     zip '/tmp/build_dir', archive: 'smalltalkx', remove: true
b6fe3a90f6e0 Added `zip()` & `unzip()` extensions to easily create / extract archives from build scripts.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 3
diff changeset
   255
  #  
172
b0b41978d5b1 Rakefiles: modernize `zip()` and `unzip()` functions
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 142
diff changeset
   256
  def zip(source, remove: false, archive: nil, sha256: true)
7
b6fe3a90f6e0 Added `zip()` & `unzip()` extensions to easily create / extract archives from build scripts.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 3
diff changeset
   257
    suffix = win32? ? '.zip' : '.tar.bz2'
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   258
    if !archive
7
b6fe3a90f6e0 Added `zip()` & `unzip()` extensions to easily create / extract archives from build scripts.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 3
diff changeset
   259
      archive = "#{source}#{suffix}"
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   260
    else
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   261
      archive = "#{archive}#{suffix}" unless archive.end_with? suffix
7
b6fe3a90f6e0 Added `zip()` & `unzip()` extensions to easily create / extract archives from build scripts.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 3
diff changeset
   262
    end
b6fe3a90f6e0 Added `zip()` & `unzip()` extensions to easily create / extract archives from build scripts.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 3
diff changeset
   263
    archive = File.expand_path(archive)
b6fe3a90f6e0 Added `zip()` & `unzip()` extensions to easily create / extract archives from build scripts.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 3
diff changeset
   264
    chdir File.dirname(source) do
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   265
      if win32?
7
b6fe3a90f6e0 Added `zip()` & `unzip()` extensions to easily create / extract archives from build scripts.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 3
diff changeset
   266
        sh "zip -q -r #{remove ? '-T -m' : ''} \"#{archive}\" \"#{File.basename(source)}\""
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   267
      else
7
b6fe3a90f6e0 Added `zip()` & `unzip()` extensions to easily create / extract archives from build scripts.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 3
diff changeset
   268
        sh "tar cjf \"#{archive}\" #{remove ? '--remove-files' : ''} \"#{File.basename(source)}\""
b6fe3a90f6e0 Added `zip()` & `unzip()` extensions to easily create / extract archives from build scripts.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 3
diff changeset
   269
      end
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   270
    end
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   271
    if sha256
7
b6fe3a90f6e0 Added `zip()` & `unzip()` extensions to easily create / extract archives from build scripts.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 3
diff changeset
   272
      require 'digest'
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   273
      File.open("#{archive}.sha256", 'w') do |sum|
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   274
        sum.write Digest::SHA256.file(archive).hexdigest
7
b6fe3a90f6e0 Added `zip()` & `unzip()` extensions to easily create / extract archives from build scripts.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 3
diff changeset
   275
      end
b6fe3a90f6e0 Added `zip()` & `unzip()` extensions to easily create / extract archives from build scripts.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 3
diff changeset
   276
    end
b6fe3a90f6e0 Added `zip()` & `unzip()` extensions to easily create / extract archives from build scripts.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 3
diff changeset
   277
  end
b6fe3a90f6e0 Added `zip()` & `unzip()` extensions to easily create / extract archives from build scripts.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 3
diff changeset
   278
b6fe3a90f6e0 Added `zip()` & `unzip()` extensions to easily create / extract archives from build scripts.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 3
diff changeset
   279
  # Extract an (compressed) archive. 
b6fe3a90f6e0 Added `zip()` & `unzip()` extensions to easily create / extract archives from build scripts.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 3
diff changeset
   280
  # 
b6fe3a90f6e0 Added `zip()` & `unzip()` extensions to easily create / extract archives from build scripts.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 3
diff changeset
   281
  # Files are extracted to the directory that contains `archive` unless 
b6fe3a90f6e0 Added `zip()` & `unzip()` extensions to easily create / extract archives from build scripts.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 3
diff changeset
   282
  # options `directory: "some/other/directory` is given. 
b6fe3a90f6e0 Added `zip()` & `unzip()` extensions to easily create / extract archives from build scripts.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 3
diff changeset
   283
  # 
b6fe3a90f6e0 Added `zip()` & `unzip()` extensions to easily create / extract archives from build scripts.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 3
diff changeset
   284
  # If file named `#{archive}.sha256` exists then SHA256 checksum is validated
b6fe3a90f6e0 Added `zip()` & `unzip()` extensions to easily create / extract archives from build scripts.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 3
diff changeset
   285
  # against the one `.sha256` file. If differ, an `Exception` is raised.
b6fe3a90f6e0 Added `zip()` & `unzip()` extensions to easily create / extract archives from build scripts.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 3
diff changeset
   286
  #
b6fe3a90f6e0 Added `zip()` & `unzip()` extensions to easily create / extract archives from build scripts.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 3
diff changeset
   287
  # If option `remove: true` is given, then the archive is removed after
b6fe3a90f6e0 Added `zip()` & `unzip()` extensions to easily create / extract archives from build scripts.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 3
diff changeset
   288
  # all files are extracted. 
b6fe3a90f6e0 Added `zip()` & `unzip()` extensions to easily create / extract archives from build scripts.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 3
diff changeset
   289
  # 
172
b0b41978d5b1 Rakefiles: modernize `zip()` and `unzip()` functions
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 142
diff changeset
   290
  def unzip(archive, directory: File.dirname(archive), remove: false)
7
b6fe3a90f6e0 Added `zip()` & `unzip()` extensions to easily create / extract archives from build scripts.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 3
diff changeset
   291
    archive = File.expand_path archive
b6fe3a90f6e0 Added `zip()` & `unzip()` extensions to easily create / extract archives from build scripts.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 3
diff changeset
   292
    sha256 = "#{archive}.sha256"
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   293
    if File.exist? sha256
7
b6fe3a90f6e0 Added `zip()` & `unzip()` extensions to easily create / extract archives from build scripts.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 3
diff changeset
   294
      require 'digest'
b6fe3a90f6e0 Added `zip()` & `unzip()` extensions to easily create / extract archives from build scripts.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 3
diff changeset
   295
      actual = Digest::SHA256.file(archive).hexdigest
b6fe3a90f6e0 Added `zip()` & `unzip()` extensions to easily create / extract archives from build scripts.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 3
diff changeset
   296
      expected = nil
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   297
      File.open(sha256) {|f| expected = f.read}
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   298
      if actual != expected
7
b6fe3a90f6e0 Added `zip()` & `unzip()` extensions to easily create / extract archives from build scripts.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 3
diff changeset
   299
        raise Exception.new("SHA256 checksum for #{archive} differ (actual #{actual}, expected #{expected}")
b6fe3a90f6e0 Added `zip()` & `unzip()` extensions to easily create / extract archives from build scripts.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 3
diff changeset
   300
      end
b6fe3a90f6e0 Added `zip()` & `unzip()` extensions to easily create / extract archives from build scripts.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 3
diff changeset
   301
    end
b6fe3a90f6e0 Added `zip()` & `unzip()` extensions to easily create / extract archives from build scripts.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 3
diff changeset
   302
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   303
    chdir directory do
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   304
      case
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   305
        when (archive.end_with? '.zip')
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   306
          sh "unzip \"#{archive}\""
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   307
        when (archive.end_with? '.tar.bz2')
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   308
          sh "tar xjf \"#{archive}\""
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   309
        when (archive.end_with? '.tar.gz')
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   310
          sh "tar xzf \"#{archive}\""
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   311
        else
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   312
          raise Exception.new("Unknown archive type: #{File.basename(archive)}")
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   313
      end
7
b6fe3a90f6e0 Added `zip()` & `unzip()` extensions to easily create / extract archives from build scripts.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 3
diff changeset
   314
    end
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   315
    rm_f archive if remove
7
b6fe3a90f6e0 Added `zip()` & `unzip()` extensions to easily create / extract archives from build scripts.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 3
diff changeset
   316
  end
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   317
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   318
9
34274130f57a Removed some old, dead code.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 7
diff changeset
   319
  # Like FileUtils.cp_r, but takes a filter proc that can return false 
34274130f57a Removed some old, dead code.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 7
diff changeset
   320
  # to skip a file
34274130f57a Removed some old, dead code.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 7
diff changeset
   321
  #
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   322
  # Note that if the filter rejects a subdirectory then everything within that
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   323
  # subdirectory is automatically skipped as well.
9
34274130f57a Removed some old, dead code.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 7
diff changeset
   324
  #
34274130f57a Removed some old, dead code.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 7
diff changeset
   325
  # Taken from http://www.ruby-forum.com/attachment/4467/filteredCopy.rb  
34274130f57a Removed some old, dead code.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 7
diff changeset
   326
  # Both of these are modified from the implementations in fileutils.rb from 
34274130f57a Removed some old, dead code.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 7
diff changeset
   327
  # Ruby 1.9.1p378  
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   328
  def cp_rx(src, dest, options = {}, &filter)
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   329
    fu_check_options options, OPT_TABLE['cp_r']
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   330
    fu_output_message "cp -r#{options[:preserve] ? 'p' : ''}#{options[:remove_destination] ? ' --remove-destination' : ''} #{[src, dest].flatten.join ' '}" if options[:verbose]
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   331
    return if options[:noop]
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   332
    fu_each_src_dest(src, dest) do |s, d|
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   333
      copy_entryx s, d, filter, options[:preserve], options[:dereference_root], options[:remove_destination]
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   334
    end
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   335
  end
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   336
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   337
  # Like FileUtils.copy_entry, but takes a filter proc that can return false to skip a file
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   338
  def copy_entryx(src, dest, filter, preserve = false, dereference_root = false, remove_destination = false)
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   339
    Entry_.new(src, nil, dereference_root).traverse do |ent|
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   340
      if filter.call(ent.path)
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   341
        destent = Entry_.new(dest, ent.rel, false)
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   342
        File.unlink destent.path if remove_destination && File.file?(destent.path)
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   343
        ent.copy destent.path
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   344
        ent.copy_metadata destent.path if preserve
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   345
      end
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   346
    end
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   347
  end
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   348
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   349
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   350
  # * ruby implementation of find that follows symbolic directory links
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   351
  # * tested on ruby 1.9.3, ruby 2.0 and jruby on Fedora 20 linux
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   352
  # * you can use Find.prune
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   353
  # * detect symlinks to dirs by path "/" suffix; does nothing with files so `symlink?` method is working fine
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   354
  # * depth first order
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   355
  # * detects cycles and raises an error
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   356
  # * raises on broken links
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   357
  # * uses recursion in the `do_find` proc when directory links are met (takes a lot of nested links until SystemStackError, that's practically never)
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   358
  #
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   359
  # * use like: find_follow(".") {|f| puts f}
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   360
  #
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   361
  # Copyright (c) 2014 Red Hat inc
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   362
  #
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   363
  # Permission is hereby granted, free of charge, to any person obtaining a copy
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   364
  # of this software and associated documentation files (the "Software"), to deal
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   365
  # in the Software without restriction, including without limitation the rights
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   366
  # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   367
  # copies of the Software, and to permit persons to whom the Software is
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   368
  # furnished to do so, subject to the following conditions:
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   369
  #
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   370
  # The above copyright notice and this permission notice shall be included in
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   371
  # all copies or substantial portions of the Software.
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   372
  #
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   373
  # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   374
  # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   375
  # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   376
  # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   377
  # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   378
  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   379
  # THE SOFTWARE.
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   380
  def find_follow(*paths)
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   381
    block_given? or return enum_for(__method__, *paths)
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   382
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   383
    link_cache = {}
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   384
    link_resolve = lambda {|path|
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   385
      # puts "++ link_resolve: #{path}" # trace
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   386
      link_cache[path] ? (return link_cache[path]) : (return link_cache[path] = Pathname.new(path).realpath.to_s)
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   387
    }
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   388
    # this lambda should cleanup `link_cache` from unnecessary entries
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   389
    link_cache_reset = lambda {|path|
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   390
      # puts "++ link_cache_reset: #{path}" # trace
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   391
      # puts link_cache.to_s # trace
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   392
      link_cache.select! {|k, v| path == k || k == '/' || path.start_with?(k + '/')}
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   393
      # puts link_cache.to_s # trace
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   394
    }
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   395
    link_is_recursive = lambda {|path|
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   396
      # puts "++ link_is_recursive: #{path}" # trace
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   397
      # the ckeck is useless if path is not a link but not our responsibility
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   398
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   399
      # we need to check full path for link cycles
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   400
      pn_initial = Pathname.new(path)
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   401
      # can we use `expand_path` here? Any issues with links?
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   402
      pn_initial = Pathname.new(File.join(Dir.pwd, path)) unless pn_initial.absolute?
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   403
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   404
      # clear unnecessary cache
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   405
      link_cache_reset.call(pn_initial.to_s)
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   406
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   407
      link_dst = link_resolve.call(pn_initial.to_s)
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   408
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   409
      pn_initial.ascend {|pn| (return {:link => path, :dst => pn}) if pn != pn_initial && link_dst == link_resolve.call(pn.to_s)}
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   410
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   411
      return false
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   412
    }
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   413
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   414
    do_find = proc {|multi_path|
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   415
      Find.find(multi_path) do |path|
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   416
        if File.symlink?(path) && File.directory?(File.realpath(path))
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   417
          if path[-1] == '/'
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   418
            # probably hitting https://github.com/jruby/jruby/issues/1895
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   419
            yield(path.dup)
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   420
            Dir.new(path).each {|subpath|
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   421
              do_find.call(path + subpath) unless %w('.' '..').include?(subpath)
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   422
            }
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   423
          elsif is_recursive == link_is_recursive.call(path) # TODO: meaning? was =
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   424
            raise "cannot handle recursive links: #{is_recursive[:link]} => #{is_recursive[:dst]}"
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   425
          else
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   426
            do_find.call(path + '/')
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   427
          end
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   428
        else
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   429
          yield(path)
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   430
        end
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   431
      end
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   432
    }
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   433
142
357ba05ad169 Fixed slip in `extensions.rb` introduced in commit e665031cade7
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 137
diff changeset
   434
    # DO NOT ever change = by ==, we do need an assignment
357ba05ad169 Fixed slip in `extensions.rb` introduced in commit e665031cade7
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 137
diff changeset
   435
    # here into `path` which is then used in the body!
357ba05ad169 Fixed slip in `extensions.rb` introduced in commit e665031cade7
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 137
diff changeset
   436
    while path = paths.shift 
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   437
      do_find.call(path)
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   438
    end
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   439
  end
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   440
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   441
  # Taken from https://gist.github.com/akostadinov/fc688feba7669a4eb784
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   442
  # based on find_follow.rb : https://gist.github.com/akostadinov/05c2a976dc16ffee9cac
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   443
  # 
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   444
  # * use like: cp_r_dereference 'src', 'dst'
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   445
  #
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   446
  # Note: if directory `src` content is copied instead of the full dir. i.e. you end up
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   447
  #                                     with `dst/*` instead of `dst/basename(src)/*`
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   448
  # 
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   449
  # Copyright (c) 2014 Red Hat inc
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   450
  # 
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   451
  # Permission is hereby granted, free of charge, to any person obtaining a copy
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   452
  # of this software and associated documentation files (the "Software"), to deal
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   453
  # in the Software without restriction, including without limitation the rights
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   454
  # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   455
  # copies of the Software, and to permit persons to whom the Software is
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   456
  # furnished to do so, subject to the following conditions:
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   457
  # 
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   458
  # The above copyright notice and this permission notice shall be included in
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   459
  # all copies or substantial portions of the Software.
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   460
  # 
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   461
  # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   462
  # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   463
  # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   464
  # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   465
  # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   466
  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   467
  # THE SOFTWARE.
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   468
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   469
  # copy recursively de-referencing symlinks
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   470
  def cp_r_dereference(src, dst)
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   471
    src_pn = Pathname.new(src)
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   472
    find_follow(src) do |path|
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   473
      relpath = Pathname.new(path).relative_path_from(src_pn).to_s
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   474
      dstpath = File.join(dst, relpath)
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   475
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   476
      if File.directory?(path) || (File.symlink?(path) && File.directory?(File.realpath(path)))
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   477
        FileUtils.mkdir_p(dstpath)
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   478
      else
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   479
        FileUtils.copy_file(path, dstpath)
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   480
      end
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   481
    end
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   482
  end
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   483
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   484
end
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   485
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   486