Module: RubyInstaller::Build::Utils

Included in:
ErbCompiler, Gems::InstallSpec, 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



90
91
92
93
# File 'lib/ruby_installer/build/utils.rb', line 90

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

#gem_expand_file(rel_file) ⇒ Object

Returns the absolute path of rel_file within the gem root directory.

Raises Errno::ENOENT if it doesn’t exist.



82
83
84
85
86
87
88
# File 'lib/ruby_installer/build/utils.rb', line 82

def gem_expand_file(rel_file)
  if File.exist?(a=File.join(GEM_ROOT, rel_file))
    File.expand_path(a)
  else
    raise Errno::ENOENT, rel_file
  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_compile_erb(erb_file_rel) ⇒ Object



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

def ovl_compile_erb(erb_file_rel)
  ErbCompiler.new(erb_file_rel).result
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.



69
70
71
72
73
74
75
76
77
# File 'lib/ruby_installer/build/utils.rb', line 69

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.



57
58
59
60
61
62
63
# File 'lib/ruby_installer/build/utils.rb', line 57

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(file_rel) ⇒ Object



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

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

#q_inno(text) ⇒ Object

Quote a string according to the rules of Inno-Setup



105
106
107
# File 'lib/ruby_installer/build/utils.rb', line 105

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.



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/ruby_installer/build/utils.rb', line 40

def rubyinstaller_build_gem_files
  spec = Gem.loaded_specs["rubyinstaller-build"]
  if spec
    # A loaded gemspec has empty #files -> fetch the files from it's 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

#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