Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 86 lines (65 sloc) 2.149 kb
040d6e9 @maccman implement
authored
1 require 'pathname'
2 require 'fileutils'
3 require 'listen'
4 require 'rack'
5 require 'thor'
6
7 module Catapult
8 class CLI < Thor
2fc8c99 @maccman add generator
authored
9 include Thor::Actions
10
11 def self.source_root
12 File.expand_path('../../..', __FILE__)
13 end
040d6e9 @maccman implement
authored
14
408c7bc @lucasmazza Add support to filter the assets that should be precompiled on the `buil...
lucasmazza authored
15 desc 'build [asset1 asset2..]', 'Build project'
040d6e9 @maccman implement
authored
16
f7a4e04 @ejholmes Add --target option to build.
ejholmes authored
17 method_option :target, :aliases => '-t', :desc => 'Directory to compile assets to'
d944cc9 @SlexAxton add --compile options to use uglifier/yui-compressor to compile JS/CSS
SlexAxton authored
18 method_option :compile, :type => :boolean, :aliases => '-c', :desc => 'Compile and minify assets'
f7a4e04 @ejholmes Add --target option to build.
ejholmes authored
19
408c7bc @lucasmazza Add support to filter the assets that should be precompiled on the `buil...
lucasmazza authored
20 def build(*assets)
f7a4e04 @ejholmes Add --target option to build.
ejholmes authored
21 target = Pathname(options[:target] || './public/assets')
040d6e9 @maccman implement
authored
22
d944cc9 @SlexAxton add --compile options to use uglifier/yui-compressor to compile JS/CSS
SlexAxton authored
23 if options[:compile]
24 Catapult.environment.js_compressor = Compressor::JS.new
25 Catapult.environment.css_compressor = Compressor::CSS.new
26 end
27
d2a05a7 @maccman use Thor say
authored
28 say "Building: #{Catapult.root}"
040d6e9 @maccman implement
authored
29
5b4791a @mikaa123 Brackets for array
mikaa123 authored
30 warnings = []
408c7bc @lucasmazza Add support to filter the assets that should be precompiled on the `buil...
lucasmazza authored
31 Catapult.environment.each_logical_path(assets) do |logical_path|
7ff2d05 @mikaa123 Issues a warning and catches the error whenever an asset isn't found. Re...
mikaa123 authored
32 begin
1cbe433 @mikaa123 Tidying a bit.
mikaa123 authored
33 if asset = Catapult.environment.find_asset(logical_path)
34 filename = target.join(logical_path)
35 FileUtils.mkpath(filename.dirname)
36 say "Write asset: #{filename}"
37 asset.write_to(filename)
38 end
e4d7489 @mikaa123 Builing assets rescue StandardError instead of Exception.
mikaa123 authored
39 rescue StandardError => exception
1cbe433 @mikaa123 Tidying a bit.
mikaa123 authored
40 say exception
41 warnings << exception
e21c7b6 @mikaa123 Provides more details about the exception that occured. Refs #9.
mikaa123 authored
42 end
43 end
44
45 unless warnings.empty?
46 say "Completed with #{warnings.length} warnings."
47 warnings.each do |warning|
48 say warning
040d6e9 @maccman implement
authored
49 end
50 end
51 end
52
b0248d5 @maccman use config.ru file if it exists
authored
53 desc 'server', 'Serve up project'
040d6e9 @maccman implement
authored
54
55 method_option :port, :aliases => '-p', :desc => 'Port'
56
b0248d5 @maccman use config.ru file if it exists
authored
57 def server
8ba65cf @maccman do not use config.ru - people can just use rackup
authored
58 Rack::Server.start(
59 :Port => options[:port] || 9292,
60 :app => Catapult.app
61 )
040d6e9 @maccman implement
authored
62 end
63
8a6370c @maccman allow users to specify paths to watch
authored
64 desc 'watch [asset1 asset2..]', 'Build project whenever it changes'
040d6e9 @maccman implement
authored
65
2e96b5c @ejholmes Add option to watch.
ejholmes authored
66 method_option :target, :aliases => '-t', :desc => 'Directory to compile assets to'
67
8a6370c @maccman allow users to specify paths to watch
authored
68 def watch(*assets)
d2a05a7 @maccman use Thor say
authored
69 say "Watching: #{Catapult.root}"
040d6e9 @maccman implement
authored
70
8a6370c @maccman allow users to specify paths to watch
authored
71 build(*assets)
040d6e9 @maccman implement
authored
72
73 paths = Catapult.environment.paths
74 paths = paths.select {|p| File.exists?(p) }
75
76 Listen.to(*paths) { build }
77 end
2fc8c99 @maccman add generator
authored
78
79 desc 'new', 'Create a new project'
80
81 def new(name)
82 directory('templates/app', name)
83 end
040d6e9 @maccman implement
authored
84 end
7ff2d05 @mikaa123 Issues a warning and catches the error whenever an asset isn't found. Re...
mikaa123 authored
85 end
Something went wrong with that request. Please try again.