Class: Skyrocket::Cmd

Inherits:
Object
  • Object
show all
Defined in:
lib/skyrocket/cmd.rb

Overview

CMD execution controller

Constant Summary collapse

COMMANDS =
%w(compile init watch)

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ Cmd

Returns a new instance of Cmd.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/skyrocket/cmd.rb', line 10

def initialize(argv)
  @options = {
    :asset_dirs => Array.new,
    :lib_dirs => Array.new,
    :output_dir => './public',
    :base_url => '/',
    :style => :raw
  }

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

  @options[:asset_dirs] << './assets/public' unless @options[:asset_dirs].length > 0
  @options[:lib_dirs]   << './assets/lib'    unless @options[:lib_dirs].length > 0
end

Instance Method Details

#format_results(action, asset_name) ⇒ Object



46
47
48
49
50
51
52
53
54
55
# File 'lib/skyrocket/cmd.rb', line 46

def format_results(action, asset_name)
  case action
  when :deleted
    puts "     \e[31;40m** Deleted  **\e[0m #{asset_name}"
  when :created
    puts "     \e[32;40m** Created  **\e[0m #{asset_name}"
  when :modified
    puts "     \e[33;40m** Modified **\e[0m #{asset_name}"
  end
end

#parserObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/skyrocket/cmd.rb', line 27

def parser
  @parser ||= OptionParser.new do |opts|
    opts.banner = "Usage: skyrocket [options] #{COMMANDS.join('|')}"

    opts.separator ""
    opts.separator "Options:"
    opts.on("-s", "--style MODE", "Compilation style ('concat' for concat only, 'minify' for concat and minification, default 'raw' for neither)") do |style|
      @options[:style] = style.to_sym
    end
    opts.on("-b", "--base URL", "Base url for assets location/ link generation") { |base| @options[:base_url] = base }
    opts.on("-a", "--asset DIR", "Assets directory. Use more than once for multiple assets directories") { |asset| @options[:asset_dirs] << asset }
    opts.on("-l", "--asset-lib DIR", "Asset lib directory. Use more than once for multiple lib directories") { |lib| @options[:lib_dirs] << lib }
    opts.on("-o", "--output DIR", "Output directory for compiled assets.") { |output| @options[:output_dir] = output}

    opts.on_tail("-h", "-?", "--help", "Show this message") { puts opts; exit }
    opts.on_tail("-v", "--version", "Show version") { puts Skyrocket::VERSION; exit }
  end
end

#runObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/skyrocket/cmd.rb', line 57

def run
  if COMMANDS.include?(@command)
    am = AssetManager.new(@options)
    case @command
    when "compile"
      am.compile(&method(:format_results))
    when "init"
      @options[:asset_dirs].each{ |dir| FileUtils.mkdir_p(dir) }
      @options[:lib_dirs].each{ |dir| FileUtils.mkdir_p(dir) }
      FileUtils.mkdir_p(@options[:output_dir])
    when "watch"
      trap("SIGINT") do
        puts "Ending watch..."
        exit!
      end
      begin
        am.watch(&method(:format_results))
      rescue Gem::LoadError => e
        puts "'watch' command requires the gem '#{e.name}' of version '#{e.requirement}'' be installed."
      end
    end
  elsif @command.nil?
    puts "Command required"
    puts @parser
    exit 1
  else
    abort "Unknown command: #{@command}. Valid commands include: #{COMMANDS.join(', ')}"
  end
end