Class: Spring::Client::Binstub

Inherits:
Command
  • Object
show all
Defined in:
lib/spring/client/binstub.rb

Instance Attribute Summary collapse

Attributes inherited from Command

#args, #env

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Binstub

Returns a new instance of Binstub.



15
16
17
18
19
20
# File 'lib/spring/client/binstub.rb', line 15

def initialize(args)
  super

  @bindir = env.root.join("bin")
  @name   = args[1]
end

Instance Attribute Details

#bindirObject (readonly)

Returns the value of attribute bindir.



4
5
6
# File 'lib/spring/client/binstub.rb', line 4

def bindir
  @bindir
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/spring/client/binstub.rb', line 4

def name
  @name
end

Class Method Details

.call(args) ⇒ Object



10
11
12
13
# File 'lib/spring/client/binstub.rb', line 10

def self.call(args)
  require "spring/commands"
  super
end

.descriptionObject



6
7
8
# File 'lib/spring/client/binstub.rb', line 6

def self.description
  "Generate spring based binstubs."
end

Instance Method Details

#callObject



22
23
24
25
26
27
28
29
30
31
# File 'lib/spring/client/binstub.rb', line 22

def call
  if Spring.command?(name) || name == "rails"
    bindir.mkdir unless bindir.exist?
    generate_spring_binstub
    generate_command_binstub
  else
    $stderr.puts "The '#{name}' command is not known to spring."
    exit 1
  end
end

#command_binstubObject



37
38
39
# File 'lib/spring/client/binstub.rb', line 37

def command_binstub
  bindir.join(name)
end

#generate_command_binstubObject



66
67
68
69
70
71
72
73
# File 'lib/spring/client/binstub.rb', line 66

def generate_command_binstub
  File.write(command_binstub, <<CODE)
#!/usr/bin/env bash
exec $(dirname $0)/spring #{name} "$@"
CODE

  command_binstub.chmod 0755
end

#generate_spring_binstubObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/spring/client/binstub.rb', line 41

def generate_spring_binstub
  File.write(spring_binstub, <<'CODE')
#!/usr/bin/env ruby

# This is a special way of invoking the spring gem in order to
# work around the performance issue discussed in
# https://github.com/rubygems/rubygems/pull/435

glob       = "{#{Gem::Specification.dirs.join(",")}}/spring-*.gemspec"
candidates = Dir[glob].to_a.sort_by { |c| Gem::Version.new(File.basename(c).split(/[-\.]/)[1...-1].join(".")) }

spec = Gem::Specification.load(candidates.last)

if spec
  spec.activate
  load spec.bin_file("spring")
else
  $stderr.puts "Could not find spring gem in #{Gem::Specification.dirs.join(", ")}."
  exit 1
end
CODE

  spring_binstub.chmod 0755
end

#spring_binstubObject



33
34
35
# File 'lib/spring/client/binstub.rb', line 33

def spring_binstub
  bindir.join("spring")
end