wake-assets 0.2.0 → 0.3.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: 44c5c197971626d1bf3ba093f1a86f0118a783dc
4
+ data.tar.gz: ac83070891e3a16829f468d6747fa473d44fe37f
5
+ SHA512:
6
+ metadata.gz: b85685b5a106a2f5ceb6f5c84f5a46315bf58d0d658af08d7259a304c1c3b4a154dd2e1995bbdc9a961f3bf8da31749fb66f4d30b7f65e38f58773fcc167b2fb
7
+ data.tar.gz: 18176ade3766d0816cc2b00b1cb0501a31848f942d64788281f965d9f7e10a811cdb9aca99ee0cc4902020fb011e04257ae7bc578cbc48ef1ce2cfea987f1955
data/README.md CHANGED
@@ -28,10 +28,10 @@ require 'wake/assets'
28
28
  dev = Rails.env.development?
29
29
 
30
30
  $wake = Wake::Assets.new(
31
- :wake => File.expand_path('node_modules/.bin/wake', Rails.root),
32
- :root => File.expand_path('public', Rails.root),
33
- :mode => dev ? :sources : :targets,
34
- :monitor => dev
31
+ :wake => File.expand_path('node_modules/.bin/wake', Rails.root),
32
+ :root => File.expand_path('public', Rails.root),
33
+ :mode => dev ? :sources : :targets,
34
+ :cache => !dev
35
35
  )
36
36
  ```
37
37
 
@@ -41,8 +41,8 @@ The options are:
41
41
  * `:root` - the document root of your application
42
42
  * `:mode` - `:sources` if you want to render links to source files, `:targets`
43
43
  if you want optimised files
44
- * `:monitor` - whether to monitor the filesystem for changes, recommended in
45
- development but not in production
44
+ * `:cache` - whether to cache `wake` metadata files in memory, recommended in
45
+ production but not in development
46
46
 
47
47
  #### At request time
48
48
 
data/lib/wake/assets.rb CHANGED
@@ -1,7 +1,6 @@
1
1
  require 'base64'
2
2
  require 'erb'
3
3
  require 'json'
4
- require 'listen'
5
4
  require 'mime/types'
6
5
  require 'pathname'
7
6
  require 'set'
@@ -10,13 +9,13 @@ module Wake
10
9
  class Assets
11
10
 
12
11
  DEFAULT_BUILD = 'min'
12
+ DEFAULT_CACHE = true
13
13
  DEFAULT_MODE = :targets
14
14
  DEFAULT_WAKE = './node_modules/wake/bin/wake'
15
15
  CACHE_FILE = '.wake.json'
16
16
  MANIFEST = '.manifest.json'
17
17
  PACKAGE_FILE = 'package.json'
18
18
  WAKE_FILE = 'wake.json'
19
- CONFIG_FILES = Set.new([CACHE_FILE, MANIFEST, PACKAGE_FILE, WAKE_FILE])
20
19
 
21
20
  CSS = 'css'
22
21
  JS = 'javascript'
@@ -27,38 +26,51 @@ module Wake
27
26
 
28
27
  autoload :Renderer, File.expand_path('../assets/renderer', __FILE__)
29
28
 
30
- def initialize(options)
31
- @pwd = File.expand_path(options.fetch(:pwd, Dir.pwd))
32
- @wake = options.fetch(:wake, File.expand_path(DEFAULT_WAKE, @pwd))
33
- @root = Pathname.new(File.expand_path(options.fetch(:root, @pwd)))
34
- @mode = options.fetch(:mode, DEFAULT_MODE)
35
- @manifest = new_manifest_cache
36
- @paths = new_path_cache
29
+ def initialize(options = {})
30
+ @pwd = File.expand_path(options.fetch(:pwd, Dir.pwd))
31
+ @cache = options.fetch(:cache, DEFAULT_CACHE)
32
+ @wake = options.fetch(:wake, File.expand_path(DEFAULT_WAKE, @pwd))
33
+ @root = Pathname.new(File.expand_path(options.fetch(:root, @pwd)))
34
+ @mode = options.fetch(:mode, DEFAULT_MODE)
37
35
 
38
- system(@wake, '--cache')
39
- read_config
36
+ clear_cache
37
+ end
40
38
 
41
- return unless options[:monitor]
39
+ def clear_cache
40
+ system(@wake, '--cache') unless @cache
42
41
 
43
- listener = Listen.to(@pwd).change do |modified, added, removed|
44
- all = (modified + added + removed).map &File.method(:basename)
45
- system(@wake, '--cache') if (added.any? or removed.any?) and not all.include?(CACHE_FILE)
46
- update! if (CONFIG_FILES & all).any?
47
- end
48
- listener.force_polling(true)
49
- listener.start
42
+ @config = nil
43
+ @index = nil
44
+ @manifest = {}
45
+ @paths = {}
50
46
  end
51
47
 
52
- def deployment?
53
- @mode == :targets
48
+ def generated_file_paths
49
+ index = read_index
50
+ paths = Set.new([File.join(@pwd, CACHE_FILE)])
51
+ index.each do |group_name, group|
52
+ group.each do |bundle_name, bundles|
53
+ bundles['targets'].each_value do |path|
54
+ paths.add(path)
55
+ manifest = File.join(File.dirname(path), MANIFEST)
56
+ source_map = path + '.map'
57
+ [manifest, source_map].each do |file|
58
+ paths.add(file) if File.file?(file)
59
+ end
60
+ end
61
+ end
62
+ end
63
+ paths.map(&method(:resolve))
54
64
  end
55
65
 
56
66
  def paths_for(group, names, options = {})
67
+ config = read_config
68
+
57
69
  build = options.fetch(:build, DEFAULT_BUILD)
58
- unless @config[group].fetch('builds', {}).has_key?(build)
70
+ unless config.fetch(group).fetch('builds', {}).has_key?(build)
59
71
  build = DEFAULT_BUILD
60
72
  end
61
- names.map { |name| @paths[group][name][build] }.flatten
73
+ names.map { |name| read_paths(group, name, build) }.flatten
62
74
  end
63
75
 
64
76
  def relative(path)
@@ -66,81 +78,76 @@ module Wake
66
78
  end
67
79
 
68
80
  def renderer(options = {})
81
+ clear_cache unless @cache
69
82
  Renderer.new(self, options)
70
83
  end
71
84
 
72
85
  private
73
86
 
74
- def find_paths_for(group, name, build)
75
- absolute_paths = begin
76
- cache = @cache[group][name]
87
+ def find_paths_for(key)
88
+ group, name, build = *key
89
+
90
+ begin
91
+ index = read_index.fetch(group).fetch(name)
77
92
  if @mode.to_s == 'sources'
78
- cache.fetch('sources')
93
+ absolute_paths = index.fetch('sources')
79
94
  else
80
- [cache.fetch('targets').fetch(build)]
95
+ absolute_paths = [index.fetch('targets').fetch(build)]
81
96
  end
82
- rescue
83
- nil
97
+ rescue KeyError
98
+ raise InvalidReference, "Could not find assets: group: '#{group}', name: '#{name}', build: '#{build}'"
84
99
  end
85
100
 
86
- if absolute_paths.nil?
87
- raise InvalidReference, "Could not find assets, group: #{group}, name: #{name}, build: #{build}"
88
- end
101
+ absolute_paths.map(&method(:resolve))
102
+ end
89
103
 
90
- absolute_paths.map do |path|
91
- basename = File.basename(path)
92
- dirname = File.dirname(path)
93
- manifest = File.join(dirname, MANIFEST)
104
+ def read_config
105
+ return @config if @config
94
106
 
95
- File.join(dirname, @manifest[manifest].fetch(basename, basename))
96
- end
97
- end
107
+ wake = File.join(@pwd, WAKE_FILE)
108
+ package = File.join(@pwd, PACKAGE_FILE)
98
109
 
99
- def new_manifest_cache
100
- Hash.new do |hash, path|
101
- hash[path] = File.file?(path) ? JSON.parse(File.read(path)) : {}
110
+ if File.file?(wake)
111
+ config = JSON.parse(File.read(wake))
112
+ elsif File.file?(package)
113
+ config = JSON.parse(File.read(package))['wake']
114
+ else
115
+ config = {}
102
116
  end
103
- end
104
117
 
105
- def new_path_cache
106
- Hash.new do |h, group|
107
- h[group] = Hash.new do |i, name|
108
- i[name] = Hash.new do |j, build|
109
- j[build] = find_paths_for(group, name, build)
110
- end
111
- end
112
- end
118
+ @config = config if @cache
119
+ config
113
120
  end
114
121
 
115
- def read_config
116
- cache = File.join(@pwd, CACHE_FILE)
117
- wake = File.join(@pwd, WAKE_FILE)
118
- package = File.join(@pwd, PACKAGE_FILE)
119
-
120
- @config = if File.file?(wake)
121
- JSON.parse(File.read(wake))
122
- elsif File.file?(package)
123
- JSON.parse(File.read(package))['wake']
124
- else
125
- {}
126
- end
122
+ def read_index
123
+ return @index if @index
124
+ path = File.join(@pwd, CACHE_FILE)
125
+ index = JSON.parse(File.read(path))
126
+ @index = index if @cache
127
+ index
128
+ end
127
129
 
128
- @cache = JSON.parse(File.read(cache))
130
+ def read_manifest(path)
131
+ return @manifest[path] if @manifest.has_key?(path)
132
+ mapping = File.file?(path) ? JSON.parse(File.read(path)) : {}
133
+ @manifest[path] = mapping if @cache
134
+ mapping
129
135
  end
130
136
 
131
- def update!
132
- paths = new_path_cache
133
- read_config
137
+ def read_paths(group, name, build)
138
+ key = [group, name, build]
139
+ return @paths[key] if @paths.has_key?(key)
140
+ paths = find_paths_for(key)
141
+ @paths[key] = paths if @cache
142
+ paths
143
+ end
134
144
 
135
- @paths.each do |group, a|
136
- a.each do |name, b|
137
- b.each do |build, files|
138
- paths[group][name][build]
139
- end
140
- end
141
- end
142
- @manifest = new_manifest_cache
143
- @paths = paths
145
+ def resolve(path)
146
+ path = File.join(@pwd, path)
147
+ basename = File.basename(path)
148
+ dirname = File.dirname(path)
149
+ manifest = File.join(dirname, MANIFEST)
150
+ File.join(dirname, read_manifest(manifest).fetch(basename, basename))
144
151
  end
145
152
 
146
153
  end
@@ -30,9 +30,7 @@ module Wake
30
30
 
31
31
  tags = if options.fetch(:inline, @inline)
32
32
  paths_for(IMG, names, options).map do |path|
33
- base64 = Base64.strict_encode64(File.read(path))
34
- mime = MIME::Types.type_for(path).first
35
- tag :img, options, :src => "data:#{mime};base64,#{base64}"
33
+ tag :img, options, :src => base64_file(path)
36
34
  end
37
35
  else
38
36
  urls_for(IMG, names, options).map { |url| tag :img, options, :src => url }
@@ -73,6 +71,12 @@ module Wake
73
71
 
74
72
  private
75
73
 
74
+ def base64_file(path)
75
+ base64 = Base64.strict_encode64(File.read(path))
76
+ mime = MIME::Types.type_for(path).first
77
+ "data:#{mime};base64,#{base64}"
78
+ end
79
+
76
80
  def extract_options(names)
77
81
  [names.grep(String), names.grep(Hash).first || {}]
78
82
  end
metadata CHANGED
@@ -1,46 +1,41 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wake-assets
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
5
- prerelease:
4
+ version: 0.3.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - James Coglan
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-08-30 00:00:00.000000000 Z
11
+ date: 2013-12-12 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
- name: listen
14
+ name: mime-types
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ! '>='
17
+ - - '>='
20
18
  - !ruby/object:Gem::Version
21
19
  version: '0'
22
20
  type: :runtime
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ! '>='
24
+ - - '>='
28
25
  - !ruby/object:Gem::Version
29
26
  version: '0'
30
27
  - !ruby/object:Gem::Dependency
31
- name: mime-types
28
+ name: sinatra
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
- - - ! '>='
31
+ - - '>='
36
32
  - !ruby/object:Gem::Version
37
33
  version: '0'
38
- type: :runtime
34
+ type: :development
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
- - - ! '>='
38
+ - - '>='
44
39
  - !ruby/object:Gem::Version
45
40
  version: '0'
46
41
  description:
@@ -55,7 +50,9 @@ files:
55
50
  - lib/wake/assets.rb
56
51
  - lib/wake/assets/renderer.rb
57
52
  homepage: http://github.com/jcoglan/wake-assets
58
- licenses: []
53
+ licenses:
54
+ - MIT
55
+ metadata: {}
59
56
  post_install_message:
60
57
  rdoc_options:
61
58
  - --main
@@ -65,21 +62,19 @@ rdoc_options:
65
62
  require_paths:
66
63
  - lib
67
64
  required_ruby_version: !ruby/object:Gem::Requirement
68
- none: false
69
65
  requirements:
70
- - - ! '>='
66
+ - - '>='
71
67
  - !ruby/object:Gem::Version
72
68
  version: '0'
73
69
  required_rubygems_version: !ruby/object:Gem::Requirement
74
- none: false
75
70
  requirements:
76
- - - ! '>='
71
+ - - '>='
77
72
  - !ruby/object:Gem::Version
78
73
  version: '0'
79
74
  requirements: []
80
75
  rubyforge_project:
81
- rubygems_version: 1.8.23
76
+ rubygems_version: 2.0.14
82
77
  signing_key:
83
- specification_version: 3
78
+ specification_version: 4
84
79
  summary: Renders links to assets managed by wake
85
80
  test_files: []