class Locomotive::Wagon::PushThemeAssetsCommand

Constants

WEBPACK_BUNDLED_ASSETS

Public Instance Methods

decorate(entity) click to toggle source
# File lib/locomotive/wagon/commands/push_sub_commands/push_theme_assets_command.rb, line 16
def decorate(entity)
  entity # already decorated
end
entities() click to toggle source
# File lib/locomotive/wagon/commands/push_sub_commands/push_theme_assets_command.rb, line 9
def entities
  repositories.theme_asset.all.map do |entity|
    next if skip?(entity)
    decorated = ThemeAssetDecorator.new(entity)
  end.compact.sort { |a, b| a.priority <=> b.priority }
end
label_for(decorated_entity) click to toggle source
# File lib/locomotive/wagon/commands/push_sub_commands/push_theme_assets_command.rb, line 35
def label_for(decorated_entity)
  decorated_entity.relative_url
end
persist(decorated_entity) click to toggle source
# File lib/locomotive/wagon/commands/push_sub_commands/push_theme_assets_command.rb, line 20
def persist(decorated_entity)
  return if decorated_entity.realname.starts_with?('_')

  precompile(decorated_entity)

  resource = if (_entity = remote_entity(decorated_entity)).nil?
    api_client.theme_assets.create(decorated_entity.to_hash)
  else
    raise SkipPersistingException.new if same?(decorated_entity, _entity)
    api_client.theme_assets.update(_entity._id, decorated_entity.to_hash)
  end

  register_url(resource)
end

Private Instance Methods

compress_and_minify(entity) click to toggle source
# File lib/locomotive/wagon/commands/push_sub_commands/push_theme_assets_command.rb, line 90
def compress_and_minify(entity)
  begin
    if WEBPACK_BUNDLED_ASSETS.include?(entity.short_relative_url)
      raise 'already compressed and minified by Webpack'
    end

    sprockets_env[entity.short_relative_url].to_s
  rescue Exception => e
    instrument :warning, message: "Unable to compress and minify it, error: #{e.message}"
    # use the original file instead"
    File.read(File.join(path, entity.source))
  end
end
precompile(entity) click to toggle source
# File lib/locomotive/wagon/commands/push_sub_commands/push_theme_assets_command.rb, line 41
def precompile(entity)
  return unless entity.stylesheet_or_javascript?

  Tempfile.new(entity.realname).tap do |file|
    content = compress_and_minify(entity)

    # replace paths to images or fonts by the absolute URL used in the Engine
    content = replace_assets(content)

    file.write(content)

    entity.filepath = file.path

    file.close
  end
end
register_url(resource) click to toggle source
# File lib/locomotive/wagon/commands/push_sub_commands/push_theme_assets_command.rb, line 85
def register_url(resource)
  @remote_urls ||= {}
  @remote_urls[resource.local_path] = "#{resource.url}?#{resource.checksum}"
end
remote_entities() click to toggle source
# File lib/locomotive/wagon/commands/push_sub_commands/push_theme_assets_command.rb, line 74
def remote_entities
  return @remote_entities if @remote_entities

  @remote_entities = {}.tap do |hash|
    api_client.theme_assets.all.each do |entity|
      hash[entity.local_path] = entity
      register_url(entity)
    end
  end
end
remote_entity(local_entity) click to toggle source
# File lib/locomotive/wagon/commands/push_sub_commands/push_theme_assets_command.rb, line 70
def remote_entity(local_entity)
  remote_entities[local_entity.relative_url]
end
replace_assets(content) click to toggle source
# File lib/locomotive/wagon/commands/push_sub_commands/push_theme_assets_command.rb, line 58
def replace_assets(content)
  content.gsub(/([("'])\/((images|fonts)\/[^)"']+)([)"''])/) do
    leading_char, local_path, trailing_char = $1, $2, $4
    local_path.gsub!(/\?[^\/]+\Z/, '') # remove query string if present
    "#{leading_char}#{(@remote_urls || {})[local_path] || local_path}#{trailing_char}"
  end
end
same?(decorated_entity, remote_entity) click to toggle source
# File lib/locomotive/wagon/commands/push_sub_commands/push_theme_assets_command.rb, line 66
def same?(decorated_entity, remote_entity)
  remote_entity.try(:checksum) == decorated_entity.checksum
end
skip?(entity) click to toggle source
# File lib/locomotive/wagon/commands/push_sub_commands/push_theme_assets_command.rb, line 109
def skip?(entity)
  return false if @only_entities.blank?

  _source = entity.source.gsub('./public/', '')

  !@only_entities.any? { |regexp| regexp.match(_source) }
end
sprockets_env() click to toggle source
# File lib/locomotive/wagon/commands/push_sub_commands/push_theme_assets_command.rb, line 104
def sprockets_env
  @sprockets_env ||= Locomotive::Steam::SprocketsEnvironment.new(File.join(path, 'public'),
    minify: ENV['WAGON_NO_MINIFY_ASSETS'].present? ? false : true)
end