sinatra-simple-assets 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/README.md +1 -0
- data/Rakefile +1 -0
- data/lib/sinatra/simple_assets.rb +151 -0
- data/sinatra-simple-assets.gemspec +20 -0
- metadata +95 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
# Sinatra Simple Assets
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,151 @@
|
|
1
|
+
require 'sinatra/base'
|
2
|
+
require 'cssmin'
|
3
|
+
require 'uglifier'
|
4
|
+
|
5
|
+
module Sinatra
|
6
|
+
module SimpleAssets
|
7
|
+
module Helpers
|
8
|
+
def stylesheet(bundle)
|
9
|
+
settings.assets.paths_for("#{bundle}.css").map do |file|
|
10
|
+
"<link rel=\"stylesheet\" href=\"#{url(file)}\">"
|
11
|
+
end.join("\n")
|
12
|
+
end
|
13
|
+
|
14
|
+
def javascript(bundle)
|
15
|
+
settings.assets.paths_for("#{bundle}.js").map do |file|
|
16
|
+
"<script src=\"#{url(file)}\"></script>"
|
17
|
+
end.join("\n")
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
class Bundle
|
22
|
+
attr_accessor :files
|
23
|
+
|
24
|
+
def initialize(name, type, root, files)
|
25
|
+
@name = name
|
26
|
+
@type = type
|
27
|
+
@root = root
|
28
|
+
@files = files
|
29
|
+
end
|
30
|
+
|
31
|
+
def name
|
32
|
+
"#{@name}.#{@type}"
|
33
|
+
end
|
34
|
+
|
35
|
+
def hash_name
|
36
|
+
"#{@name}-#{hash}.#{@type}"
|
37
|
+
end
|
38
|
+
|
39
|
+
def hashed_path
|
40
|
+
"#{path}/#{hash_name}"
|
41
|
+
end
|
42
|
+
|
43
|
+
def hash
|
44
|
+
@hash ||= Digest::SHA1.hexdigest content
|
45
|
+
end
|
46
|
+
|
47
|
+
def content
|
48
|
+
case @type
|
49
|
+
when :js
|
50
|
+
@content ||= Uglifier.new.compress combined
|
51
|
+
when :css
|
52
|
+
@content ||= CSSMin.minify combined
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def combined
|
57
|
+
@combined ||= @files.map do |file|
|
58
|
+
File.open(@root + file) { |f| f.read }
|
59
|
+
end.join("\n")
|
60
|
+
end
|
61
|
+
|
62
|
+
def path
|
63
|
+
@type == :js ? 'javascripts' : 'stylesheets'
|
64
|
+
end
|
65
|
+
|
66
|
+
def compile
|
67
|
+
File.open("#{@root}/#{hashed_path}", 'w') do |f|
|
68
|
+
f << content
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
class Assets
|
74
|
+
def initialize(app, &block)
|
75
|
+
@app = app
|
76
|
+
@bundles = {}
|
77
|
+
@hashes = {}
|
78
|
+
instance_eval(&block)
|
79
|
+
end
|
80
|
+
|
81
|
+
def css(bundle, files)
|
82
|
+
create_bundle(bundle, :css, files)
|
83
|
+
end
|
84
|
+
|
85
|
+
def js(bundle, files)
|
86
|
+
create_bundle(bundle, :js, files)
|
87
|
+
end
|
88
|
+
|
89
|
+
def create_bundle(name, type, files)
|
90
|
+
bundle = Bundle.new(name, type, @app.public_folder, files)
|
91
|
+
@bundles[bundle.name] = bundle
|
92
|
+
end
|
93
|
+
|
94
|
+
def paths_for(bundle)
|
95
|
+
bundle = @bundles[bundle]
|
96
|
+
return [] unless bundle
|
97
|
+
|
98
|
+
if @app.environment == :production
|
99
|
+
@hashes[bundle.hash_name] = bundle.name
|
100
|
+
[bundle.hashed_path]
|
101
|
+
else
|
102
|
+
bundle.files
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
def content_for(bundle)
|
107
|
+
bundle = @bundles[@hashes[bundle]]
|
108
|
+
bundle.content if bundle
|
109
|
+
end
|
110
|
+
|
111
|
+
def precompile
|
112
|
+
@bundles.values.each do |bundle|
|
113
|
+
bundle.compile
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
def bundle_exists?(bundle)
|
118
|
+
@hashes[bundle]
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
def assets(&block)
|
123
|
+
@assets ||= Assets.new(self, &block)
|
124
|
+
end
|
125
|
+
|
126
|
+
def self.registered(app)
|
127
|
+
app.helpers SimpleAssets::Helpers
|
128
|
+
|
129
|
+
app.get '/stylesheets/:bundle' do
|
130
|
+
serve_content(params[:bundle], :css)
|
131
|
+
end
|
132
|
+
|
133
|
+
app.get '/javascripts/:bundle' do
|
134
|
+
serve_content(params[:bundle], :js)
|
135
|
+
end
|
136
|
+
|
137
|
+
def serve_content(bundle, type)
|
138
|
+
assets = settings.assets
|
139
|
+
exists = assets.bundle_exists?(bundle)
|
140
|
+
|
141
|
+
etag bundle if exists
|
142
|
+
not_found unless exists
|
143
|
+
|
144
|
+
cache_control :public, :must_revalidate, :max_age => 86400
|
145
|
+
|
146
|
+
content_type type
|
147
|
+
assets.content_for(bundle)
|
148
|
+
end
|
149
|
+
end
|
150
|
+
end
|
151
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
Gem::Specification.new do |s|
|
3
|
+
s.name = 'sinatra-simple-assets'
|
4
|
+
s.version = '0.0.1'
|
5
|
+
s.authors = ["Pete O'Grady"]
|
6
|
+
s.email = ['pete@peteogrady.com']
|
7
|
+
s.homepage = ""
|
8
|
+
s.summary = %q{Asset minification and bundling for Sinatra}
|
9
|
+
s.description = %q{Asset minification and bundling for Sinatra}
|
10
|
+
|
11
|
+
s.files = `git ls-files`.split("\n")
|
12
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
13
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
14
|
+
s.require_paths = ['lib']
|
15
|
+
|
16
|
+
s.add_dependency 'cssmin', '1.0.2'
|
17
|
+
s.add_dependency 'sinatra', '~> 1.3.0'
|
18
|
+
s.add_dependency 'therubyracer-heroku', '0.8.1.pre3'
|
19
|
+
s.add_dependency 'uglifier', '1.1.0'
|
20
|
+
end
|
metadata
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sinatra-simple-assets
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Pete O'Grady
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-12-15 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: cssmin
|
16
|
+
requirement: &70317838571960 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - =
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 1.0.2
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70317838571960
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: sinatra
|
27
|
+
requirement: &70317838570620 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ~>
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 1.3.0
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70317838570620
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: therubyracer-heroku
|
38
|
+
requirement: &70317838646680 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - =
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 0.8.1.pre3
|
44
|
+
type: :runtime
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *70317838646680
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: uglifier
|
49
|
+
requirement: &70317838644740 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - =
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 1.1.0
|
55
|
+
type: :runtime
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *70317838644740
|
58
|
+
description: Asset minification and bundling for Sinatra
|
59
|
+
email:
|
60
|
+
- pete@peteogrady.com
|
61
|
+
executables: []
|
62
|
+
extensions: []
|
63
|
+
extra_rdoc_files: []
|
64
|
+
files:
|
65
|
+
- .gitignore
|
66
|
+
- Gemfile
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- lib/sinatra/simple_assets.rb
|
70
|
+
- sinatra-simple-assets.gemspec
|
71
|
+
homepage: ''
|
72
|
+
licenses: []
|
73
|
+
post_install_message:
|
74
|
+
rdoc_options: []
|
75
|
+
require_paths:
|
76
|
+
- lib
|
77
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
78
|
+
none: false
|
79
|
+
requirements:
|
80
|
+
- - ! '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
84
|
+
none: false
|
85
|
+
requirements:
|
86
|
+
- - ! '>='
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
requirements: []
|
90
|
+
rubyforge_project:
|
91
|
+
rubygems_version: 1.8.11
|
92
|
+
signing_key:
|
93
|
+
specification_version: 3
|
94
|
+
summary: Asset minification and bundling for Sinatra
|
95
|
+
test_files: []
|