Module: ScmsBundler

Defined in:
lib/scms/scms-bundler.rb

Class Method Summary collapse

Class Method Details

.bundle(bundle) ⇒ Object



12
13
14
15
16
17
18
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
# File 'lib/scms/scms-bundler.rb', line 12

def ScmsBundler.bundle(bundle)
	puts "Parsing bundle: #{bundle}"

	content = ""
		if File::exists?(bundle)
			wd = File.dirname(bundle)
        

			File.readlines(bundle).each do |line|
bundleFile = line.strip
bundleFile = bundleFile.gsub('\n', '')

next  if bundleFile == nil
next  if bundleFile == ""
next if line.match(/^generate:/)

if !line.match(/^#/)
	b = File.join(wd, bundleFile)
	puts "Including: #{line}"
	if File::exists?(b)
		fileContents = File.read(b)

		if File.extname(b) == ".js"
			if b.include? ".fat."
                     	puts "Minifing: #{b}"
                     	fileContents = Packr.pack(fileContents) unless /(-min)|(\.min)|(\-pack)|(\.pack)/.match(b)
                 	end
		end

		content += fileContents + "\n"
	else
		puts "Can not read: #{b}"
	end
end
			end

			out = ScmsBundler.getGeneratedBundleName(bundle)
			begin
            File.open(out, 'w') {|f| f.write(content) }
            ScmsUtils.successLog("Created: #{out}")
			rescue Exception=>e
            ScmsUtils.errLog("Error creating bundle: #{out}")
            ScmsUtils.errLog(e.message)
            ScmsUtils.log(e.backtrace.inspect)
			end
		end
end

.getBundleFiles(bundle) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/scms/scms-bundler.rb', line 81

def ScmsBundler.getBundleFiles(bundle)
	files = []
		if File::exists?(bundle)
			wd = File.dirname(bundle)
			File.readlines(bundle).each do |line|
bundleFile = line.strip
bundleFile = bundleFile.gsub('\n', '')

next  if bundleFile == nil
next  if bundleFile == ""
next if line.match(/^generate:/)

if !line.match(/^#/)
	b = File.join(wd, bundleFile)
	if File::exists?(b)
		files << b
	end
end
			end
		end
		return files
end

.getGeneratedBundleName(bundle) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/scms/scms-bundler.rb', line 108

def ScmsBundler.getGeneratedBundleName(bundle)
		name = ScmsBundler.toStub(bundle)

		if File::exists?(bundle)
			wd = File.dirname(bundle)
			File.readlines(bundle).each do |line|
if line.match(/^generate:/)
	name = File.join(wd, line.gsub("generate:", "").strip)
	break
end
			end
		end
	return name
end

.runObject



6
7
8
9
10
# File 'lib/scms/scms-bundler.rb', line 6

def ScmsBundler.run()
		Dir.glob('**/*.bundle').each do|bundle|
			ScmsBundler.bundle(bundle)
		end
end

.toStub(bundle) ⇒ Object



104
105
106
# File 'lib/scms/scms-bundler.rb', line 104

def ScmsBundler.toStub(bundle)
	return bundle.gsub(".bundle", "")
end

.watchObject



60
61
62
63
64
65
# File 'lib/scms/scms-bundler.rb', line 60

def ScmsBundler.watch()
	# Listen to changes to files withing a bundle
 Dir.glob('**/*.bundle').each do|bundle|
			ScmsBundler.watchBundle(bundle)
		end	
end

.watchBundle(bundle) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/scms/scms-bundler.rb', line 67

def ScmsBundler.watchBundle(bundle)
	files = ScmsBundler.getBundleFiles(bundle)
	Thread.new {
			FileWatcher.new(files).watch do |filename|
begin
	ScmsBundler.bundle(bundle)
rescue Exception=>e
             ScmsUtils.errLog(e.message)
             ScmsUtils.log(e.backtrace.inspect)
end
			end
	}
end