Module: ScmsWatcher

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

Class Method Summary collapse

Class Method Details

.watch(settings, options, configdir) ⇒ Object



7
8
9
10
11
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
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/scms/scms-watcher.rb', line 7

def ScmsWatcher.watch(settings, options, configdir)   

 # Listen to sass, bundle and _config.yml file changes
 watcher = Thread.new {
 	files = []
 	Dir.glob('**/*.scss').each do|f|
files << f
			end
			Dir.glob('**/*.bundle').each do|f|
files << f
			end
			configfile = File.join(configdir, "_config.yml")
			files << configfile if File::exists?(configfile)

			FileWatcher.new(files).watch do |filename|
ext = File.extname(filename)  

begin
	case ext
	when ".scss"
		puts ""
		puts "***********************************"
		puts " Sass file changed: #{filename}"
		puts "***********************************"
		puts ""
		Scms.sass(filename)
		#Scms.sassall(Folders[:website])
	when ".yml"
		puts ""
		puts "******************************************************"
		puts " Config Modification #{filename} "
		puts "******************************************************"
		puts ""
		settings = Scms.getSettings(configdir)
		Scms.bundle(settings, Folders[:website])
		Scms.build(Folders[:website], settings, options)
	when ".bundle"
		puts ""
		puts "******************************************************"
		puts " Bundle Modified: #{filename} "
		puts "******************************************************"
		puts ""
		Scms.bundler(filename)
	end
rescue Exception=>e
             ScmsUtils.errLog(e.message)
             ScmsUtils.log(e.backtrace.inspect)
end
			end
 }

 # Listen to changes to files withing a bundle
		ScmsBundler.watch()

		# Listen to changed to folders that start with an underscore (_)
		folders = []
 Dir.glob('*').select { |fn| File.directory?(fn) and (fn.match(/^_/) ) }.each do|f|
			folders.push(f) 
		end
 puts "Listening to #{folders}"
 listener = Listen.to(folders, force_polling: true) do |modified, added, removed|

			sassfile = false
			bundlefile = false
			buildfile = false

			if removed.length > 0
removed.each{|filename|
	removedfile = Pathname.new(filename).relative_path_from(Pathname.new(Folders[:website])).to_s
	#ext = File.extname(removedfile)  

	puts ""
	puts "***********************************************"
	puts "  Deleted: #{removedfile}"
	puts "***********************************************"
	puts ""

	if removedfile.start_with?('_pages/')
		buildfile = true
	end
}
			end

			if added.length > 0
added.each{|filename|
	addedfile = Pathname.new(filename).relative_path_from(Pathname.new(Folders[:website])).to_s
	#ext = File.extname(addedfile)  

	puts ""
	puts "***********************************************"
	puts "  Added: #{addedfile}"
	puts "***********************************************"
	puts ""

	if addedfile.start_with?('_pages/')
		buildfile = true
	end
}
			end

			if modified.length > 0
modified.each{|filename|
	modifiedfile = Pathname.new(filename).relative_path_from(Pathname.new(Folders[:website])).to_s
	#ext = File.extname(modifiedfile)  

	puts ""
	puts "***********************************************"
	puts "  Modified: #{modifiedfile}"
	puts "***********************************************"
	puts ""

	buildfile = true

	if modifiedfile.start_with?('_source/')
		bundlefile = true
		buildfile = false
	end
}
			end

			begin
Scms.sassall(Folders[:website]) if sassfile
Scms.bundle(settings, Folders[:website]) if bundlefile
Scms.build(Folders[:website], settings, options) if buildfile
			rescue Exception=>e
            ScmsUtils.errLog(e.message)
            ScmsUtils.log(e.backtrace.inspect)
			end

 end

 listener.start # not blocking
 listener.ignore! /\.png/
 listener.ignore! /\.gif/
 listener.ignore! /\.jpg/
end