Class: ExecutableHooks::Wrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/executable-hooks/wrapper.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Wrapper

Returns a new instance of Wrapper.



18
19
20
# File 'lib/executable-hooks/wrapper.rb', line 18

def initialize(options)
  @options = options || RegenerateBinstubsCommand.default_install_options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



16
17
18
# File 'lib/executable-hooks/wrapper.rb', line 16

def options
  @options
end

Class Method Details

.expanded_wrapper_nameObject



12
13
14
# File 'lib/executable-hooks/wrapper.rb', line 12

def self.expanded_wrapper_name
  Gem.default_exec_format % self.wrapper_name
end

.wrapper_nameObject



8
9
10
# File 'lib/executable-hooks/wrapper.rb', line 8

def self.wrapper_name
  'ruby_executable_hooks'
end

Instance Method Details

#calculate_bindir(options) ⇒ Object



58
59
60
61
# File 'lib/executable-hooks/wrapper.rb', line 58

def calculate_bindir(options)
  return options[:bin_dir] if options[:bin_dir]
  Gem.respond_to?(:bindir,true) ? Gem.send(:bindir) : File.join(Gem.dir, 'bin')
end

#calculate_destination(bindir) ⇒ Object



63
64
65
# File 'lib/executable-hooks/wrapper.rb', line 63

def calculate_destination(bindir)
  File.expand_path(self.class.expanded_wrapper_name, bindir)
end

#ensure_custom_shebangObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/executable-hooks/wrapper.rb', line 68

def ensure_custom_shebang
  expected_shebang = "$env #{self.class.expanded_wrapper_name}"

  Gem.configuration[:custom_shebang] ||= expected_shebang

  if Gem.configuration[:custom_shebang] != expected_shebang
    warn("
Warning:
Found    custom_shebang: '#{Gem.configuration[:custom_shebang]}',
Expected custom_shebang: '#{expected_shebang}',
this can potentially break 'executable-hooks' and gem executables overall!
Check your '~/.gemrc' and '/etc/gemrc' for 'custom_shebang' and remove it.
")
  end
end

#installObject



22
23
24
25
26
27
28
29
30
31
# File 'lib/executable-hooks/wrapper.rb', line 22

def install
  @bindir = options[:bin_dir] if options[:bin_dir]
  ensure_custom_shebang

  executable_hooks_spec = ExecutableHooks::Specification.find

  if executable_hooks_spec
    install_from( executable_hooks_spec.full_gem_path )
  end
end

#install_from(full_gem_path) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/executable-hooks/wrapper.rb', line 33

def install_from(full_gem_path)
  wrapper_path = File.expand_path( "bin/#{self.class.wrapper_name}", full_gem_path )
  bindir       = calculate_bindir(options)
  destination  = calculate_destination(bindir)

  if File.exist?(wrapper_path) && !same_file(wrapper_path, destination)
    FileUtils.mkdir_p(bindir) unless File.exist?(bindir)
    # exception based on Gem::Installer.generate_bin
    raise Gem::FilePermissionError.new(bindir) unless File.writable?(bindir)
    FileUtils.cp(wrapper_path, destination)
    File.chmod(0775, destination)
  end
end

#uninstallObject



53
54
55
56
# File 'lib/executable-hooks/wrapper.rb', line 53

def uninstall
  destination = calculate_destination(calculate_bindir(options))
  FileUtils.rm_f(destination) if File.exist?(destination)
end