assetify 0.2.2 → 0.3.0

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/Gemfile CHANGED
@@ -1,7 +1,6 @@
1
1
  source "http://rubygems.org"
2
- # Add dependencies required to use your gem here.
3
- # Example:
4
- # gem "activesupport", ">= 2.3.5"
2
+
3
+ # gem "execjs"
5
4
 
6
5
  # Add dependencies to develop your gem here.
7
6
  # Include everything needed to run rake, tests, features, etc.
data/README.md CHANGED
@@ -24,14 +24,23 @@ This will create a Jsfile if not found.
24
24
  Jsfile
25
25
  ======
26
26
 
27
- Just like a Gemfile, but you choose the filetype before:
27
+ Just like a Gemfile, but you choose the filetype (or extension) before:
28
28
 
29
29
  type "name", "url", <"version"> or <:options>
30
30
 
31
31
  js "jquery", "http://code.jquery.com/jquery-{VERSION}.min.js", "1.6"
32
32
  js "tipsy", "https://github.com/jaz303/tipsy/.../jquery.tipsy.js"
33
33
 
34
- Now just run assetify to make sure everything is installed/up-to-date.
34
+ Stylesheets:
35
+
36
+ css "tipsy", "https://github.com/jaz303/tipsy/.../jquery.tipsy.css"
37
+
38
+ Any file:
39
+
40
+ mp3 "alert", "http://link/to/audio"
41
+
42
+
43
+ Now just run `assetify` to make sure everything is installed/up-to-date.
35
44
 
36
45
 
37
46
  Groups
@@ -47,6 +56,15 @@ in handy:
47
56
 
48
57
  This will install as "public/javascripts/forms/validator.js"
49
58
 
59
+ You can nest groups too:
60
+
61
+ group "forms" do
62
+ js "validator", "link"
63
+ group "extra" do
64
+ js "another", "link"
65
+ end
66
+ end
67
+
50
68
 
51
69
  Pkgs
52
70
  ----
@@ -54,13 +72,47 @@ Pkgs
54
72
  Big projects makes you download tons of files for some .min files and css.
55
73
 
56
74
  pkg "fancy", "http://to.tgz.or.zip" do
57
- js "cool", "internal/js/cool"
58
- css "cool", "internal/css/cool"
75
+ js "cool", "internal/js/cool.js"
76
+ css "cool", "internal/css/cool.css"
59
77
  end
60
78
 
61
79
  This downloads and 'cherry pick' the files.
80
+ Files will be written with the namespace "fancy":
81
+
82
+ /javascripts/fancy/cool.js
83
+
84
+ You can pass :shallow => true to avoid the namespace:
85
+
86
+ pkg "fancy", "http://to.tgz.or.zip", :shallow => true do
87
+
88
+ Results in:
89
+
90
+ /javascript/cool.js
91
+
92
+
93
+ Also, please check out the note about link inside pkgs below.
94
+
95
+
96
+ Dir
97
+ ___
98
+
99
+ You can resource a full directory of files, too. Very useful when
100
+ dealing with pkgs:
101
+
102
+ pkg "complexfw", "link" do
103
+ js "complex.min.js"
104
+ dir "images/", :to => "images/complexfw"
105
+ # Another option, treat all as a type:
106
+ dir "src/", :as => :js
107
+ end
108
+
109
+ All files inside images will be copied to "images/complexfw" and
110
+ all files in 'src' to 'javascripts' (or whatever else jspath is).
62
111
 
63
112
 
113
+ Note: Have in mind that the "link" inside dir/packages *is a regex*
114
+ that returns the *first match* inside the archive. Check out libarchive
115
+ or the pkg.rb source for more info.
64
116
 
65
117
  Other
66
118
  -----
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.2
1
+ 0.3.0
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{assetify}
8
- s.version = "0.2.2"
8
+ s.version = "0.3.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Marcos Piccinini"]
12
- s.date = %q{2011-05-30}
12
+ s.date = %q{2011-06-04}
13
13
  s.default_executable = %q{assetify}
14
14
  s.description = %q{Downloads/updates assets based on a Jsfile. Any framework.}
15
15
  s.email = %q{x@nofxx.com}
@@ -40,6 +40,7 @@ Gem::Specification.new do |s|
40
40
  "spec/assetify/helpers_spec.rb",
41
41
  "spec/assetify/pkg_spec.rb",
42
42
  "spec/assetify_spec.rb",
43
+ "spec/fixtures/complex.tgz",
43
44
  "spec/fixtures/fancy.tgz",
44
45
  "spec/spec_helper.rb"
45
46
  ]
@@ -14,6 +14,7 @@ module Assetify
14
14
  :jspath => "public/javascripts",
15
15
  :csspath => "public/stylesheets",
16
16
  :imgpath => "public/images",
17
+ :vendor => "public/vendor",
17
18
  :newname => true
18
19
  }
19
20
 
@@ -71,14 +72,20 @@ TXT
71
72
  end
72
73
  end
73
74
 
75
+ # Fuzzy find files
76
+ def find_assets(filter = nil)
77
+ return @assets unless filter
78
+ @assets.select { |a| "#{a.name}#{a.pkg}" =~ /#{filter}/ }
79
+ end
80
+
74
81
  def work_on params
75
82
  case params.first
76
83
  when /^i/, nil
77
84
  check_param params, "install" if params[0]
78
- @assets.map(&:install!)
85
+ find_assets(params[1]).map(&:install!)
79
86
  when /^u/
80
87
  check_param params, "update"
81
- @assets.map { |a| a.install! :force }
88
+ find_assets(params[1]).map { |a| a.install! :force }
82
89
  else
83
90
  puts "Dunno how to #{params.join}."
84
91
  end
@@ -4,7 +4,8 @@ require 'fileutils'
4
4
  module Assetify
5
5
  class Asset
6
6
  include Helpers
7
- attr_accessor :type, :name, :url, :ns
7
+ attr_accessor :type, :name, :url, :ns, :pkg
8
+ alias :ext :type
8
9
 
9
10
  def initialize(type, name, url, ver = nil, params={})
10
11
  raise "NoType" unless type
@@ -48,6 +49,11 @@ module Assetify
48
49
  v ? "v#{v[0]} " : nil
49
50
  end
50
51
 
52
+ def data
53
+ # Get data, from a pkg or download directly
54
+ @data ||= @pkg ? @pkg.get(url, :force).values.first : get_data(url)
55
+ end
56
+
51
57
  def install!(force = false)
52
58
  LINE.p "-> #{name}.#{type}"
53
59
  if !force && check?
@@ -55,13 +61,14 @@ module Assetify
55
61
  return LINE.f "#{print_version}Installed"
56
62
  end
57
63
  begin
64
+ # Creates a thread to insert dots while downloading
58
65
  points = Thread.new { loop do; LINE.p "."; sleep 1; end }
59
- @data ||= @pkg ? @pkg.get(url) : get_data(url)
60
- write @data
66
+
67
+ write data
61
68
  LINE.f "#{print_version}ok"
62
69
  rescue => e
63
70
  LINE.f :FAIL, :red
64
- p "Fail: #{e}"
71
+ p "Fail: #{e} #{e.backtrace}"
65
72
  ensure
66
73
  points.kill
67
74
  end
@@ -1,26 +1,49 @@
1
1
  module Assetify
2
2
 
3
3
  class DSL
4
+ attr_reader :assets
4
5
 
5
- def pkg name, url, &block
6
+ def set_namespace(name)
7
+ @ns = @ns.nil? ? name : "#{@ns}/#{name}"
8
+ end
9
+
10
+ def pkg name, url, opts = {}, &block
6
11
  @pkg = Pkg.new name, url
7
- @ns = name
8
- instance_exec(&block)
9
- @ns = nil
12
+ if block_given?
13
+ set_namespace name unless opts[:shallow]
14
+ instance_exec &block
15
+ @ns = @pkg = nil
16
+ else
17
+ @pkg.unpack_to_vendor
18
+ end
10
19
  assets
11
20
  end
12
21
 
13
22
  def group name, &block
14
- @ns = name
15
- instance_exec(&block)
23
+ set_namespace name
24
+ instance_exec &block
16
25
  @ns = nil
17
26
  assets
18
27
  end
19
28
 
20
- def assets
21
- @assets
29
+ def dir regex, to
30
+ to = to[:to]
31
+ if @pkg
32
+ @pkg.get(regex).each do |path, data|
33
+ next if path =~ /\/$/ # dont let dirs get in... ugly
34
+ ext, *name = path.split(".").reverse
35
+ name = name.reverse.join(".").split("/").last
36
+ (@assets ||= []) << Asset.new(ext, name, path, nil, {
37
+ :pkg => @pkg,
38
+ :to => to})# h.split(".").last, )
39
+ end
40
+ end
22
41
  end
23
42
 
43
+ #
44
+ # js "foo", "http://foo.com"
45
+ # js "foo", "http://foo.com", :to => "/other/place"
46
+ #
24
47
  def method_missing method, name, uri, *params
25
48
  params, ver = params.partition { |param| param.is_a?(Hash) }
26
49
  opts = {:ns => @ns, :pkg => @pkg}
@@ -8,6 +8,7 @@ module Assetify
8
8
  # Detects numerical software version from text.
9
9
  #
10
10
  def find_version(txt)
11
+ return unless txt
11
12
  version = txt.match /(?:(\d+)\.)?(?:(\d+)\.)?(\d+)?\.?(\d+)/
12
13
  # If matches a dot, it`s text. Otherwise make it number.
13
14
  v = version.to_a.reject(&:nil?).map { |d| d =~ /\./ ? d : d.to_i }
@@ -40,7 +41,7 @@ module Assetify
40
41
  end
41
42
 
42
43
  def write(binary)
43
- FileUtils.mkdir_p path unless Dir.exists? path
44
+ FileUtils.mkdir_p path unless Dir.exists?(path)
44
45
  File.open(fullpath, "w") { |f| f.puts(binary) }
45
46
  end
46
47
 
@@ -22,20 +22,36 @@ module Assetify
22
22
  File.join(path, @pkgname)
23
23
  end
24
24
 
25
- def get(file)
26
- get_data(url) unless File.exists? File.join(PATH, @pkgname)
27
- data = nil
25
+ def read_from_pkg(regex = ".*")
26
+ data = {}
28
27
  Archive.read_open_filename(fullpath) do |ar|
29
28
  while entry = ar.next_header
30
- if entry.pathname =~ /#{file}/
31
- data = ar.read_data
32
- return data
29
+ if entry.pathname =~ /#{regex}/
30
+ data.merge! entry.pathname => ar.read_data
31
+ # return data
33
32
  end
34
33
  end
35
34
  end
36
35
  data
37
36
  end
38
37
 
38
+ def get(file, force = false)
39
+ # Download and write to tmp if force or doensnt exists
40
+ write(get_data(url)) if force || !File.exists?(File.join(fullpath))
41
+ # Better way when multiple are found....?
42
+ read_from_pkg(file)
43
+ end
44
+
45
+ def unpack_to_vendor
46
+ read_from_pkg.each do |file, data|
47
+ fname, *dir = file =~ /\/$/ ? [nil, file] : file.split("/").reverse
48
+ dir = File.join Opt[:vendor], dir.reverse.join("/")
49
+ FileUtils.mkdir_p dir unless Dir.exists?(dir)
50
+ next if file =~ /\/$/ # next if data.empty?
51
+ File.open(Opt[:vendor] + "/#{file}", "w+") { |f| f.puts(data) }
52
+ end
53
+ end
54
+
39
55
  end
40
56
 
41
57
  end
@@ -41,6 +41,12 @@ describe DSL do
41
41
  a[1].fullpath.should eql("public/javascripts/rock.js")
42
42
  end
43
43
 
44
+ it "should work with nested namespaces" do
45
+ a = Assetify::DSL.parse "group 'common' do; group 'nice' do; js 'foo', 'foolink'; end; end"
46
+ a[0].should be_an Asset
47
+ a[0].fullpath.should eql("public/javascripts/common/nice/foo.js")
48
+ end
49
+
44
50
  end
45
51
 
46
52
  describe "Pkg Assets" do
@@ -51,13 +57,41 @@ describe DSL do
51
57
  a[0].fullpath.should eql("public/javascripts/fancy/foo.js")
52
58
  end
53
59
 
60
+ it "should accept shallow too" do
61
+ a = Assetify::DSL.parse "pkg 'fancy', 'http://fancy.zip', :shallow => true do; js 'foo', 'foolink'; end"
62
+ a[0].should be_an Asset
63
+ a[0].fullpath.should eql("public/javascripts/foo.js")
64
+ end
65
+
54
66
  it "should fetch inside archive" do
55
67
  a = Assetify::DSL.parse "pkg 'fancy', 'http://fancy.zip' do; js 'foo', 'foolink'; end"
56
68
  a[0].should be_an Asset
57
69
  a[0].fullpath.should eql("public/javascripts/fancy/foo.js")
58
70
  end
59
71
 
72
+ it "should unpack to vendor if no block given" do
73
+ Pkg.should_receive(:new).with("fancy", "http://fancy.zip").and_return(mp = mock(Pkg))
74
+ mp.should_receive :unpack_to_vendor
75
+ a = Assetify::DSL.parse "pkg 'fancy', 'http://fancy.zip'"
76
+ end
60
77
  end
61
78
 
79
+ describe "Directories" do
80
+
81
+ it "should read from pkg the regex" do
82
+ Pkg.should_receive(:new).with("fancy", "http://fancy.zip").and_return(mp = mock(Pkg))
83
+ mp.should_receive(:get).with("images/").and_return([])
84
+ a = Assetify::DSL.parse "pkg 'fancy', 'http://fancy.zip' do; dir 'images/', :to => 'images/complex/'; end"
85
+ end
86
+
87
+ it "should read from pkg the regex" do
88
+ as = Assetify::DSL.parse "pkg 'complex', 'http://complex.tgz' do; dir 'images/', :to => 'images/complex/'; end"
89
+ as[0].name.should eql("two")
90
+ as[0].ext.should eql("png")
91
+ as[0].fullpath.should eql("png")
92
+ end
93
+
94
+
95
+ end
62
96
 
63
97
  end
@@ -4,8 +4,10 @@ describe Pkg do
4
4
 
5
5
 
6
6
  before do
7
- `mkdir /tmp/fancy` unless Dir.exists? "/tmp/fancy"
8
- `cp spec/fixtures/fancy.tgz /tmp/fancy`
7
+ [:fancy, :complex].each do |ns|
8
+ `mkdir /tmp/#{ns}` unless Dir.exists? "/tmp/#{ns}"
9
+ `cp spec/fixtures/#{ns}.tgz /tmp/#{ns}/`
10
+ end
9
11
  end
10
12
 
11
13
  it "should spawn!" do
@@ -21,8 +23,19 @@ describe Pkg do
21
23
 
22
24
  it "should get file from unpack" do
23
25
  as = Pkg.new "fancy", "/tmp/fancy/fancy.tgz"
24
- as.get("fancy/fancy.css").should eql("// Fancy css!\n\n#foo {\n padding: 10px;\n}\n")
26
+ as.get("fancy/fancy.css").should eql("fancy/fancy.css" => "// Fancy css!\n\n#foo {\n padding: 10px;\n}\n")
27
+ end
28
+
29
+ it "should read all assets within pkg" do
30
+ as = Pkg.new "complex", "/tmp/complex/complex.tgz"
31
+ as.read_from_pkg.should be_a Hash #(hash_including "complex/images/3.png"=>"Im a png image..3\n")
32
+ end
25
33
 
34
+ it "should unpack all to vendor" do
35
+ as = Pkg.new "complex", "/tmp/complex/complex.tgz"
36
+ as.unpack_to_vendor #.should be_a Hash #(hash_including "complex/images/3.png"=>"Im a png image..3\n")
37
+ File.exists?("public/vendor/complex/src/main.js").should be_true
38
+ `rm -rf public/vendor`
26
39
  end
27
40
 
28
41
  end
Binary file
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 2
8
- - 2
9
- version: 0.2.2
7
+ - 3
8
+ - 0
9
+ version: 0.3.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Marcos Piccinini
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-05-30 00:00:00 -03:00
17
+ date: 2011-06-04 00:00:00 -03:00
18
18
  default_executable: assetify
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -106,6 +106,7 @@ files:
106
106
  - spec/assetify/helpers_spec.rb
107
107
  - spec/assetify/pkg_spec.rb
108
108
  - spec/assetify_spec.rb
109
+ - spec/fixtures/complex.tgz
109
110
  - spec/fixtures/fancy.tgz
110
111
  - spec/spec_helper.rb
111
112
  has_rdoc: true
@@ -122,7 +123,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
122
123
  requirements:
123
124
  - - ">="
124
125
  - !ruby/object:Gem::Version
125
- hash: -2020690596737373236
126
+ hash: -2700940674252686388
126
127
  segments:
127
128
  - 0
128
129
  version: "0"