assetify 2.0.1 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,22 +1,24 @@
1
1
  module Assetify
2
+ #
3
+ # *Attempt* to fix assets in js/css for #image_url
4
+ #
2
5
  class Pathfix
3
-
4
- def initialize chunk, as = :erb, ns = nil
5
- @chunk, @as, @ns = chunk, as, ns
6
+ def initialize(chunk, renderer = :erb, ns = nil)
7
+ @chunk = chunk
8
+ @renderer = renderer
9
+ @ns = ns
6
10
  @images = scan_images
7
11
  end
8
12
 
9
- def images
10
- @images
11
- end
13
+ attr_reader :images
12
14
 
13
15
  def scan_images
14
- @chunk.scan(/url\(([a-zA-Z0-9\/\_\-\.]*\.\w+)\)/xo).flatten
16
+ @chunk.scan(%r{url\(([a-zA-Z0-9/\_\-\.]*\.\w+)\)}xo).flatten
15
17
  end
16
18
 
17
- def replace src
19
+ def replace(src)
18
20
  fpath = @ns ? "#{@ns}/#{src}" : src
19
- if @as == :erb
21
+ if @renderer == :erb
20
22
  "url('<%= image_path('#{fpath}') %>')"
21
23
  else
22
24
  "image-url('#{fpath}')"
@@ -25,20 +27,16 @@ module Assetify
25
27
 
26
28
  def fix
27
29
  @images.each do |path|
28
- @chunk["url(#{path})"] = replace path.split("/").last
30
+ @chunk["url(#{path})"] = replace path.split('/').last
29
31
  end
30
- @as != :erb ? tmpl_chunk : @chunk
32
+ @renderer != :erb ? tmpl_chunk : @chunk
31
33
  end
32
34
 
33
35
  def tmpl_chunk
34
- begin
35
- require 'sass/css'
36
- Sass::CSS.new(@chunk).render(@as)
37
- rescue Sass::SyntaxError => e
38
- @error = e
39
- end
36
+ require 'sass/css'
37
+ Sass::CSS.new(@chunk).render(@renderer)
38
+ rescue Sass::SyntaxError => e
39
+ @error = e
40
40
  end
41
-
42
41
  end
43
-
44
42
  end
@@ -1,16 +1,18 @@
1
1
  require 'libarchive'
2
2
 
3
3
  module Assetify
4
-
4
+ #
5
+ # Some Assets are inside rocks, need tools...
6
+ #
5
7
  class Pkg
6
8
  include Helpers
7
9
  attr_accessor :name, :url
8
10
 
9
- PATH = "/tmp/"
11
+ PATH = '/tmp/'.freeze
10
12
 
11
- def initialize(name, url, opts={})
13
+ def initialize(name, url, _opts = {})
12
14
  @name = name
13
- @pkgname = url.split("/").last
15
+ @pkgname = url.split('/').last
14
16
  @url = url
15
17
  end
16
18
 
@@ -22,13 +24,13 @@ module Assetify
22
24
  File.join(path, @pkgname)
23
25
  end
24
26
 
25
- def read_from_pkg(regex = ".*")
27
+ def read_from_pkg(regex = '.*')
26
28
  data = {}
27
29
  Archive.read_open_filename(fullpath) do |ar|
28
- while entry = ar.next_header
30
+ while (entry = ar.next_header)
29
31
  if entry.pathname =~ /#{regex}/
30
- data.merge! entry.pathname => ar.read_data
31
- # return data
32
+ data[entry.pathname] = ar.read_data
33
+ # return data
32
34
  end
33
35
  end
34
36
  end
@@ -37,7 +39,7 @@ module Assetify
37
39
 
38
40
  def get(file, force = false)
39
41
  # Download and write to tmp if force or doensnt exists
40
- write(get_data(url)) if force || !File.exists?(fullpath)
42
+ write(get_data(url)) if force || !File.exist?(fullpath)
41
43
  # Better way when multiple are found....?
42
44
  read_from_pkg(file)
43
45
  end
@@ -47,14 +49,12 @@ module Assetify
47
49
  #
48
50
  def unpack_all
49
51
  read_from_pkg.each do |file, data|
50
- fname, *dir = file =~ /\/$/ ? [nil, file] : file.split("/").reverse
51
- dir = File.join Opt[:vendor], dir.reverse.join("/")
52
- FileUtils.mkdir_p dir unless Dir.exists?(dir)
53
- next if file =~ /\/$/ # next if data.empty?
54
- File.open(Opt[:vendor] + "/#{file}", "w+") { |f| f.puts(data) }
52
+ _fname, *dir = file =~ %r{/$} ? [nil, file] : file.split('/').reverse
53
+ dir = File.join Opt[:vendor], dir.reverse.join('/')
54
+ FileUtils.mkdir_p dir unless Dir.exist?(dir)
55
+ next if file =~ %r{/$} # next if data.empty?
56
+ File.open(Opt[:vendor] + "/#{file}", 'w+') { |f| f.puts(data) }
55
57
  end
56
58
  end
57
-
58
59
  end
59
-
60
60
  end
@@ -1,16 +1,17 @@
1
1
  module Assetify
2
+ #
3
+ # The Assetfile!
4
+ #
2
5
  class Assetfile
3
6
  class << self
4
-
5
-
6
7
  #
7
8
  # Assetfile stuff
8
9
  #
9
10
  def missing!
10
- print "Assetfile not found, create one? [Y/n] "
11
+ print 'Assetfile not found, create one? [Y/n] '
11
12
  res = $stdin.gets.chomp # dont forget stdin
12
13
  unless res =~ /n|N/
13
- File.open("Assetfile", "w+") do |f|
14
+ File.open('Assetfile', 'w+') do |f|
14
15
  f.print <<TXT
15
16
  #
16
17
  # #{Dir.pwd.split('/').last.capitalize} Assetfile
@@ -25,7 +26,7 @@ end
25
26
 
26
27
  TXT
27
28
  end
28
- puts "Assetfile created!"
29
+ puts 'Assetfile created!'
29
30
  exit 0
30
31
  end
31
32
  end
@@ -35,21 +36,21 @@ TXT
35
36
  #
36
37
  #
37
38
  def find
38
- missing! unless File.exists?("Assetfile")
39
+ missing! unless File.exist?('Assetfile')
39
40
  end
40
41
 
41
42
  def read
42
- file = File.open("Assetfile") # ruby 1.8/1.9 (ugly) fix
43
- code = file.send(file.respond_to?(:lines) ? :lines : :readlines).map do |line|
43
+ file = File.open('Assetfile') # ruby 1.8/1.9 (ugly) fix
44
+ code = file.each_line.map do |line|
44
45
  # Parse options
45
46
  if line =~ /^\w{2,3}path/
46
- key, val = line.split(" ")
47
+ key, val = line.split(' ')
47
48
  Opt[key.to_sym] = val
48
49
  next
49
50
  end
50
51
  line
51
52
  end.reject(&:nil?)
52
- DSL.parse code.join("")
53
+ DSL.parse code.join('')
53
54
  end
54
55
 
55
56
  # def write
@@ -2,14 +2,13 @@
2
2
  # To be refactored...
3
3
  #
4
4
  module Assetify
5
- LINE = CLI.new
5
+ LINE = CLI.new
6
6
 
7
7
  class << self
8
-
9
8
  #
10
9
  # Text Interface
11
10
  #
12
- def check_param params, string
11
+ def check_param(params, string)
13
12
  unless string.include? params[0]
14
13
  puts "Did you mean #{string}?"
15
14
  exit 0
@@ -22,6 +21,40 @@ module Assetify
22
21
  Asset.filter params
23
22
  end
24
23
 
24
+ def check(assets)
25
+ assets.each do |a|
26
+ LINE.p a.header
27
+ if a.file_exists? # Return if file is on path
28
+ a.read_data
29
+ LINE.f "#{a.print_version} Installed"
30
+ else
31
+ LINE.f 'Not Found', :red
32
+ end
33
+ end
34
+ end
35
+
36
+ def install(assets, force = false)
37
+ assets.each do |a|
38
+ LINE.p a.header
39
+ if !force && a.file_exists? # Return if file is on path
40
+ a.read_data
41
+ return LINE.f "#{a.print_version} Installed"
42
+ end
43
+ begin
44
+ # Creates a thread to insert dots while downloading
45
+ points = Thread.new { loop { ; LINE.p '.'; sleep 1; } }
46
+
47
+ a.install! force
48
+ LINE.f "#{a.print_version} ok"
49
+ rescue => e
50
+ LINE.f :FAIL, :red
51
+ p "Fail: #{e} #{e.backtrace}"
52
+ ensure
53
+ points.kill
54
+ end
55
+ end
56
+ end
57
+
25
58
  #
26
59
  # CLI Master case/switch!
27
60
  #
@@ -34,19 +67,20 @@ module Assetify
34
67
  # c -> check
35
68
  # w -> web
36
69
  #
37
- def work_on params
70
+ def work_on(params)
71
+ assets = find_assets(params[1])
38
72
  case params.first
39
73
  when /^i/, nil
40
- check_param params, "install" if params[0]
41
- find_assets(params[1]).map(&:install!)
74
+ check_param params, 'install' if params[0]
75
+ install assets
42
76
  when /^u/
43
- check_param params, "update"
44
- find_assets(params[1]).map { |a| a.install! :force }
77
+ check_param params, 'update'
78
+ install assets, :force
45
79
  when /^c/
46
- check_param params, "check"
47
- find_assets(params[1]).map { |a| a.check! }
80
+ check_param params, 'check'
81
+ check assets
48
82
  when /^w/
49
- check_param params, "web"
83
+ check_param params, 'web'
50
84
  GUI.boot!
51
85
  else
52
86
  puts "Dunno how to #{params.join}."
@@ -57,7 +91,7 @@ module Assetify
57
91
  # Divider bar
58
92
  #
59
93
  def bar
60
- puts "-" * TSIZE
94
+ puts '-' * TSIZE
61
95
  end
62
96
 
63
97
  def work!(params)
@@ -65,13 +99,11 @@ module Assetify
65
99
  Assetfile.find
66
100
  print "Assetify - #{Asset.all.size} assets"
67
101
  print " | #{params[1..-1].join(' . ')}" if params[1]
68
- puts " |"
102
+ puts ' |'
69
103
  bar
70
104
  work_on params
71
105
  bar
72
106
  puts "Done in #{Time.now - start}s"
73
107
  end
74
-
75
108
  end
76
-
77
109
  end
@@ -2,9 +2,9 @@ require 'Win32/Console/ANSI' if RUBY_PLATFORM =~ /win32/
2
2
 
3
3
  ##
4
4
  # cute.
5
- #
5
+ #
6
6
  # >> "this is red".red
7
- #
7
+ #
8
8
  # >> "this is red with a blue background (read: ugly)".red_on_blue
9
9
  #
10
10
  # >> "this is red with an underline".red.underline
@@ -15,53 +15,51 @@ require 'Win32/Console/ANSI' if RUBY_PLATFORM =~ /win32/
15
15
  module Colored
16
16
  extend self
17
17
 
18
- COLORS = {
18
+ COLORS = {
19
19
  'black' => 30,
20
- 'red' => 31,
21
- 'green' => 32,
20
+ 'red' => 31,
21
+ 'green' => 32,
22
22
  'yellow' => 33,
23
23
  'blue' => 34,
24
24
  'magenta' => 35,
25
25
  'cyan' => 36,
26
26
  'white' => 37
27
- }
27
+ }.freeze
28
28
 
29
29
  EXTRAS = {
30
- 'clear' => 0,
30
+ 'clear' => 0,
31
31
  'bold' => 1,
32
32
  'underline' => 4,
33
33
  'reversed' => 7
34
- }
35
-
36
- COLORS.each do |color, value|
37
- define_method(color) do
38
- colorize(self, :foreground => color)
34
+ }.freeze
35
+
36
+ COLORS.each do |color, _value|
37
+ define_method(color) do
38
+ colorize(self, foreground: color)
39
39
  end
40
40
 
41
41
  define_method("on_#{color}") do
42
- colorize(self, :background => color)
42
+ colorize(self, background: color)
43
43
  end
44
44
 
45
- COLORS.each do |highlight, value|
45
+ COLORS.each do |highlight, _value|
46
46
  next if color == highlight
47
47
  define_method("#{color}_on_#{highlight}") do
48
- colorize(self, :foreground => color, :background => highlight)
48
+ colorize(self, foreground: color, background: highlight)
49
49
  end
50
50
  end
51
51
  end
52
52
 
53
- EXTRAS.each do |extra, value|
53
+ EXTRAS.each do |extra, _value|
54
54
  next if extra == 'clear'
55
- define_method(extra) do
56
- colorize(self, :extra => extra)
55
+ define_method(extra) do
56
+ colorize(self, extra: extra)
57
57
  end
58
58
  end
59
59
 
60
60
  define_method(:to_eol) do
61
61
  tmp = sub(/^(\e\[[\[\e0-9;m]+m)/, "\\1\e[2K")
62
- if tmp == self
63
- return "\e[2K" << self
64
- end
62
+ return "\e[2K" << self if tmp == self
65
63
  tmp
66
64
  end
67
65
 
@@ -84,7 +82,7 @@ module Colored
84
82
  background = color_name.to_s =~ /on_/
85
83
  color_name = color_name.to_s.sub('on_', '')
86
84
  return unless color_name && COLORS[color_name]
87
- "\e[#{COLORS[color_name] + (background ? 10 : 0)}m"
85
+ "\e[#{COLORS[color_name] + (background ? 10 : 0)}m"
88
86
  end
89
87
  end unless Object.const_defined? :Colored
90
88
 
@@ -1,18 +1,20 @@
1
1
  module Assetify
2
-
2
+ #
3
+ # Command Line Interface
4
+ #
3
5
  class CLI
4
6
  def initialize(size = TSIZE)
5
7
  @size = size
6
8
  @chars = 0
7
9
  end
8
10
 
9
- def p txt
11
+ def p(txt)
10
12
  @chars += txt.size
11
13
  print txt
12
14
  end
13
15
 
14
- def f txt, color = :green
15
- puts "[#{txt}]".send(color).bold.rjust (TSIZE - @chars + 17)
16
+ def f(txt, color = :green)
17
+ puts "[#{txt}]".send(color).bold.rjust(TSIZE - @chars + 17)
16
18
  @chars = 0
17
19
  end
18
20
  end
@@ -1,13 +1,12 @@
1
1
  module Assetify
2
-
3
2
  Opt = {
4
- :vendor => "public/vendor",
5
- :newname => true
3
+ vendor: 'public/vendor',
4
+ newname: true
6
5
  }
7
6
 
8
7
  TSIZE = 80
9
- ASSETS_PATH = "vendor/assets"
10
- ASSETS = [:javascripts, :stylesheets, :images]
8
+ ASSETS_PATH = 'vendor/assets'.freeze
9
+ ASSETS = [:javascripts, :stylesheets, :images].freeze
11
10
  ASSETS.each do |asset|
12
11
  Opt.merge!(asset => "#{ASSETS_PATH}/#{asset}")
13
12
  end
@@ -1,5 +1,7 @@
1
1
  module Assetify
2
-
2
+ #
3
+ # Nice Assetfile reader
4
+ #
3
5
  class DSL
4
6
  attr_reader :assets
5
7
 
@@ -9,11 +11,11 @@ module Assetify
9
11
  # pkg :foo, "http://to.tgz" do
10
12
  # end
11
13
  #
12
- def pkg name, url, opts = {}, &block
14
+ def pkg(name, url, opts = {}, &block)
13
15
  @pkg = Pkg.new name, url
14
16
  if block_given?
15
- set_namespace name unless opts[:shallow]
16
- instance_exec &block
17
+ use_namespace(name) unless opts[:shallow]
18
+ instance_exec(&block)
17
19
  else
18
20
  @pkg.unpack_all
19
21
  end
@@ -28,9 +30,9 @@ module Assetify
28
30
  # group :foo do
29
31
  # end
30
32
  #
31
- def group name, &block
32
- set_namespace name
33
- instance_exec &block
33
+ def group(name, &block)
34
+ use_namespace(name)
35
+ instance_exec(&block)
34
36
  @ns = nil
35
37
  assets
36
38
  ensure
@@ -45,14 +47,14 @@ module Assetify
45
47
  # dir "images/*jpg"
46
48
  # end
47
49
  #
48
- def dir regex, params = {}
50
+ def dir(regex, params = {})
49
51
  to = params[:to]
50
52
  if @pkg
51
- @pkg.get(regex).each do |path, data|
52
- next if path =~ /\/$/ # dont let dirs get in... ugly
53
- ext, *name = path.split(".").reverse
54
- name = name.reverse.join(".").split("/").last
55
- create_asset(ext, name, path, nil, { :to => to } )
53
+ @pkg.get(regex).each do |path, _data|
54
+ next if path =~ %r{/$} # dont let dirs get in... ugly
55
+ ext, *name = path.split('.').reverse
56
+ name = name.reverse.join('.').split('/').last
57
+ create_asset(ext, name, path, nil, to: to)
56
58
  end
57
59
  end
58
60
  end
@@ -62,20 +64,20 @@ module Assetify
62
64
  #
63
65
  # a "jquery", "http://...jquery.js"
64
66
  #
65
- def a name, url, *params
66
- extension = url.split(".").last
67
+ def a(name, url, *_params)
68
+ extension = url.split('.').last
67
69
  parse_method extension, name, url
68
70
  end
69
- alias :asset :a
71
+ alias asset a
70
72
 
71
73
  #
72
74
  # Filter/validate DSL to parse
73
75
  #
74
- def method_missing method, name=nil, uri=nil, *params
75
- unless name && uri
76
- raise SyntaxError.new "Syntax Error on Assetfile. `#{method} :#{name}`"
77
- else
76
+ def method_missing(method, name = nil, uri = nil, *params)
77
+ if name && uri
78
78
  parse_method method, name, uri, params
79
+ else
80
+ raise SyntaxError, "Syntax Error on Assetfile. `#{method} :#{name}`"
79
81
  end
80
82
  end
81
83
 
@@ -90,6 +92,15 @@ module Assetify
90
92
  end
91
93
  end
92
94
 
95
+ #
96
+ # DSL.parse()
97
+ #
98
+ def self.parse(chunk)
99
+ # puts "Assetify - Error Parsing 'Assetfile'."
100
+ # Instance eval with 2nd, 3rd args to the rescue
101
+ new.instance_eval(chunk, 'Assetfile', 1)
102
+ end
103
+
93
104
  private
94
105
 
95
106
  #
@@ -98,7 +109,7 @@ module Assetify
98
109
  # js "foo", "http://foo.com"
99
110
  # js "foo", "http://foo.com", :to => "/other/place"
100
111
  #
101
- def parse_method method, name, uri, params=[]
112
+ def parse_method(method, name, uri, params = [])
102
113
  params, ver = params.partition { |param| param.is_a?(Hash) }
103
114
  opts = {}
104
115
  params.each { |hsh| opts.merge! hsh }
@@ -110,25 +121,14 @@ module Assetify
110
121
  # Helper to create asset with correct options
111
122
  #
112
123
  def create_asset(ext, name, path, ver, opts = {})
113
- opts.merge! ({pkg: @pkg, ns: @ns })
124
+ opts[:pkg] = @pkg
125
+ opts[:ns] = @ns
114
126
  @assets ||= []
115
127
  @assets << Asset.new(ext, name, path, ver, opts)
116
128
  end
117
129
 
118
-
119
- def set_namespace name
130
+ def use_namespace(name)
120
131
  @ns = @ns.nil? ? name : "#{@ns}/#{name}"
121
132
  end
122
-
123
- #
124
- # DSL.parse()
125
- #
126
- def self.parse chunk
127
- # puts "Assetify - Error Parsing 'Assetfile'."
128
- # Instance eval with 2nd, 3rd args to the rescue
129
- new.instance_eval(chunk, "Assetfile", 1)
130
- end
131
-
132
133
  end
133
-
134
134
  end