sqweeze 0.0.2 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -9,7 +9,8 @@ With a little help from a number of well known open source third party tools, _S
9
9
  * Lossless compresses .PNG, .JPEG and .GIF image assets.
10
10
  * Compresses and concatenates into single files multiple Stylesheets and Javascript files.
11
11
  * Embeds the compressed assets referenced into the stylesheet, using using {Data-uris}[https://developer.mozilla.org/En/The_data_URL_scheme] (for proper browsers and IE8) and {MHTML}[http://en.wikipedia.org/wiki/MHTML] (for IE6-7).
12
- * Finally, it provides some basic functionality to linking the compressed Javascript and CSS files back to the document.
12
+ * Finally, it provides some basic functionality for linking the compressed Javascript and CSS just created to the main HTML document,
13
+ and removing automatically the links pointing to the old uncompressed ones.
13
14
 
14
15
  === Installing it
15
16
 
@@ -18,11 +19,22 @@ On an Ubuntu box, you can install these by running a single command line
18
19
 
19
20
  sudo apt-get install pngcrush gifsicle libjpeg-progs
20
21
 
21
- (Mac OSX users can install the first two as Macports packages. The third (part of libjpeg) instead, has to be
22
- {compiled manually}[http://www.phpied.com/installing-jpegtran-mac-unix-linux]. However, Jpegtrain will not be needed as long as you do not have Jpeg files into your project.)
22
+ Mac OSX users can install the first two as Macports packages. The third (part of libjpeg) instead, has to be
23
+ {compiled manually}[http://www.phpied.com/installing-jpegtran-mac-unix-linux].
23
24
 
24
- Once installed the requirements above, you can install sqweeze, and its ruby dependencies, through ruby gems
25
+ Once installed these, you can install _Sqweeze_ using rubygems
25
26
 
26
27
  gem install sqweeze
27
28
 
28
-
29
+ === Configuration
30
+
31
+ When run for the first time, the script creates a config file in the user's $HOME directory and sets therein the default paths of command line tools such as pngcrush and friends.
32
+
33
+ # file $HOME/.sqweeze.yml
34
+
35
+ bin_paths:
36
+ - pngcrush: /usr/bin/pngcrush
37
+ - jpegtran: /usr/bin/jpegtran
38
+ - gifsicle: /usr/bin/gifsicle
39
+
40
+ If you have installed these anywhere else than in /usr/bin+, you will have to edit this file so that sqweeze can find them.
@@ -2,7 +2,7 @@
2
2
  # rubygems
3
3
  %w[rubygems yui/compressor closure-compiler hpricot].each{|lib| require lib}
4
4
  # ruby standard library
5
- %w[logger singleton yaml base64 open-uri pathname optparse].each{|lib| require lib}
5
+ %w[logger singleton yaml base64 open-uri pathname optparse fileutils].each{|lib| require lib}
6
6
 
7
7
  # add lib sub-directories to load path
8
8
  SQWEEZE_DIR=File.expand_path( File.dirname(File.dirname(__FILE__)))
@@ -77,6 +77,16 @@ op=OptionParser.new do |opts|
77
77
  options[:default_js_compressor] = j.downcase.to_sym
78
78
  end
79
79
 
80
+ opts.separator ""
81
+ opts.separator "Console output:"
82
+
83
+ opts.on('-q',"--quite", "suppress console output ") do |q|
84
+ options[:suppress_warn] = true
85
+ options[:suppress_info] = true
86
+ end
87
+ opts.on('-d',"--debug", "show debug information") do |q|
88
+ options[:suppress_debug] = false
89
+ end
80
90
  end
81
91
 
82
92
  begin
@@ -86,7 +96,6 @@ begin
86
96
  exit(1) }.call unless File.exists?(ARGV[0])
87
97
 
88
98
 
89
- $log=Logger.new( STDERR )
90
99
  cm=ConfManager.instance
91
100
  cm.prepare(ARGV[0], ARGV[1], options)
92
101
 
@@ -1,8 +1,9 @@
1
+ # Turn link to extenrnal stylesheets into inline style elements..
2
+
1
3
  class CssDomCompiler < DOMCompiler
2
-
3
4
  def compile
4
5
  iterate_over('link[@rel="stylesheet"]') do |elm, doc|
5
- next unless elm.has_attribute?('href') and not elm.get_attribute('href').nil?
6
+ next unless elm.has_attribute?('href') and not elm.get_attribute('href').empty?
6
7
 
7
8
  fbody=get_resource(elm.get_attribute('href'))
8
9
 
@@ -11,7 +12,6 @@ class CssDomCompiler < DOMCompiler
11
12
 
12
13
  style_html = "<style type=\"text/css\">\n" + css_cdata(fbody,:compress) + '</style>'
13
14
  doc.search('head').append(style_html)
14
-
15
15
  end
16
16
  end
17
17
 
@@ -82,7 +82,7 @@ class Compressor
82
82
  gsub('%input%',inputpath).
83
83
  gsub('%output%', output_path)
84
84
  system(cl)
85
- $log.debug("run command #{cl}, pid=#{$?.pid} exitstatus=#{$?.exitstatus}")
85
+ notify("run command #{cl}, pid=#{$?.pid} exitstatus=#{$?.exitstatus}", :debug)
86
86
  output_path
87
87
  end
88
88
 
@@ -101,7 +101,10 @@ class Compressor
101
101
  files.each do |path|
102
102
  output_path=process(path,cmd)
103
103
  size_check(path,output_path) unless %w(.css .js).include?( File.extname(output_path))
104
- end
104
+ end
105
+
106
+ # summarise compression stats
107
+ print_summary unless ['.css','.js' ].include?(@input_file_extensions)
105
108
  end
106
109
 
107
110
  # Makes sure that the byteweight of a compressed file is not larger that the original one.
@@ -110,10 +113,10 @@ class Compressor
110
113
  before_size, after_size = byteweight(input_path), byteweight(output_path)
111
114
  # if the compressed file is bigger than the original one, copy the original in the target dir
112
115
  if after_size < before_size
113
- $log.info("compressed #{input_path} to the #{ compression_percentage(before_size,after_size)}% of its original size (from: #{before_size} to #{after_size} bytes ) ")
116
+ notify("compressed #{input_path} to the #{ compression_percentage(before_size,after_size)}% of its original size (from: #{before_size} to #{after_size} bytes ) ", :debug)
114
117
  @byteweight_after+=after_size
115
118
  else
116
- $log.debug("Oppss!!! output(#{after_size}) >= input(#{before_size}), using the original file #{input_path} ")
119
+ notify("output(#{after_size}) >= input(#{before_size}), keeping the original file #{input_path}",:debug)
117
120
  FileUtils.cp(input_path,output_path)
118
121
  @byteweight_after+=before_size
119
122
  end
@@ -124,5 +127,11 @@ class Compressor
124
127
  def concatenate_files
125
128
  @input_files.collect{|f|File.open(f,'r').read}.join("\n")
126
129
  end
130
+
131
+ def print_summary
132
+ notify("Compressed #{ansi_bold(@input_files.size)} #{@input_file_extensions.first.upcase} files".ljust(60) +
133
+ "#{ansi_green(compression_percentage(@byteweight_before,@byteweight_after)+'%')} (was #{ansi_bold(@byteweight_before)}, is #{ansi_bold(@byteweight_after)} bytes now) \n",
134
+ :info) unless @byteweight_before == 0
135
+ end
127
136
 
128
- end
137
+ end
@@ -40,6 +40,9 @@ class CssCompressor < Compressor
40
40
 
41
41
  unless compressed_output.chomp.empty?
42
42
  write_file(compressed_output,"#{@cm.target_dir}/stylesheets.min.css")
43
+
44
+ # set the total byte-weight
45
+ @byteweight_after=byteweight(compressed_output)
43
46
  embed_datauris( compressed_output )
44
47
  embed_mhtml( compressed_output ) if @cm.get_conf(:mhtml_root)
45
48
  end
@@ -7,18 +7,20 @@ class JsCompressor < Compressor
7
7
 
8
8
 
9
9
  def process(input_str,cmd=nil)
10
- unless @cm.get_conf(:default_js_compressor) == :closure
11
- compressor, method = YUI::JavaScriptCompressor.new( :munge => true), :compress
12
- else
10
+ if @cm.get_conf(:default_js_compressor) == :closure
13
11
  compressor, method = Closure::Compiler.new, :compiler
12
+ else
13
+ compressor, method = YUI::JavaScriptCompressor.new( :munge => true), :compress
14
14
  end
15
-
16
15
  fout= (@concatenate_input)? "#{@cm.target_dir}/javascripts.min.js" : @cm.get_target_path(inputpath)
16
+
17
17
 
18
18
  File.open(fout,'w') do |f|
19
19
  f.write(compressor.send( method, input_str))
20
20
  end
21
-
21
+
22
+ # set the total byte-weight
23
+ @byteweight_after=byteweight(fout)
22
24
  fout
23
25
  end
24
26
 
@@ -1,7 +1,8 @@
1
1
  # Main component for handling Sqweeze's file and command-line configuration parameters
2
2
 
3
3
 
4
- class ConfManager
4
+ class ConfManager
5
+ include SqweezeUtils
5
6
  include Singleton
6
7
  # Class constructor.
7
8
  #
@@ -17,6 +18,10 @@ class ConfManager
17
18
  @files=[]
18
19
 
19
20
  @conf={
21
+ :suppress_info => false,
22
+ :suppress_debug => true,
23
+ :suppress_warn => false,
24
+ :suppress_error => false,
20
25
  :bin_paths => {},
21
26
  :dom_documents => [],
22
27
  :include_files => [],
@@ -62,7 +67,8 @@ class ConfManager
62
67
  else
63
68
  target
64
69
  end
65
-
70
+ copy_source
71
+
66
72
  write_globalconf
67
73
  # Parses the global configuration file in $HOME.
68
74
  parse_conf
@@ -76,7 +82,7 @@ class ConfManager
76
82
  # Creates the list of the files in the project directory.
77
83
 
78
84
  list_files
79
- copy_source
85
+
80
86
  end
81
87
 
82
88
  # Copies the source into the target directory.
@@ -123,7 +129,7 @@ class ConfManager
123
129
  # Parses configuration files and sets user-defined file inclusion/exlusion patterns.
124
130
 
125
131
  def parse_conf(configfile="#{ENV['HOME']}/#{@conf_filename}")
126
- $log.debug("Parsing configuration file: #{configfile}")
132
+ notify("Parsing configuration file: #{configfile}",:debug)
127
133
  conf=YAML::load_file( configfile )
128
134
 
129
135
  bin_paths={}
@@ -150,7 +156,8 @@ class ConfManager
150
156
 
151
157
  # others..
152
158
  %w(include exclude).each {|k| set_conf(k, conf[k].to_a)}
153
- %w(optimisation_strategy append_scripts_to).each{|k| set_conf(k,conf[k]) if conf[k]}
159
+ %w(optimisation_strategy append_scripts_to
160
+ suppress_messagess suppress_debug suppress_warnings).each{|k| set_conf(k,conf[k]) if conf[k]}
154
161
  end
155
162
 
156
163
  # Explodes the inclusion/exclusion patterns provided by the user into a list, and populates the @files attribute.
@@ -170,12 +177,11 @@ class ConfManager
170
177
  exclude_files=get_files(get_conf(:exclude))
171
178
 
172
179
 
173
- $log.debug("Excluding #{exclude_files.size} file/s from user list")
180
+ notify("Excluding #{exclude_files.size} file/s from user list", :debug)
174
181
  @files -= exclude_files
175
- $log.info("#{@files.size} file/s found")
182
+ notify("#{@files.size} file/s found", :info)
176
183
  end
177
184
 
178
-
179
185
  # Get all the files matching an array of patterns
180
186
  #
181
187
  # (Any file expansion patterns accepted ruby's Dir[] method can be used).
@@ -29,7 +29,7 @@ class DOMCompiler
29
29
  write_file(doc.innerHTML, path )
30
30
  end
31
31
  else
32
- $log.error("DOMCompiler cannot parse #{path}")
32
+ notify("DOMCompiler cannot parse #{path}",:error)
33
33
  end
34
34
  end
35
35
  end
@@ -12,6 +12,7 @@ module SqweezeUtils
12
12
  '.otf' => 'font/opentype'
13
13
  }
14
14
 
15
+
15
16
  def write_file(fbody,fpath)
16
17
  File.open(fpath,'w') do |f|
17
18
  f.write(fbody)
@@ -75,12 +76,50 @@ module SqweezeUtils
75
76
  # TODO: rewrite this! (it sucks..)
76
77
 
77
78
  def find_file_in_targetdir(endpath)
78
- pattern= [@cm.target_dir,"**/*#{File.extname(endpath)}"].join('/')
79
+ pattern= [get_confManager.target_dir,"**/*#{File.extname(endpath)}"].join('/')
79
80
  #puts "searcing for files in #{pattern} ending with #{endpath}"
80
81
  Dir[pattern].find{|path| path =~ Regexp.new("#{endpath}$")}
81
82
  end
82
83
 
83
-
84
-
85
-
86
- end
84
+
85
+ # Prints to STDERR and adds a log entry.
86
+ #
87
+ # Log entries can be of four types:
88
+ #
89
+ # * info
90
+ # * warning
91
+ # * error
92
+ # * debug
93
+ #
94
+
95
+ def notify( message, log_level=:info)
96
+ raise "Log level #{log_level} does not exist!" unless [:info,:warn,:error,:debug].include?(log_level.to_sym)
97
+ cm=get_confManager
98
+
99
+ $log=Logger.new( "#{cm.target_dir}/sqweeze.log" ) unless $log
100
+ $log.send(log_level, ansi_nocolour(message))
101
+
102
+ puts message unless cm.get_conf( "suppress_#{log_level}".to_sym)
103
+ end
104
+
105
+ def ansi_bold(bold)
106
+ "\033[1m#{bold}\033[0m"
107
+ end
108
+ def ansi_red(red)
109
+ "\033[31m#{red}\033[0m"
110
+ end
111
+ def ansi_green(green)
112
+ "\033[32m#{green}\033[0m"
113
+ end
114
+ def ansi_yellow(yellow)
115
+ "\033[33m#{yellow}\033[0m"
116
+ end
117
+ def ansi_nocolour(text)
118
+ text.gsub(/\033\[[0-9]{1,2}/,'')
119
+ end
120
+
121
+ private
122
+ def get_confManager
123
+ (@cm) ? @cm : self
124
+ end
125
+ end
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{sqweeze}
5
- s.version = "0.0.2"
5
+ s.version = "0.0.4"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Andrea Fiore"]
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 2
9
- version: 0.0.2
8
+ - 4
9
+ version: 0.0.4
10
10
  platform: ruby
11
11
  authors:
12
12
  - Andrea Fiore