polymer 1.0.0.beta.3 → 1.0.0.beta.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/History.md CHANGED
@@ -21,8 +21,15 @@ v1.0.0 / HEAD (Unreleased)
21
21
  +polymer("sprite_name/source_name")
22
22
 
23
23
  Mixins still permit you to supply an optional x-offset and y-offset as
24
- the second and third parameters. the "polymer-pos()" mixin is also
25
- available as an alternative to the old "sprite-name-pos()" mixins.
24
+ the second and third parameters. the "polymer-position()" mixin is
25
+ also available as an alternative to the old "sprite-name-pos()"
26
+ mixins.
27
+
28
+ * The "polymer-pos" Sass mixin, which sets only the background position
29
+ of a source, without including the background-image property, has been
30
+ renamed to "polymer-position".
31
+
32
+ * The "generate" command is now "bond": `$ polymer bond`
26
33
 
27
34
  * A new "position" command shows information about a source within a
28
35
  sprite, and provides useful CSS for use when building your own
data/lib/polymer/cache.rb CHANGED
@@ -4,6 +4,16 @@ module Polymer
4
4
  # the cache was last generated.
5
5
  class Cache
6
6
 
7
+ # The highest cache version supported by Cache.
8
+ CACHE_VERSION = 3
9
+
10
+ # The default cache.
11
+ EMPTY_CACHE = {
12
+ :cache_version => CACHE_VERSION,
13
+ :sprites => {},
14
+ :paths => {}
15
+ }
16
+
7
17
  # Returns the path to the cache file.
8
18
  #
9
19
  # @return [Pathname]
@@ -24,63 +34,71 @@ module Polymer
24
34
 
25
35
  if @path and @path.file?
26
36
  @cache = YAML.load_file @path
37
+ @cache = EMPTY_CACHE if @cache[:cache_version] < CACHE_VERSION
27
38
  else
28
- @cache = { :cache_version => 2, :sprites => {} }
39
+ @cache = EMPTY_CACHE
29
40
  end
30
41
  end
31
42
 
32
43
  # Checks whether the given +sprite+ is different to the cached version.
33
44
  #
34
- # @param [Polymer::Sprite] sprite
35
- # The sprite whose "freshness" is to be checked.
45
+ # @param [Polymer::Sprite] thing
46
+ # The sprite or path whose "freshness" is to be checked.
36
47
  #
37
48
  # @return [Boolean]
38
49
  #
39
- def stale?(sprite)
40
- not fresh?(sprite)
50
+ def stale?(thing)
51
+ not fresh?(thing)
41
52
  end
42
53
 
43
- # Checks whether the given +sprite+ is identical to the cached version.
54
+ # Checks whether the given +thing+ is identical to the cached version.
44
55
  #
45
- # @param [Polymer::Sprite] sprite
46
- # The sprite whose "freshness" is to be checked.
56
+ # @param [Polymer::Sprite, Pathname] thing
57
+ # The sprite or path whose "freshness" is to be checked.
47
58
  #
48
59
  # @return [Boolean]
49
60
  #
50
- def fresh?(sprite)
51
- sprite.save_path.file? and
52
- @cache[:sprites].has_key?(sprite.name) and
53
- @cache[:sprites][sprite.name] == sprite.digest
61
+ def fresh?(thing)
62
+ return false if thing.is_a?(Sprite) and not thing.save_path.file?
63
+ return false if thing.is_a?(Pathname) and not thing.cleanpath.file?
64
+
65
+ @cache[section(thing)][key(thing)] == digest(thing)
54
66
  end
55
67
 
56
- # Updates the cached value of +sprite+.
68
+ # Updates the cached value of +thing+.
57
69
  #
58
- # @param [Polymer::Sprite] sprite
59
- # The sprite whose digest is to be stored in the cache.
70
+ # @param [Polymer::Sprite, Pathname] thing
71
+ # The sprite or Pathname whose digest is to be stored in the cache.
60
72
  #
61
- def set(sprite)
62
- @cache[:sprites][sprite.name] = sprite.digest
73
+ def set(thing)
74
+ @cache[section(thing)][key(thing)] = digest(thing)
63
75
  end
64
76
 
65
77
  # Removes a +sprite+'s cached values.
66
78
  #
67
- # @param [Polymer::Sprite] sprite
68
- # The sprite whose digest is to be removed from the cache.
79
+ # @param [Polymer::Sprite, Pathname] thing
80
+ # The sprite or Pathname whose digest is to be removed from the cache.
69
81
  #
70
- def remove(sprite)
71
- @cache[:sprites].delete(sprite.name)
82
+ def remove(thing)
83
+ @cache[section(thing)].delete(key(thing))
72
84
  end
73
85
 
74
- # Removes all sprite cache entries, except those in +retain+.
86
+ # Removes any sprites no longer present in a project, and any cached
87
+ # images which cannot be located.
75
88
  #
76
- # @param [Array<Polymer::Cache>] retain
77
- # An array of cache entries which are _not_ to be removed.
89
+ # @param [Flexo::Project] project
78
90
  #
79
- def remove_all_except(retain)
80
- names = retain.map { |sprite| sprite.name }
91
+ def clean!(project)
92
+ return false unless @path
93
+
94
+ @cache[:paths].delete_if do |key, _|
95
+ not @path.dirname.join(key).file?
96
+ end
97
+
98
+ sprite_keys = project.sprites.map { |sprite| key(sprite) }
81
99
 
82
100
  @cache[:sprites].delete_if do |key, _|
83
- not names.include?(key)
101
+ not sprite_keys.include?(key)
84
102
  end
85
103
  end
86
104
 
@@ -102,5 +120,41 @@ module Polymer
102
120
  true
103
121
  end
104
122
 
123
+ #######
124
+ private
125
+ #######
126
+
127
+ # @return [Symbol]
128
+ # Returns the cache section (:sprites or :paths) for the given object.
129
+ def section(thing)
130
+ thing.is_a?(Pathname) ? :paths : :sprites
131
+ end
132
+
133
+ # @return [String]
134
+ # Returns the key which represents the given Sprite or Pathname in the
135
+ # cache file.
136
+ def key(thing)
137
+ if thing.is_a?(Pathname)
138
+ if @path
139
+ # Store Pathnames as relative to the cache.
140
+ Pathname.pwd.join(thing).relative_path_from(@path.dirname).to_s
141
+ else
142
+ thing.cleanpath.to_s
143
+ end
144
+ else
145
+ thing.name.to_s
146
+ end
147
+ end
148
+
149
+ # @return [String]
150
+ # Returns the SHA256 digest of the given Pathname or Sprite.
151
+ def digest(thing)
152
+ if thing.is_a?(Pathname)
153
+ Digest::SHA256.file(thing.cleanpath).to_s
154
+ else
155
+ thing.digest
156
+ end
157
+ end
158
+
105
159
  end # Cache
106
160
  end # Polymer
data/lib/polymer/cli.rb CHANGED
@@ -61,18 +61,11 @@ module Polymer
61
61
  say_status('generated', sprite.name, :green)
62
62
 
63
63
  unless options[:fast]
64
- say " optimising #{sprite.name} ... "
65
- before = sprite.save_path.size
64
+ run_optimisation(sprite.save_path, sprite.name)
66
65
 
67
- reduction = Polymer::Optimisation.optimise_file(sprite.save_path)
68
-
69
- if reduction > 0
70
- saved = '- saved %.2fkb (%.1f' %
71
- [reduction.to_f / 1024, (reduction.to_f / before) * 100]
72
- say_status "\r\e[0K optimised", "#{sprite.name} #{saved}%)", :green
73
- else
74
- print "\r\e[0K"
75
- end
66
+ # Store the cached image so that running polymer-optimise
67
+ # skips this image.
68
+ project.cache.set(sprite.save_path.relative_path_from(project.root))
76
69
  end
77
70
  end
78
71
 
@@ -91,7 +84,7 @@ module Polymer
91
84
  end
92
85
 
93
86
  # Clean up the cache, removing sprites which no longer exist.
94
- project.cache.remove_all_except(project.sprites)
87
+ project.cache.clean!(project)
95
88
 
96
89
  # Finish by writing the new cache.
97
90
  project.cache.write
@@ -108,22 +101,22 @@ module Polymer
108
101
  def help(command = nil)
109
102
  page_map = {
110
103
  # Main manual page.
111
- nil => 'polymer.1',
104
+ nil => 'polymer.1',
112
105
  'polymer' => 'polymer.1',
113
106
 
114
107
  # Sub-commands.
115
- 'init' => 'polymer-init.1',
116
- 'bond' => 'polymer-bond.1',
117
- 'optimise' => 'polymer-optimise.1',
118
- 'optimize' => 'polymer-optimise.1',
119
- 'position' => 'polymer-position.1',
108
+ 'init' => 'polymer-init.1',
109
+ 'bond' => 'polymer-bond.1',
110
+ 'optimise' => 'polymer-optimise.1',
111
+ 'optimize' => 'polymer-optimise.1',
112
+ 'position' => 'polymer-position.1',
120
113
 
121
114
  # Configuration format.
122
115
  'polymer(5)' => 'polymer.5',
123
116
  'polymer.5' => 'polymer.5',
124
117
  '.polymer' => 'polymer.5',
125
118
  'polymer.rb' => 'polymer.5',
126
- 'config' => 'polymer.5'
119
+ 'config' => 'polymer.5'
127
120
  }
128
121
 
129
122
  if page_map.has_key?(command)
@@ -207,33 +200,42 @@ module Polymer
207
200
  paths are relative to the current working directory.
208
201
  DESC
209
202
 
203
+ method_option :force, :type => :boolean, :default => false,
204
+ :desc => "Re-optimise images which haven't changed since the last " \
205
+ "time they were optimised; has no effect unless in a " \
206
+ "project directory."
207
+
210
208
  map 'optimize' => :optimise
211
209
 
212
210
  def optimise(*paths)
213
211
  dir = Pathname.new(Dir.pwd)
214
- paths = paths.map { |path| dir + path }
215
212
 
216
- paths.each do |path|
217
- fpath = path.relative_path_from(dir).to_s
213
+ # Try to use the project cache.
214
+ begin
215
+ project = find_project
216
+ cache = project.cache
217
+ rescue Polymer::MissingProject
218
+ project, cache = nil, Polymer::Cache.new
219
+ end
218
220
 
219
- # Ensure the file is a PNG.
220
- unless path.to_s =~ /\.png/
221
- say_status 'skipped', "#{fpath} - not a PNG", :yellow
222
- next
223
- end
221
+ paths = paths.map do |path|
222
+ path = dir + path
223
+ # If given a directory, append a glob which recursively looks
224
+ # for PNG files, otherwise use the path literally.
225
+ path.directory? ? Pathname.glob(path + '**' + '*.png') : path.cleanpath
226
+ end.flatten
224
227
 
225
- before = path.size
226
- say " optimising #{fpath} "
227
- reduction = Polymer::Optimisation.optimise_file(path)
228
+ paths.each do |path|
229
+ relative = path.relative_path_from(dir)
228
230
 
229
- if reduction > 0
230
- saved = '- saved %.2fkb (%.1f' %
231
- [reduction.to_f / 1024, (reduction.to_f / before) * 100]
232
- say_status "\r\e[0K optimised", "#{fpath} #{saved}%)", :green
233
- else
234
- say_status "\r\e[0K optimised", "#{fpath} - no savings", :green
231
+ if options[:force] or cache.stale?(relative)
232
+ run_optimisation(path, relative)
233
+ cache.set(relative)
235
234
  end
236
235
  end
236
+
237
+ cache.clean!(project) if project
238
+ cache.write
237
239
  end
238
240
 
239
241
  # --- position -----------------------------------------------------------
@@ -301,7 +303,7 @@ module Polymer
301
303
  # @return [Polymer::Project]
302
304
  #
303
305
  def find_project!
304
- Polymer::DSL.load Polymer::Project.find_config(Dir.pwd)
306
+ find_project
305
307
  rescue Polymer::MissingProject
306
308
  say <<-ERROR.compress_lines, :red
307
309
  Couldn't find a Polymer project in the current directory, or any of
@@ -311,6 +313,43 @@ module Polymer
311
313
  exit 1
312
314
  end
313
315
 
316
+ # Trys to find a project, and raises if one is not available.
317
+ #
318
+ # @return [Polymer::Project]
319
+ #
320
+ def find_project
321
+ Polymer::DSL.load Polymer::Project.find_config(Dir.pwd)
322
+ end
323
+
324
+ # Runs optimisation on a given +path+.
325
+ #
326
+ # @param [Pathname] path
327
+ # Path to the file to optimise.
328
+ # @param [String] name
329
+ # The name of the "thing" being optimised. This is the value shown to
330
+ # the user, and allows removal of the path prefix, or use of a sprite
331
+ # name.
332
+ #
333
+ def run_optimisation(path, name)
334
+ # Ensure the file is a PNG.
335
+ unless path.to_s =~ /\.png/
336
+ say_status 'skipped', "#{name} - not a PNG", :yellow
337
+ return
338
+ end
339
+
340
+ before = path.size
341
+ say " optimising #{name} "
342
+ reduction = Polymer::Optimisation.optimise_file(path)
343
+
344
+ if reduction > 0
345
+ saved = '- saved %.2fkb (%.1f' %
346
+ [reduction.to_f / 1024, (reduction.to_f / before) * 100]
347
+ say_status "\r\e[0K optimised", "#{name} #{saved}%)", :green
348
+ else
349
+ say_status "\r\e[0K optimised", "#{name} - no savings", :green
350
+ end
351
+ end
352
+
314
353
  # Returns if the current machine has groff available.
315
354
  #
316
355
  # @return [Boolean]
@@ -2,7 +2,7 @@
2
2
  .\" http://github.com/rtomayko/ronn/tree/0.7.3
3
3
  .ad l
4
4
  .
5
- .TH "POLYMER\-BOND" "1" "September 2010" "POLYMER 1.0.0.BETA.3" "Polymer Manual"
5
+ .TH "POLYMER\-BOND" "1" "September 2010" "POLYMER 1.0.0.BETA.4" "Polymer Manual"
6
6
  .
7
7
  .SH "NAME"
8
8
  \fBpolymer\-bond\fR \- Create sprite images defined in your \.polymer file
@@ -63,4 +63,4 @@ SEE ALSO
63
63
 
64
64
 
65
65
 
66
- POLYMER 1.0.0.BETA.3 September 2010 POLYMER-BOND(1)
66
+ POLYMER 1.0.0.BETA.4 September 2010 POLYMER-BOND(1)
@@ -2,7 +2,7 @@
2
2
  .\" http://github.com/rtomayko/ronn/tree/0.7.3
3
3
  .ad l
4
4
  .
5
- .TH "POLYMER\-INIT" "1" "September 2010" "POLYMER 1.0.0.BETA.3" "Polymer Manual"
5
+ .TH "POLYMER\-INIT" "1" "September 2010" "POLYMER 1.0.0.BETA.4" "Polymer Manual"
6
6
  .
7
7
  .SH "NAME"
8
8
  \fBpolymer\-init\fR \- Create a new Polymer project in the current directory
@@ -39,4 +39,4 @@ OPTIONS
39
39
 
40
40
 
41
41
 
42
- POLYMER 1.0.0.BETA.3 September 2010 POLYMER-INIT(1)
42
+ POLYMER 1.0.0.BETA.4 September 2010 POLYMER-INIT(1)
@@ -2,7 +2,7 @@
2
2
  .\" http://github.com/rtomayko/ronn/tree/0.7.3
3
3
  .ad l
4
4
  .
5
- .TH "POLYMER\-OPTIMISE" "1" "September 2010" "POLYMER 1.0.0.BETA.3" "Polymer Manual"
5
+ .TH "POLYMER\-OPTIMISE" "1" "September 2010" "POLYMER 1.0.0.BETA.4" "Polymer Manual"
6
6
  .
7
7
  .SH "NAME"
8
8
  \fBpolymer\-optimise\fR \- Optimise PNG images
@@ -10,6 +10,12 @@
10
10
  .SH "SYNOPSIS"
11
11
  \fBpolymer optimise\fR PATH [PATH [PATH \.\.\.]]
12
12
  .
13
+ .SH "OPTIONS"
14
+ .
15
+ .TP
16
+ \fB\-\-force\fR
17
+ Something something something dark side: re\-optimise images even if they haven\'t changed since last time they were optimised\.
18
+ .
13
19
  .SH "DESCRIPTION"
14
20
  Optimises the PNG images at \fIPATH\fR\. Does not require you to be in a Polymer project directory\.
15
21
  .
@@ -8,6 +8,11 @@ NAME
8
8
  SYNOPSIS
9
9
  polymer optimise PATH [PATH [PATH ...]]
10
10
 
11
+ OPTIONS
12
+ --force
13
+ Something something something dark side: re-optimise images even
14
+ if they haven't changed since last time they were optimised.
15
+
11
16
  DESCRIPTION
12
17
  Optimises the PNG images at PATH. Does not require you to be in a Poly-
13
18
  mer project directory.
@@ -22,4 +27,4 @@ DESCRIPTION
22
27
 
23
28
 
24
29
 
25
- POLYMER 1.0.0.BETA.3 September 2010 POLYMER-OPTIMISE(1)
30
+ POLYMER 1.0.0.BETA.4 September 2010 POLYMER-OPTIMISE(1)
@@ -2,7 +2,7 @@
2
2
  .\" http://github.com/rtomayko/ronn/tree/0.7.3
3
3
  .ad l
4
4
  .
5
- .TH "POLYMER\-POSITION" "1" "September 2010" "POLYMER 1.0.0.BETA.3" "Polymer Manual"
5
+ .TH "POLYMER\-POSITION" "1" "September 2010" "POLYMER 1.0.0.BETA.4" "Polymer Manual"
6
6
  .
7
7
  .SH "NAME"
8
8
  \fBpolymer\-position\fR \- Information about your sprite sources
@@ -39,4 +39,4 @@ DESCRIPTION
39
39
 
40
40
 
41
41
 
42
- POLYMER 1.0.0.BETA.3 September 2010 POLYMER-POSITION(1)
42
+ POLYMER 1.0.0.BETA.4 September 2010 POLYMER-POSITION(1)
@@ -2,7 +2,7 @@
2
2
  .\" http://github.com/rtomayko/ronn/tree/0.7.3
3
3
  .ad l
4
4
  .
5
- .TH "POLYMER" "1" "September 2010" "POLYMER 1.0.0.BETA.3" "Polymer Manual"
5
+ .TH "POLYMER" "1" "September 2010" "POLYMER 1.0.0.BETA.4" "Polymer Manual"
6
6
  .
7
7
  .SH "NAME"
8
8
  \fBpolymer\fR \- Image spriting for web applications
@@ -57,4 +57,4 @@ SEE ALSO
57
57
 
58
58
 
59
59
 
60
- POLYMER 1.0.0.BETA.3 September 2010 POLYMER(1)
60
+ POLYMER 1.0.0.BETA.4 September 2010 POLYMER(1)
@@ -2,7 +2,7 @@
2
2
  .\" http://github.com/rtomayko/ronn/tree/0.7.3
3
3
  .ad l
4
4
  .
5
- .TH "\.POLYMER" "5" "September 2010" "POLYMER 1.0.0.BETA.3" "Polymer Manual"
5
+ .TH "\.POLYMER" "5" "September 2010" "POLYMER 1.0.0.BETA.4" "Polymer Manual"
6
6
  .
7
7
  .SH "NAME"
8
8
  \fB\.polymer\fR \- a format for describing image sprites and their sources
@@ -88,7 +88,7 @@ sprites "sources/:name/*" => "sprites/:name\.png"
88
88
  .IP "" 0
89
89
  .
90
90
  .P
91
- In this case, Polymer will look inside the "sources/" directory for sub\-directories\. The contents of sub\-directory will be used to create individual sprites where the final sprite name is the same as the directory name\. For example, given the following directory structure\.\.\.
91
+ In this case, Polymer will look inside the "sources/" directory for sub\-directories\. The contents of each sub\-directory will be used to create individual sprites where the final sprite name is the same as the directory name\. For example, given the following directory structure\.\.\.
92
92
  .
93
93
  .IP "" 4
94
94
  .
@@ -126,5 +126,8 @@ sprite "path/to/sources/*" => "path/to/sprite\.png",
126
126
  .P
127
127
  Each configuration option is prefixed with a colon rather than "config\.", is separated from the value with " => ", and all but the final option should be followed with a comma\.
128
128
  .
129
+ .P
130
+ You may use \fBsprite\fR as many times as you need\.
131
+ .
129
132
  .SH "SEE ALSO"
130
133
  polymer(1), polymer\-init(1)
@@ -101,9 +101,10 @@ DEFINING SPRITES
101
101
 
102
102
 
103
103
  In this case, Polymer will look inside the "sources/" directory for
104
- sub-directories. The contents of sub-directory will be used to create
105
- individual sprites where the final sprite name is the same as the
106
- directory name. For example, given the following directory structure...
104
+ sub-directories. The contents of each sub-directory will be used to
105
+ create individual sprites where the final sprite name is the same as
106
+ the directory name. For example, given the following directory struc-
107
+ ture...
107
108
 
108
109
 
109
110
 
@@ -137,9 +138,11 @@ DEFINING SPRITES
137
138
  fig.", is separated from the value with " => ", and all but the final
138
139
  option should be followed with a comma.
139
140
 
141
+ You may use sprite as many times as you need.
142
+
140
143
  SEE ALSO
141
144
  polymer(1), polymer-init(1)
142
145
 
143
146
 
144
147
 
145
- POLYMER 1.0.0.BETA.3 September 2010 .POLYMER(5)
148
+ POLYMER 1.0.0.BETA.4 September 2010 .POLYMER(5)
@@ -15,7 +15,7 @@
15
15
  <% end %>
16
16
  <% end %>
17
17
 
18
- @mixin polymer-pos($source, $x-offset: 0px, $y-offset: 0px)
18
+ @mixin polymer-position($source, $x-offset: 0px, $y-offset: 0px)
19
19
  <% project.sprites.each_with_index do |sprite, sprite_index| %>
20
20
  <% sprite.sources.each_with_index do |source, source_index| %>
21
21
  <% if sprite_index == 0 and source_index == 0 %>
@@ -1,4 +1,4 @@
1
1
  module Polymer
2
2
  # The current version of the Polymer library.
3
- VERSION = '1.0.0.beta.3'.freeze
3
+ VERSION = '1.0.0.beta.4'.freeze
4
4
  end
data/polymer.gemspec CHANGED
@@ -8,8 +8,8 @@ Gem::Specification.new do |s|
8
8
  # rake task. It it completely safe to edit them, but using the rake task
9
9
  # is easier.
10
10
  s.name = 'polymer'
11
- s.version = '1.0.0.beta.3'
12
- s.date = '2010-09-24'
11
+ s.version = '1.0.0.beta.4'
12
+ s.date = '2010-09-29'
13
13
  s.rubyforge_project = 'polymer'
14
14
 
15
15
  # You may safely edit the section below.
metadata CHANGED
@@ -7,8 +7,8 @@ version: !ruby/object:Gem::Version
7
7
  - 0
8
8
  - 0
9
9
  - beta
10
- - 3
11
- version: 1.0.0.beta.3
10
+ - 4
11
+ version: 1.0.0.beta.4
12
12
  platform: ruby
13
13
  authors:
14
14
  - Anthony Williams
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2010-09-24 00:00:00 +01:00
19
+ date: 2010-09-29 00:00:00 +01:00
20
20
  default_executable:
21
21
  dependencies:
22
22
  - !ruby/object:Gem::Dependency