class ThinService::Runner

Constants

COMMANDS

list of available commands

Attributes

command[RW]

command to be runned

options[RW]

parsed options

Public Class Methods

new(argv) click to toggle source
# File lib/thin-service.rb, line 15
def initialize(argv)
  @argv = argv

  @options = {
    :ruby_executables => %w(ruby.exe jruby.exe),
    :port             => 80,
    :environment      => 'production',
    :service_path     => "HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services\\",
    :service_key      => 'Parameters',
    :directory        => Dir.pwd
  }

  parse!
end

Public Instance Methods

parse!() click to toggle source
# File lib/thin-service.rb, line 44
def parse!
  parser.parse! @argv
  @command = @argv.shift
  @service = @argv.shift
end
parser() click to toggle source
# File lib/thin-service.rb, line 30
def parser
  @parser ||= OptionParser.new do |opts|
    opts.banner = "Usage: thin-service #{COMMANDS.join('|')} [options]"

    opts.separator ""
    opts.separator "Options:"

    opts.on("-p", "--port PORT", "use PORT (default: #{@options[:port]})") { |port| @options[:port] = port }
    opts.on("-d", "--directory DIR", "Project directory (default: #{@options[:directory]})") { |directory| @options[:directory] = directory }
    opts.on("-e", "--environment ENVIRONMENT", "use ENVIRONMENT (default: #{@options[:environment]})") { |environment| @options[:environment] = environment }
    opts.on("-h", "--help", "Show this message") { puts opts; exit }
  end
end
run!() click to toggle source
# File lib/thin-service.rb, line 50
def run!
  if ThinService::Runner::COMMANDS.include?(@command) && respond_to?(@command)
    send(@command)
  else
    raise ThinService::CommandNotSupported
  end
end

Protected Instance Methods

delete() click to toggle source
# File lib/thin-service.rb, line 78
def delete
  # windows command
  system('sc delete ' + @service)
end
find_ruby_path() click to toggle source
# File lib/thin-service.rb, line 83
def find_ruby_path
  ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
    @options[:ruby_executables].each do |exe|
      file = "#{path}\\#{exe}"
      return file if File.executable?(file)
    end
  end
end
install() click to toggle source
# File lib/thin-service.rb, line 60
def install
  # get paths
  file_path = windows_path(@options[:directory] || Dir.pwd)
  ruby_path = windows_path(find_ruby_path)
  svranyexe = windows_path(File.join(File.expand_path('../..', __FILE__), 'bin', 'srvany.exe'))

  # default display name
  display_name = @options[:display_name] || File.basename(file_path)

  # windows command
  system("sc create \"#{@service}\" binPath= \"#{svranyexe}\" DisplayName= \"#{@service}\"")
  
  system("reg add \"#{@options[:service_path]}#{@service}\\#{@options[:service_key]}\" ")
  system("reg add \"#{@options[:service_path]}#{@service}\\#{@options[:service_key]}\" /v Application /t REG_SZ /d \"#{ruby_path}\"")
  system("reg add \"#{@options[:service_path]}#{@service}\\#{@options[:service_key]}\" /v AppDirectory /t REG_SZ /d \"#{file_path}\"")
  system("reg add \"#{@options[:service_path]}#{@service}\\#{@options[:service_key]}\" /v AppParameters /t REG_SZ /d \"#{File.dirname(ruby_path)}\\thin start -p #{@options[:port]} -e #{@options[:environment]}\"")
end
windows_path(path) click to toggle source
# File lib/thin-service.rb, line 92
def windows_path(path)
  path.gsub('/', '\\')
end