-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGruntUtils.coffee
322 lines (248 loc) · 8.95 KB
/
GruntUtils.coffee
1
2
3
4
5
6
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# Load grunt API
grunt = require "grunt"
# Load path API
nodePath = require "path"
# System path separator
sep = nodePath.sep
###
Utility methods.
###
class GruntUtils
###
Shortcut for JSON.stringify(object, null, " ")
###
toJSON: ( object ) ->
return JSON.stringify object, null, " "
###
Simply add a slash to a dir if there is not.
###
addSeparatorToPath: ( path ) ->
dir = path
if dir.charAt( dir.length - 1 ) isnt sep
dir += sep
return dir
###
An utility that sorts files acording to their dependencie->
@provide & @require
###
resolveJavascriptDependencies: ( patterns, taskOptions ) ->
# Get all non sorted sources
sources = grunt.file.expand patterns
if taskOptions?.ignore then ignoreList = grunt.file.expand taskOptions.ignore
grunt.verbose.debug "List of files that will be ignored : #{ignoreList}"
grunt.verbose.debug "\nSOURCES BEFORE RESOLVE PHASE: \n #{sources.join '\n'} \n"
# Create the compiled object and array
providerMap = {}
nodes = []
# Create all require and provide arr
allRequires = []
allProvides = []
ignoreThisFile = false
# Loop on all sources to find dependencies
for source in sources
# Tracking files to be ignored
if source in ignoreList then ignoreThisFile = true else ignoreThisFile = false
grunt.verbose.debug "Ignore: #{ignoreThisFile}"
# Read the content of the source
content = grunt.file.read source
# Create require and provides arr
requires = []
provides = []
provide = null
# Skipping regexp search for files in ignoreList
if !ignoreThisFile
# Searching for require("id") expressions
requireRegexp = /.*require\s*\(\s*"([^"]*)"\s*\).*/g
match = requireRegexp.exec content
while match?
line = match[ 0 ].trim()
requireName = match[ 1 ]
if not (line.indexOf( "@ignore" ) >= 0 or line.chartAt(0) is "/" and (line.chartAt(1) is "*" or line.chartAt(1) is "/"))
requires.push requireName
match = requireRegexp.exec content
# Searching for define("id") expressions
provideRegexp = /.*define\s*\(\s*"([^"]*)"\s*,.*/g
match = provideRegexp.exec content
while match?
line = match[ 0 ].trim()
provideName = match[ 1 ]
if not (line.indexOf( "@ignore" ) >= 0 or line.chartAt(0) is "/" and (line.chartAt(1) is "*" or line.chartAt(1) is "/"))
provides.push provideName
match = provideRegexp.exec content
# keeping only one provide per source
if provides.length
provide = provides[ 0 ].trim()
allProvides.push provide
# Find all lines containing @require
requires = requires || []
for require, j in requires
# replace the current value in the array
requires[ j ] = requires[ j ].trim()
# save the value in the allRequires array
if require not in allRequires
allRequires.push require
grunt.verbose.debug "In #{source}..."
grunt.verbose.debug " Provide: #{provide}"
grunt.verbose.debug " Requires: #{requires.join ' / '}"
# Save information for the source into a dictionary object
node =
source: source
requires: requires
provide: provide
# Save the compiled object to the nodes Array
nodes.push node
# Save the object into the compiled dictionnary with the provides as keys
providerMap[ provide ] = node
grunt.verbose.debug "----------------------------------------------------"
for node in nodes
node.edges = []
for require in node.requires
# First, let's see if every require has a provide
if require not in allProvides
grunt.fail.warn "Missing provider : #{require} is not provided !", 3
# Second, let's change require form from a string (provide) to an object n
node.edges.push providerMap[ require ]
# Final list of nodes in order
resolved = []
# Searching leafs nodes which are at the bottom of the tree
leafs = []
for node in nodes
# Removing until there is only the leafs one
if !node.edges.length || !node.provide
resolved.push node
else
leafs.push node
# Iiterative to climb the dependency tre->
resolve = ( node, resolved, unresolved ) ->
# Keeping track of yet unresolved nodes
unresolved.push node
for edge in node.edges
if edge not in resolved
# Checking cyclic dependencies
if edge in unresolved
grunt.fail.fatal "Error : #{node.source} raised cyclic dependencies with #{edge.source}!"
# Digging again
resolve( edge, resolved, unresolved )
# Adding if not present in list of resolved nodes
if node not in resolved
resolved.push node
# Removing from list of unresolved nodes
positionInUnresolved = unresolved.indexOf node
if positionInUnresolved >= 0
unresolved.splice positionInUnresolved, 1
# Climbing the resolve tree from the ground
for node in leafs
# For one path, we cannot have cyclic dependencies
unresolved = []
resolve node, resolved, unresolved
results = []
# Loop on the nodes and save sources into result
for node in resolved
results.push node.source
grunt.verbose.debug "\nSOURCES AFTER RESOLVE PHASE: \n #{results.join '\n'}"
# Return the results array
return results
###
An utility that sorts files acording to their dependencie->
@provide & @require
###
resolveSassDependencies: ( patterns ) ->
# Get all non sorted sources
sources = grunt.file.expand patterns
grunt.verbose.debug "sources: #{@toJSON sources}"
grunt.verbose.debug "\nSOURCES BEFORE RESOLVE PHASE: \n #{sources.join '\n'} \n"
# Create the compiled object and array
providerMap = {}
nodes = []
# Create all require and provide arr
allRequires = []
allProvides = []
# Loop on all sources to find dependences
for source in sources
# Read the content of the source
content = grunt.file.read source
# Get all documentation block comments
comments = content.match( /\/\*([^*]|[\r\n]|(\*+([^*\/]|[\r\n])))*\*+\//g ) || []
# Create requires and provides arrays
requires = []
provides = []
provide = null
# Loop on the comments to get the requires and provides
for comment in comments
requires = requires.concat comment.match( /(@require).*/g ) || []
provides = provides.concat comment.match( /(@provide).*/g ) || []
# Keeping only one provide per source
if provides.length
provide = provides[ 0 ].replace( "@provide", "" ).trim()
allProvides.push provide
# Find all lines containing @require
for require, j in requires
# Replace the current value in the array
requires[j] = requires[j].replace( "@require", "" ).trim()
grunt.verbose.debug "requires[j]: #{@toJSON requires[j]}"
# Save the value in the allRequires array
if requires[j] not in allRequires
allRequires.push requires[j]
grunt.verbose.debug "requires: #{@toJSON requires}"
grunt.verbose.debug "allRequires: #{@toJSON allRequires}"
grunt.verbose.debug "In #{source}..."
grunt.verbose.debug " Provide: #{provide}"
grunt.verbose.debug " Requires: #{requires.join ' / '}"
# Save information for the source into a dictionary object
node =
source: source
requires: requires
provide: provide
# Save the compiled object to the nodes Array
nodes.push node
# Save the object into the compiled dictionnary with the provides as keys
providerMap[ provide ] = node
for node in nodes
node.edges = []
for require in node.requires
# First, let's see if every require has a provide
if require not in allProvides
grunt.fail.warn "Missing provider : #{require} is not provided !", 3
# Second, let's change require form from a string (provide) to an object node
node.edges.push providerMap[ require]
# Final list of nodes in order
resolved = []
# Searching leafs nodes which are at the bottom of the tree
leafs = []
for node in nodes
# Removing until there is only the leafs one
if !node.edges.length || !node.provide
resolved.push node
else
leafs.push node
# Iterative to climb the dependency tre->
resolve = ( node, resolved, unresolved ) ->
# Keeping track of yet unresolved nodes
unresolved.push node
for edge in node.edges
if edge not in resolved
# Checking cyclic dependencies
if edge in unresolved
grunt.fail.fatal "Error : #{node.source} raised cyclic dependencies !"
# Digging again
resolve edge, resolved, unresolved
# Adding if not present in list of resolved nodes
if node not in resolved
resolved.push node
# Removing from list of unresolved nodes
positionInUnresolved = unresolved.indexOf node
if positionInUnresolved >= 0
unresolved.splice positionInUnresolved, 1
# Climbing the resolve tree from the ground
for node in leafs
# For one path, we cannot have cyclic dependencies
unresolved = []
resolve node, resolved, unresolved
results = []
# Loop on the nodes and save sources into result
for node in resolved
results.push node.source
grunt.verbose.debug "\nSOURCES AFTER RESOLVE PHASE: \n #{results.join '\n'}"
# Return the results array
return results
module.exports = new GruntUtils()