rakelib/extensions.rb
author Jan Vrany <jan.vrany@labware.com>
Wed, 04 Nov 2020 11:10:18 +0000
changeset 310 2cf08578aa5f
parent 308 4b2539ae38f1
permissions -rw-r--r--
Do not ship VDB as part of default build / toy archive The reason is that few people used it and those who did (or will) are those who may want to hack on it. To make this easier when using toy archive, we won't ship VDB in toy archive anymore and instead we'll document on how to install it in VDB's `README.md`
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)
308
4b2539ae38f1 Rakefiles: show `warn()`ings by default
Jan Vrany <jan.vrany@labware.com>
parents: 236
diff changeset
    54
      $stderr.puts "[WARN] #{message}"
4b2539ae38f1 Rakefiles: show `warn()`ings by default
Jan Vrany <jan.vrany@labware.com>
parents: 236
diff changeset
    55
      $stderr.puts "      #{details}" if details
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    56
    end
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    57
  end
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    58
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    59
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    60
  rake_extension('error') do
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    61
    def error(message)
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    62
      raise Exception.new(message)
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    63
    end
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    64
  end
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    65
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    66
  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
    67
    def error_unsupported_platform
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    68
      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
    69
    end
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    70
  end
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    71
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    72
  rake_extension('win32?') do
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    73
    def win32?
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    74
      return true if win32_wine?
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    75
      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
    76
    end
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    77
  end
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    78
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    79
  rake_extension('win32_wine?') do
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    80
    def win32_wine?
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    81
      return ENV['CROSSCOMPILE'] == 'wine'
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    82
    end
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    83
  end
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    84
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    85
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    86
  rake_extension('unix?') do
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    87
    def unix?
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    88
      return false if win32_wine?
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    89
      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
    90
    end
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    91
  end
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    92
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    93
  rake_extension('linux?') do
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    94
    def linux?
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    95
      return false if win32_wine?
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
    96
      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
    97
    end
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    98
  end
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
    99
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   100
  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
   101
    def x86_64?
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   102
      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
   103
    end
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   104
  end
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   105
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   106
  rake_extension('i386?') do
3
18c56730fabf Renamed `amd64?` to `x86_64?` for consistency
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 0
diff changeset
   107
    def i386?
18c56730fabf Renamed `amd64?` to `x86_64?` for consistency
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 0
diff changeset
   108
      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
   109
    end
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   110
  end
3
18c56730fabf Renamed `amd64?` to `x86_64?` for consistency
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 0
diff changeset
   111
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   112
  rake_extension('redefine') do
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   113
    def redefine(*args, &block)
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   114
      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
   115
      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
   116
      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
   117
      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
   118
      task.clear
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   119
      task.set_arg_names(arg_names)
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   120
      task.enhance(deps, &block)
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   121
    end
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   122
  end
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   123
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   124
  rake_extension('clear') do
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   125
    def clear(*args, &block)
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   126
      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
   127
      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
   128
      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
   129
      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
   130
      return nil unless task
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   131
      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
   132
      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
   133
      return task
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   134
    end
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   135
  end
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   136
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   137
  class << self
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   138
    alias :__const_missing__ :const_missing
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   139
  end
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   140
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   141
  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
   142
    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
   143
  end
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   144
end
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   145
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   146
module RakeFileUtils
81
2a1efb99c83d Oops, fixed bug introduced in de0e8016c2d5
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 62
diff changeset
   147
  # 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
   148
  # 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
   149
  # 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
   150
  # 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
   151
  #
de0e8016c2d5 Cleanup: Cleaned up command executing code in `rakelib\scm.rb`.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 42
diff changeset
   152
  # 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
   153
  # 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
   154
  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
   155
    when_writing(cmd) do
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   156
      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
   157
        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
   158
          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
   159
          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
   160
        end
de0e8016c2d5 Cleanup: Cleaned up command executing code in `rakelib\scm.rb`.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 42
diff changeset
   161
        case block.arity
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   162
          when 1
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   163
            yield status
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   164
          when 2
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   165
            yield status, stdout
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   166
          when 3
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   167
            yield status, stdout, stderr
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   168
          else
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   169
            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
   170
        end
de0e8016c2d5 Cleanup: Cleaned up command executing code in `rakelib\scm.rb`.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 42
diff changeset
   171
        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
   172
      else
81
2a1efb99c83d Oops, fixed bug introduced in de0e8016c2d5
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 62
diff changeset
   173
        success = false
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   174
        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
   175
          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
   176
          system(*cmd)
81
2a1efb99c83d Oops, fixed bug introduced in de0e8016c2d5
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 62
diff changeset
   177
          success = $?.success?
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   178
        end
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   179
        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
   180
      end
de0e8016c2d5 Cleanup: Cleaned up command executing code in `rakelib\scm.rb`.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 42
diff changeset
   181
    end
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   182
    true
62
de0e8016c2d5 Cleanup: Cleaned up command executing code in `rakelib\scm.rb`.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 42
diff changeset
   183
  end
de0e8016c2d5 Cleanup: Cleaned up command executing code in `rakelib\scm.rb`.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 42
diff changeset
   184
86
f2a7a4378c22 Added task `workflow:merge' to ease merging code from eXept
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 81
diff changeset
   185
  # 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
   186
  # 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
   187
  # 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
   188
  #
43ecb6a2bcbd Rakelib: put `zip()` in par with `unzip()` and guess archive type
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 172
diff changeset
   189
  # Examples
86
f2a7a4378c22 Added task `workflow:merge' to ease merging code from eXept
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 81
diff changeset
   190
  #
f2a7a4378c22 Added task `workflow:merge' to ease merging code from eXept
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 81
diff changeset
   191
  #   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
   192
  #
43ecb6a2bcbd Rakelib: put `zip()` in par with `unzip()` and guess archive type
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 172
diff changeset
   193
  #   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
   194
  #
86
f2a7a4378c22 Added task `workflow:merge' to ease merging code from eXept
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 81
diff changeset
   195
  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
   196
    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
   197
    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
   198
      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
   199
        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
   200
        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
   201
      }
f2a7a4378c22 Added task `workflow:merge' to ease merging code from eXept
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 81
diff changeset
   202
    end
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   203
    nil
86
f2a7a4378c22 Added task `workflow:merge' to ease merging code from eXept
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 81
diff changeset
   204
  end
f2a7a4378c22 Added task `workflow:merge' to ease merging code from eXept
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 81
diff changeset
   205
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   206
  def make(args = '')
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   207
    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
   208
      # 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
   209
      # 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
   210
      # 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
      # 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
   212
      mkdir_p OBJ_DIR
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   213
      sh "#{MAKE} #{args}"
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   214
    else
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   215
      sh "#{MAKE} #{args}"
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   216
    end
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   217
  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
   218
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
   219
  # 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
   220
  # 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
   221
  # 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
   222
  # `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
   223
  #
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
  # 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
   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
  #   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
   227
  #   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
   228
  #
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
  #   
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
  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
   232
    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
   233
      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
   234
    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
   235
      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
   236
      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
   237
        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
   238
          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
   239
            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
   240
              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
   241
              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
   242
                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
   243
              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
   244
                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
   245
                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
   246
              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
   247
            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
   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
          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
   250
            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
   251
          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
   252
        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
   253
          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
   254
        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
   255
      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
   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
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
  # 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
   260
  # 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
   261
  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
   262
    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
   263
  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
   264
35
9d3a72ddb471 Fixed 32bit Linux builds on 32bit Linux.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 9
diff changeset
   265
  # 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
   266
  # 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
   267
  # printed on `STDOUT`.
9d3a72ddb471 Fixed 32bit Linux builds on 32bit Linux.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 9
diff changeset
   268
  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
   269
    contents = File.read(file)
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   270
93
650412e81596 Automatically extract package dependencies from project definition file
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 86
diff changeset
   271
    # 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
   272
    # 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
   273
    #
650412e81596 Automatically extract package dependencies from project definition file
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 86
diff changeset
   274
    #    `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
   275
    #
650412e81596 Automatically extract package dependencies from project definition file
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 86
diff changeset
   276
    # 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
   277
    # approach, though. 
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   278
    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
   279
35
9d3a72ddb471 Fixed 32bit Linux builds on 32bit Linux.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 9
diff changeset
   280
    contents.gsub!(pattern, replacement)
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   281
    if inplace
35
9d3a72ddb471 Fixed 32bit Linux builds on 32bit Linux.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 9
diff changeset
   282
      cp file, "#{file}.bak"
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   283
      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
   284
    else
9d3a72ddb471 Fixed 32bit Linux builds on 32bit Linux.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 9
diff changeset
   285
      STDOUT.puts contents
9d3a72ddb471 Fixed 32bit Linux builds on 32bit Linux.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 9
diff changeset
   286
    end
9d3a72ddb471 Fixed 32bit Linux builds on 32bit Linux.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 9
diff changeset
   287
  end
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   288
173
43ecb6a2bcbd Rakelib: put `zip()` in par with `unzip()` and guess archive type
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 172
diff changeset
   289
  # 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
   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
  # 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
   292
  # 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
   293
  # 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
   294
  # 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
   295
  #
89e357ae48b4 Rakefiles: allow `zip()` to archive only a subset of files
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 173
diff changeset
   296
  # 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
   297
  # `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
   298
  # 
43ecb6a2bcbd Rakelib: put `zip()` in par with `unzip()` and guess archive type
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 172
diff changeset
   299
  # 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
   300
  # 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
   301
  #
173
43ecb6a2bcbd Rakelib: put `zip()` in par with `unzip()` and guess archive type
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 172
diff changeset
   302
  # 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
   303
  # 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
   304
  # 
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
  # 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
   306
  #
172
b0b41978d5b1 Rakefiles: modernize `zip()` and `unzip()` functions
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 142
diff changeset
   307
  # 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
   308
  #
b6fe3a90f6e0 Added `zip()` & `unzip()` extensions to easily create / extract archives from build scripts.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 3
diff changeset
   309
  #     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
   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
  # 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
   312
  # 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
   313
  # 
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
  #     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
   315
  #  
234
05943c900d5e Rakefiles: added new target `artifacts:source`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 227
diff changeset
   316
  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
   317
    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
   318
    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
   319
      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
   320
    end      
89e357ae48b4 Rakefiles: allow `zip()` to archive only a subset of files
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 173
diff changeset
   321
    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
   322
    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
   323
    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
   324
      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
   325
    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
   326
    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
   327
      case
43ecb6a2bcbd Rakelib: put `zip()` in par with `unzip()` and guess archive type
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 172
diff changeset
   328
        when (archive.end_with? '.zip')
234
05943c900d5e Rakefiles: added new target `artifacts:source`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 227
diff changeset
   329
          if not exclude.empty? 
05943c900d5e Rakefiles: added new target `artifacts:source`
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 227
diff changeset
   330
            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
   331
          end         
174
89e357ae48b4 Rakefiles: allow `zip()` to archive only a subset of files
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 173
diff changeset
   332
          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
   333
        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
   334
          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
   335
          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
   336
        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
   337
          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
   338
          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
   339
        else
43ecb6a2bcbd Rakelib: put `zip()` in par with `unzip()` and guess archive type
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 172
diff changeset
   340
          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
   341
      end      
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   342
    end
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   343
    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
   344
      require 'digest'
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   345
      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
   346
        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
   347
      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
   348
    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
   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
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
  # 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
   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
  # 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
   354
  # 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
   355
  # 
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
  # 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
   357
  # 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
   358
  #
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
  # 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
   360
  # 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
   361
  # 
172
b0b41978d5b1 Rakefiles: modernize `zip()` and `unzip()` functions
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 142
diff changeset
   362
  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
   363
    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
   364
    sha256 = "#{archive}.sha256"
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   365
    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
   366
      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
   367
      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
   368
      expected = nil
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   369
      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
   370
      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
   371
        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
   372
      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
   373
    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
   374
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   375
    chdir directory do
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   376
      case
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   377
        when (archive.end_with? '.zip')
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   378
          sh "unzip \"#{archive}\""
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   379
        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
   380
          sh "tar xjf \"#{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.gz')
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   382
          sh "tar xzf \"#{archive}\""
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   383
        else
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   384
          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
   385
      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
   386
    end
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   387
    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
   388
  end
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   389
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   390
9
34274130f57a Removed some old, dead code.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 7
diff changeset
   391
  # 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
   392
  # to skip a file
34274130f57a Removed some old, dead code.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 7
diff changeset
   393
  #
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   394
  # 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
   395
  # subdirectory is automatically skipped as well.
9
34274130f57a Removed some old, dead code.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 7
diff changeset
   396
  #
34274130f57a Removed some old, dead code.
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 7
diff changeset
   397
  # 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
   398
  # 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
   399
  # 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
   400
  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
   401
         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
   402
    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
   403
    return if noop
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   404
    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
   405
      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
   406
    end
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   407
  end
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   408
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   409
  # 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
   410
  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
   411
    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
   412
      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
   413
        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
   414
        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
   415
        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
   416
        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
   417
      end
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   418
    end
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   419
  end
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   420
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   421
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   422
  # * 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
   423
  # * 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
   424
  # * you can use Find.prune
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   425
  # * 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
   426
  # * depth first order
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   427
  # * detects cycles and raises an error
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   428
  # * raises on broken links
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   429
  # * 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
   430
  #
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   431
  # * 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
   432
  #
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   433
  # Copyright (c) 2014 Red Hat inc
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
  # 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
   436
  # 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
   437
  # 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
   438
  # 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
   439
  # 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
   440
  # 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
   441
  #
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   442
  # 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
   443
  # 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
   444
  #
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   445
  # 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
   446
  # 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
   447
  # 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
   448
  # 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
   449
  # 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
   450
  # 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
   451
  # THE SOFTWARE.
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   452
  def find_follow(*paths)
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   453
    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
   454
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   455
    link_cache = {}
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   456
    link_resolve = lambda {|path|
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   457
      # 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
   458
      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
   459
    }
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   460
    # 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
   461
    link_cache_reset = lambda {|path|
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   462
      # puts "++ link_cache_reset: #{path}" # trace
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   463
      # 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
   464
      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
   465
      # puts link_cache.to_s # trace
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   466
    }
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   467
    link_is_recursive = lambda {|path|
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   468
      # puts "++ link_is_recursive: #{path}" # trace
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   469
      # 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
   470
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   471
      # 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
   472
      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
   473
      # 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
   474
      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
   475
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   476
      # clear unnecessary cache
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   477
      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
   478
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   479
      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
   480
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   481
      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
   482
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   483
      return false
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
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   486
    do_find = proc {|multi_path|
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   487
      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
   488
        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
   489
          if path[-1] == '/'
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   490
            # 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
   491
            yield(path.dup)
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   492
            Dir.new(path).each {|subpath|
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   493
              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
   494
            }
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   495
          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
   496
            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
   497
          else
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   498
            do_find.call(path + '/')
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   499
          end
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   500
        else
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   501
          yield(path)
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   502
        end
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   503
      end
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   504
    }
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   505
142
357ba05ad169 Fixed slip in `extensions.rb` introduced in commit e665031cade7
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 137
diff changeset
   506
    # 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
   507
    # 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
   508
    while path = paths.shift 
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   509
      do_find.call(path)
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   510
    end
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   511
  end
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   512
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   513
  # 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
   514
  # 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
   515
  # 
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   516
  # * 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
   517
  #
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   518
  # 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
   519
  #                                     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
   520
  # 
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   521
  # Copyright (c) 2014 Red Hat inc
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
  # 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
   524
  # 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
   525
  # 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
   526
  # 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
   527
  # 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
   528
  # 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
   529
  # 
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   530
  # 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
   531
  # 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
   532
  # 
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   533
  # 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
   534
  # 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
   535
  # 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
   536
  # 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
   537
  # 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
   538
  # 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
   539
  # THE SOFTWARE.
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   540
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   541
  # copy recursively de-referencing symlinks
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   542
  def cp_r_dereference(src, dst)
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   543
    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
   544
    find_follow(src) do |path|
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   545
      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
   546
      dstpath = File.join(dst, relpath)
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   547
137
e665031cade7 Cleaning up code and having it more rubyish
Patrik Svestka <patrik.svestka@gmail.com>
parents: 117
diff changeset
   548
      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
   549
        FileUtils.mkdir_p(dstpath)
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   550
      else
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   551
        FileUtils.copy_file(path, dstpath)
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   552
      end
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   553
    end
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   554
  end
210
e02c0c5b6434 Rakefiles: hacks to support long paths on Windows
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 175
diff changeset
   555
end
e02c0c5b6434 Rakefiles: hacks to support long paths on Windows
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 175
diff changeset
   556
e02c0c5b6434 Rakefiles: hacks to support long paths on Windows
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 175
diff changeset
   557
module FileUtils::Entry_Extensions
e02c0c5b6434 Rakefiles: hacks to support long paths on Windows
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 175
diff changeset
   558
  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
   559
    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
   560
      p = "\\\\?\\#{p.gsub('/','\\')}"      
e02c0c5b6434 Rakefiles: hacks to support long paths on Windows
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 175
diff changeset
   561
    end
e02c0c5b6434 Rakefiles: hacks to support long paths on Windows
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 175
diff changeset
   562
    p
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
e02c0c5b6434 Rakefiles: hacks to support long paths on Windows
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 175
diff changeset
   565
  def path
e02c0c5b6434 Rakefiles: hacks to support long paths on Windows
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 175
diff changeset
   566
    fix_long_path(super)    
e02c0c5b6434 Rakefiles: hacks to support long paths on Windows
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 175
diff changeset
   567
  end
e02c0c5b6434 Rakefiles: hacks to support long paths on Windows
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 175
diff changeset
   568
e02c0c5b6434 Rakefiles: hacks to support long paths on Windows
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 175
diff changeset
   569
  def copy_file(dest)
e02c0c5b6434 Rakefiles: hacks to support long paths on Windows
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 175
diff changeset
   570
    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
   571
  end
0
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   572
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   573
end
f46260ba26b1 Initial shot of "new" rake-based builder
Jan Vrany <jan.vrany@fit.cvut.cz>
parents:
diff changeset
   574
210
e02c0c5b6434 Rakefiles: hacks to support long paths on Windows
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 175
diff changeset
   575
class FileUtils::Entry_
e02c0c5b6434 Rakefiles: hacks to support long paths on Windows
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 175
diff changeset
   576
  prepend FileUtils::Entry_Extensions
e02c0c5b6434 Rakefiles: hacks to support long paths on Windows
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 175
diff changeset
   577
end