Class: RubyInstaller::Build::Msys2Installation

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_installer/build/msys2_installation.rb

Overview

:nodoc:

Defined Under Namespace

Classes: CommandError, MsysNotFound

Constant Summary collapse

MSYS2_INSTALL_KEY =
"SOFTWARE/Microsoft/Windows/CurrentVersion/Uninstall/"
MSYS2_INSTALL_KEY_WOW =
"SOFTWARE/WOW6432Node/Microsoft/Windows/CurrentVersion/Uninstall/"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(msys_path: nil, mingwarch: nil, mingw_package_prefix: nil, ruby_bin_dir: nil) ⇒ Msys2Installation

Returns a new instance of Msys2Installation.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/ruby_installer/build/msys2_installation.rb', line 20

def initialize(msys_path: nil, mingwarch: nil, mingw_package_prefix: nil, ruby_bin_dir: nil)
  @msys_path = msys_path
  @msys_path_fixed = msys_path ? true : false
  @mingwdir = nil
  @mingwarch = mingwarch || (
      case RUBY_PLATFORM
        when /x64.*ucrt/ then 'ucrt64'
        when /x64.*mingw32/ then 'mingw64'
        when /i386.*mingw32/ then 'mingw32'
        else raise "unsupported ruby platform #{RUBY_PLATFORM.inspect}"
      end
    )
  @mingw_package_prefix = mingw_package_prefix || begin
    case @mingwarch
      when 'mingw32' then "mingw-w64-i686"
      when 'mingw64' then "mingw-w64-x86_64"
      when 'ucrt64'  then "mingw-w64-ucrt-x86_64"
      else raise "unknown mingwarch #{@mingwarch.inspect}"
    end
  end

  @ruby_bin_dir = ruby_bin_dir || File.join(RbConfig::TOPDIR, "bin")
end

Instance Attribute Details

#mingw_package_prefixObject (readonly)

Returns the value of attribute mingw_package_prefix.



17
18
19
# File 'lib/ruby_installer/build/msys2_installation.rb', line 17

def mingw_package_prefix
  @mingw_package_prefix
end

#mingwarchObject (readonly)

Returns the value of attribute mingwarch.



16
17
18
# File 'lib/ruby_installer/build/msys2_installation.rb', line 16

def mingwarch
  @mingwarch
end

#mingwdirObject (readonly)

Returns the value of attribute mingwdir.



15
16
17
# File 'lib/ruby_installer/build/msys2_installation.rb', line 15

def mingwdir
  @mingwdir
end

#ruby_bin_dirObject (readonly)

Returns the value of attribute ruby_bin_dir.



18
19
20
# File 'lib/ruby_installer/build/msys2_installation.rb', line 18

def ruby_bin_dir
  @ruby_bin_dir
end

Instance Method Details

#disable_dll_search_pathsObject



132
133
134
135
# File 'lib/ruby_installer/build/msys2_installation.rb', line 132

def disable_dll_search_paths
  @mingwdir.remove if @mingwdir
  @mingwdir = nil
end

#disable_msys_apps(if_no_msys: :hint) ⇒ Object



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/ruby_installer/build/msys2_installation.rb', line 234

def disable_msys_apps(if_no_msys: :hint)
  vars = with_msys_install_hint(if_no_msys) do
    msys_apps_envvars
  end
  changed = false
  if path=vars.delete("PATH")
    old_path = ENV['PATH']
    ENV['PATH'] = old_path.gsub(path + ";", "")
    changed = ENV['PATH'] != old_path
  end
  vars.each do |key, val|
    changed = true if ENV[key]
    ENV.delete(key)
  end

  changed
end

#disable_msys_apps_per_cmdObject

This method is used for the ridk command.



282
283
284
285
286
287
288
289
290
291
# File 'lib/ruby_installer/build/msys2_installation.rb', line 282

def disable_msys_apps_per_cmd
  vars = with_msys_install_hint{ msys_apps_envvars }
  str = "".dup
  if path=vars.delete("PATH")
    str << "PATH=#{ ENV['PATH'].gsub(path + ";", "") }\n"
  end
  str << vars.map do |key, val|
    "#{key}="
  end.join("\n")
end

#disable_msys_apps_per_ps1Object

This method is used for the ridk command.



305
306
307
308
309
310
311
312
313
314
# File 'lib/ruby_installer/build/msys2_installation.rb', line 305

def disable_msys_apps_per_ps1
  vars = with_msys_install_hint{ msys_apps_envvars }
  str = "".dup
  if path=vars.delete("PATH")
    str << "$env:PATH=\"#{ ENV['PATH'].gsub(path + ";", "").gsub('"', '`"') }\";"
  end
  str << vars.map do |key, val|
    "$env:#{key}=''"
  end.join(";")
end

#enable_dll_search_pathsObject



122
123
124
125
126
127
128
129
130
# File 'lib/ruby_installer/build/msys2_installation.rb', line 122

def enable_dll_search_paths
  @mingwdir ||= begin
    DllDirectory.set_defaults
    path = mingw_bin_path
    DllDirectory.new(path) if File.directory?(path)
  rescue MsysNotFound
    # We silently ignore this error to allow Ruby installations without MSYS2.
  end
end

#enable_msys_apps(if_no_msys: :hint, for_gem_install: false) ⇒ Object



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/ruby_installer/build/msys2_installation.rb', line 209

def enable_msys_apps(if_no_msys: :hint, for_gem_install: false)
  vars = with_msys_install_hint(if_no_msys) do
    msys_apps_envvars
  end

  changed = false

  if (path=vars.delete("PATH")) && !ENV['PATH'].include?(path)
    phrase = "Temporarily enhancing PATH for MSYS/MINGW..."
    if for_gem_install && defined?(Gem)
      Gem.ui.say(phrase) if Gem.configuration.verbose
    elsif $DEBUG
      $stderr.puts phrase
    end
    changed = true
    ENV['PATH'] = path + ";" + ENV['PATH']
  end
  vars.each do |key, val|
    changed = true if ENV[key] != val
    ENV[key] = val
  end

  changed
end

#enable_msys_apps_per_cmdObject

This method is used for the ridk command.



271
272
273
274
275
276
277
278
279
# File 'lib/ruby_installer/build/msys2_installation.rb', line 271

def enable_msys_apps_per_cmd
  vars = with_msys_install_hint{ msys_apps_envvars }
  if (path=vars.delete("PATH")) && !ENV['PATH'].include?(path)
    vars['PATH'] = path + ";" + ENV['PATH']
  end
  vars.map do |key, val|
    "#{key}=#{val}"
  end.join("\n")
end

#enable_msys_apps_per_ps1Object

This method is used for the ridk command.



294
295
296
297
298
299
300
301
302
# File 'lib/ruby_installer/build/msys2_installation.rb', line 294

def enable_msys_apps_per_ps1
  vars = with_msys_install_hint{ msys_apps_envvars }
  if (path=vars.delete("PATH")) && !ENV['PATH'].include?(path)
    vars['PATH'] = path + ";" + ENV['PATH']
  end
  vars.map do |key, val|
    "$env:#{key}=\"#{val.gsub('"', '`"')}\""
  end.join(";")
end

#install_mingw_packages(packages, verbose: false) ⇒ Object



343
344
345
346
# File 'lib/ruby_installer/build/msys2_installation.rb', line 343

def install_mingw_packages(packages, verbose: false)
  packages = packages.map{|pack| "#{mingw_package_prefix}-#{pack}" }
  install_packages(packages, verbose: verbose)
end

#install_packages(packages, verbose: false) ⇒ Object



316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# File 'lib/ruby_installer/build/msys2_installation.rb', line 316

def install_packages(packages, verbose: false)
  return if packages.empty?

  with_msys_apps_enabled do
    # Find packages that are already installed
    skips, installs = packages.partition do |package|
      IO.popen(["pacman", "-Q", package], err: :out, &:read)
      $?.success?
    end

    Gem.ui.say("Using msys2 packages: #{skips.join(" ")}") if verbose && skips.any?

    # Install required packages
    if installs.any?
      Gem.ui.say("Installing required msys2 packages: #{installs.join(" ")}") if verbose

      args = ["pacman", "-S", "--needed", "--noconfirm", *installs]
      Gem.ui.say("> #{args.join(" ")}") if verbose==1

      res = IO.popen(args, &:read)
      raise CommandError, "pacman failed with the following output:\n#{res}" if !$? || $?.exitstatus != 0

      Gem.ui.say(res) if verbose==1
    end
  end
end

#iterate_msys_paths {|File.join(RbConfig::TOPDIR, "msys64")| ... } ⇒ Object

Yields:

  • (File.join(RbConfig::TOPDIR, "msys64"))

Raises:



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/ruby_installer/build/msys2_installation.rb', line 48

def iterate_msys_paths
  # Prefer MSYS2 when installed within the ruby directory.
  yield File.join(RbConfig::TOPDIR, "msys64")
  yield File.join(RbConfig::TOPDIR, "msys32")

  # Then try MSYS2 next to the ruby directory.
  yield File.join(File.dirname(RbConfig::TOPDIR), "msys64")
  yield File.join(File.dirname(RbConfig::TOPDIR), "msys32")

  # If msys2 is installed at the default location
  yield "c:/msys64"
  yield "c:/msys32"

  # If msys2 is installed per installer.exe
  require "win32/registry"
  [
    [Win32::Registry::HKEY_CURRENT_USER, MSYS2_INSTALL_KEY],
    [Win32::Registry::HKEY_CURRENT_USER, MSYS2_INSTALL_KEY_WOW],
    [Win32::Registry::HKEY_LOCAL_MACHINE, MSYS2_INSTALL_KEY],
    [Win32::Registry::HKEY_LOCAL_MACHINE, MSYS2_INSTALL_KEY_WOW],
  ].each do |reg_root, base_key|
    begin
      reg_root.open(backslachs(base_key)) do |reg|
        reg.each_key do |subkey|
          subreg = reg.open(subkey)
          begin
            if subreg['DisplayName'] =~ /^MSYS2 / && File.directory?(il=subreg['InstallLocation'])
              yield il
            end
          rescue Encoding::InvalidByteSequenceError, Win32::Registry::Error
            # Ignore entries without valid installer data or broken character encoding
          end
        end
      end
    rescue Win32::Registry::Error
    end
  end

  ENV['PATH'] && ENV['PATH'].split(";").each do |path|
    # If /path/to/msys64 is in the PATH (e.g. Chocolatey)
    yield path
  end

  # If msys2 is installed by scoop package manager
  begin
    yield IO.popen(["scoop", "prefix", "msys2"], &:read).strip
  rescue SystemCallError
  end

  raise MsysNotFound, "MSYS2 could not be found"
end

#mingw_bin_pathObject



114
115
116
# File 'lib/ruby_installer/build/msys2_installation.rb', line 114

def mingw_bin_path
  backslachs( File.join(msys_path, mingwarch, "bin") )
end

#mingw_prefixObject



118
119
120
# File 'lib/ruby_installer/build/msys2_installation.rb', line 118

def mingw_prefix
  "/#{mingwarch}"
end

#msys_bin_pathObject



110
111
112
# File 'lib/ruby_installer/build/msys2_installation.rb', line 110

def msys_bin_path
  backslachs( File.join(msys_path, "/usr/bin") )
end

#msys_pathObject



100
101
102
103
104
105
106
107
108
# File 'lib/ruby_installer/build/msys2_installation.rb', line 100

def msys_path
  @msys_path ||= begin
    iterate_msys_paths do |path|
      if File.exist?(File.join(path, "usr/bin/msys-2.0.dll"))
        break backslachs(path)
      end
    end
  end
end

#reset_cacheObject



44
45
46
# File 'lib/ruby_installer/build/msys2_installation.rb', line 44

def reset_cache
  @msys_path = nil unless @msys_path_fixed
end

#with_msys_apps_disabled(**args) ⇒ Object



261
262
263
264
265
266
267
268
# File 'lib/ruby_installer/build/msys2_installation.rb', line 261

def with_msys_apps_disabled(**args)
  changed = disable_msys_apps(**args)
  begin
    yield
  ensure
    enable_msys_apps(**args) if changed
  end
end

#with_msys_apps_enabled(**args) ⇒ Object



252
253
254
255
256
257
258
259
# File 'lib/ruby_installer/build/msys2_installation.rb', line 252

def with_msys_apps_enabled(**args)
  changed = enable_msys_apps(**args)
  begin
    yield
  ensure
    disable_msys_apps(**args) if changed
  end
end