1
+ /*
2
+ * monoco
3
+ * A Model and a NoSQL Database for Components
4
+ * http://monoco.io/
5
+ * @ecarriou
6
+ *
7
+ * Copyright (C) 2015 - Erwan Carriou
8
+ *
9
+ * This program is free software: you can redistribute it and/or modify
10
+ * it under the terms of the GNU General Public License as published by
11
+ * the Free Software Foundation, either version 3 of the License, or
12
+ * (at your option) any later version.
13
+ *
14
+ * This program is distributed in the hope that it will be useful,
15
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
+ * GNU General Public License for more details.
18
+ *
19
+ * You should have received a copy of the GNU General Public License
20
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
21
+ */
22
+
23
+
1
24
module . exports = function ( grunt ) {
2
- // config
3
25
grunt . initConfig ( {
4
26
pkg : grunt . file . readJSON ( 'package.json' ) ,
5
27
watch : {
@@ -98,11 +120,10 @@ module.exports = function (grunt) {
98
120
}
99
121
} ,
100
122
concat : {
101
- systemBehaviors : {
123
+ systemInfos : {
102
124
options : {
103
125
process : function ( src , filepath ) {
104
- var uid = generateId ( ) ,
105
- result = '' ;
126
+ var result = '' ;
106
127
107
128
function generateId ( ) {
108
129
function gen ( ) {
@@ -111,41 +132,64 @@ module.exports = function (grunt) {
111
132
return gen ( ) + gen ( ) + gen ( ) ;
112
133
}
113
134
114
- if ( filepath . indexOf ( 'banner' ) !== - 1 || filepath . indexOf ( 'footer' ) !== - 1 ) {
135
+ // ID & version
136
+ src = src . replace ( '{version}' , grunt . file . readJSON ( 'package.json' ) . version ) . trim ( ) ;
137
+ result = src . replace ( '{id}' , generateId ( ) ) ;
115
138
116
- if ( filepath . indexOf ( 'banner' ) !== - 1 ) {
117
-
118
- // ID & version
119
- src = src . replace ( '{version}' , grunt . file . readJSON ( 'package.json' ) . version ) . trim ( ) ;
120
- src = src . replace ( '{id}' , generateId ( ) ) ;
139
+ return result ;
140
+ }
141
+ } ,
142
+ files : {
143
+ 'build/monoco.json' : [ 'src/template/banner/system.txt' ]
144
+ }
145
+ } ,
146
+ systemBehaviors : {
147
+ options : {
148
+ process : function ( src , filepath ) {
149
+ var result = '' ,
150
+ uuid = '' ,
151
+ behaviors = { } ;
121
152
122
- result = src + '\n"behaviors" : {' ;
123
- } else {
124
- result = src ;
153
+ function generateId ( ) {
154
+ function gen ( ) {
155
+ return Math . floor ( ( 1 + Math . random ( ) ) * 0x10000 ) . toString ( 16 ) ;
125
156
}
157
+ return gen ( ) + gen ( ) + gen ( ) ;
158
+ }
126
159
160
+ if ( filepath . indexOf ( 'build' ) !== - 1 ) {
161
+ grunt . option ( 'behaviors' , { } ) ;
162
+ result = src + '\n"behaviors" : {},' ;
127
163
} else {
128
- src = src . replace ( '{' , '{"_id":"' + uid + '",' ) ;
129
- result = '\"' + uid + '\"' + ':' + src . trim ( ) + ',' ;
164
+ behaviors = grunt . option ( 'behaviors' ) ;
165
+ uuid = JSON . parse ( src ) . _id ;
166
+ if ( typeof uuid === 'undefined' ) {
167
+ uuid = generateId ( ) ;
168
+ src = src . replace ( '{' , '{"_id":"' + uuid + '",' ) ;
169
+ }
170
+ behaviors [ uuid ] = JSON . parse ( src ) ;
130
171
}
131
172
return result ;
132
173
}
133
174
} ,
134
175
files : {
135
- 'build/monoco.json' : [ 'src/template/banner/system.txt ' , 'src/system/behaviors/*/*.json' ]
176
+ 'build/monoco.json' : [ 'build/monoco.json ' , 'src/system/behaviors/*/*.json' ]
136
177
}
137
178
} ,
138
179
systemSchemas : {
139
180
options : {
140
181
process : function ( src , filepath ) {
141
182
var result = '' ,
142
- uid = '' ;
183
+ uuid = '' ,
184
+ schemas = { } ;
143
185
144
186
if ( filepath . indexOf ( 'build' ) !== - 1 ) {
145
- result = src + '},\n"schemas" : {' ;
187
+ grunt . option ( 'schemas' , { } ) ;
188
+ result = src + '\n"schemas" : {},' ;
146
189
} else {
147
- uid = JSON . parse ( src ) . _id ;
148
- result = '\"' + uid + '\"' + ':' + src . trim ( ) + ',' ;
190
+ uuid = JSON . parse ( src ) . _id ;
191
+ schemas = grunt . option ( 'schemas' ) ;
192
+ schemas [ uuid ] = JSON . parse ( src ) ;
149
193
}
150
194
return result ;
151
195
}
@@ -158,13 +202,16 @@ module.exports = function (grunt) {
158
202
options : {
159
203
process : function ( src , filepath ) {
160
204
var result = '' ,
161
- uid = '' ;
205
+ uuid = '' ,
206
+ types = { } ;
162
207
163
208
if ( filepath . indexOf ( 'build' ) !== - 1 ) {
164
- result = src + '},\n"types" : {' ;
209
+ grunt . option ( 'types' , { } ) ;
210
+ result = src + '\n"types" : {},' ;
165
211
} else {
166
- uid = JSON . parse ( src ) . name ;
167
- result = '\"' + uid + '\"' + ':' + src . trim ( ) + ',' ;
212
+ uuid = JSON . parse ( src ) . name ;
213
+ types = grunt . option ( 'types' ) ;
214
+ types [ uuid ] = JSON . parse ( src ) ;
168
215
}
169
216
return result ;
170
217
}
@@ -177,18 +224,28 @@ module.exports = function (grunt) {
177
224
options : {
178
225
process : function ( src , filepath ) {
179
226
var result = '' ,
180
- uid = '' ,
181
- collectionName = '' ;
227
+ uuid = '' ,
228
+ collectionName = '' ,
229
+ components = { } ;
182
230
183
231
if ( filepath . indexOf ( 'build' ) !== - 1 ) {
184
- result = src + '},\n"components" : {' ;
232
+ result = src + '\n"components" : {}\n}' ;
233
+ grunt . option ( 'components' , { } ) ;
185
234
} else {
186
- uid = JSON . parse ( src ) . _id ;
235
+ components = grunt . option ( 'components' ) ;
236
+
237
+ uuid = JSON . parse ( src ) . _id ;
187
238
188
239
collectionName = filepath . split ( 'components/' ) [ 1 ] ;
189
240
collectionName = collectionName . split ( '/' ) [ 0 ] ;
241
+
190
242
src = src . replace ( '{version}' , grunt . file . readJSON ( 'package.json' ) . version ) . trim ( ) ;
191
- result = '\"' + collectionName + '\": {\"' + uid + '\"' + ':' + src . trim ( ) + '},' ;
243
+
244
+ if ( typeof components [ collectionName ] === 'undefined' ) {
245
+ components [ collectionName ] = { } ;
246
+ }
247
+
248
+ components [ collectionName ] [ uuid ] = JSON . parse ( src ) ;
192
249
}
193
250
return result ;
194
251
}
@@ -197,23 +254,22 @@ module.exports = function (grunt) {
197
254
'build/monoco.json' : [ 'build/monoco.json' , 'src/system/components/*/*.json' ]
198
255
}
199
256
} ,
200
- systemClean : {
257
+ systemFill : {
201
258
options : {
202
259
process : function ( src , filepath ) {
203
- var result = '' ;
260
+ var system = { } ;
204
261
205
- if ( filepath . indexOf ( 'build' ) !== - 1 ) {
206
- src = src . replace ( / \} \, \} \, / g, '}},' ) ;
207
- result = src . substring ( 0 , src . length - 1 ) ;
208
- } else {
209
- result = src ;
210
- }
262
+ system = JSON . parse ( src ) ;
263
+ system . components = grunt . option ( 'components' ) ;
264
+ system . schemas = grunt . option ( 'schemas' ) ;
265
+ system . types = grunt . option ( 'types' ) ;
266
+ system . behaviors = grunt . option ( 'behaviors' ) ;
211
267
212
- return result ;
268
+ return JSON . stringify ( system ) ;
213
269
}
214
270
} ,
215
271
files : {
216
- 'build/monoco.json' : [ 'build/monoco.json' , 'src/template/footer/system.txt' ]
272
+ 'build/monoco.json' : [ 'build/monoco.json' ]
217
273
}
218
274
} ,
219
275
systemModule : {
@@ -235,6 +291,7 @@ module.exports = function (grunt) {
235
291
236
292
} ) ;
237
293
294
+
238
295
// default tasks
239
296
grunt . loadNpmTasks ( 'grunt-browserify' ) ;
240
297
grunt . loadNpmTasks ( 'grunt-contrib-watch' ) ;
@@ -251,11 +308,12 @@ module.exports = function (grunt) {
251
308
252
309
// system JSON task
253
310
grunt . registerTask ( 'system-json' , [
311
+ 'concat:systemInfos' ,
254
312
'concat:systemBehaviors' ,
255
313
'concat:systemSchemas' ,
256
- 'concat:systemComponents' ,
257
314
'concat:systemTypes' ,
258
- 'concat:systemClean'
315
+ 'concat:systemComponents' ,
316
+ 'concat:systemFill'
259
317
] ) ;
260
318
261
319
// system node task
@@ -298,4 +356,4 @@ module.exports = function (grunt) {
298
356
'concat:licence' ,
299
357
'karma:monoco'
300
358
] ) ;
301
- } ;
359
+ } ;
0 commit comments