Class: Marv::Project::Assets

Inherits:
Object
  • Object
show all
Defined in:
lib/marv/project/builder/assets.rb

Instance Method Summary collapse

Constructor Details

#initialize(builder) ⇒ Assets

Initialize assets builder



9
10
11
12
13
14
15
16
# File 'lib/marv/project/builder/assets.rb', line 9

def initialize(builder)
  @builder = builder
  @task = builder.task
  @project = builder.project
  @config = builder.project.config

  init_sprockets
end

Instance Method Details

#build_asset_file(asset) ⇒ Object

Build asset file



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/marv/project/builder/assets.rb', line 59

def build_asset_file(asset)
  destination = ::File.join(@project.build_path, asset)

  @task.shell.mute do
    sprocket = @sprockets.find_asset(asset.last)
    # Create asset destination
    @task.empty_directory ::File.dirname(destination) unless ::File.directory?(::File.dirname(destination))
    # Write file to destination
    sprocket.write_to(destination) unless sprocket.nil?
  end
end

#build_assetsObject

Build assets



47
48
49
50
51
52
53
54
55
56
# File 'lib/marv/project/builder/assets.rb', line 47

def build_assets
  @project.assets.each do |asset|
    # Catch any sprockets errors and continue the process
    begin
      build_asset_file asset
    rescue Exception => e
      print_asset_error asset, e.message
    end
  end
end

#clean_imagesObject

Clean images



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/marv/project/builder/assets.rb', line 19

def clean_images
  @task.shell.mute do
    # Remove screenshot image
    ::Dir.glob(::File.join(@project.build_path, 'screenshot.*')).each do |file|
      @task.remove_file file
    end

    # Remove images folder
    @task.remove_dir ::File.join(@project.build_path, 'images')
  end
end

#copy_imagesObject

Copy images



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/marv/project/builder/assets.rb', line 32

def copy_images
  @task.shell.mute do
    ::Dir.glob(::File.join(@project.assets_path, 'images', '*')).each do |filename|
      # Check for screenshot and move it into main build directory
      if filename.index(/screenshot/)
        @task.copy_file filename, ::File.join(@project.build_path, ::File.basename(filename)), :force => true
      else
        # Copy the other files in images directory
        @task.copy_file filename, ::File.join(@project.build_path, 'images', ::File.basename(filename)), :force => true
      end
    end
  end
end

#init_sprocketsObject

Init sprockets



84
85
86
87
88
89
90
91
92
93
94
95
96
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
# File 'lib/marv/project/builder/assets.rb', line 84

def init_sprockets
  if ExecJS::Runtimes.runtimes.none?(&:available?)
    @task.say_error "No execjs runtime found! Aborting..."
    abort
  end

  @sprockets   = ::Sprockets::Environment.new
  autoprefixer = @config[:autoprefixer]

  ['javascripts', 'stylesheets'].each do |dir|
    @sprockets.append_path ::File.join(@project.assets_path, dir)
  end

  # Check for js compression
  if @config[:compress_js]
    @sprockets.js_compressor = :uglify
  end

  # Check for css compression
  if @config[:compress_css]
    @sprockets.css_compressor = :scss
  end

  # Passing the @project instance variable to the Sprockets::Context instance
  # used for processing the asset ERB files
  @sprockets.context_class.instance_exec(@project) do |project|
    define_method :config do
      project.config
    end
  end

  unless autoprefixer == false
    runtimes = AutoprefixerRails::Processor::SUPPORTED_RUNTIMES

    if runtimes.any?(&:available?)
      AutoprefixerRails.install(@sprockets, Hash(autoprefixer))
    else
      names = runtimes.map(&:name).join(', ')
      @task.say_warning "Autoprefixer disabled: no supported runtime found [ #{names} ]"
    end
  end
end

Print error to screen and file



72
73
74
75
76
77
78
79
80
81
# File 'lib/marv/project/builder/assets.rb', line 72

def print_asset_error(asset, message)
  destination = ::File.join(@project.build_path, asset)

  @task.say_error "Error while building #{asset.last}:", message

  @task.shell.mute do
    @task.create_file destination unless ::File.exists?(destination)
    @task.append_to_file destination, message
  end
end