nrispring 2.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,18 @@
1
+ module Spring
2
+ module Client
3
+ class Server < Command
4
+ def self.description
5
+ "Explicitly start a Spring server in the foreground"
6
+ end
7
+
8
+ def call
9
+ require "spring/server"
10
+ Spring::Server.boot(foreground: foreground?)
11
+ end
12
+
13
+ def foreground?
14
+ !args.include?("--background")
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,30 @@
1
+ module Spring
2
+ module Client
3
+ class Status < Command
4
+ def self.description
5
+ "Show current status."
6
+ end
7
+
8
+ def call
9
+ if env.server_running?
10
+ puts "Spring is running:"
11
+ puts
12
+ print_process env.pid
13
+ application_pids.each { |pid| print_process pid }
14
+ else
15
+ puts "Spring is not running."
16
+ end
17
+ end
18
+
19
+ def print_process(pid)
20
+ puts `ps -p #{pid} -o pid= -o command=`
21
+ end
22
+
23
+ def application_pids
24
+ candidates = `ps -ax -o ppid= -o pid=`.lines
25
+ candidates.select { |l| l =~ /^(\s+)?#{env.pid} / }
26
+ .map { |l| l.split(" ").last }
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,22 @@
1
+ require "spring/version"
2
+
3
+ module Spring
4
+ module Client
5
+ class Stop < Command
6
+ def self.description
7
+ "Stop all Spring processes for this project."
8
+ end
9
+
10
+ def call
11
+ case env.stop
12
+ when :stopped
13
+ puts "Spring stopped."
14
+ when :killed
15
+ $stderr.puts "Spring did not stop; killing forcibly."
16
+ when :not_running
17
+ puts "Spring is not running"
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,11 @@
1
+ require "spring/version"
2
+
3
+ module Spring
4
+ module Client
5
+ class Version < Command
6
+ def call
7
+ puts "Spring version #{Spring::VERSION}"
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,82 @@
1
+ require "spring/configuration"
2
+
3
+ module Spring
4
+ class CommandWrapper
5
+ attr_reader :name, :command
6
+
7
+ def initialize(name, command = nil)
8
+ @name = name
9
+ @command = command
10
+ @setup = false
11
+ end
12
+
13
+ def description
14
+ if command.respond_to?(:description)
15
+ command.description
16
+ else
17
+ "Runs the #{name} command"
18
+ end
19
+ end
20
+
21
+ def setup?
22
+ @setup
23
+ end
24
+
25
+ def setup
26
+ if !setup? && command.respond_to?(:setup)
27
+ command.setup
28
+ @setup = true
29
+ return true
30
+ else
31
+ @setup = true
32
+ return false
33
+ end
34
+ end
35
+
36
+ def call
37
+ if command.respond_to?(:call)
38
+ command.call
39
+ else
40
+ load exec
41
+ end
42
+ end
43
+
44
+ def gem_name
45
+ if command.respond_to?(:gem_name)
46
+ command.gem_name
47
+ else
48
+ exec_name
49
+ end
50
+ end
51
+
52
+ def exec_name
53
+ if command.respond_to?(:exec_name)
54
+ command.exec_name
55
+ else
56
+ name
57
+ end
58
+ end
59
+
60
+ def binstub
61
+ Spring.application_root_path.join(binstub_name)
62
+ end
63
+
64
+ def binstub_name
65
+ "bin/#{name}"
66
+ end
67
+
68
+ def exec
69
+ if binstub.exist?
70
+ binstub.to_s
71
+ else
72
+ Gem.bin_path(gem_name, exec_name)
73
+ end
74
+ end
75
+
76
+ def env(args)
77
+ if command.respond_to?(:env)
78
+ command.env(args)
79
+ end
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,50 @@
1
+ require "spring/watcher"
2
+ require "spring/command_wrapper"
3
+
4
+ module Spring
5
+ @commands = {}
6
+
7
+ class << self
8
+ attr_reader :commands
9
+ end
10
+
11
+ def self.register_command(name, command = nil)
12
+ commands[name] = CommandWrapper.new(name, command)
13
+ end
14
+
15
+ def self.command?(name)
16
+ commands.include? name
17
+ end
18
+
19
+ def self.command(name)
20
+ commands.fetch name
21
+ end
22
+
23
+ require "spring/commands/rails"
24
+ require "spring/commands/rake"
25
+
26
+ # Load custom commands, if any.
27
+ # needs to be at the end to allow modification of existing commands.
28
+ config = File.expand_path("~/.spring.rb")
29
+ require config if File.exist?(config)
30
+
31
+ # If the config/spring.rb contains requires for commands from other gems,
32
+ # then we need to be under bundler.
33
+ require "bundler/setup"
34
+
35
+ # Auto-require any Spring extensions which are in the Gemfile
36
+ Gem::Specification.map(&:name).grep(/^spring-/).each do |command|
37
+ begin
38
+ require command
39
+ rescue LoadError => error
40
+ if error.message.include?(command)
41
+ require command.gsub("-", "/")
42
+ else
43
+ raise
44
+ end
45
+ end
46
+ end
47
+
48
+ config = File.expand_path("./config/spring.rb")
49
+ require config if File.exist?(config)
50
+ end
@@ -0,0 +1,112 @@
1
+ module Spring
2
+ module Commands
3
+ class Rails
4
+ def call
5
+ ARGV.unshift command_name
6
+ load Dir.glob(::Rails.root.join("{bin,script}/rails")).first
7
+ end
8
+
9
+ def description
10
+ nil
11
+ end
12
+ end
13
+
14
+ class RailsConsole < Rails
15
+ def env(args)
16
+ return args.first if args.first && !args.first.index("-")
17
+
18
+ environment = nil
19
+
20
+ args.each.with_index do |arg, i|
21
+ if arg =~ /--environment=(\w+)/
22
+ environment = $1
23
+ elsif i > 0 && args[i - 1] == "-e"
24
+ environment = arg
25
+ end
26
+ end
27
+
28
+ environment
29
+ end
30
+
31
+ def command_name
32
+ "console"
33
+ end
34
+ end
35
+
36
+ class RailsGenerate < Rails
37
+ def command_name
38
+ "generate"
39
+ end
40
+ end
41
+
42
+ class RailsDestroy < Rails
43
+ def command_name
44
+ "destroy"
45
+ end
46
+ end
47
+
48
+ class RailsRunner < Rails
49
+ def call
50
+ ARGV.replace extract_environment(ARGV).first
51
+ super
52
+ end
53
+
54
+ def env(args)
55
+ extract_environment(args).last
56
+ end
57
+
58
+ def command_name
59
+ "runner"
60
+ end
61
+
62
+ def extract_environment(args)
63
+ environment = nil
64
+
65
+ args = args.select.with_index { |arg, i|
66
+ case arg
67
+ when "-e"
68
+ false
69
+ when /--environment=(\w+)/
70
+ environment = $1
71
+ false
72
+ else
73
+ if i > 0 && args[i - 1] == "-e"
74
+ environment = arg
75
+ false
76
+ else
77
+ true
78
+ end
79
+ end
80
+ }
81
+
82
+ [args, environment]
83
+ end
84
+ end
85
+
86
+ class RailsTest < Rails
87
+ def env(args)
88
+ environment = "test"
89
+
90
+ args.each.with_index do |arg, i|
91
+ if arg =~ /--environment=(\w+)/
92
+ environment = $1
93
+ elsif i > 0 && args[i - 1] == "-e"
94
+ environment = arg
95
+ end
96
+ end
97
+
98
+ environment
99
+ end
100
+
101
+ def command_name
102
+ "test"
103
+ end
104
+ end
105
+
106
+ Spring.register_command "rails_console", RailsConsole.new
107
+ Spring.register_command "rails_generate", RailsGenerate.new
108
+ Spring.register_command "rails_destroy", RailsDestroy.new
109
+ Spring.register_command "rails_runner", RailsRunner.new
110
+ Spring.register_command "rails_test", RailsTest.new
111
+ end
112
+ end
@@ -0,0 +1,30 @@
1
+ module Spring
2
+ module Commands
3
+ class Rake
4
+ class << self
5
+ attr_accessor :environment_matchers
6
+ end
7
+
8
+ self.environment_matchers = {
9
+ :default => "test",
10
+ /^test($|:)/ => "test"
11
+ }
12
+
13
+ def env(args)
14
+ # This is an adaption of the matching that Rake itself does.
15
+ # See: https://github.com/jimweirich/rake/blob/3754a7639b3f42c2347857a0878beb3523542aee/lib/rake/application.rb#L691-L692
16
+ if env = args.grep(/^(RAILS|RACK)_ENV=(.*)$/m).last
17
+ return env.split("=").last
18
+ end
19
+
20
+ self.class.environment_matchers.each do |matcher, environment|
21
+ return environment if matcher === (args.first || :default)
22
+ end
23
+
24
+ nil
25
+ end
26
+ end
27
+
28
+ Spring.register_command "rake", Rake.new
29
+ end
30
+ end
@@ -0,0 +1,58 @@
1
+ require "spring/errors"
2
+
3
+ module Spring
4
+ class << self
5
+ attr_accessor :application_root, :quiet
6
+
7
+ def gemfile
8
+ if /\s1.9.[0-9]/ === Bundler.ruby_scope.gsub(/[\/\s]+/,'')
9
+ ENV["BUNDLE_GEMFILE"] || "Gemfile"
10
+ else
11
+ Bundler.default_gemfile
12
+ end
13
+ end
14
+
15
+ def after_fork_callbacks
16
+ @after_fork_callbacks ||= []
17
+ end
18
+
19
+ def after_fork(&block)
20
+ after_fork_callbacks << block
21
+ end
22
+
23
+ def verify_environment
24
+ application_root_path
25
+ end
26
+
27
+ def application_root_path
28
+ @application_root_path ||= begin
29
+ if application_root
30
+ path = Pathname.new(File.expand_path(application_root))
31
+ else
32
+ path = project_root_path
33
+ end
34
+
35
+ raise MissingApplication.new(path) unless path.join("config/application.rb").exist?
36
+ path
37
+ end
38
+ end
39
+
40
+ def project_root_path
41
+ @project_root_path ||= find_project_root(Pathname.new(File.expand_path(Dir.pwd)))
42
+ end
43
+
44
+ private
45
+
46
+ def find_project_root(current_dir)
47
+ if current_dir.join(gemfile).exist?
48
+ current_dir
49
+ elsif current_dir.root?
50
+ raise UnknownProject.new(Dir.pwd)
51
+ else
52
+ find_project_root(current_dir.parent)
53
+ end
54
+ end
55
+ end
56
+
57
+ self.quiet = false
58
+ end
@@ -0,0 +1,116 @@
1
+ require "pathname"
2
+ require "fileutils"
3
+ require "digest/md5"
4
+ require "tmpdir"
5
+
6
+ require "spring/version"
7
+ require "spring/sid"
8
+ require "spring/configuration"
9
+
10
+ module Spring
11
+ IGNORE_SIGNALS = %w(INT QUIT)
12
+ STOP_TIMEOUT = 2 # seconds
13
+
14
+ class Env
15
+ attr_reader :log_file
16
+
17
+ def initialize(options = {})
18
+ @root = options[:root]
19
+ @project_root = options[:root]
20
+ @log_file = options[:log_file] || File.open(ENV["SPRING_LOG"] || File::NULL, "a")
21
+ end
22
+
23
+ def root
24
+ @root ||= Spring.application_root_path
25
+ end
26
+
27
+ def project_root
28
+ @project_root ||= Spring.project_root_path
29
+ end
30
+
31
+ def version
32
+ Spring::VERSION
33
+ end
34
+
35
+ def tmp_path
36
+ path = Pathname.new(
37
+ ENV["SPRING_TMP_PATH"] ||
38
+ File.join(ENV['XDG_RUNTIME_DIR'] || Dir.tmpdir, "spring-#{Process.uid}")
39
+ )
40
+ FileUtils.mkdir_p(path) unless path.exist?
41
+ path
42
+ end
43
+
44
+ def application_id
45
+ ENV["SPRING_APPLICATION_ID"] || Digest::MD5.hexdigest(RUBY_VERSION + project_root.to_s)
46
+ end
47
+
48
+ def socket_path
49
+ Pathname.new(ENV["SPRING_SOCKET"] || tmp_path.join(application_id))
50
+ end
51
+
52
+ def socket_name
53
+ socket_path.to_s
54
+ end
55
+
56
+ def pidfile_path
57
+ Pathname.new(ENV["SPRING_PIDFILE"] || socket_path.dirname.join("#{socket_path.basename(".*")}.pid"))
58
+ end
59
+
60
+ def pid
61
+ pidfile_path.exist? ? pidfile_path.read.to_i : nil
62
+ rescue Errno::ENOENT
63
+ # This can happen if the pidfile is removed after we check it
64
+ # exists
65
+ end
66
+
67
+ def app_name
68
+ root.basename
69
+ end
70
+
71
+ def server_running?
72
+ pidfile = pidfile_path.open('r+')
73
+ !pidfile.flock(File::LOCK_EX | File::LOCK_NB)
74
+ rescue Errno::ENOENT
75
+ false
76
+ ensure
77
+ if pidfile
78
+ pidfile.flock(File::LOCK_UN)
79
+ pidfile.close
80
+ end
81
+ end
82
+
83
+ def log(message)
84
+ log_file.puts "[#{Time.now}] [#{Process.pid}] #{message}"
85
+ log_file.flush
86
+ end
87
+
88
+ def stop
89
+ if server_running?
90
+ timeout = Time.now + STOP_TIMEOUT
91
+ kill 'TERM'
92
+ sleep 0.1 until !server_running? || Time.now >= timeout
93
+
94
+ if server_running?
95
+ kill 'KILL'
96
+ :killed
97
+ else
98
+ :stopped
99
+ end
100
+ else
101
+ :not_running
102
+ end
103
+ end
104
+
105
+ def kill(sig)
106
+ pid = self.pid
107
+ Process.kill(sig, pid) if pid
108
+ rescue Errno::ESRCH
109
+ # already dead
110
+ end
111
+
112
+ def server_command
113
+ ENV["SPRING_SERVER_COMMAND"] || "#{File.expand_path("../../../bin/spring", __FILE__)} server --background"
114
+ end
115
+ end
116
+ end