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
# 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"
                     puts "Minifing: #{b}"
                     fileContents = Packr.pack(fileContents) unless /(-min)|(\.min)|(\-pack)|(\.pack)/.match(b)
		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



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

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



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

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

.run ⇒ Object



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



102
103
104
# File 'lib/scms/scms-bundler.rb', line 102

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

.watch ⇒ Object



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

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



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

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