Class: Spring::Client::Binstub::Item

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command) ⇒ Item

Returns a new instance of Item.



50
51
52
53
54
55
56
57
58
59
# File 'lib/spring/client/binstub.rb', line 50

def initialize(command)
  @command = command

  if command.binstub.exist?
    @existing = command.binstub.read
  elsif command.name == "rails"
    scriptfile = Spring.application_root_path.join("script/rails")
    @existing = scriptfile.read if scriptfile.exist?
  end
end

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



48
49
50
# File 'lib/spring/client/binstub.rb', line 48

def command
  @command
end

#existingObject (readonly)

Returns the value of attribute existing.



48
49
50
# File 'lib/spring/client/binstub.rb', line 48

def existing
  @existing
end

Instance Method Details

#addObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/spring/client/binstub.rb', line 65

def add
  if existing
    if existing.include?(OLD_BINSTUB)
      fallback = existing.match(/#{Regexp.escape OLD_BINSTUB}\n(.*)else/m)[1]
      fallback.gsub!(/^  /, "")
      fallback = nil if fallback.include?("exec")
      generate(fallback)
      status "upgraded"
    elsif existing.include?(LOADER)
      status "Spring already present"
    elsif existing =~ BINSTUB_VARIATIONS
      upgraded = existing.sub(BINSTUB_VARIATIONS, LOADER)
      File.write(command.binstub, upgraded)
      status "upgraded"
    else
      head, shebang, tail = existing.partition(SHEBANG)

      if shebang.include?("ruby")
        unless command.binstub.exist?
          FileUtils.touch command.binstub
          command.binstub.chmod 0755
        end

        File.write(command.binstub, "#{head}#{shebang}#{LOADER}#{tail}")
        status "Spring inserted"
      else
        status "doesn't appear to be ruby, so cannot use Spring", $stderr
        exit 1
      end
    end
  else
    generate
    status "generated with Spring"
  end
end

#generate(fallback = nil) ⇒ Object



101
102
103
104
105
106
107
108
109
# File 'lib/spring/client/binstub.rb', line 101

def generate(fallback = nil)
  unless fallback
    fallback = "require 'bundler/setup'\n" \
               "load Gem.bin_path('#{command.gem_name}', '#{command.exec_name}')\n"
  end

  File.write(command.binstub, "#!/usr/bin/env ruby\n#{LOADER}#{fallback}")
  command.binstub.chmod 0755
end

#removeObject



111
112
113
114
115
116
# File 'lib/spring/client/binstub.rb', line 111

def remove
  if existing
    File.write(command.binstub, existing.sub(BINSTUB_VARIATIONS, ""))
    status "Spring removed"
  end
end

#status(text, stream = $stdout) ⇒ Object



61
62
63
# File 'lib/spring/client/binstub.rb', line 61

def status(text, stream = $stdout)
  stream.puts "* #{command.binstub_name}: #{text}"
end