Sha256: 5405f38fae94278da709a828d9b87acde3a458aeae578ac2fd347665cd950cdd

Contents?: true

Size: 1.78 KB

Versions: 1

Compression:

Stored size: 1.78 KB

Contents

require 'erb'
require 'fileutils'

module GemWrappers
  class Installer
    attr_reader :environment_file

    def initialize(environment_file = nil)
      @environment_file = environment_file
    end

    def self.wrappers_path
      Gem.configuration && Gem.configuration[:wrappers_path] ||
      File.join(Gem.dir, 'wrappers')
    end

    def wrappers_path
      @wrappers_path ||= self.class.wrappers_path
    end

    def ensure
      FileUtils.mkdir_p(wrappers_path) unless File.exist?(wrappers_path)
      # exception based on Gem::Installer.generate_bin
      raise Gem::FilePermissionError.new(wrappers_path) unless File.writable?(wrappers_path)
    end

    def uninstall(executable)
      file_name = File.join(wrappers_path, executable)
      File.delete(file_name) if File.exist?(file_name)
    end

    def install(executable)
      raise "Missing environment file for initialize!" unless @environment_file
      @executable = executable
      content = ERB.new(template).result(binding)
      file_name = File.join(wrappers_path, File.basename(executable))
      File.open(file_name, 'w') do |file|
        file.write(content)
      end
      file_set_executable(file_name)
    end

    def executable_expanded
      if File.extname(@executable) == ".rb"
      then "ruby #{@executable}"
      else @executable
      end
    end

    def template
      @template ||= <<-TEMPLATE
#!/usr/bin/env bash

if
  [[ -s "<%= environment_file %>" ]]
then
  source "<%= environment_file %>"
  exec <%= executable_expanded %> "$@"
else
  echo "ERROR: Missing RVM environment file: '<%= environment_file %>'" >&2
  exit 1
fi
TEMPLATE
    end

  private
    def file_set_executable(file_name)
      stat = File.stat(file_name)
      File.chmod(stat.mode|0111, file_name) unless stat.mode&0111 == 0111
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
gem-wrappers-1.2.4 lib/gem-wrappers/installer.rb