Class: Exefy::GeneratorFromBatch

Inherits:
Generator show all
Defined in:
lib/exefy.rb

Instance Method Summary collapse

Methods inherited from Generator

#compile, #compile_resources, #generate_executable, #install_executable_stub, #link, #log_message

Constructor Details

#initialize(gem) ⇒ GeneratorFromBatch

Returns a new instance of GeneratorFromBatch.



125
126
127
# File 'lib/exefy.rb', line 125

def initialize(gem)
  @gem = gem
end

Instance Method Details

#batch_files(gem) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/exefy.rb', line 173

def batch_files(gem)
  bf = {}
  test_paths = Gem.path.map {|gp| File.join(gp, gem.bindir)}.
    unshift(Gem.bindir).uniq

  gem.executables.each do |executable|
    test_paths.map {|tp| File.join(tp, "#{executable}.bat")}.each do |bat|
      bf[executable] = [] unless bf[executable]
      bf[executable] << bat if File.exist?(bat)
    end
  end
  bf.values
end

#exefy_gemObject



129
130
131
132
133
# File 'lib/exefy.rb', line 129

def exefy_gem
  batch_files(@gem).each do |list|
    process(list)
  end
end

#process(batch_files) ⇒ Object



163
164
165
166
167
168
169
170
171
# File 'lib/exefy.rb', line 163

def process(batch_files)
  batch_files.each do |bf|
    target = bf.gsub(".bat", ".exe")
    install_executable_stub(target)

    log_message "Removing batch file '#{File.basename(bf)}'"
    File.unlink bf
  end
end

#revert_gemObject



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/exefy.rb', line 135

def revert_gem
  require "rubygems/installer"

  return if @gem.executables.nil? or @gem.executables.empty?

  # Instantiate Gem::Installer object with no additional options
  # WARNING!!! at the moment RubyGems do not handle additional
  # install options correctly (--install-dir or --bindir). Once
  # gem is installed in non-default location it becomes unusable.
  # We cannot get path to its .bat files nor path where it is
  # installed (https://github.com/rubygems/rubygems/issues/342).
  # Therefore we will, for now, process gems as they are always
  # installed in default locations.
  @gem.executables.each do |filename|
    filename.untaint
    exe_file = File.join(Gem.bindir, "#{filename}.exe")
    if File.exist? exe_file
      Gem::Installer.new(@gem).generate_windows_script(filename, Gem.bindir)
      if File.exist? exe_file.gsub(".exe", ".bat")
        log_message "Removing #{exe_file}"
        File.unlink exe_file
      else
        log_message "Reverting batch file failed. Executable file will remain in bin directory."
      end
    end
  end
end