spritely 0.3.2 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. checksums.yaml +4 -4
  2. data/lib/spritely.rb +11 -34
  3. data/lib/spritely/collection.rb +12 -11
  4. data/lib/spritely/engine.rb +4 -0
  5. data/lib/spritely/generators/base.rb +0 -16
  6. data/lib/spritely/generators/chunky_png.rb +1 -4
  7. data/lib/spritely/image_set.rb +2 -2
  8. data/lib/spritely/sass_functions.rb +30 -29
  9. data/lib/spritely/sprite_map.rb +14 -31
  10. data/lib/spritely/sprockets/preprocessor.rb +65 -0
  11. data/lib/spritely/sprockets/transformer.rb +43 -0
  12. data/lib/spritely/version.rb +1 -1
  13. data/spec/fixtures/rails-app-changes/app/assets/stylesheets/sprites.css.scss +10 -18
  14. data/spec/fixtures/rails-app/app/assets/images/sprites/application.png.sprite +5 -0
  15. data/spec/fixtures/rails-app/app/assets/images/sprites/foo.png.sprite +1 -0
  16. data/spec/fixtures/rails-app/app/assets/stylesheets/sprites.css.scss +6 -14
  17. data/spec/fixtures/rails-app/app/assets/stylesheets/sprites_2.css.scss +4 -6
  18. data/spec/integration/precompilation_spec.rb +2 -14
  19. data/spec/spec_helper.rb +0 -4
  20. data/spec/spritely/collection_spec.rb +9 -8
  21. data/spec/spritely/generators/chunky_png_spec.rb +8 -13
  22. data/spec/spritely/image_set_spec.rb +2 -8
  23. data/spec/spritely/sass_functions_spec.rb +38 -37
  24. data/spec/spritely/sprite_map_spec.rb +16 -52
  25. data/spec/spritely/sprockets/preprocessor_spec.rb +33 -0
  26. data/spec/support/rails_app_helpers.rb +4 -5
  27. metadata +38 -49
  28. data/lib/generators/spritely/install_generator.rb +0 -17
  29. data/lib/spritely/adapters/sprockets_2.rb +0 -13
  30. data/lib/spritely/adapters/sprockets_3.rb +0 -11
  31. data/lib/spritely/cache.rb +0 -51
  32. data/lib/spritely/options.rb +0 -59
  33. data/lib/spritely/sprockets/manifest.rb +0 -20
  34. data/spec/generators/spritely/install_generator_spec.rb +0 -28
  35. data/spec/spritely/cache_spec.rb +0 -24
  36. data/spec/spritely/options_spec.rb +0 -29
  37. data/spec/spritely_spec.rb +0 -41
  38. data/spec/support/shared_examples.rb +0 -40
  39. data/spec/support/shared_examples/sprockets_2_sass_functions_helpers.rb +0 -13
  40. data/spec/support/shared_examples/sprockets_3_sass_functions_helpers.rb +0 -15
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+
3
+ describe Spritely::Sprockets::Preprocessor do
4
+ let(:data) { "//= spacing some-new-image 789\n//= position some-new-image right\n//= repeat another-image true\n//= repeat yet-another-image false\n//= spacing 901\n//= position left" }
5
+ let(:input) { {
6
+ data: data,
7
+ filename: "sprites/foo.png.sprite",
8
+ metadata: {}
9
+ } }
10
+
11
+ subject(:preprocessor) { Spritely::Sprockets::Preprocessor.new(comments: ["//", ["/*", "*/"]]) }
12
+
13
+ it 'saves the processed options as part of the metadata' do
14
+ preprocessor._call(input)
15
+
16
+ expect(input[:metadata][:sprite_directives]).to eq(
17
+ global: { spacing: '901', position: 'left' },
18
+ images: {
19
+ "some-new-image" => { spacing: '789', position: 'right' },
20
+ "another-image" => { repeat: 'true', spacing: '901', position: 'left' },
21
+ "yet-another-image" => { repeat: 'false', spacing: '901', position: 'left' }
22
+ }
23
+ )
24
+ end
25
+
26
+ describe 'invalid global option' do
27
+ let(:data) { "//= repeat true" }
28
+
29
+ it 'raises an exception' do
30
+ expect { preprocessor._call(input) }.to raise_error(ArgumentError, "'repeat' is not a valid global option")
31
+ end
32
+ end
33
+ end
@@ -1,3 +1,6 @@
1
+ require 'sprockets/rails/version'
2
+ require 'sprockets/version'
3
+
1
4
  module RailsAppHelpers
2
5
  GENERATOR_FLAGS = [
3
6
  '--skip-active-record',
@@ -21,7 +24,6 @@ module RailsAppHelpers
21
24
  f.write("gem 'sprockets', '#{Sprockets::VERSION}'")
22
25
  end
23
26
  %x(bundle install)
24
- %x(rails generate spritely:install)
25
27
  FileUtils.cp_r "#{__dir__}/../fixtures/rails-app/.", "."
26
28
  yield
27
29
  end
@@ -35,8 +37,7 @@ module RailsAppHelpers
35
37
  end
36
38
 
37
39
  def spite_image_path(sprite_name)
38
- # TODO: When Sprockets 2.0 support is dropped (probably when Sprockets 4.0 is released), swap `digest` for `hexdigest`
39
- fingerprint = runner(%~puts Rails.application.assets["sprites/#{sprite_name}.png"].digest~)
40
+ fingerprint = runner(%~puts Rails.application.assets["sprites/#{sprite_name}.png"].hexdigest~)
40
41
  "/assets/sprites/application-#{fingerprint.strip}.png"
41
42
  end
42
43
 
@@ -44,8 +45,6 @@ module RailsAppHelpers
44
45
  %x(RAILS_ENV=production rake assets:precompile)
45
46
  end
46
47
 
47
- private
48
-
49
48
  def runner(command)
50
49
  %x(rails runner -e development '#{command}')
51
50
  end
metadata CHANGED
@@ -1,71 +1,71 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spritely
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Robbin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-01-02 00:00:00.000000000 Z
11
+ date: 2016-02-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: activesupport
14
+ name: chunky_png
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">="
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '4.1'
19
+ version: '1.3'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ">="
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '4.1'
26
+ version: '1.3'
27
27
  - !ruby/object:Gem::Dependency
28
- name: chunky_png
28
+ name: sass
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '1.3'
33
+ version: '3.1'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '1.3'
40
+ version: '3.1'
41
41
  - !ruby/object:Gem::Dependency
42
- name: sass
42
+ name: sprockets
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '3.1'
47
+ version: '3.0'
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '3.1'
54
+ version: '3.0'
55
55
  - !ruby/object:Gem::Dependency
56
- name: sprockets-rails
56
+ name: activesupport
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - ">="
60
60
  - !ruby/object:Gem::Version
61
- version: '2.0'
62
- type: :runtime
61
+ version: '0'
62
+ type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
- version: '2.0'
68
+ version: '0'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: pry-byebug
71
71
  requirement: !ruby/object:Gem::Requirement
@@ -81,7 +81,7 @@ dependencies:
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
83
  - !ruby/object:Gem::Dependency
84
- name: railties
84
+ name: sprockets-rails
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
87
  - - ">="
@@ -95,7 +95,7 @@ dependencies:
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
97
  - !ruby/object:Gem::Dependency
98
- name: rake
98
+ name: railties
99
99
  requirement: !ruby/object:Gem::Requirement
100
100
  requirements:
101
101
  - - ">="
@@ -109,35 +109,35 @@ dependencies:
109
109
  - !ruby/object:Gem::Version
110
110
  version: '0'
111
111
  - !ruby/object:Gem::Dependency
112
- name: rspec
112
+ name: rake
113
113
  requirement: !ruby/object:Gem::Requirement
114
114
  requirements:
115
115
  - - ">="
116
116
  - !ruby/object:Gem::Version
117
- version: '3.0'
117
+ version: '0'
118
118
  type: :development
119
119
  prerelease: false
120
120
  version_requirements: !ruby/object:Gem::Requirement
121
121
  requirements:
122
122
  - - ">="
123
123
  - !ruby/object:Gem::Version
124
- version: '3.0'
124
+ version: '0'
125
125
  - !ruby/object:Gem::Dependency
126
- name: rspec-its
126
+ name: rspec
127
127
  requirement: !ruby/object:Gem::Requirement
128
128
  requirements:
129
129
  - - ">="
130
130
  - !ruby/object:Gem::Version
131
- version: '0'
131
+ version: '3.0'
132
132
  type: :development
133
133
  prerelease: false
134
134
  version_requirements: !ruby/object:Gem::Requirement
135
135
  requirements:
136
136
  - - ">="
137
137
  - !ruby/object:Gem::Version
138
- version: '0'
138
+ version: '3.0'
139
139
  - !ruby/object:Gem::Dependency
140
- name: appraisal
140
+ name: rspec-its
141
141
  requirement: !ruby/object:Gem::Requirement
142
142
  requirements:
143
143
  - - ">="
@@ -151,7 +151,7 @@ dependencies:
151
151
  - !ruby/object:Gem::Version
152
152
  version: '0'
153
153
  - !ruby/object:Gem::Dependency
154
- name: generator_spec
154
+ name: appraisal
155
155
  requirement: !ruby/object:Gem::Requirement
156
156
  requirements:
157
157
  - - ">="
@@ -171,20 +171,17 @@ executables: []
171
171
  extensions: []
172
172
  extra_rdoc_files: []
173
173
  files:
174
- - lib/generators/spritely/install_generator.rb
175
174
  - lib/spritely.rb
176
- - lib/spritely/adapters/sprockets_2.rb
177
- - lib/spritely/adapters/sprockets_3.rb
178
- - lib/spritely/cache.rb
179
175
  - lib/spritely/collection.rb
176
+ - lib/spritely/engine.rb
180
177
  - lib/spritely/generators/base.rb
181
178
  - lib/spritely/generators/chunky_png.rb
182
179
  - lib/spritely/image.rb
183
180
  - lib/spritely/image_set.rb
184
- - lib/spritely/options.rb
185
181
  - lib/spritely/sass_functions.rb
186
182
  - lib/spritely/sprite_map.rb
187
- - lib/spritely/sprockets/manifest.rb
183
+ - lib/spritely/sprockets/preprocessor.rb
184
+ - lib/spritely/sprockets/transformer.rb
188
185
  - lib/spritely/version.rb
189
186
  - spec/fixtures/correct-sprite.png
190
187
  - spec/fixtures/rails-app-changes/app/assets/images/application/dropdown-arrow.png
@@ -194,27 +191,23 @@ files:
194
191
  - spec/fixtures/rails-app/app/assets/images/application/football.png
195
192
  - spec/fixtures/rails-app/app/assets/images/application/mario.png
196
193
  - spec/fixtures/rails-app/app/assets/images/foo/fool.png
194
+ - spec/fixtures/rails-app/app/assets/images/sprites/application.png.sprite
195
+ - spec/fixtures/rails-app/app/assets/images/sprites/foo.png.sprite
197
196
  - spec/fixtures/rails-app/app/assets/stylesheets/sprites.css.scss
198
197
  - spec/fixtures/rails-app/app/assets/stylesheets/sprites_2.css.scss
199
198
  - spec/fixtures/rails-app/config/initializers/assets.rb
200
199
  - spec/fixtures/test/foo.png
201
- - spec/generators/spritely/install_generator_spec.rb
202
200
  - spec/integration/precompilation_spec.rb
203
201
  - spec/integration/stylesheet_spec.rb
204
202
  - spec/spec_helper.rb
205
- - spec/spritely/cache_spec.rb
206
203
  - spec/spritely/collection_spec.rb
207
204
  - spec/spritely/generators/chunky_png_spec.rb
208
205
  - spec/spritely/image_set_spec.rb
209
206
  - spec/spritely/image_spec.rb
210
- - spec/spritely/options_spec.rb
211
207
  - spec/spritely/sass_functions_spec.rb
212
208
  - spec/spritely/sprite_map_spec.rb
213
- - spec/spritely_spec.rb
209
+ - spec/spritely/sprockets/preprocessor_spec.rb
214
210
  - spec/support/rails_app_helpers.rb
215
- - spec/support/shared_examples.rb
216
- - spec/support/shared_examples/sprockets_2_sass_functions_helpers.rb
217
- - spec/support/shared_examples/sprockets_3_sass_functions_helpers.rb
218
211
  homepage: https://github.com/agrobbin/spritely
219
212
  licenses:
220
213
  - MIT
@@ -238,8 +231,8 @@ rubyforge_project:
238
231
  rubygems_version: 2.5.1
239
232
  signing_key:
240
233
  specification_version: 4
241
- summary: Hooks into the Rails asset pipeline to allow you to easily generate sprite
242
- maps
234
+ summary: Hooks into the Sprockets asset packaging system to allow you to easily generate
235
+ sprite maps
243
236
  test_files:
244
237
  - spec/fixtures/correct-sprite.png
245
238
  - spec/fixtures/rails-app/app/assets/images/application/background.png
@@ -247,26 +240,22 @@ test_files:
247
240
  - spec/fixtures/rails-app/app/assets/images/application/football.png
248
241
  - spec/fixtures/rails-app/app/assets/images/application/mario.png
249
242
  - spec/fixtures/rails-app/app/assets/images/foo/fool.png
243
+ - spec/fixtures/rails-app/app/assets/images/sprites/application.png.sprite
244
+ - spec/fixtures/rails-app/app/assets/images/sprites/foo.png.sprite
250
245
  - spec/fixtures/rails-app/app/assets/stylesheets/sprites.css.scss
251
246
  - spec/fixtures/rails-app/app/assets/stylesheets/sprites_2.css.scss
252
247
  - spec/fixtures/rails-app/config/initializers/assets.rb
253
248
  - spec/fixtures/rails-app-changes/app/assets/images/application/dropdown-arrow.png
254
249
  - spec/fixtures/rails-app-changes/app/assets/stylesheets/sprites.css.scss
255
250
  - spec/fixtures/test/foo.png
256
- - spec/generators/spritely/install_generator_spec.rb
257
251
  - spec/integration/precompilation_spec.rb
258
252
  - spec/integration/stylesheet_spec.rb
259
253
  - spec/spec_helper.rb
260
- - spec/spritely/cache_spec.rb
261
254
  - spec/spritely/collection_spec.rb
262
255
  - spec/spritely/generators/chunky_png_spec.rb
263
256
  - spec/spritely/image_set_spec.rb
264
257
  - spec/spritely/image_spec.rb
265
- - spec/spritely/options_spec.rb
266
258
  - spec/spritely/sass_functions_spec.rb
267
259
  - spec/spritely/sprite_map_spec.rb
268
- - spec/spritely_spec.rb
260
+ - spec/spritely/sprockets/preprocessor_spec.rb
269
261
  - spec/support/rails_app_helpers.rb
270
- - spec/support/shared_examples/sprockets_2_sass_functions_helpers.rb
271
- - spec/support/shared_examples/sprockets_3_sass_functions_helpers.rb
272
- - spec/support/shared_examples.rb
@@ -1,17 +0,0 @@
1
- require 'rails/generators/base'
2
-
3
- module Spritely
4
- module Generators
5
- class InstallGenerator < ::Rails::Generators::Base
6
- def create_empty_sprites_folder
7
- empty_directory Spritely.relative_folder_path
8
- create_file "#{Spritely.relative_folder_path}/.keep"
9
- end
10
-
11
- def add_sprites_folder_to_gitignore
12
- create_file '.gitignore', skip: true
13
- inject_into_file '.gitignore', "\n/#{Spritely.relative_folder_path.join('*.png')}\n", after: /\z/
14
- end
15
- end
16
- end
17
- end
@@ -1,13 +0,0 @@
1
- module Spritely
2
- module Adapters
3
- class Sprockets2
4
- def reset_cache!(environment, filename)
5
- environment.instance_variable_get(:@assets).delete_if { |_, asset| asset.pathname == filename }
6
- environment.send(:trail).tap do |trail|
7
- trail.instance_variable_get(:@entries).delete(Spritely.directory.to_s)
8
- trail.instance_variable_get(:@stats).delete(filename.to_s)
9
- end
10
- end
11
- end
12
- end
13
- end
@@ -1,11 +0,0 @@
1
- module Spritely
2
- module Adapters
3
- class Sprockets3
4
- def reset_cache!(environment, filename)
5
- environment.instance_variable_get(:@uris).delete_if { |_, asset| asset.pathname == filename }
6
- environment.instance_variable_get(:@entries).delete(Spritely.directory.to_s)
7
- environment.instance_variable_get(:@stats).delete(filename.to_s)
8
- end
9
- end
10
- end
11
- end
@@ -1,51 +0,0 @@
1
- require 'digest/md5'
2
- require 'spritely/version'
3
-
4
- module Spritely
5
- # `Cache` is responsible for generating and fetching the current sprite cache
6
- # `value. It is used to determine whether to generate a new sprite map or not.
7
- #
8
- # Passing in a set of objects that respond to `cache_key` will generate an MD5
9
- # hash that can then be stored in the image itself.
10
- #
11
- # Fetching an existing cache key actually reads the PNG file itself. It scans
12
- # the chunks of the PNG until it finds the `cache_key` keyword, returning the
13
- # subsequent value found.
14
- class Cache < Struct.new(:filename)
15
- PNG_SIGNATURE_LENGTH = 8 # http://www.w3.org/TR/PNG/#5PNG-file-signature
16
- PNG_INFO_LENGTH = 8 # http://www.w3.org/TR/PNG/#5DataRep
17
- PNG_CRC_LENGTH = 4 # Cyclic Redundancy Check (CRC) byte-length; http://www.w3.org/TR/PNG/#5Chunk-layout
18
-
19
- def self.generate(*objects)
20
- Digest::MD5.hexdigest(VERSION + objects.collect(&:cache_key).join)
21
- end
22
-
23
- def self.busted?(filename, expected_cache_key)
24
- new(filename).key != expected_cache_key
25
- end
26
-
27
- def key
28
- return @key if @key
29
-
30
- File.open(filename) do |file|
31
- file.read(PNG_SIGNATURE_LENGTH) # we first have to read the signature to fast-forward the IO#pos
32
- until file.eof?
33
- each_chunk(file) do |keyword, value|
34
- if keyword == 'cache_key'
35
- return @key = value
36
- break
37
- end
38
- end
39
- end
40
- end
41
- end
42
-
43
- private
44
-
45
- def each_chunk(file, &block)
46
- length = file.read(PNG_INFO_LENGTH).unpack('Na4').first
47
- yield *file.read(length).unpack('Z*a*')
48
- file.read(PNG_CRC_LENGTH)
49
- end
50
- end
51
- end
@@ -1,59 +0,0 @@
1
- require 'active_support/core_ext/hash/deep_merge'
2
- require 'active_support/core_ext/hash/except'
3
- require 'active_support/core_ext/hash/keys'
4
- require 'active_support/core_ext/hash/slice'
5
-
6
- module Spritely
7
- # Provides a simpler method of querying a particular image's options. Rather
8
- # than each `Image` needing to know how to parse the keyword arguments passed
9
- # into the `spritely-map` function, this simplifies and de-duplicates the
10
- # passed option keys.
11
- #
12
- # $application-sprite: spritely-map("application/*.png",
13
- # $arrow-repeat: true,
14
- # $arrow-spacing: 10px,
15
- # $another-image-position: right,
16
- # $spacing: 5px
17
- # );
18
- #
19
- # The options passed in above will be easily accessible via an instance of
20
- # this class.
21
- #
22
- # options['arrow'] => {repeat: true, spacing: 10}
23
- # options['another-image'] => {position: 'right', spacing: 5}
24
- class Options < Struct.new(:hash)
25
- GLOBAL_OPTIONS = ['spacing', 'position']
26
-
27
- def cache_key
28
- stripped_hash.to_s
29
- end
30
-
31
- def inspect
32
- "#<Spritely::Options global_options=#{global_options} options=#{options}>"
33
- end
34
-
35
- def [](key)
36
- options[key.gsub('-', '_')] || global_options
37
- end
38
-
39
- private
40
-
41
- def options
42
- @options ||= stripped_hash.except(*GLOBAL_OPTIONS).inject({}) do |h, (key, value)|
43
- image, _, option = key.rpartition('_')
44
- h[image] ||= global_options.dup
45
- h.deep_merge!(image => {option.to_sym => value})
46
- end
47
- end
48
-
49
- def global_options
50
- @global_options ||= stripped_hash.slice(*GLOBAL_OPTIONS).symbolize_keys!
51
- end
52
-
53
- def stripped_hash
54
- @stripped_hash ||= hash.inject({}) do |h, (key, sass_object)|
55
- h.merge!(key => sass_object.value)
56
- end
57
- end
58
- end
59
- end