Module: RubyInstaller::Build::Utils

Included in:
ErbCompiler, Task
Defined in:
lib/ruby_installer/build/utils.rb

Constant Summary collapse

WINDOWS_CMD_SHEBANG =
<<-EOT.freeze
:""||{ ""=> %q<-*- ruby -*-
@"%~dp0ruby" -x "%~f0" %*
@exit /b %ERRORLEVEL%
};{ #
bindir="${0%/*}" #
exec "$bindir/ruby" -x "$0" "$@" #
>, #
} #
EOT
GEM_ROOT =
File.expand_path("../../../..", __FILE__)

Instance Method Summary collapse

Instance Method Details

#eval_file(filename) ⇒ Object



86
87
88
89
# File 'lib/ruby_installer/build/utils.rb', line 86

def eval_file(filename)
  code = File.read(filename, encoding: "UTF-8")
  instance_eval(code, filename)
end

#file(name, *args, &block) ⇒ Object

Extend rake’s file task to be defined only once and to check the expected file is indeed generated

The same as #task, but for #file. In addition this file task raises an error, if the file that is expected to be generated is not present after the block was executed.



108
109
110
111
112
113
114
115
116
# File 'lib/ruby_installer/build/utils.rb', line 108

def file(name, *args, &block)
  task_once(name, block) do
    super(name, *args) do |ta|
      block&.call(ta).tap do
        raise "file #{ta.name} is missing after task executed" unless File.exist?(ta.name)
      end
    end
  end
end

#msys_sh(cmd) ⇒ Object



15
16
17
18
19
# File 'lib/ruby_installer/build/utils.rb', line 15

def msys_sh(cmd)
  Build.enable_msys_apps
  pwd = Dir.pwd
  sh "sh", "-lc", "cd `cygpath -u #{pwd.inspect}`; #{cmd}"
end

#ovl_expand_file(rel_file) ⇒ Object

Returns the absolute path of rel_file within the current directory or, if it doesn’t exist, from the gem root directory.

Raises Errno::ENOENT if neither of them exist.



76
77
78
79
80
81
82
83
84
# File 'lib/ruby_installer/build/utils.rb', line 76

def ovl_expand_file(rel_file)
  if File.exist?(rel_file)
    File.expand_path(rel_file)
  elsif File.exist?(a=File.join(GEM_ROOT, rel_file))
    File.expand_path(a)
  else
    raise Errno::ENOENT, rel_file
  end
end

#ovl_glob(rel_pattern) ⇒ Object

Scan the current and the gem root directory for files matching rel_pattern.

All paths returned are relative.



64
65
66
67
68
69
70
# File 'lib/ruby_installer/build/utils.rb', line 64

def ovl_glob(rel_pattern)
  gem_files = Dir.glob(File.join(GEM_ROOT, rel_pattern)).map do |path|
    path.sub(GEM_ROOT+"/", "")
  end

  (gem_files + Dir.glob(rel_pattern)).uniq
end

#ovl_read_file(rel_file) ⇒ Object

Read rel_file from the current directory or, if it doesn’t exist, from the gem root directory. Raises Errno::ENOENT if neither of them exist.

Returns the file content as String with UTF-8 encoding.



95
96
97
# File 'lib/ruby_installer/build/utils.rb', line 95

def ovl_read_file(rel_file)
  File.read(ovl_expand_file(rel_file), encoding: "UTF-8")
end

#q_inno(text) ⇒ Object

Quote a string according to the rules of Inno-Setup



100
101
102
# File 'lib/ruby_installer/build/utils.rb', line 100

def q_inno(text)
  '"' + text.to_s.gsub('"', '""') + '"'
end

#rubyinstaller_build_gem_filesObject

Return the gem files of “rubyinstaller-build”

The gemspec is either already loaded or taken from our root directory.



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/ruby_installer/build/utils.rb', line 47

def rubyinstaller_build_gem_files
  spec = Gem.loaded_specs["rubyinstaller-build"]
  if spec
    # A loaded gemspec has empty #files -> fetch the files from its path.
    # This is preferred to gemspec loading to avoid a dependency to git.
    Dir["**/*", base: spec.full_gem_path].select do |f|
      FileTest.file?(File.join(spec.full_gem_path, f))
    end
  else
    # Not yet loaded -> load the gemspec and return the files added to the gemspec.
    Gem::Specification.load(File.join(GEM_ROOT, "rubyinstaller-build.gemspec")).files
  end
end

#task(name, *args, &block) ⇒ Object

Extend rake’s task definition to be defined only once, even if called several times

This allows to define common tasks next to specific tasks. It is expected that any variation of the task’s block is reflected in the task name or namespace. If the task name is identical, the task block is executed only once, even if the file task definition is executed twice.



123
124
125
126
127
# File 'lib/ruby_installer/build/utils.rb', line 123

def task(name, *args, &block)
  task_once(name, block) do
    super
  end
end

#with_env(hash) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/ruby_installer/build/utils.rb', line 21

def with_env(hash)
  olds = hash.map{|k, _| [k, ENV[k.to_s]] }
  hash.each do |k, v|
    ENV[k.to_s] = v
  end
  begin
    yield
  ensure
    olds.each do |k, v|
      ENV[k.to_s] = v
    end
  end
end

#with_sandbox_rubyObject



35
36
37
38
39
40
# File 'lib/ruby_installer/build/utils.rb', line 35

def with_sandbox_ruby
  path = "#{ File.expand_path(File.join(sandboxdir, "bin")) };#{ ENV["PATH"] }"
  with_env(GEM_HOME: nil, GEM_PATH: nil, RUBYOPT: nil, RUBYLIB: nil, PATH: path) do
    yield
  end
end