Class: Cyborg::Assets::AssetType
- Inherits:
-
Object
- Object
- Cyborg::Assets::AssetType
show all
- Defined in:
- lib/cyborg/plugin/assets/asset.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(plugin, base) ⇒ AssetType
Returns a new instance of AssetType.
6
7
8
9
|
# File 'lib/cyborg/plugin/assets/asset.rb', line 6
def initialize(plugin, base)
@base = base
@plugin = plugin
end
|
Instance Attribute Details
#base ⇒ Object
Returns the value of attribute base.
4
5
6
|
# File 'lib/cyborg/plugin/assets/asset.rb', line 4
def base
@base
end
|
#plugin ⇒ Object
Returns the value of attribute plugin.
4
5
6
|
# File 'lib/cyborg/plugin/assets/asset.rb', line 4
def plugin
@plugin
end
|
Instance Method Details
#build_failure(file) ⇒ Object
45
46
47
48
49
|
# File 'lib/cyborg/plugin/assets/asset.rb', line 45
def build_failure(file)
msg = "\nFAILED TO BUILD"
msg += ": #{local_path(file)}" if file
log_error msg
end
|
#build_success(file) ⇒ Object
41
42
43
|
# File 'lib/cyborg/plugin/assets/asset.rb', line 41
def build_success(file)
log_success "Built: #{local_path(file)}"
end
|
#change(modified, added, removed) ⇒ Object
127
128
129
130
131
132
133
134
135
136
|
# File 'lib/cyborg/plugin/assets/asset.rb', line 127
def change(modified, added, removed)
return if (Time.now.to_i - @last_build) < @throttle
puts "Added: #{file_event(added)}".colorize(:light_green) unless added.empty?
puts "Removed: #{file_event(removed)}".colorize(:light_red) unless removed.empty?
puts "Modified: #{file_event(modified)}".colorize(:light_yellow) unless modified.empty?
build
@last_build = Time.now.to_i
end
|
#compress(file) ⇒ Object
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
# File 'lib/cyborg/plugin/assets/asset.rb', line 145
def compress(file)
return unless Cyborg.production?
mtime = File.mtime(file)
gz_file = "#{file}.gz"
return if File.exist?(gz_file) && File.mtime(gz_file) >= mtime
File.open(gz_file, "wb") do |dest|
gz = Zlib::GzipWriter.new(dest, Zlib::BEST_COMPRESSION)
gz.mtime = mtime.to_i
IO.copy_stream(open(file), gz)
gz.close
end
File.utime(mtime, mtime, gz_file)
end
|
#destination(path) ⇒ Object
84
85
86
|
# File 'lib/cyborg/plugin/assets/asset.rb', line 84
def destination(path)
plugin.asset_path(versioned(path))
end
|
#file_event(files) ⇒ Object
138
139
140
141
142
143
|
# File 'lib/cyborg/plugin/assets/asset.rb', line 138
def file_event(files)
list = files.flat_map { |f| f.sub(base+'/', '') }.join(" \n")
list = " \n#{files}" if 1 < files.size
list
end
|
#filter_files(names) ⇒ Object
25
26
27
28
29
30
31
|
# File 'lib/cyborg/plugin/assets/asset.rb', line 25
def filter_files(names)
find_files.select do |f|
names.include? File.basename(f).sub(/(\..+)$/,'')
end
end
|
#find_files ⇒ Object
11
12
13
14
15
16
17
18
19
20
21
22
23
|
# File 'lib/cyborg/plugin/assets/asset.rb', line 11
def find_files
if @files
@files
else
files = Dir[File.join(base, "*.#{ext}")].reject do |f|
File.basename(f).start_with?('_')
end
@files = files if Cyborg.production?
files
end
end
|
#find_node_module(cmd) ⇒ Object
Determine if an NPM module is installed by checking paths with ‘npm bin` Returns path to binary if installed
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
# File 'lib/cyborg/plugin/assets/asset.rb', line 61
def find_node_module(cmd)
require 'open3'
(@modules ||= {})[cmd] ||= begin
local = "$(npm bin)/#{cmd}"
global = "$(npm -g bin)/#{cmd}"
if Open3.capture3(local)[2].success?
local
elsif Open3.capture3(global)[2].success?
global
end
end
end
|
#local_path(file) ⇒ Object
37
38
39
|
# File 'lib/cyborg/plugin/assets/asset.rb', line 37
def local_path(file)
destination(file).sub(plugin.root+'/','')
end
|
#log_error(msg) ⇒ Object
55
56
57
|
# File 'lib/cyborg/plugin/assets/asset.rb', line 55
def log_error( msg )
STDERR.puts msg.to_s.colorize(:red)
end
|
#log_success(msg) ⇒ Object
51
52
53
|
# File 'lib/cyborg/plugin/assets/asset.rb', line 51
def log_success( msg )
STDOUT.puts msg.to_s.colorize(:green)
end
|
#npm_command(cmd) ⇒ Object
77
78
79
80
81
82
|
# File 'lib/cyborg/plugin/assets/asset.rb', line 77
def npm_command(cmd)
cmd = cmd.split(' ')
if path = find_node_module(cmd.shift)
system "#{path} #{cmd.join(' ')}"
end
end
|
#url(path) ⇒ Object
88
89
90
|
# File 'lib/cyborg/plugin/assets/asset.rb', line 88
def url(path)
plugin.asset_url(versioned(path))
end
|
#urls(names = nil) ⇒ Object
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
# File 'lib/cyborg/plugin/assets/asset.rb', line 92
def urls(names=nil)
names = [names].flatten.compact.map do |n|
File.basename(n).sub(/(\..+)$/,'')
end
if names.empty?
find_files.map{ |file| url(file) }
else
filter_files(names).map{ |file| url(file) }
end
end
|
#versioned(path) ⇒ Object
33
34
35
|
# File 'lib/cyborg/plugin/assets/asset.rb', line 33
def versioned(path)
File.basename(path).sub(/(\.\w+)$/, '-'+plugin.version+'\1')
end
|
#watch ⇒ Object
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
# File 'lib/cyborg/plugin/assets/asset.rb', line 110
def watch
@throttle = 4
@last_build = 0
puts "Watching for changes to #{base.sub(plugin.root+'/', '')}...".colorize(:light_yellow)
Thread.new {
listener = Listen.to(base) do |modified, added, removed|
change(modified, added, removed)
end
listener.start sleep
}
end
|