1 | var FS = require("fs");
|
2 | var Libs = require("./compiler-com-libs");
|
3 | var Path = require("path");
|
4 | var Tree = require("./htmltree");
|
5 | var Fatal = require("./fatal");
|
6 | var Utils = require("./pathutils");
|
7 | var Source = require("./source");
|
8 |
|
9 | var Components = [];
|
10 | var TagRegExps = [];
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | exports.loadComponents = function(prj) {
|
16 | var pathes = [prj.srcPath("com")];
|
17 | prj.getExtraModulesPath().forEach(
|
18 | function(extraModulePath) {
|
19 | pathes.push(Path.join(extraModulePath, "com"));
|
20 | }
|
21 | );
|
22 | pathes.push(prj.libPath("com"));
|
23 |
|
24 | Components = [];
|
25 | TagRegExps = [];
|
26 |
|
27 |
|
28 |
|
29 | var foundComponents = [];
|
30 |
|
31 | pathes.forEach(function (path) {
|
32 | var components = [];
|
33 | Utils.findFilesByExtension(path, ".com.js").forEach(function (comPath) {
|
34 | var name = Path.relative(path, comPath);
|
35 | var id = Path.basename(comPath);
|
36 | id = id.substr(0, id.length - 7);
|
37 |
|
38 | if (foundComponents.indexOf(name) > -1) {
|
39 |
|
40 | return;
|
41 | }
|
42 | foundComponents.push(name);
|
43 |
|
44 |
|
45 | var com;
|
46 | try {
|
47 | com = require(comPath);
|
48 | }
|
49 | catch (ex) {
|
50 | Fatal.bubble(ex, comPath);
|
51 | }
|
52 | com.$ = { path: comPath, name: name, id: id };
|
53 |
|
54 |
|
55 | var cssName = com.$.name.substr(0, com.$.name.length - 3) + ".css";
|
56 | var cssPath = prj.srcOrLibPath(Path.join("com", cssName));
|
57 | if (FS.existsSync(cssPath)) {
|
58 | com.$.css = FS.readFileSync(cssPath).toString();
|
59 |
|
60 | }
|
61 |
|
62 |
|
63 | var resPath = comPath.substr(0, comPath.length - 3);
|
64 | if (FS.existsSync(resPath)) {
|
65 | com.$.res = Path.join('com', Path.relative(path, resPath));
|
66 | }
|
67 |
|
68 | if (typeof com.tags === 'undefined') {
|
69 | Fatal.fire(
|
70 | "Missing the mandatory attribute \"tags\"!",
|
71 | "Bad Component Definition",
|
72 | comPath
|
73 | );
|
74 | }
|
75 | if (typeof com.priotity !== 'number') com.priotity = 0;
|
76 | if (typeof com.compile !== 'function') {
|
77 | Fatal.fire(
|
78 | "Missing the mandatory function \"compile(root libs)\"!",
|
79 | "Bad Component Definition",
|
80 | comPath
|
81 | );
|
82 | }
|
83 | components.push(com);
|
84 | });
|
85 | components.sort(function(a, b) {
|
86 | return b.priotity - a.priotity;
|
87 | });
|
88 | components.forEach(function(item) {
|
89 | Components.push(item);
|
90 | });
|
91 | });
|
92 |
|
93 | Components.forEach(function (com) {
|
94 | var filters = com.tags;
|
95 | if (!Array.isArray(filters)) {
|
96 | filters = [filters];
|
97 | }
|
98 | var regexps = [];
|
99 | filters.forEach(function (filter) {
|
100 | try {
|
101 | var rx = new RegExp("^" + filter + "$", "i");
|
102 | regexps.push(rx);
|
103 | }
|
104 | catch (ex) {
|
105 | Fatal.fire(
|
106 | "Bad regexp for component " + com.$.path + "\n" + ex,
|
107 | "Tags was defined as " + JSON.stringify(com.tags)
|
108 | );
|
109 | }
|
110 | });
|
111 | TagRegExps.push(regexps);
|
112 | });
|
113 | };
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 | exports.getCompilerForTag = function(tagName) {
|
121 | var i, k, rx, regexps, component, cssFile;
|
122 | for (i = 0; i < Components.length; i++) {
|
123 | regexps = TagRegExps[i];
|
124 | for (k = 0; k < regexps.length; k++) {
|
125 | rx = regexps[k];
|
126 | if (rx.test(tagName)) {
|
127 | component = Components[i];
|
128 | return component;
|
129 | }
|
130 | }
|
131 | }
|
132 | return null;
|
133 | };
|
134 |
|
135 |
|
136 |
|
137 |
|
138 | function isHtmlFileUptodate(source) {
|
139 | var dependencies = source.tag("dependencies") || [];
|
140 | var i, dep, file, prj = source.prj(),
|
141 | stat,
|
142 | mtime = source.modificationTime();
|
143 | for (i = 0 ; i < dependencies.length ; i++) {
|
144 | dep = dependencies[i];
|
145 | file = prj.srcOrLibPath(dep);
|
146 | if (file) {
|
147 | stat = FS.statSync(file);
|
148 | if (stat.mtime > mtime) return false;
|
149 | }
|
150 | }
|
151 | return source.isUptodate();
|
152 | }
|