Class: Spring::Client::Binstub

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

Defined Under Namespace

Classes: Item

Constant Summary collapse

SHEBANG =
/\#\!.*\n/
LOADER =

If loading the bin/spring file works, it’ll run spring which will eventually call Kernel.exit. This means that in the client process we will never execute the lines after this block. But if the spring client is not invoked for whatever reason, then the Kernel.exit won’t happen, and so we’ll fall back to the lines after this block, which should cause the “unsprung” version of the command to run.

<<CODE
begin
  spring_bin_path = File.expand_path('../spring', __FILE__)
  load spring_bin_path
rescue LoadError => e
  raise unless e.message.end_with? spring_bin_path, 'spring/binstub'
end
CODE
SPRING =

The defined? check ensures these lines don’t execute when we load the binstub from the application process. Which means that in the application process we’ll execute the lines which come after the LOADER block, which is what we want.

Parsing the lockfile in this way is pretty nasty but reliable enough The regex ensures that the match must be between a GEM line and an empty line, so it won’t go on to the next section.

<<'CODE'
#!/usr/bin/env ruby

# This file loads spring without using Bundler, in order to be fast.
# It gets overwritten when you run the `spring binstub` command.

unless defined?(Spring)
  require 'rubygems'
  require 'bundler'

  if (match = Bundler.default_lockfile.read.match(/^GEM$.*?^    (?:  )*spring \((.*?)\)$.*?^$/m))
    Gem.paths = { 'GEM_PATH' => [Bundler.bundle_path.to_s, *Gem.path].uniq }
    gem 'spring', match[1]
    require 'spring/binstub'
  end
end
CODE
OLD_BINSTUB =
%{if !Process.respond_to?(:fork) || Gem::Specification.find_all_by_name("spring").empty?}
BINSTUB_VARIATIONS =
Regexp.union [
  %{begin\n  load File.expand_path("../spring", __FILE__)\nrescue LoadError\nend\n},
  %{begin\n  load File.expand_path('../spring', __FILE__)\nrescue LoadError\nend\n},
  LOADER
]

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.



144
145
146
147
148
149
150
151
152
153
154
# File 'lib/spring/client/binstub.rb', line 144

def initialize(args)
  super

  @bindir = env.root.join("bin")
  @all    = false
  @mode   = :add
  @items  = args.drop(1)
                .map { |name| find_commands name }
                .inject(Set.new, :|)
                .map { |command| Item.new(command) }
end

Instance Attribute Details

#bindirObject (readonly)

Returns the value of attribute bindir.



129
130
131
# File 'lib/spring/client/binstub.rb', line 129

def bindir
  @bindir
end

#itemsObject (readonly)

Returns the value of attribute items.



129
130
131
# File 'lib/spring/client/binstub.rb', line 129

def items
  @items
end

Class Method Details

.call(args) ⇒ Object



139
140
141
142
# File 'lib/spring/client/binstub.rb', line 139

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

.descriptionObject



131
132
133
# File 'lib/spring/client/binstub.rb', line 131

def self.description
  "Generate spring based binstubs. Use --all to generate a binstub for all known commands. Use --remove to revert."
end

.rails_commandObject



135
136
137
# File 'lib/spring/client/binstub.rb', line 135

def self.rails_command
  @rails_command ||= CommandWrapper.new("rails")
end

Instance Method Details

#callObject



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/spring/client/binstub.rb', line 178

def call
  case @mode
  when :add
    bindir.mkdir unless bindir.exist?

    File.write(spring_binstub, SPRING)
    spring_binstub.chmod 0755

    items.each(&:add)
  when :remove
    spring_binstub.delete if @all
    items.each(&:remove)
  else
    raise ArgumentError
  end
end

#find_commands(name) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/spring/client/binstub.rb', line 156

def find_commands(name)
  case name
  when "--all"
    @all = true
    commands = Spring.commands.dup
    commands.delete_if { |command_name, _| command_name.start_with?("rails_") }
    commands.values + [self.class.rails_command]
  when "--remove"
    @mode = :remove
    []
  when "rails"
    [self.class.rails_command]
  else
    if command = Spring.commands[name]
      [command]
    else
      $stderr.puts "The '#{name}' command is not known to spring."
      exit 1
    end
  end
end

#spring_binstubObject



195
196
197
# File 'lib/spring/client/binstub.rb', line 195

def spring_binstub
  bindir.join("spring")
end