Class: Spring

Inherits:
Object
  • Object
show all
Defined in:
lib/spring.rb,
lib/spring/env.rb,
lib/spring/sid.rb,
lib/spring/server.rb,
lib/spring/version.rb,
lib/spring/commands.rb,
lib/spring/application.rb,
lib/spring/application_manager.rb,
lib/spring/application_watcher.rb

Defined Under Namespace

Modules: Commands, SID Classes: Application, ApplicationManager, ApplicationWatcher, Env, Server

Constant Summary collapse

SERVER_COMMAND =
[
  File.join(*RbConfig::CONFIG.values_at('bindir', 'RUBY_INSTALL_NAME')),
  "-r", "bundler/setup",
  File.expand_path("../spring/server.rb", __FILE__)
]
VERSION =
"0.0.2"

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSpring

Returns a new instance of Spring.



24
25
26
# File 'lib/spring.rb', line 24

def initialize
  @env = Env.new
end

Class Attribute Details

.application_watcherObject

Returns the value of attribute application_watcher.



7
8
9
# File 'lib/spring/application.rb', line 7

def application_watcher
  @application_watcher
end

.commandsObject (readonly)

Returns the value of attribute commands.



5
6
7
# File 'lib/spring/commands.rb', line 5

def commands
  @commands
end

Instance Attribute Details

#envObject (readonly)

Returns the value of attribute env.



22
23
24
# File 'lib/spring.rb', line 22

def env
  @env
end

Class Method Details

.command(name) ⇒ Object



12
13
14
# File 'lib/spring/commands.rb', line 12

def self.command(name)
  commands.fetch name
end

.register_command(name, klass) ⇒ Object



8
9
10
# File 'lib/spring/commands.rb', line 8

def self.register_command(name, klass)
  commands[name] = klass.new
end

.run(args) ⇒ Object



18
19
20
# File 'lib/spring.rb', line 18

def self.run(args)
  exit new.run(args)
end

Instance Method Details

#boot_serverObject



32
33
34
35
36
37
38
# File 'lib/spring.rb', line 32

def boot_server
  # Boot the server into the process group of the current session.
  # This will cause it to be automatically killed once the session
  # ends (i.e. when the user closes their terminal).
  Process.spawn(*SERVER_COMMAND, pgroup: SID.pgid)
  sleep 0.1 until server_running?
end

#run(args) ⇒ Object



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

def run(args)
  boot_server unless server_running?

  socket = UNIXSocket.open(env.socket_name)
  socket.write rails_env_for(args.first)
  socket.close

  socket = UNIXSocket.open(env.socket_name)

  socket.send_io STDOUT
  socket.send_io STDERR
  socket.send_io stdin_slave

  socket.puts args.length

  args.each do |arg|
    socket.puts  arg.length
    socket.write arg
  end

  # FIXME: receive exit status from server
  socket.read
  true
rescue Errno::ECONNRESET
  false
ensure
  socket.close
end

#server_running?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/spring.rb', line 28

def server_running?
  env.socket_path.exist?
end