Method: Forge::Builder#build_assets

Defined in:
lib/forge/builder.rb

#build_assetsObject



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
# File 'lib/forge/builder.rb', line 145

def build_assets
  [['style.css'], ['javascripts', 'theme.js'], ['javascripts', 'admin.js']].each do |asset|
    destination = File.join(@project.build_path, asset)

    sprocket = @sprockets.find_asset(asset.last)

    # Catch any sprockets errors and continue the process
    begin
      @task.shell.mute do
        FileUtils.mkdir_p(File.dirname(destination)) unless File.directory?(File.dirname(destination))
        sprocket.write_to(destination) unless sprocket.nil?

        if @project.config[:compress_js] && destination.end_with?('.js')
          require "yui/compressor"

          # Grab the initial sprockets output
          sprockets_output = File.open(destination, 'r').read

          # Re-write the file, minified
          File.open(destination, 'w') do |file|
            file.write(YUI::JavaScriptCompressor.new.compress(sprockets_output))
          end
        end
      end
    rescue Exception => e
      @task.say "Error while building #{asset.last}:"
      @task.say e.message, Thor::Shell::Color::RED
      File.open(destination, 'w') do |file|
        file.puts(e.message)
      end

      # Re-initializing sprockets to prevent further errors
      # TODO: This is done for lack of a better solution
      init_sprockets
    end
  end

  # Copy the images directory over
  FileUtils.cp_r(File.join(@assets_path, 'images'), @project.build_path) if File.exists?(File.join(@assets_path, 'images'))

  # Check for screenshot and move it into main build directory
  Dir.glob(File.join(@project.build_path, 'images', '*')).each do |filename|
    if filename.index(/screenshot\.(png|jpg|jpeg|gif)/)
      FileUtils.mv(filename, @project.build_path + File::SEPARATOR )
    end
  end
end