branchable_cdn_assets-middleman 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a9ae41911f2c5847089e9f35916266e6af72f5c5
4
+ data.tar.gz: 50534f96859ea1fb073d75f1a71068ecad23e6b3
5
+ SHA512:
6
+ metadata.gz: df4c5833e1795f56d7f39ab50cb79b68daaa5d725bb27a69650a7090882a7dfa918f338c0c71bf1e250ea7aed31b84081a5162d447be740558981925744f85ef
7
+ data.tar.gz: 179305796158df8cbba3512aeb6c1bdb98cad9214001ced1e08d550b007351b3fdedee020ce408b412e23e0e21441414beccfedfc1773d15daeff07d1d1ce58d
@@ -0,0 +1,2 @@
1
+ require 'branchable_cdn_assets'
2
+ require_relative 'branchable_cdn_assets/middleman'
@@ -0,0 +1,4 @@
1
+ require 'middleman'
2
+
3
+ require_relative 'middleman/helpers'
4
+ require_relative 'middleman/extension'
@@ -0,0 +1,58 @@
1
+ module BranchableCDNAssets
2
+ module Middleman
3
+
4
+ class Extension < ::Middleman::Extension
5
+
6
+ option :environments, {}, 'environment specific configuration'
7
+ option :cloudfront, {}, 'access keys for cloudfront account'
8
+ option :cdn_dir, 'cdn', 'source directory for cdn files'
9
+ option :production_branch, 'master', 'which branch is the production branch'
10
+ option :default_env, 'staging', 'the default environment if none is set'
11
+
12
+ def after_configuration
13
+ if defined?(::Sass::Script::Functions)
14
+ $app = @app
15
+ ::Sass::Script::Functions.send :include, SassHelpers
16
+ end
17
+ end
18
+
19
+ def manipulate_resource_list resources
20
+ resources + local_files_as_resources
21
+ end
22
+
23
+ def config
24
+ data = {
25
+ production_branch: options.production_branch,
26
+ default_env: options.default_env,
27
+ environments: options.environments,
28
+ cloudfront: options.cloudfront,
29
+ cdn_dir: options.cdn_dir
30
+ }
31
+ Config.new data
32
+ end
33
+
34
+ def file_manager
35
+ BranchableCDNAssets::FileManager.new config
36
+ end
37
+
38
+ helpers do
39
+ include Helpers
40
+ end
41
+
42
+ private
43
+
44
+ def local_files_as_resources
45
+ file_manager.list( :local ).map do |file|
46
+ ::Middleman::Sitemap::Resource.new(
47
+ app.sitemap,
48
+ File.join( "assets/cdn", file ),
49
+ File.join( app.root, "cdn", file )
50
+ )
51
+ end
52
+ end
53
+
54
+ end
55
+ ::Middleman::Extensions.register(:cdn_assets, BranchableCDNAssets::Middleman::Extension)
56
+
57
+ end
58
+ end
@@ -0,0 +1,41 @@
1
+ module BranchableCDNAssets
2
+ module Middleman
3
+
4
+ module Helpers
5
+
6
+ # return the correct path to an asset depending on
7
+ # what environment you're in and the state of the cdn
8
+ #
9
+ # @param path [String]
10
+ # @return [String] path to asset
11
+ def cdn_asset_url path
12
+
13
+ # check for file in sitemap
14
+ if environment != :build && resource = sitemap.find_resource_by_path( File.join( "assets/cdn", path ) )
15
+ return "/" + resource.destination_path
16
+ end
17
+
18
+ # check for the file in the file_manager
19
+ file = extensions[:cdn_assets].file_manager.find(path)
20
+ return file if file && file != :local
21
+
22
+ # not found at any location
23
+ raise "missing asset at '#{path}'"
24
+ end
25
+ alias :cdn_asset_path :cdn_asset_url
26
+
27
+ end
28
+
29
+
30
+ module SassHelpers
31
+
32
+ def cdn_asset_url( path )
33
+ file = $app.cdn_asset_url( path.value )
34
+ ::Sass::Script::String.new("url(\"#{file}\")")
35
+ end
36
+ alias :cdn_asset_path :cdn_asset_url
37
+
38
+ end
39
+
40
+ end
41
+ end
@@ -0,0 +1,27 @@
1
+ require 'rake'
2
+ require 'branchable_cdn_assets-middleman'
3
+ require 'branchable_cdn_assets/rake_tasks'
4
+
5
+ module BranchableCDNAssets
6
+ module Middleman
7
+
8
+ class RakeTasks < BranchableCDNAssets::RakeTasks
9
+
10
+ class << self
11
+ def register namespace=:cdn
12
+ RakeTasks.new( namespace ).register_tasks
13
+ end
14
+ end
15
+
16
+ attr_reader :mm
17
+
18
+ def initialize namespace
19
+ @mm = ::Middleman::Application.server.inst
20
+ @file_manager = FileManager.new @mm.extensions[:cdn_assets].config
21
+ @rake_namespace = namespace
22
+ end
23
+
24
+ end
25
+
26
+ end
27
+ end
@@ -0,0 +1,5 @@
1
+ module BranchableCDNAssets
2
+ module Middleman
3
+ VERSION = '0.5.0'
4
+ end
5
+ end
@@ -0,0 +1 @@
1
+ require 'branchable_cdn_assets-middleman'
@@ -0,0 +1,36 @@
1
+ set :environment, :test
2
+ set :show_exceptions, false
3
+
4
+ set :css_dir, "assets/css"
5
+ set :js_dir, "assets/scripts"
6
+ set :images_dir, "assets/images"
7
+
8
+ activate :cdn_assets do |c|
9
+
10
+ c.environments = {
11
+ production: {
12
+ host: 'production_host',
13
+ url: 'http://production.com',
14
+ root: '/var/www/production'
15
+ },
16
+ test: {
17
+ host: 'test_host',
18
+ url: 'http://test.com',
19
+ root: '/var/www/test'
20
+ },
21
+ staging: {
22
+ host: 'staging_host',
23
+ url: 'http://staging.com',
24
+ root: '/var/www/staging'
25
+ }
26
+ }
27
+
28
+ c.cloudfront = {
29
+ access_key: 'cf_access_key',
30
+ secret_key: 'cf_secret_key',
31
+ distribution_id: 'cd_dist_id'
32
+ }
33
+
34
+ end
35
+
36
+ activate :directory_indexes
@@ -0,0 +1,9 @@
1
+ require 'spec_helper'
2
+
3
+ describe BranchableCDNAssets::Middleman do
4
+
5
+ it "has a version" do
6
+ expect( BranchableCDNAssets::Middleman::VERSION >= "0.0.0" ).to be_truthy
7
+ end
8
+
9
+ end
@@ -0,0 +1,62 @@
1
+ require 'spec_helper'
2
+ require 'middleman_spec_helper'
3
+
4
+ describe BranchableCDNAssets::Middleman::Extension do
5
+
6
+ before :each do
7
+ Given.fixture('base')
8
+ Given.file 'cdn/img_one', ''
9
+ Given.file 'cdn/img_two', ''
10
+
11
+ @mm = Middleman::Fixture.app
12
+ @extension = @mm.extensions[:cdn_assets]
13
+ end
14
+
15
+ after :each do
16
+ Given.cleanup!
17
+ end
18
+
19
+ describe "#config" do
20
+
21
+ it "returns a Config instance" do
22
+ expect( @extension.config.is_a?( BranchableCDNAssets::Config ) ).to be_truthy
23
+ end
24
+
25
+ it "sets config with extension options" do
26
+ allow( Asgit ).to receive(:current_branch).and_return('master')
27
+ @extension.options[:production_branch] = 'master'
28
+
29
+ expect( @extension.config.host ).to eq 'production_host'
30
+ end
31
+
32
+ it "sets env to production if on :production_branch" do
33
+ allow( Asgit ).to receive(:current_branch).and_return('master')
34
+ @extension.options[:production_branch] = 'master'
35
+
36
+ expect( @extension.config.env ).to eq :production
37
+ end
38
+
39
+ it "sets env to current_branch if there is a matching env key" do
40
+ allow( Asgit ).to receive(:current_branch).and_return('test')
41
+
42
+ expect( @extension.config.env ).to eq :test
43
+ end
44
+
45
+ it "falls back to :default_env if no matching env data" do
46
+ allow( Asgit ).to receive(:current_branch).and_return('no_match')
47
+ @extension.options[:default_env] = 'set_default_env'
48
+
49
+ expect( @extension.config.env ).to eq :set_default_env
50
+ end
51
+
52
+ end
53
+
54
+ describe "#manipulate_resource_list" do
55
+ it "adds local files to the resource list" do
56
+ resource_paths = @mm.sitemap.resources.map(&:url)
57
+ expect( resource_paths ).to include '/assets/cdn/img_one'
58
+ expect( resource_paths ).to include '/assets/cdn/img_two'
59
+ end
60
+ end
61
+
62
+ end
@@ -0,0 +1,70 @@
1
+ require 'spec_helper'
2
+ require 'middleman_spec_helper'
3
+
4
+ describe CDNAssets::Middleman::Helpers do
5
+
6
+ before :each do
7
+ Given.fixture 'base'
8
+ Given.file 'cdn/production.manifest', "image_one\nimage_remote_one\n"
9
+ Given.file 'cdn/staging.manifest', "image_one\nimage_remote_two\n"
10
+ Given.file 'cdn/image_one', ''
11
+ Given.file 'cdn/image_two', ''
12
+
13
+ @mm = Middleman::Fixture.app
14
+ @extension = @mm.extensions[:cdn_assets]
15
+ end
16
+
17
+ after :each do
18
+ Given.cleanup!
19
+ end
20
+
21
+ describe "sitemap helpers" do
22
+ describe "#cdn_asset_url" do
23
+ context "when asset is on production cdn" do
24
+ it "returns the 'live' url of the asset" do
25
+ Asgit.stub(current_branch: 'master')
26
+ expect( @mm.cdn_asset_url('image_remote_one') ).to eq 'http://production.com/image_remote_one'
27
+ end
28
+ end
29
+
30
+ context "when asset is on staging cdn" do
31
+ it "returns the 'live' url of the asset" do
32
+ Asgit.stub(current_branch: 'some_branch')
33
+ expect( @mm.cdn_asset_url('image_remote_two') ).to eq 'http://staging.com/some_branch/image_remote_two'
34
+ end
35
+ end
36
+
37
+ context "when asset is local" do
38
+ it "returns the 'localhost' url of the asset" do
39
+ expect( @mm.cdn_asset_url('image_two') ).to eq '/assets/cdn/image_two'
40
+ end
41
+ end
42
+
43
+ context "when asset is local and listed in a manifest" do
44
+ it "returns the 'localhost' url of the asset" do
45
+ expect( @mm.cdn_asset_url('image_one') ).to eq '/assets/cdn/image_one'
46
+ end
47
+ end
48
+
49
+ context "when the asset is missing" do
50
+ it "raises an error with the asset name" do
51
+ expect{
52
+ @mm.cdn_asset_url('missing_image')
53
+ }.to raise_error "missing asset at 'missing_image'"
54
+ end
55
+ end
56
+ end
57
+ end
58
+
59
+ describe "sass helpers" do
60
+ describe "#cdn_asset_url" do
61
+ it "calls mm#cdn_asset_url with the asset path" do
62
+ expect( @mm ).to receive(:cdn_asset_url).with('image_path')
63
+
64
+ source = 'body { background: url( cdn_asset_url("image_path") ); }'
65
+ Sass.compile( source )
66
+ end
67
+ end
68
+ end
69
+
70
+ end
@@ -0,0 +1,56 @@
1
+ require 'spec_helper'
2
+ require 'middleman_spec_helper'
3
+
4
+ require 'branchable_cdn_assets/middleman/rake_tasks'
5
+
6
+ describe BranchableCDNAssets::Middleman::RakeTasks do
7
+
8
+ describe "::register" do
9
+
10
+ before :each do
11
+ Given.fixture 'base'
12
+ Rake::Task.clear
13
+ end
14
+
15
+ after :each do
16
+ Given.cleanup!
17
+ end
18
+
19
+ it "adds tasks under provided namespace" do
20
+ described_class.register(:test)
21
+
22
+ expect( Rake::Task.tasks.map(&:to_s) ).to include 'test:list'
23
+ end
24
+
25
+ it "defaults to :cdn for namespace" do
26
+ described_class.register
27
+
28
+ expect( Rake::Task.tasks.map(&:to_s) ).to include 'cdn:list'
29
+ end
30
+
31
+ it "sends method with check to the file_manager" do
32
+ class FileManagerStub
33
+ extend BranchableCDNAssets::CheckBefore
34
+
35
+ def list *args
36
+ []
37
+ end
38
+
39
+ def pull!
40
+ end
41
+ end
42
+
43
+ file_manager_double = FileManagerStub.new
44
+ allow_any_instance_of( described_class ).to receive(:file_manager).and_return( file_manager_double )
45
+
46
+ expect( file_manager_double ).to receive(:list)
47
+ expect( file_manager_double ).to receive(:pull!)
48
+
49
+ described_class.register
50
+ Rake::Task['cdn:list'].invoke
51
+ Rake::Task['cdn:pull'].invoke
52
+ end
53
+
54
+ end
55
+
56
+ end
@@ -0,0 +1,4 @@
1
+ require 'rack/test'
2
+ require 'middleman'
3
+ require 'support/middleman/fixture'
4
+ require 'support/middleman/browser'
@@ -0,0 +1,21 @@
1
+ require 'rspec'
2
+ require 'pry'
3
+ require 'tempfile'
4
+ require 'catch_and_release'
5
+ require 'catch_and_release/rspec'
6
+ require 'support/given'
7
+ require 'support/hash'
8
+
9
+ require 'branchable_cdn_assets-middleman'
10
+
11
+ # silence stdout
12
+ $stdout = StringIO.open('','w+')
13
+
14
+ RSpec.configure do |config|
15
+ config.mock_with :rspec do |mocks|
16
+ mocks.verify_doubled_constant_names = true
17
+ mocks.verify_partial_doubles = true
18
+ end
19
+
20
+ config.include CatchAndRelease::RSpec
21
+ end
@@ -0,0 +1,28 @@
1
+ module Given
2
+ ROOT = Dir.pwd
3
+ TMP = File.join( Dir.pwd, 'tmp' )
4
+
5
+ class << self
6
+
7
+ def fixture name
8
+ cleanup!
9
+
10
+ `rsync -av ./spec/fixtures/#{name}/ #{TMP}/`
11
+ Dir.chdir TMP
12
+ end
13
+
14
+ def file name, content
15
+ file_path = File.join( TMP, name )
16
+ FileUtils.mkdir_p( File.dirname(file_path) )
17
+ File.open( file_path, 'w' ) do |file|
18
+ file.write content
19
+ end
20
+ end
21
+
22
+ def cleanup!
23
+ Dir.chdir ROOT
24
+ `rm -rf #{TMP}`
25
+ end
26
+
27
+ end
28
+ end
@@ -0,0 +1,8 @@
1
+ class Hash
2
+ def without_key key
3
+ new_hash = self.dup
4
+ new_hash.delete(key)
5
+
6
+ return new_hash
7
+ end
8
+ end
@@ -0,0 +1,16 @@
1
+ module Middleman
2
+ class Browser
3
+
4
+ attr_reader :browser
5
+
6
+ def initialize app
7
+ app_on_rack = app.class.to_rack_app
8
+ @browser = ::Rack::Test::Session.new( ::Rack::MockSession.new(app_on_rack) )
9
+ end
10
+
11
+ def get path
12
+ browser.get( URI.escape(path) ).body
13
+ end
14
+
15
+ end
16
+ end
@@ -0,0 +1,14 @@
1
+ module Middleman
2
+ module Fixture
3
+
4
+ class << self
5
+ def app &block
6
+ ENV['MM_ROOT'] = Given::TMP
7
+ Middleman::Application.server.inst do
8
+ instance_eval(&block) if block
9
+ end
10
+ end
11
+ end
12
+
13
+ end
14
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: branchable_cdn_assets-middleman
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.5.0
5
+ platform: ruby
6
+ authors:
7
+ - Steven Sloan
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-01-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: middleman
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '3.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '3.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: branchable_cdn_assets
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '0.5'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '0.5'
41
+ description: ' Middleman Adaptor for branchable_cdn_assets '
42
+ email: marketing-dev@mailchimp.com
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - lib/branchable_cdn_assets-middleman.rb
48
+ - lib/branchable_cdn_assets/middleman.rb
49
+ - lib/branchable_cdn_assets/middleman/extension.rb
50
+ - lib/branchable_cdn_assets/middleman/helpers.rb
51
+ - lib/branchable_cdn_assets/middleman/rake_tasks.rb
52
+ - lib/branchable_cdn_assets/middleman/version.rb
53
+ - lib/middleman_extension.rb
54
+ - spec/fixtures/base/config.rb
55
+ - spec/lib/branchable_cdn_assets-middleman_spec.rb
56
+ - spec/lib/branchable_cdn_assets/middleman/extension_spec.rb
57
+ - spec/lib/branchable_cdn_assets/middleman/helpers_specs.rb
58
+ - spec/lib/branchable_cdn_assets/middleman/rake_tasks_spec.rb
59
+ - spec/middleman_spec_helper.rb
60
+ - spec/spec_helper.rb
61
+ - spec/support/given.rb
62
+ - spec/support/hash.rb
63
+ - spec/support/middleman/browser.rb
64
+ - spec/support/middleman/fixture.rb
65
+ homepage: http://github.com/mailchimp/branchable_cdn_assets-middleman
66
+ licenses:
67
+ - MIT
68
+ metadata: {}
69
+ post_install_message:
70
+ rdoc_options: []
71
+ require_paths:
72
+ - lib
73
+ required_ruby_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - '>='
76
+ - !ruby/object:Gem::Version
77
+ version: 1.9.3
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ requirements: []
84
+ rubyforge_project:
85
+ rubygems_version: 2.2.0
86
+ signing_key:
87
+ specification_version: 4
88
+ summary: Middleman Adaptor for branchable_cdn_assets
89
+ test_files:
90
+ - spec/fixtures/base/config.rb
91
+ - spec/lib/branchable_cdn_assets/middleman/extension_spec.rb
92
+ - spec/lib/branchable_cdn_assets/middleman/helpers_specs.rb
93
+ - spec/lib/branchable_cdn_assets/middleman/rake_tasks_spec.rb
94
+ - spec/lib/branchable_cdn_assets-middleman_spec.rb
95
+ - spec/middleman_spec_helper.rb
96
+ - spec/spec_helper.rb
97
+ - spec/support/given.rb
98
+ - spec/support/hash.rb
99
+ - spec/support/middleman/browser.rb
100
+ - spec/support/middleman/fixture.rb