compressit 1.0.4 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,31 +3,41 @@ module Compressit
3
3
  class << self
4
4
 
5
5
  def compressit(options)
6
- @folder, @version, @ext = options[:folder], options[:version], options[:ext]
7
-
8
- @java, @yuicompressor = '/usr/bin/java', File.expand_path(File.dirname(__FILE__) + './../yuicompressor-2.4.6.jar')
9
-
10
- Dir.exists?("#{@folder}/compressed") ? true : Dir.mkdir("#{@folder}/compressed")
11
- @destination_path = Dir.open("#{@folder}/compressed")
12
-
13
- @files_to_compress, @compressed = Dir.glob("#{@folder}/**/*#{@ext}"), "compressed-#{@version}#{@ext}"
14
-
6
+ java, yuicompressor = '/usr/bin/java', File.expand_path(File.dirname(__FILE__) + './../yuicompressor-2.4.6.jar')
7
+
8
+ case File.ftype(options[:data])
9
+ when 'file'
10
+ destination_path = Dir.open(File.dirname(options[:data]))
11
+ files_to_compress, compressed = Array[options[:data]], "#{File.basename(options[:data], ".#{options[:ext]}")}.min.#{options[:ext]}"
12
+ when 'directory'
13
+ Dir.exists?("#{options[:data]}/compressed") ? true : Dir.mkdir("#{options[:data]}/compressed")
14
+ destination_path = Dir.open("#{options[:data]}/compressed")
15
+ files_to_compress, compressed = Dir.glob("#{options[:data]}/**/*#{options[:ext]}"), "#{options[:version]}.min.#{options[:ext]}"
16
+ else
17
+ puts "Oops!"
18
+ exit
19
+ end
20
+
15
21
  # remove compressed file if the version hasn't been updated to avoid duplicate compression
16
- `rm -f #{File.path(@destination_path)}/#{@compressed}` if File.exists?("#{File.path(@destination_path)}/#{@compressed}")
22
+ `rm -f #{File.path(destination_path)}/#{compressed}` if File.exists?("#{File.path(destination_path)}/#{compressed}")
23
+
24
+ puts "Compressing #{options[:ext]} file(s) into '#{File.path(destination_path)}/#{compressed}'"
17
25
 
18
- # compress each file in @files_to_compress and save the compressed file to @destination_path
26
+ # compress each file in files_to_compress and save the compressed file to destination_path
19
27
  begin
20
- @files_to_compress.each do |file|
28
+ puts "----- FILES: #{files_to_compress}"
29
+ files_to_compress.each do |file|
30
+ puts "----- FILE: #{file}"
21
31
  unless File.path(file).include?('compressed')
22
- `#{@java} -jar #{@yuicompressor} #{file} >> #{File.path(@destination_path)}/#{@compressed}`
32
+ `#{java} -jar #{yuicompressor} #{file} >> #{File.path(destination_path)}/#{compressed}`
23
33
  puts "Added: #{File.basename(file)}... \n"
24
34
  end
25
35
  end
26
36
 
27
37
  # confirm compression and show destination path where file can be found
28
- puts "Complete! Compressed #{@ext} file '#{@compressed}', can be found in '#{File.path(@destination_path)}'"
38
+ puts "Complete! Compressed #{options[:ext]} file '#{compressed}', can be found in '#{File.dirname(destination_path)}'"
29
39
  rescue
30
- puts "Hmm... looks like this folder doesn't contain any #{@ext} files."
40
+ puts "Hmm... looks like this folder doesn't contain any #{options[:ext]} files."
31
41
  end
32
42
  end
33
43
 
@@ -1,3 +1,3 @@
1
1
  module Compressit
2
- VERSION = "1.0.4"
2
+ VERSION = "1.1.0"
3
3
  end
data/lib/compressit.rb CHANGED
@@ -7,40 +7,46 @@ module Compressit
7
7
 
8
8
  def run(arguments)
9
9
  unless arguments.empty? || !arguments[0].include?('-')
10
- options = Hash.new
11
-
12
10
  optparser = OptionParser.new do|opts|
13
-
14
- opts.banner = "Usage: compressit [option] [FOLDER] [VERSION]"
11
+ opts.banner = "Usage: compressit -command [FILE/DIR]"
15
12
 
16
13
  opts.on('-h', '--help', 'Display this help') do
17
14
  puts optparser
15
+ exit
18
16
  end
19
17
  opts.on('-v', '--version', 'Display current gem version') do
20
- puts "Currently using version: #{VERSION}"
18
+ puts "Compressit-#{VERSION}"
21
19
  end
22
- opts.on('-c', '--css FOLDER VERSION', 'Compress css files from [FOLDER] into [FOLDER]/compressed') do
23
- puts "Compressing css files from '#{arguments[0]}' into '#{arguments[0]}/compressed/compressed-#{arguments[1]}.css'"
24
- options[:folder], options[:version], options[:ext] = arguments[0], arguments[1], '.css'
25
- Compressit::Base.compressit(options)
20
+ opts.on('-f', '--file FILE', 'Compress file in place') do |file|
21
+ prepare(file)
26
22
  end
27
- opts.on('-j', '--js FOLDER VERSION', 'Compress javascript files from [FOLDER] into [FOLDER]/compressed') do
28
- puts "Compressing javascript files from '#{arguments[0]}' into '#{arguments[0]}/compressed/compressed-#{arguments[1]}.js'"
29
- options[:folder], options[:version], options[:ext] = arguments[0], arguments[1], '.js'
30
- Compressit::Base.compressit(options)
23
+ opts.on('-F', '--files DIR', 'Compress files from [DIR] into [DIR]/compressed') do |dir|
24
+ prepare(dir)
31
25
  end
32
26
  end
33
27
 
34
28
  begin optparser.parse!(arguments)
35
- rescue OptionParser::InvalidOption => error
36
- puts "#{error}, try one of this: "
29
+ rescue OptionParser::ParseError => error
30
+ puts "#{error}"
37
31
  puts optparser
38
- exit 1
32
+ exit
39
33
  end
40
34
  else
41
35
  puts `compressit -h`
42
36
  end
43
37
  end
44
-
38
+
39
+ def prepare(data)
40
+ options = Hash[:data, data]
41
+
42
+ puts "Specify a version (ex. x.x.x):"
43
+ options[:version] = gets.strip
44
+
45
+ puts "css/js:"
46
+ options[:ext] = gets.strip
47
+
48
+ Compressit::Base.compressit(options)
49
+ end
50
+
45
51
  end
46
52
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: compressit
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.4
4
+ version: 1.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2011-09-09 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &70181998155980 !ruby/object:Gem::Requirement
16
+ requirement: &70221588443380 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,7 +21,7 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70181998155980
24
+ version_requirements: *70221588443380
25
25
  description: Compressit uses the yuicompressor-2.4.6.jar java file created by Yahoo
26
26
  to compress all of your .css and .js files into single compressed files respectively.
27
27
  This not only reduces the size of your files, but also results in less http requests