Skip to content

Commit

Permalink
Make it work with Lotus Container apps. Ensure other type of assets a…
Browse files Browse the repository at this point in the history
…re successfully copied over at the deploy time.
  • Loading branch information
jodosha committed Oct 30, 2015
1 parent 393de62 commit e54be07
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 46 deletions.
9 changes: 0 additions & 9 deletions lib/lotus/assets.rb
Expand Up @@ -37,15 +37,6 @@ def self.sources

def self.duplicate(mod, assets = 'Assets', &blk)
dupe.tap do |duplicated|
# mod.module_eval %{ module #{ assets }; end } if assets
# mod.module_eval %{ Assets = Lotus::Assets.dup unless defined?(#{ mod }::Assets) } unless assets.nil?

# duplicated.module_eval %{
# configure do
# action_module #{ mod }::Action
# end
# }

duplicated.configure(&blk) if block_given?
duplicates << duplicated
end
Expand Down
15 changes: 10 additions & 5 deletions lib/lotus/assets/bundler.rb
Expand Up @@ -7,6 +7,11 @@ module Lotus
module Assets
class Bundler
DEFAULT_PERMISSIONS = 0644
JAVASCRIPT_EXT = '.js'.freeze
STYLESHEET_EXT = '.css'.freeze
WILDCARD_EXT = '.*'.freeze
URL_SEPARATOR = '/'.freeze
URL_REPLACEMENT = ''.freeze

def initialize(configuration)
@configuration = configuration
Expand All @@ -32,14 +37,14 @@ def assets

def compress(asset)
case File.extname(asset)
when ".js" then _compress(YUI::JavaScriptCompressor.new(munge: true), asset)
when ".css" then _compress(YUI::CssCompressor.new, asset)
when JAVASCRIPT_EXT then _compress(YUI::JavaScriptCompressor.new(munge: true), asset)
when STYLESHEET_EXT then _compress(YUI::CssCompressor.new, asset)
end
end

def checksum(asset)
digest = Digest::MD5.file(asset)
filename, ext = ::File.basename(asset, '.*'), ::File.extname(asset)
filename, ext = ::File.basename(asset, WILDCARD_EXT), ::File.extname(asset)
directory = ::File.dirname(asset)
target = [directory, "#{ filename }-#{ digest }#{ ext }"].join(::File::SEPARATOR)

Expand All @@ -62,8 +67,8 @@ def _compress(compressor, asset)
end

def _convert_to_url(path)
path.sub(@configuration.public_path, '').
gsub(File::SEPARATOR, '/')
path.sub(destination.to_s, URL_REPLACEMENT).
gsub(File::SEPARATOR, URL_SEPARATOR)
end

def _write(path, content)
Expand Down
13 changes: 2 additions & 11 deletions lib/lotus/assets/config/asset_types.rb
Expand Up @@ -3,12 +3,6 @@

module Lotus
module Assets
class UnknownAssetType < ::StandardError
def initialize(type)
super("Unknown asset type: `#{ type }'")
end
end

module Config
class AssetTypes
def initialize_copy(original)
Expand Down Expand Up @@ -39,10 +33,7 @@ def define(type, &blk)
end

def asset(type)
@types.fetch(type) do
extension_lookup(type) or
raise UnknownAssetType.new(type)
end
@types.fetch(type) { extension_lookup(type) }
end

def types
Expand All @@ -56,7 +47,7 @@ def extension_lookup(filename)
return asset_type if filename.match(/#{ asset_type.ext }/)
end

nil
Config::AssetType.new(@prefix) { ext ::File.extname(filename.to_s) }
end
end
end
Expand Down
10 changes: 4 additions & 6 deletions lib/lotus/assets/configuration.rb
Expand Up @@ -67,14 +67,10 @@ def destination(value = nil)
if value.nil?
@destination
else
@destination = Pathname.new(value)
@destination = Pathname.new(::File.expand_path(value))
end
end

def public_path
destination.join('..').realpath.to_s
end

def manifest(value = nil)
if value.nil?
@manifest
Expand Down Expand Up @@ -124,7 +120,9 @@ def reset!
end

def load!
@registry = JSON.load(manifest_path.read)
if digest && manifest_path.exist?
@registry = JSON.load(manifest_path.read)
end
end

# @api private
Expand Down
4 changes: 2 additions & 2 deletions test/bundler_test.rb
Expand Up @@ -8,7 +8,7 @@
dest.rmtree if dest.exist?
dest.mkpath

FileUtils.copy_entry(source, dest)
FileUtils.copy_entry(source, dest.join('assets'))
config.destination.must_equal(dest) # better safe than sorry ;-)

Lotus::Assets::Bundler.new(config).run
Expand All @@ -20,7 +20,7 @@
end
end

let(:dest) { TMP.join('deploy', 'public', 'assets') }
let(:dest) { TMP.join('deploy', 'public') }
let(:source) { __dir__ + '/fixtures/deploy/public/assets' }

it "compresses javascripts" do
Expand Down
17 changes: 11 additions & 6 deletions test/configuration_test.rb
Expand Up @@ -90,7 +90,7 @@
it 'allows to set a custom location' do
dest = __dir__ + '/../tmp'
@configuration.destination(dest)
@configuration.destination.must_equal(Pathname.new(dest))
@configuration.destination.must_equal(Pathname.new(File.expand_path(dest)))
end
end

Expand Down Expand Up @@ -119,12 +119,15 @@

describe '#asset' do
it 'returns an asset definition' do
@configuration.asset(:javascript).must_be_kind_of(Lotus::Assets::Config::AssetType)
asset_type = @configuration.asset(:javascript)
asset_type.must_be_kind_of(Lotus::Assets::Config::AssetType)
asset_type.ext.must_equal '.js'
end

it 'raises error for unkown type' do
exception = -> { @configuration.asset(:unkown) }.must_raise(Lotus::Assets::UnknownAssetType)
exception.message.must_equal %(Unknown asset type: `unkown')
it 'returns anonymous asset type' do
asset_type = @configuration.asset('logo.png')
asset_type.must_be_kind_of(Lotus::Assets::Config::AssetType)
asset_type.ext.must_equal '.png'
end
end

Expand Down Expand Up @@ -160,7 +163,9 @@
end

it 'removes custom defined asset types' do
-> { @configuration.asset(:cuztom) }.must_raise Lotus::Assets::UnknownAssetType
asset_type = @configuration.asset(:cuztom)
asset_type.must_be_kind_of(Lotus::Assets::Config::AssetType)
asset_type.ext.must_equal ""
end

it 'sets default value for predefined asset type' do
Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 9 additions & 3 deletions test/fixtures/bookshelf/config/environment.rb
Expand Up @@ -5,7 +5,7 @@
require 'lotus/emberjs'

Lotus::Assets.configure do
destination __dir__ + '/../../../../tmp/bookshelf/public/assets'
destination __dir__ + '/../../../../tmp/bookshelf/public'
end

unless defined?(Web)
Expand All @@ -21,6 +21,8 @@ module Web

Assets = Lotus::Assets.duplicate(self) do
root __dir__ + '/../../../fixtures/bookshelf/apps/web'
destination __dir__ + '/../../../../tmp/bookshelf/public/assets'
manifest '../assets.json'
prefix '/'
compile true

Expand Down Expand Up @@ -54,7 +56,9 @@ module Admin
end

Assets = Lotus::Assets.duplicate(self) do
root __dir__ + '/../../../fixtures/bookshelf/apps/admin'
root __dir__ + '/../../../fixtures/bookshelf/apps/admin'
destination __dir__ + '/../../../../tmp/bookshelf/public/assets'
manifest '../assets.json'
prefix '/admin'
compile true

Expand Down Expand Up @@ -87,7 +91,9 @@ module Metrics
end

Assets = Lotus::Assets.duplicate(self) do
root __dir__ + '/../../../fixtures/bookshelf/apps/metrics'
root __dir__ + '/../../../fixtures/bookshelf/apps/metrics'
destination __dir__ + '/../../../../tmp/bookshelf/public/assets'
manifest '../assets.json'
prefix '/metrics'
compile true

Expand Down
11 changes: 7 additions & 4 deletions test/integration/precompile_test.rb
Expand Up @@ -7,8 +7,10 @@
dest.mkpath
end

let(:target) { dest.join('assets') }

describe "standalone framework" do
let(:dest) { TMP.join('standalone', 'public', 'assets') }
let(:dest) { TMP.join('standalone', 'public') }

let(:assets) do
['users.js']
Expand All @@ -21,10 +23,11 @@
end

describe "duplicated frameworks" do
let(:dest) { TMP.join('bookshelf', 'public', 'assets') }
let(:dest) { TMP.join('bookshelf', 'public') }

let(:assets) do
['jquery.js',
'bookshelf.jpg',
'ember.js', # this is a duplicate
'ember-source.js', # this is a duplicate
'application.js',
Expand Down Expand Up @@ -57,13 +60,13 @@ def assert_successful_command(configuration_path)

def assert_successful_output(expected)
expected.each do |asset|
result = dest.join(asset)
result = target.join(asset)
result.must_be :exist?

checksum = Digest::MD5.file(result)
filename, ext = ::File.basename(asset, '.*'), ::File.extname(asset)
directory = Pathname.new(::File.dirname(asset))
dest.join(directory, "#{ filename }-#{ checksum }#{ ext }").must_be :exist?
target.join(directory, "#{ filename }-#{ checksum }#{ ext }").must_be :exist?
end
end
end

0 comments on commit e54be07

Please sign in to comment.