Class: Slinky::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/slinky/runner.rb

Constant Summary collapse

COMMANDS =
%w{start build}

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ Runner

Returns a new instance of Runner.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/slinky/runner.rb', line 12

def initialize argv
  # While slinky largely works in Ruby 1.8, the tests don't run
  # properly and using 1.9 is highly recommended.
  if RUBY_VERSION.start_with?("1.8")
    $stderr.puts(("Slinky is unsupported on Ruby 1.8." + \
                  " Using 1.9 is highly recommended.").foreground(:red))
  end

  puts "slinky #{version}"

  @argv = argv
  @options = {}

  parser.parse! @argv
  @command = @argv.shift
  @arguments = @argv

  config_path = @options[:config] || "#{@options[:src_dir] || "."}/slinky.yaml"
  begin
    @config = if File.exist?(config_path) 
                ConfigReader.from_file(config_path)
              else
                ConfigReader.empty
              end
  rescue InvalidConfigError => e
    $stderr.puts("The configuration file at #{config_path} is invalid:".foreground(:red))
    $stderr.puts(e.message.foreground(:red))
    exit(1)
  end
end

Instance Method Details

#command_buildObject



107
108
109
# File 'lib/slinky/runner.rb', line 107

def command_build
  Builder.build(@options, @config)
end

#command_startObject



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
100
101
102
103
104
105
# File 'lib/slinky/runner.rb', line 68

def command_start
  EM::run {
    @config ||= Config.empty

    Slinky::Server.dir = @options[:src_dir] || @config.src_dir
    Slinky::Server.config = @config
    manifest = Manifest.new(Slinky::Server.dir,
                            Slinky::Server.config)
    Slinky::Server.manifest = manifest

    port = @options[:port] || @config.port

    should_proxy = !(@config.no_proxy || @options[:no_proxy])
    if !@config.proxy.empty? && should_proxy
      server = EM::start_server "127.0.0.1", port+1, Slinky::Server
      ProxyServer.run(@config.proxy, port, port+1)
    else
      EM::start_server "127.0.0.1", port, Slinky::Server
    end

    if !@config.no_livereload && !@options[:no_livereload]
      lr_port = @options[:livereload_port] || @config.livereload_port
      livereload = LiveReload.new("127.0.0.1", lr_port)
      livereload.run
    end

    Listener.new(manifest, livereload).run

    Signal.trap('INT') {
      Listen.stop
      EM::stop
      puts "Slinky fading away ... "
      exit(0)
    }

    puts "Started static file server on port #{port}"
  }
end

#parserObject



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/slinky/runner.rb', line 43

def parser
  @parser ||= OptionParser.new do |opts|
    opts.banner = "Usage: slinky [options] #{COMMANDS.join('|')}"
    opts.on("-v", "--version", "Outputs current version number and exits"){ exit }
    opts.on("-o DIR", "--build-dir DIR", "Directory to which the site will be built.", "Use in conjunction with the 'build' command."){|dir| @options[:build_dir] = File.expand_path(dir)}
    opts.on("-p PORT", "--port PORT", "Port to run on (default: #{@options[:port]})"){|p| @options[:port] = p.to_i}
    opts.on("-s DIR", "--src-dir DIR", "Directory containing project source"){|p| @options[:src_dir] = p}
    opts.on("-n", "--no-proxy", "Don't set up proxy server"){ @options[:no_proxy] = true }
    opts.on("-r", "--no-livereload", "Don't start a livereload server"){ @options[:no_livereload] = true }
    opts.on("-c FILE", "--config FILE", "Path to configuration file"){|f| @options[:config] = f}
    opts.on("-m", "--dont-minify", "Don't minify js/css"){ @options[:no_minify] = true }
  end
end

#runObject



57
58
59
60
61
62
63
64
65
66
# File 'lib/slinky/runner.rb', line 57

def run
  case @command
  when "start" then command_start
  when "build" then command_build
  when nil
    abort "Must provide a command (one of #{COMMANDS.join(', ')})"
  else
    abort "Unknown command: #{@command}. Must be on of #{COMMANDS.join(', ')}."
  end
end

#versionObject



5
6
7
8
9
10
# File 'lib/slinky/runner.rb', line 5

def version
  root = File.expand_path(File.dirname(__FILE__))
  File.open("#{root}/../../VERSION"){|f|
    f.read.strip
  }
end