rakelib/extensions.rb
author Jan Vrany <jan.vrany@fit.cvut.cz>
Mon, 24 Sep 2018 20:55:06 +0100
changeset 258 65caf0201157
parent 236 5a4e789cdd40
child 308 4b2539ae38f1
permissions -rw-r--r--
Fix missing `smalltalk.svg` and `smalltalkx.desktop` in toy archive ...which caused a warning when run on system without these already installed: cp: cannot stat './smalltalkx.svg': No such file or directory cp: cannot stat './smalltalkx.desktop': No such file or directory Thanks Pocho for reporting.
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
210
e02c0c5b6434 Rakefiles: hacks to support long paths on Windows
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 175
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)
210
e02c0c5b6434 Rakefiles: hacks to support long paths on Windows
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 175
diff changeset
    10
      if win32? && r.size >= 260 then        
e02c0c5b6434 Rakefiles: hacks to support long paths on Windows
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 175
diff changeset
    11
        halt
e02c0c5b6434 Rakefiles: hacks to support long paths on Windows
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 175
diff changeset
    12
        r = "\\\\?\\#{r}" if not r.start_with?('\\\\?\\')
e02c0c5b6434 Rakefiles: hacks to support long paths on Windows
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 175
diff changeset
    13
      end
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    14
      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
    15
        r = Dir.glob(r)
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    16
        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
    17
      end
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    18
      return r
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    19
    end
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    20
  end
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    21
end
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    22
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    23
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    24
(defined? VERBOSE) || (VERBOSE = nil)
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    25
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    26
class Object
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    27
  rake_extension('halt') do
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    28
    def halt
40
f75898c6595d Debugging support
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 9
diff changeset
    29
      begin
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    30
        require 'pry'
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    31
      rescue LoadError
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    32
        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
    33
      end
f75898c6595d Debugging support
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 9
diff changeset
    34
      begin
f75898c6595d Debugging support
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 9
diff changeset
    35
        require 'pry-byebug'
f75898c6595d Debugging support
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 9
diff changeset
    36
      rescue LoadError
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    37
        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
    38
      end
f75898c6595d Debugging support
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 9
diff changeset
    39
      binding.pry
f75898c6595d Debugging support
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 9
diff changeset
    40
    end
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    41
  end
40
f75898c6595d Debugging support
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 9
diff changeset
    42
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    43
  rake_extension('info') do
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    44
    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
    45
      unless VERBOSE.nil?
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    46
        $stderr.puts "[INFO] #{message}"
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    47
        $stderr.puts "      #{details}" if details
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    48
      end
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    49
    end
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    50
  end
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    51
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    52
  rake_extension('warn') do
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    53
    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
    54
      unless VERBOSE.nil?
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    55
        $stderr.puts "[WARN] #{message}"
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    56
        $stderr.puts "      #{details}" if details
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    57
      end
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    58
    end
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    59
  end
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    60
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    61
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    62
  rake_extension('error') do
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    63
    def error(message)
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    64
      raise Exception.new(message)
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    65
    end
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    66
  end
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    67
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    68
  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
    69
    def error_unsupported_platform
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    70
      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
    71
    end
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    72
  end
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    73
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    74
  rake_extension('win32?') do
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    75
    def win32?
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    76
      return true if win32_wine?
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    77
      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
    78
    end
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    79
  end
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    80
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    81
  rake_extension('win32_wine?') do
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    82
    def win32_wine?
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    83
      return ENV['CROSSCOMPILE'] == 'wine'
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    84
    end
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    85
  end
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    86
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    87
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    88
  rake_extension('unix?') do
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    89
    def unix?
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    90
      return false if win32_wine?
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    91
      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
    92
    end
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    93
  end
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    94
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    95
  rake_extension('linux?') do
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    96
    def linux?
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    97
      return false if win32_wine?
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    98
      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
    99
    end
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   100
  end
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   101
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   102
  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
   103
    def x86_64?
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   104
      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
   105
    end
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   106
  end
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   107
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   108
  rake_extension('i386?') do
3
18c56730fabf Renamed `amd64?` to `x86_64?` for consistency
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 0
diff changeset
   109
    def i386?
18c56730fabf Renamed `amd64?` to `x86_64?` for consistency
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 0
diff changeset
   110
      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
   111
    end
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   112
  end
3
18c56730fabf Renamed `amd64?` to `x86_64?` for consistency
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 0
diff changeset
   113
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   114
  rake_extension('redefine') do
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   115
    def redefine(*args, &block)
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   116
      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
   117
      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
   118
      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
   119
      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
   120
      task.clear
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   121
      task.set_arg_names(arg_names)
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   122
      task.enhance(deps, &block)
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   123
    end
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   124
  end
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   125
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   126
  rake_extension('clear') do
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   127
    def clear(*args, &block)
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   128
      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
   129
      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
   130
      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
   131
      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
   132
      return nil unless task
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   133
      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
   134
      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
   135
      return task
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   136
    end
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   137
  end
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   138
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   139
  class << self
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   140
    alias :__const_missing__ :const_missing
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
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   143
  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
   144
    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
   145
  end
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   146
end
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   147
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   148
module RakeFileUtils
81
2a1efb99c83d Oops, fixed bug introduced in de0e8016c2d5
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 62
diff changeset
   149
  # 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
   150
  # 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
   151
  # 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
   152
  # 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
   153
  #
de0e8016c2d5 Cleanup: Cleaned up command executing code in `rakelib\scm.rb`.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 42
diff changeset
   154
  # 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
   155
  # 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
   156
  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
   157
    when_writing(cmd) do
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   158
      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
   159
        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
   160
          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
   161
          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
   162
        end
de0e8016c2d5 Cleanup: Cleaned up command executing code in `rakelib\scm.rb`.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 42
diff changeset
   163
        case block.arity
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   164
          when 1
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   165
            yield status
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   166
          when 2
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   167
            yield status, stdout
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   168
          when 3
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   169
            yield status, stdout, stderr
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   170
          else
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   171
            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
   172
        end
de0e8016c2d5 Cleanup: Cleaned up command executing code in `rakelib\scm.rb`.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 42
diff changeset
   173
        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
   174
      else
81
2a1efb99c83d Oops, fixed bug introduced in de0e8016c2d5
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 62
diff changeset
   175
        success = false
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   176
        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
   177
          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
   178
          system(*cmd)
81
2a1efb99c83d Oops, fixed bug introduced in de0e8016c2d5
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 62
diff changeset
   179
          success = $?.success?
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   180
        end
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   181
        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
   182
      end
de0e8016c2d5 Cleanup: Cleaned up command executing code in `rakelib\scm.rb`.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 42
diff changeset
   183
    end
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   184
    true
62
de0e8016c2d5 Cleanup: Cleaned up command executing code in `rakelib\scm.rb`.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 42
diff changeset
   185
  end
de0e8016c2d5 Cleanup: Cleaned up command executing code in `rakelib\scm.rb`.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 42
diff changeset
   186
86
f2a7a4378c22 Added task `workflow:merge' to ease merging code from eXept
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 81
diff changeset
   187
  # Cross-platform way of finding an executable in the $PATH.
173
43ecb6a2bcbd Rakelib: put `zip()` in par with `unzip()` and guess archive type
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 172
diff changeset
   188
  # Return full path to the `cmd` or `nil` if given command
43ecb6a2bcbd Rakelib: put `zip()` in par with `unzip()` and guess archive type
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 172
diff changeset
   189
  # is not in the path. 
43ecb6a2bcbd Rakelib: put `zip()` in par with `unzip()` and guess archive type
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 172
diff changeset
   190
  #
43ecb6a2bcbd Rakelib: put `zip()` in par with `unzip()` and guess archive type
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 172
diff changeset
   191
  # Examples
86
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
  #   which('ruby') #=> /usr/bin/ruby  
173
43ecb6a2bcbd Rakelib: put `zip()` in par with `unzip()` and guess archive type
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 172
diff changeset
   194
  #
43ecb6a2bcbd Rakelib: put `zip()` in par with `unzip()` and guess archive type
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 172
diff changeset
   195
  #   which('boguscommand') #=> nil
43ecb6a2bcbd Rakelib: put `zip()` in par with `unzip()` and guess archive type
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 172
diff changeset
   196
  #
86
f2a7a4378c22 Added task `workflow:merge' to ease merging code from eXept
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 81
diff changeset
   197
  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
   198
    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
   199
    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
   200
      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
   201
        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
   202
        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
   203
      }
f2a7a4378c22 Added task `workflow:merge' to ease merging code from eXept
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 81
diff changeset
   204
    end
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   205
    nil
86
f2a7a4378c22 Added task `workflow:merge' to ease merging code from eXept
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 81
diff changeset
   206
  end
f2a7a4378c22 Added task `workflow:merge' to ease merging code from eXept
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 81
diff changeset
   207
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   208
  def make(args = '')
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   209
    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
   210
      # 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
   211
      # 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
   212
      # 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
   213
      # 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
   214
      mkdir_p OBJ_DIR
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   215
      sh "#{MAKE} #{args}"
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   216
    else
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   217
      sh "#{MAKE} #{args}"
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   218
    end
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   219
  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
   220
175
c28460fb4b9d Rakefiles: introduced new fileutils functions - `rm_r_ex()` and `rm_rf_ex()`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 174
diff changeset
   221
  # Like FileUtils::rm_r but does NOT remove files explicitly listed 
c28460fb4b9d Rakefiles: introduced new fileutils functions - `rm_r_ex()` and `rm_rf_ex()`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 174
diff changeset
   222
  # in `exceptions` (i.e., these files are NOT removed). Exceptions
c28460fb4b9d Rakefiles: introduced new fileutils functions - `rm_r_ex()` and `rm_rf_ex()`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 174
diff changeset
   223
  # are interpreted as relative paths to directory (directories) in 
c28460fb4b9d Rakefiles: introduced new fileutils functions - `rm_r_ex()` and `rm_rf_ex()`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 174
diff changeset
   224
  # `list`
c28460fb4b9d Rakefiles: introduced new fileutils functions - `rm_r_ex()` and `rm_rf_ex()`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 174
diff changeset
   225
  #
c28460fb4b9d Rakefiles: introduced new fileutils functions - `rm_r_ex()` and `rm_rf_ex()`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 174
diff changeset
   226
  # Examples: 
c28460fb4b9d Rakefiles: introduced new fileutils functions - `rm_r_ex()` and `rm_rf_ex()`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 174
diff changeset
   227
  #
c28460fb4b9d Rakefiles: introduced new fileutils functions - `rm_r_ex()` and `rm_rf_ex()`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 174
diff changeset
   228
  #   Completely wipe-out `build` directory except `build/stx-config.rake`
c28460fb4b9d Rakefiles: introduced new fileutils functions - `rm_r_ex()` and `rm_rf_ex()`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 174
diff changeset
   229
  #   and `build/stx-config.make`:
c28460fb4b9d Rakefiles: introduced new fileutils functions - `rm_r_ex()` and `rm_rf_ex()`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 174
diff changeset
   230
  #
c28460fb4b9d Rakefiles: introduced new fileutils functions - `rm_r_ex()` and `rm_rf_ex()`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 174
diff changeset
   231
  #   
c28460fb4b9d Rakefiles: introduced new fileutils functions - `rm_r_ex()` and `rm_rf_ex()`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 174
diff changeset
   232
  #
c28460fb4b9d Rakefiles: introduced new fileutils functions - `rm_r_ex()` and `rm_rf_ex()`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 174
diff changeset
   233
  def rm_r_ex(list, force: nil, noop: nil, verbose: nil, secure: nil, exceptions: [])        
c28460fb4b9d Rakefiles: introduced new fileutils functions - `rm_r_ex()` and `rm_rf_ex()`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 174
diff changeset
   234
    if exceptions.empty? 
c28460fb4b9d Rakefiles: introduced new fileutils functions - `rm_r_ex()` and `rm_rf_ex()`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 174
diff changeset
   235
      rm_r list, force: force, noop: noop, verbose: verbose, secure: secure
c28460fb4b9d Rakefiles: introduced new fileutils functions - `rm_r_ex()` and `rm_rf_ex()`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 174
diff changeset
   236
    else
c28460fb4b9d Rakefiles: introduced new fileutils functions - `rm_r_ex()` and `rm_rf_ex()`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 174
diff changeset
   237
      list = fu_list(list)
c28460fb4b9d Rakefiles: introduced new fileutils functions - `rm_r_ex()` and `rm_rf_ex()`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 174
diff changeset
   238
      list.each do | path |
c28460fb4b9d Rakefiles: introduced new fileutils functions - `rm_r_ex()` and `rm_rf_ex()`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 174
diff changeset
   239
        if File.directory? path        
c28460fb4b9d Rakefiles: introduced new fileutils functions - `rm_r_ex()` and `rm_rf_ex()`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 174
diff changeset
   240
          Dir.foreach(path) do | entry |            
c28460fb4b9d Rakefiles: introduced new fileutils functions - `rm_r_ex()` and `rm_rf_ex()`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 174
diff changeset
   241
            if (entry != '.') and (entry != '..') and !(exceptions.include? entry)                        
c28460fb4b9d Rakefiles: introduced new fileutils functions - `rm_r_ex()` and `rm_rf_ex()`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 174
diff changeset
   242
              entry_exceptions = exceptions.select { | each_exception | each_exception.start_with? "#{entry}/" }                          
c28460fb4b9d Rakefiles: introduced new fileutils functions - `rm_r_ex()` and `rm_rf_ex()`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 174
diff changeset
   243
              if entry_exceptions.empty? 
c28460fb4b9d Rakefiles: introduced new fileutils functions - `rm_r_ex()` and `rm_rf_ex()`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 174
diff changeset
   244
                rm_r(File.join(path , entry), force: force, noop: noop, verbose: verbose, secure: secure)
c28460fb4b9d Rakefiles: introduced new fileutils functions - `rm_r_ex()` and `rm_rf_ex()`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 174
diff changeset
   245
              else
c28460fb4b9d Rakefiles: introduced new fileutils functions - `rm_r_ex()` and `rm_rf_ex()`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 174
diff changeset
   246
                entry_exceptions = entry_exceptions.collect { | each_exception | each_exception.slice(entry.size+1..-1) }
c28460fb4b9d Rakefiles: introduced new fileutils functions - `rm_r_ex()` and `rm_rf_ex()`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 174
diff changeset
   247
                rm_r_ex(File.join(path , entry), force: force, noop: noop, verbose: verbose, secure: secure, exceptions: entry_exceptions)                                          
c28460fb4b9d Rakefiles: introduced new fileutils functions - `rm_r_ex()` and `rm_rf_ex()`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 174
diff changeset
   248
              end              
c28460fb4b9d Rakefiles: introduced new fileutils functions - `rm_r_ex()` and `rm_rf_ex()`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 174
diff changeset
   249
            end          
c28460fb4b9d Rakefiles: introduced new fileutils functions - `rm_r_ex()` and `rm_rf_ex()`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 174
diff changeset
   250
          end        
c28460fb4b9d Rakefiles: introduced new fileutils functions - `rm_r_ex()` and `rm_rf_ex()`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 174
diff changeset
   251
          if Dir.entries(path).size <= 2
c28460fb4b9d Rakefiles: introduced new fileutils functions - `rm_r_ex()` and `rm_rf_ex()`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 174
diff changeset
   252
            rmdir path
c28460fb4b9d Rakefiles: introduced new fileutils functions - `rm_r_ex()` and `rm_rf_ex()`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 174
diff changeset
   253
          end
c28460fb4b9d Rakefiles: introduced new fileutils functions - `rm_r_ex()` and `rm_rf_ex()`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 174
diff changeset
   254
        else 
c28460fb4b9d Rakefiles: introduced new fileutils functions - `rm_r_ex()` and `rm_rf_ex()`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 174
diff changeset
   255
          rm_r path, force: force, noop: noop, verbose: verbose, secure: secure
c28460fb4b9d Rakefiles: introduced new fileutils functions - `rm_r_ex()` and `rm_rf_ex()`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 174
diff changeset
   256
        end
c28460fb4b9d Rakefiles: introduced new fileutils functions - `rm_r_ex()` and `rm_rf_ex()`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 174
diff changeset
   257
      end
c28460fb4b9d Rakefiles: introduced new fileutils functions - `rm_r_ex()` and `rm_rf_ex()`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 174
diff changeset
   258
    end
c28460fb4b9d Rakefiles: introduced new fileutils functions - `rm_r_ex()` and `rm_rf_ex()`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 174
diff changeset
   259
  end
c28460fb4b9d Rakefiles: introduced new fileutils functions - `rm_r_ex()` and `rm_rf_ex()`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 174
diff changeset
   260
c28460fb4b9d Rakefiles: introduced new fileutils functions - `rm_r_ex()` and `rm_rf_ex()`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 174
diff changeset
   261
  # Like FileUtils::rm_rf but does NOT remove files explicitly listed 
c28460fb4b9d Rakefiles: introduced new fileutils functions - `rm_r_ex()` and `rm_rf_ex()`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 174
diff changeset
   262
  # in `exceptions`. See `rm_r_ex` for details
c28460fb4b9d Rakefiles: introduced new fileutils functions - `rm_r_ex()` and `rm_rf_ex()`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 174
diff changeset
   263
  def rm_rf_ex(list, noop: nil, verbose: nil, secure: nil, exceptions: [])
c28460fb4b9d Rakefiles: introduced new fileutils functions - `rm_r_ex()` and `rm_rf_ex()`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 174
diff changeset
   264
    rm_r_ex list, force: true, noop: noop, verbose: verbose, secure: secure, exceptions: exceptions
c28460fb4b9d Rakefiles: introduced new fileutils functions - `rm_r_ex()` and `rm_rf_ex()`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 174
diff changeset
   265
  end
c28460fb4b9d Rakefiles: introduced new fileutils functions - `rm_r_ex()` and `rm_rf_ex()`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 174
diff changeset
   266
35
9d3a72ddb471 Fixed 32bit Linux builds on 32bit Linux.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 9
diff changeset
   267
  # 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
   268
  # 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
   269
  # printed on `STDOUT`.
9d3a72ddb471 Fixed 32bit Linux builds on 32bit Linux.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 9
diff changeset
   270
  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
   271
    contents = File.read(file)
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   272
93
650412e81596 Automatically extract package dependencies from project definition file
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 86
diff changeset
   273
    # 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
   274
    # 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
   275
    #
650412e81596 Automatically extract package dependencies from project definition file
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 86
diff changeset
   276
    #    `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
   277
    #
650412e81596 Automatically extract package dependencies from project definition file
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 86
diff changeset
   278
    # 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
   279
    # approach, though. 
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   280
    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
   281
35
9d3a72ddb471 Fixed 32bit Linux builds on 32bit Linux.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 9
diff changeset
   282
    contents.gsub!(pattern, replacement)
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   283
    if inplace
35
9d3a72ddb471 Fixed 32bit Linux builds on 32bit Linux.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 9
diff changeset
   284
      cp file, "#{file}.bak"
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   285
      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
   286
    else
9d3a72ddb471 Fixed 32bit Linux builds on 32bit Linux.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 9
diff changeset
   287
      STDOUT.puts contents
9d3a72ddb471 Fixed 32bit Linux builds on 32bit Linux.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 9
diff changeset
   288
    end
9d3a72ddb471 Fixed 32bit Linux builds on 32bit Linux.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 9
diff changeset
   289
  end
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   290
173
43ecb6a2bcbd Rakelib: put `zip()` in par with `unzip()` and guess archive type
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 172
diff changeset
   291
  # Create a compressed archive of `directory`. 
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
   292
  # 
173
43ecb6a2bcbd Rakelib: put `zip()` in par with `unzip()` and guess archive type
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 172
diff changeset
   293
  # The archive is created in the same directory as the `directory` and
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
  # has the same name unless explicitily specified by `archive:` option. 
173
43ecb6a2bcbd Rakelib: put `zip()` in par with `unzip()` and guess archive type
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 172
diff changeset
   295
  # If `remove: true` option is set to true, the `directory` to archive is
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
   296
  # removed after adding the archive. 
174
89e357ae48b4 Rakefiles: allow `zip()` to archive only a subset of files
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 173
diff changeset
   297
  #
89e357ae48b4 Rakefiles: allow `zip()` to archive only a subset of files
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 173
diff changeset
   298
  # If `include` is given (i.e., not `nil`), then only files within
89e357ae48b4 Rakefiles: allow `zip()` to archive only a subset of files
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 173
diff changeset
   299
  # `directory` are added to the archive. 
173
43ecb6a2bcbd Rakelib: put `zip()` in par with `unzip()` and guess archive type
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 172
diff changeset
   300
  # 
43ecb6a2bcbd Rakelib: put `zip()` in par with `unzip()` and guess archive type
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 172
diff changeset
   301
  # The type of the archive is automatically derived from `archive` name (if 
43ecb6a2bcbd Rakelib: put `zip()` in par with `unzip()` and guess archive type
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 172
diff changeset
   302
  # provided) or defaults to `.tar.bz2` on UNIXes and to `.zip` on Windows. 
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
   303
  #
173
43ecb6a2bcbd Rakelib: put `zip()` in par with `unzip()` and guess archive type
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 172
diff changeset
   304
  # As a side-effect, it generates a SHA256 checksum in file .sha256 unless
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
   305
  # 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
   306
  # 
b6fe3a90f6e0 Added `zip()` & `unzip()` extensions to easily create / extract archives from build scripts.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 3
diff changeset
   307
  # 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
   308
  #
172
b0b41978d5b1 Rakefiles: modernize `zip()` and `unzip()` functions
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 142
diff changeset
   309
  # 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
   310
  #
b6fe3a90f6e0 Added `zip()` & `unzip()` extensions to easily create / extract archives from build scripts.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 3
diff changeset
   311
  #     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
   312
  #
b6fe3a90f6e0 Added `zip()` & `unzip()` extensions to easily create / extract archives from build scripts.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 3
diff changeset
   313
  # 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
   314
  # 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
   315
  # 
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
  #     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
   317
  #  
234
05943c900d5e Rakefiles: added new target `artifacts:source`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 227
diff changeset
   318
  def zip(directory, remove: false, archive: nil, sha256: true, include: nil, exclude: [])        
173
43ecb6a2bcbd Rakelib: put `zip()` in par with `unzip()` and guess archive type
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 172
diff changeset
   319
    archive = directory unless archive
43ecb6a2bcbd Rakelib: put `zip()` in par with `unzip()` and guess archive type
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 172
diff changeset
   320
    if !(archive.end_with? '.zip') && !(archive.end_with? '.tar.gz') && !(archive.end_with? '.tar.bz2')
43ecb6a2bcbd Rakelib: put `zip()` in par with `unzip()` and guess archive type
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 172
diff changeset
   321
      archive = "#{directory}#{win32? ? '.zip' : '.tar.bz2'}"
174
89e357ae48b4 Rakefiles: allow `zip()` to archive only a subset of files
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 173
diff changeset
   322
    end      
89e357ae48b4 Rakefiles: allow `zip()` to archive only a subset of files
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 173
diff changeset
   323
    archive = File.expand_path(archive)
89e357ae48b4 Rakefiles: allow `zip()` to archive only a subset of files
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 173
diff changeset
   324
    source = [ "\"#{File.basename(directory)}\"" ]
89e357ae48b4 Rakefiles: allow `zip()` to archive only a subset of files
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 173
diff changeset
   325
    unless include.nil?
89e357ae48b4 Rakefiles: allow `zip()` to archive only a subset of files
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 173
diff changeset
   326
      source = include.collect { | each | "\"#{File.join(File.basename(directory), each)}\"" }
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
   327
    end
173
43ecb6a2bcbd Rakelib: put `zip()` in par with `unzip()` and guess archive type
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 172
diff changeset
   328
    chdir File.dirname(directory) do
43ecb6a2bcbd Rakelib: put `zip()` in par with `unzip()` and guess archive type
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 172
diff changeset
   329
      case
43ecb6a2bcbd Rakelib: put `zip()` in par with `unzip()` and guess archive type
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 172
diff changeset
   330
        when (archive.end_with? '.zip')
234
05943c900d5e Rakefiles: added new target `artifacts:source`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 227
diff changeset
   331
          if not exclude.empty? 
05943c900d5e Rakefiles: added new target `artifacts:source`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 227
diff changeset
   332
            raise Exception.new("zip(): exclude: parameter not supported for .zip archives")
05943c900d5e Rakefiles: added new target `artifacts:source`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 227
diff changeset
   333
          end         
174
89e357ae48b4 Rakefiles: allow `zip()` to archive only a subset of files
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 173
diff changeset
   334
          sh "zip -q -r #{remove ? '-T -m' : ''} \"#{archive}\" #{source.join(' ')}"
173
43ecb6a2bcbd Rakelib: put `zip()` in par with `unzip()` and guess archive type
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 172
diff changeset
   335
        when (archive.end_with? '.tar.bz2')
234
05943c900d5e Rakefiles: added new target `artifacts:source`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 227
diff changeset
   336
          ex = (exclude.collect { | e | "\"--exclude=#{e}\" "}).join(' ')
05943c900d5e Rakefiles: added new target `artifacts:source`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 227
diff changeset
   337
          sh "tar cjf \"#{archive}\" #{remove ? '--remove-files' : ''} #{ex} #{source.join(' ')}"
173
43ecb6a2bcbd Rakelib: put `zip()` in par with `unzip()` and guess archive type
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 172
diff changeset
   338
        when (archive.end_with? '.tar.gz')
234
05943c900d5e Rakefiles: added new target `artifacts:source`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 227
diff changeset
   339
          ex = (exclude.collect { | e | "\"--exclude=#{e}\" "}).join(' ')
05943c900d5e Rakefiles: added new target `artifacts:source`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 227
diff changeset
   340
          sh "tar czf \"#{archive}\" #{remove ? '--remove-files' : ''} #{ex} #{source.join(' ')}"
173
43ecb6a2bcbd Rakelib: put `zip()` in par with `unzip()` and guess archive type
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 172
diff changeset
   341
        else
43ecb6a2bcbd Rakelib: put `zip()` in par with `unzip()` and guess archive type
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 172
diff changeset
   342
          raise Exception.new("Unknown archive type: #{File.basename(archive)}")
43ecb6a2bcbd Rakelib: put `zip()` in par with `unzip()` and guess archive type
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 172
diff changeset
   343
      end      
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   344
    end
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   345
    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
   346
      require 'digest'
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   347
      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
   348
        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
   349
      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
   350
    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
   351
  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
   352
b6fe3a90f6e0 Added `zip()` & `unzip()` extensions to easily create / extract archives from build scripts.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 3
diff changeset
   353
  # 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
   354
  # 
b6fe3a90f6e0 Added `zip()` & `unzip()` extensions to easily create / extract archives from build scripts.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 3
diff changeset
   355
  # 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
   356
  # 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
   357
  # 
b6fe3a90f6e0 Added `zip()` & `unzip()` extensions to easily create / extract archives from build scripts.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 3
diff changeset
   358
  # 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
   359
  # 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
   360
  #
b6fe3a90f6e0 Added `zip()` & `unzip()` extensions to easily create / extract archives from build scripts.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 3
diff changeset
   361
  # 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
   362
  # 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
   363
  # 
172
b0b41978d5b1 Rakefiles: modernize `zip()` and `unzip()` functions
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 142
diff changeset
   364
  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
   365
    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
   366
    sha256 = "#{archive}.sha256"
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   367
    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
   368
      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
   369
      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
   370
      expected = nil
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   371
      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
   372
      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
   373
        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
   374
      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
   375
    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
   376
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   377
    chdir directory do
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   378
      case
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   379
        when (archive.end_with? '.zip')
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   380
          sh "unzip \"#{archive}\""
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   381
        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
   382
          sh "tar xjf \"#{archive}\""
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   383
        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
   384
          sh "tar xzf \"#{archive}\""
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   385
        else
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   386
          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
   387
      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
   388
    end
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   389
    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
   390
  end
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   391
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   392
9
34274130f57a Removed some old, dead code.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 7
diff changeset
   393
  # 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
   394
  # to skip a file
34274130f57a Removed some old, dead code.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 7
diff changeset
   395
  #
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   396
  # 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
   397
  # subdirectory is automatically skipped as well.
9
34274130f57a Removed some old, dead code.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 7
diff changeset
   398
  #
34274130f57a Removed some old, dead code.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 7
diff changeset
   399
  # 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
   400
  # 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
   401
  # Ruby 1.9.1p378  
227
640f942d4e63 Rakefiles: fixed `cp_rx()` extension to work with Ruby 2.5
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 210
diff changeset
   402
  def cp_rx(src, dest, preserve: nil, noop: nil, verbose: nil,
640f942d4e63 Rakefiles: fixed `cp_rx()` extension to work with Ruby 2.5
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 210
diff changeset
   403
         dereference_root: true, remove_destination: nil, &filter)        
640f942d4e63 Rakefiles: fixed `cp_rx()` extension to work with Ruby 2.5
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 210
diff changeset
   404
    fu_output_message "cp_rx -r#{preserve ? 'p' : ''}#{remove_destination ? ' --remove-destination' : ''} #{[src,dest].flatten.join ' '} " if verbose 
640f942d4e63 Rakefiles: fixed `cp_rx()` extension to work with Ruby 2.5
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 210
diff changeset
   405
    return if noop
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   406
    fu_each_src_dest(src, dest) do |s, d|
227
640f942d4e63 Rakefiles: fixed `cp_rx()` extension to work with Ruby 2.5
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 210
diff changeset
   407
      copy_entryx s, d, filter, preserve, dereference_root, remove_destination
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   408
    end
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   409
  end
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
  # 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
   412
  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
   413
    Entry_.new(src, nil, dereference_root).traverse do |ent|
210
e02c0c5b6434 Rakefiles: hacks to support long paths on Windows
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 175
diff changeset
   414
      if filter.call(ent.path())
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   415
        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
   416
        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
   417
        ent.copy destent.path
227
640f942d4e63 Rakefiles: fixed `cp_rx()` extension to work with Ruby 2.5
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 210
diff changeset
   418
        ent.copy_metadata destent.path if nil
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   419
      end
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   420
    end
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   421
  end
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
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   424
  # * 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
   425
  # * 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
   426
  # * you can use Find.prune
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   427
  # * 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
   428
  # * depth first order
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   429
  # * detects cycles and raises an error
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   430
  # * raises on broken links
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   431
  # * 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
   432
  #
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   433
  # * 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
   434
  #
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   435
  # Copyright (c) 2014 Red Hat inc
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   436
  #
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   437
  # 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
   438
  # 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
   439
  # 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
   440
  # 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
   441
  # 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
   442
  # 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
   443
  #
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   444
  # 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
   445
  # 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
   446
  #
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   447
  # 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
   448
  # 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
   449
  # 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
   450
  # 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
   451
  # 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
   452
  # 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
   453
  # THE SOFTWARE.
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   454
  def find_follow(*paths)
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   455
    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
   456
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   457
    link_cache = {}
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   458
    link_resolve = lambda {|path|
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   459
      # 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
   460
      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
   461
    }
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   462
    # 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
   463
    link_cache_reset = lambda {|path|
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   464
      # puts "++ link_cache_reset: #{path}" # trace
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   465
      # 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
   466
      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
   467
      # puts link_cache.to_s # trace
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   468
    }
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   469
    link_is_recursive = lambda {|path|
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   470
      # puts "++ link_is_recursive: #{path}" # trace
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   471
      # 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
   472
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   473
      # 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
   474
      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
   475
      # 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
   476
      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
   477
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   478
      # clear unnecessary cache
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   479
      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
   480
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   481
      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
   482
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   483
      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
   484
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   485
      return false
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   486
    }
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   487
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   488
    do_find = proc {|multi_path|
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   489
      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
   490
        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
   491
          if path[-1] == '/'
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   492
            # 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
   493
            yield(path.dup)
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   494
            Dir.new(path).each {|subpath|
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   495
              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
   496
            }
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   497
          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
   498
            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
   499
          else
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   500
            do_find.call(path + '/')
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   501
          end
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   502
        else
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   503
          yield(path)
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   504
        end
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   505
      end
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   506
    }
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   507
142
357ba05ad169 Fixed slip in `extensions.rb` introduced in commit e665031cade7
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 137
diff changeset
   508
    # 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
   509
    # 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
   510
    while path = paths.shift 
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   511
      do_find.call(path)
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   512
    end
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   513
  end
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   514
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   515
  # 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
   516
  # 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
   517
  # 
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   518
  # * 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
   519
  #
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   520
  # 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
   521
  #                                     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
   522
  # 
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   523
  # Copyright (c) 2014 Red Hat inc
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   524
  # 
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   525
  # 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
   526
  # 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
   527
  # 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
   528
  # 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
   529
  # 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
   530
  # 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
   531
  # 
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   532
  # 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
   533
  # 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
   534
  # 
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   535
  # 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
   536
  # 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
   537
  # 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
   538
  # 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
   539
  # 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
   540
  # 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
   541
  # THE SOFTWARE.
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   542
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   543
  # copy recursively de-referencing symlinks
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   544
  def cp_r_dereference(src, dst)
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   545
    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
   546
    find_follow(src) do |path|
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   547
      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
   548
      dstpath = File.join(dst, relpath)
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   549
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   550
      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
   551
        FileUtils.mkdir_p(dstpath)
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   552
      else
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   553
        FileUtils.copy_file(path, dstpath)
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   554
      end
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   555
    end
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   556
  end
210
e02c0c5b6434 Rakefiles: hacks to support long paths on Windows
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 175
diff changeset
   557
end
e02c0c5b6434 Rakefiles: hacks to support long paths on Windows
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 175
diff changeset
   558
e02c0c5b6434 Rakefiles: hacks to support long paths on Windows
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 175
diff changeset
   559
module FileUtils::Entry_Extensions
e02c0c5b6434 Rakefiles: hacks to support long paths on Windows
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 175
diff changeset
   560
  def fix_long_path(p)
e02c0c5b6434 Rakefiles: hacks to support long paths on Windows
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 175
diff changeset
   561
    if win32? && p.size >= 260 && !p.start_with?("\\\\?\\") then      
e02c0c5b6434 Rakefiles: hacks to support long paths on Windows
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 175
diff changeset
   562
      p = "\\\\?\\#{p.gsub('/','\\')}"      
e02c0c5b6434 Rakefiles: hacks to support long paths on Windows
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 175
diff changeset
   563
    end
e02c0c5b6434 Rakefiles: hacks to support long paths on Windows
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 175
diff changeset
   564
    p
e02c0c5b6434 Rakefiles: hacks to support long paths on Windows
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 175
diff changeset
   565
  end
e02c0c5b6434 Rakefiles: hacks to support long paths on Windows
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 175
diff changeset
   566
e02c0c5b6434 Rakefiles: hacks to support long paths on Windows
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 175
diff changeset
   567
  def path
e02c0c5b6434 Rakefiles: hacks to support long paths on Windows
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 175
diff changeset
   568
    fix_long_path(super)    
e02c0c5b6434 Rakefiles: hacks to support long paths on Windows
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 175
diff changeset
   569
  end
e02c0c5b6434 Rakefiles: hacks to support long paths on Windows
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 175
diff changeset
   570
e02c0c5b6434 Rakefiles: hacks to support long paths on Windows
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 175
diff changeset
   571
  def copy_file(dest)
e02c0c5b6434 Rakefiles: hacks to support long paths on Windows
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 175
diff changeset
   572
    super(fix_long_path(dest))
e02c0c5b6434 Rakefiles: hacks to support long paths on Windows
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 175
diff changeset
   573
  end
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   574
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   575
end
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   576
210
e02c0c5b6434 Rakefiles: hacks to support long paths on Windows
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 175
diff changeset
   577
class FileUtils::Entry_
e02c0c5b6434 Rakefiles: hacks to support long paths on Windows
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 175
diff changeset
   578
  prepend FileUtils::Entry_Extensions
e02c0c5b6434 Rakefiles: hacks to support long paths on Windows
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 175
diff changeset
   579
end