1 | define([ |
---|
2 | "../buildControl", |
---|
3 | "../fileUtils", |
---|
4 | "../fs", |
---|
5 | "dojo/_base/lang", |
---|
6 | "dojo/_base/array", |
---|
7 | "dojo/json" |
---|
8 | ], function(bc, fileUtils, fs, lang, array, json){ |
---|
9 | return function(resource, callback){ |
---|
10 | if(!bc.depsDumpDotFilename && !bc.depsDumpFilename){ |
---|
11 | return 0; |
---|
12 | } |
---|
13 | |
---|
14 | var dotModules = 0, traceForDot = {}, traceForDotDone = {}; |
---|
15 | if(bc.dotModules){ |
---|
16 | dotModules = {}; |
---|
17 | array.forEach(bc.dotModules.split(","), function(module){ |
---|
18 | dotModules[lang.trim(module)] = traceForDot; |
---|
19 | }); |
---|
20 | } |
---|
21 | |
---|
22 | var modules = [], |
---|
23 | midToId = {}, |
---|
24 | i = 0, |
---|
25 | dotOutput = "digraph {\n", |
---|
26 | r, p, destFilename; |
---|
27 | for(p in bc.resources){ |
---|
28 | r = bc.resources[p]; |
---|
29 | if(r.deps){ |
---|
30 | if(!dotModules || dotModules[r.mid]){ |
---|
31 | dotModules[r.mid] = traceForDotDone; |
---|
32 | r.deps.forEach(function(module){ |
---|
33 | dotOutput += '"' + r.mid + '" -> "' + module.mid + '";\n'; |
---|
34 | if (dotModules[module.mid]!==traceForDotDone){ |
---|
35 | dotModules[module.mid] = traceForDot; |
---|
36 | } |
---|
37 | }); |
---|
38 | } |
---|
39 | r.uid = i; |
---|
40 | midToId[bc.resources[p].mid] = i; |
---|
41 | modules.push(r); |
---|
42 | i++; |
---|
43 | } |
---|
44 | } |
---|
45 | |
---|
46 | if(bc.depsDumpDotFilename){ |
---|
47 | var foundOne = dotModules; |
---|
48 | while(foundOne){ |
---|
49 | foundOne = false; |
---|
50 | for(p in bc.resources){ |
---|
51 | r = bc.resources[p]; |
---|
52 | if(dotModules[r.mid]==traceForDot){ |
---|
53 | foundOne = true; |
---|
54 | dotModules[r.mid] = traceForDotDone; |
---|
55 | if(r.deps){ |
---|
56 | r.deps.forEach(function(module){ |
---|
57 | dotOutput += '"' + r.mid + '" -> "' + module.mid + '";\n'; |
---|
58 | if (dotModules[module.mid]!==traceForDotDone){ |
---|
59 | dotModules[module.mid] = traceForDot; |
---|
60 | } |
---|
61 | }); |
---|
62 | } |
---|
63 | } |
---|
64 | } |
---|
65 | } |
---|
66 | dotOutput += "}\n"; |
---|
67 | |
---|
68 | var filename = fileUtils.computePath(bc.depsDumpDotFilename, bc.destBasePath); |
---|
69 | fileUtils.ensureDirectory(fileUtils.getFilepath(filename)); |
---|
70 | fs.writeFileSync(filename, dotOutput, "ascii"); |
---|
71 | } |
---|
72 | |
---|
73 | if(bc.depsDumpFilename){ |
---|
74 | var depsTree = modules.map(function(module){ |
---|
75 | return module.deps.map(function(item){ return item.uid; }); |
---|
76 | }), |
---|
77 | idTree = {}, |
---|
78 | getItem = function(parts, bag){ |
---|
79 | var part = parts.shift(); |
---|
80 | if(!(part in bag)){ |
---|
81 | bag[part] = {}; |
---|
82 | } |
---|
83 | if(parts.length){ |
---|
84 | return getItem(parts, bag[part]); |
---|
85 | }else{ |
---|
86 | return bag[part]; |
---|
87 | } |
---|
88 | }; |
---|
89 | modules.forEach(function(item, i){ |
---|
90 | var parts = item.mid.split("/"); |
---|
91 | getItem(parts, idTree)["*"] = i; |
---|
92 | }); |
---|
93 | |
---|
94 | filename = fileUtils.computePath(bc.depsDumpFilename, bc.destBasePath); |
---|
95 | fileUtils.ensureDirectory(fileUtils.getFilepath(filename)); |
---|
96 | fs.writeFileSync(filename, json.stringify({depsTree:depsTree, idTree:idTree}), "ascii"); |
---|
97 | } |
---|
98 | |
---|
99 | return 0; |
---|
100 | }; |
---|
101 | }); |
---|