Class: JSPreprocessor
- Inherits:
-
Object
show all
- Defined in:
- lib/JSPreprocessor.rb
Defined Under Namespace
Classes: MissingDependencyException
Instance Method Summary
collapse
Constructor Details
Returns a new instance of JSPreprocessor.
8
9
10
11
|
# File 'lib/JSPreprocessor.rb', line 8
def initialize(env)
@env = env
@defines = []
end
|
Instance Method Details
#process(cwd, main_file) ⇒ Object
13
14
15
16
17
|
# File 'lib/JSPreprocessor.rb', line 13
def process(cwd, main_file)
main = File.open(cwd + main_file).read.split "\n"
compiled_file = recurse_files(cwd, main).join "\n"
replace_defines compiled_file
end
|
#recurse_files(cwd, file) ⇒ Object
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
# File 'lib/JSPreprocessor.rb', line 19
def recurse_files(cwd, file)
file.each_with_index do |line, index|
if line.include? '#include'
if line.include? '<' if File.file? '.bowerrc' bowerrc = JSON.parse File.open('.bowerrc').read
if !bowerrc['directory'].nil? @bower_dir = bowerrc['directory']
else
@bower_dir = 'bower_components'
end
else
@bower_dir = 'bower_components'
end
package = line.match(/<(.*)>/)[1]
bower_json = "#{@bower_dir}/#{package}/bower.json"
package_json = "#{@bower_dir}/#{package}/package.json"
if File.exists? bower_json
json = File.open(bower_json).read
elsif File.exists? package_json
json = File.open(package_json).read
else
raise MissingDependencyException, "bower.json, package.json is missing, or #{package} does not exist."
end
package_path = JSON.parse(json)['main']
if package_path.class == Array
package_path.each_with_index do |package, index|
if package[-2..-1] == 'js'
package_path = package
break
end
end
end
library = File.open "#{@bower_dir}/#{package}/#{package_path}"
if @env == 'development'
filestamp = "// #{File.basename(library)} at #{Time.now}\n"
library = library.read
library.prepend filestamp
else
library = library.read
end
file[index] = library
else to_include = cwd + line[10..-2] + '.js'
next_wd = Pathname.new(to_include).dirname.to_s + '/'
next_file = File.open(to_include).read.split("\n")
if @env == 'development'
filestamp = "// #{to_include} at #{Time.now}"
next_file.unshift filestamp
end
file[index] = recurse_files next_wd, next_file
end
elsif line.include? '#define'
@defines << line[8..-1].split(' ')
file.delete_at index
end
end
end
|
#replace_defines(file) ⇒ Object
87
88
89
90
91
92
93
|
# File 'lib/JSPreprocessor.rb', line 87
def replace_defines(file)
@defines.each do |definition|
file.gsub! definition[0], definition[1]
end
file
end
|