Class: Assette::CLI

Inherits:
Thor
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/assette/cli.rb

Constant Summary collapse

ASSET_PREFIX_MATCHER =
/^([a-f0-9]{9}|\d{6}_\d{6})\//
DEFAULT_PID_FILE =
'.assette_pid'

Instance Method Summary collapse

Constructor Details

#initializeCLI

Returns a new instance of CLI.



195
196
197
198
# File 'lib/assette/cli.rb', line 195

def initialize(*)
  super
  get_services
end

Instance Method Details

#compileObject



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/assette/cli.rb', line 97

def compile
  files = []
  all_files = []
  Assette.config.compiling = true
  
  unless File.exist?('assets')
    Dir.mkdir('assets')
  end
  
  sha = Git.open('.').log.first.sha[0...8] rescue Time.now.strftime("%y%m%d_%H%M%S")
  Assette.config.sha = sha
  
  Assette.config.file_paths.each do |path|
    all_files |= Dir[File.join(path,'**',"*")]
    Assette::Reader::ALL.keys.each do |extension|
      files |= Dir[File.join(path,'**',"*.#{extension}")]
    end
  end
  
  not_compiled = all_files - files
  
  files.delete_if {|f| f =~ /\/_/} # remove any mixins to speed up process
  
  # files = files.collect {|f| Assette::File.open(f)}
  
  container = if Assette.config.cache_method.to_s == 'path'
    File.join(Assette.config.build_target,sha)
  else
    Assette.config.build_target
  end
  
  made_dirs = []
  
  say "Compiling all asset files to #{container}"

  files_to_minify = []
  
  files.each do |file_path|
    Assette::File.open(file_path) do |file|

      if file.minify?
        files_to_minify.push(file_path)
      else
        unless options.minified? || options.static_min?
          target_path = file.relative_target_path
          
          Assette.config.file_paths.each do |p|
            # target_path.gsub!(p,'')
          end
          
          new_path = File.join(container,target_path)
          
          Assette.logger.debug("Compiling file") {"#{file.path} -> #{new_path}"}
          create_file(new_path, file.all_code)
        end
      end
    end
  end

  Assette.config.minifying do
    say "\nCreating minified versions of files" unless files_to_minify.empty?

    files_to_minify.each do |file_path|
      Assette::File.open(file_path) do |file|
        target_path = file.relative_target_path

        new_path = File.join(container,target_path).sub(/(\.[A-Za-z0-9]{2,10})$/,'.min\1')

        Assette.logger.debug("Compiling minified file") {"#{file.path} -> #{new_path}"}
        create_file(new_path, file.all_code)
      end
    end
  end
  
  say "\nCopying all non-compiled assets to #{container}"
  not_compiled.each do |file|
    next if File.directory?(file)
    next if options.minified?
    target_path = file.dup

    Assette.config.file_paths.each do |p|
      target_path.gsub!(Regexp.new(p+'/'),'')
    end
    
    new_path = File.join(container,target_path)
    
    Assette.logger.debug("Copying file") {"#{file} -> #{new_path}"}
    copy_file(file,new_path)
  end


  version_file = Assette.config.asset_version_file
  File.delete(version_file) if File.exist?(version_file)
  create_file(version_file,sha)

  Assette.config.after_compile.call(sha)
end

#server(cmd = nil) ⇒ Object



52
53
54
55
56
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
86
87
88
89
90
91
92
# File 'lib/assette/cli.rb', line 52

def server(cmd = nil)
  if cmd.nil? || cmd == 'start'
    
    opts = {}
    opts[:Port] = options[:port] || 4747
    opts[:config] = File.join(File.dirname(__FILE__),'run.ru')
  
    unless options['dont-daemonize']
      
      if File.exist?(pid_file)
        pid = File.open(pid_file).read.chomp.to_i
        begin
          Process.kill("INT",pid)
          say "Server already running with PID #{pid}, killing before restart"
        rescue Errno::ESRCH

        end
      end
      
      opts[:daemonize] = true
      opts[:pid] = pid_file
    else
      Assette.logger.level = Logger::DEBUG
      Assette.logger.datetime_format = "%H:%M:%S"
      Assette.logger.formatter = Proc.new do |severity, datetime, progname, msg|
        "#{severity}: #{progname} - #{msg}\n"
      end
    end
  
    say "Starting Assette server on port #{opts[:Port]}"
    ret = Rack::Server.start(opts)
  elsif cmd == 'stop'
    if File.exist?(pid_file)
      pid = File.open(pid_file).read.chomp.to_i
      say "Killing server with PID #{pid}"
      Process.kill("INT",pid)
    else
      say "No pid file found at #{pid_file}"
    end
  end
end

#versionObject



44
45
46
# File 'lib/assette/cli.rb', line 44

def version
  say Assette::VERSION
end