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(\#.*\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.

"begin\n  load File.expand_path('../spring', __FILE__)\nrescue LoadError => e\n  raise unless e.message.include?('spring')\nend\n"
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.

"#!/usr/bin/env ruby\n\n# This file loads Spring without using Bundler, in order to be fast.\n# It gets overwritten when you run the `spring binstub` command.\n\nunless defined?(Spring)\n  require 'rubygems'\n  require 'bundler'\n\n  lockfile = Bundler::LockfileParser.new(Bundler.default_lockfile.read)\n  spring = lockfile.specs.detect { |spec| spec.name == 'spring' }\n  if spring\n    Gem.use_paths Gem.dir, Bundler.bundle_path.to_s, *Gem.path\n    gem 'spring', spring.version\n    require 'spring/binstub'\n  end\nend\n"
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  spring_bin_path = File.expand_path('../spring', __FILE__)\n  load spring_bin_path\nrescue LoadError => e\n  raise unless e.message.end_with? spring_bin_path, 'spring/binstub'\nend\n},
  LOADER
].map { |binstub| /#{Regexp.escape(binstub).gsub("'", "['\"]")}/ }

Instance Attribute Summary collapse

Attributes inherited from Command

#args, #env

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Binstub


141
142
143
144
145
146
147
148
149
150
151
# File 'lib/spring/client/binstub.rb', line 141

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


126
127
128
# File 'lib/spring/client/binstub.rb', line 126

def bindir
  @bindir
end

#itemsObject (readonly)

Returns the value of attribute items


126
127
128
# File 'lib/spring/client/binstub.rb', line 126

def items
  @items
end

Class Method Details

.call(args) ⇒ Object


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

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

.descriptionObject


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

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

.rails_commandObject


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

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

Instance Method Details

#callObject


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

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


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

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


192
193
194
# File 'lib/spring/client/binstub.rb', line 192

def spring_binstub
  bindir.join("spring")
end