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+
124module . exports = function ( grunt ) {
2- // config
325 grunt . initConfig ( {
426 pkg : grunt . file . readJSON ( 'package.json' ) ,
527 watch : {
@@ -98,11 +120,10 @@ module.exports = function (grunt) {
98120 }
99121 } ,
100122 concat : {
101- systemBehaviors : {
123+ systemInfos : {
102124 options : {
103125 process : function ( src , filepath ) {
104- var uid = generateId ( ) ,
105- result = '' ;
126+ var result = '' ;
106127
107128 function generateId ( ) {
108129 function gen ( ) {
@@ -111,41 +132,64 @@ module.exports = function (grunt) {
111132 return gen ( ) + gen ( ) + gen ( ) ;
112133 }
113134
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 ( ) ) ;
115138
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 = { } ;
121152
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 ) ;
125156 }
157+ return gen ( ) + gen ( ) + gen ( ) ;
158+ }
126159
160+ if ( filepath . indexOf ( 'build' ) !== - 1 ) {
161+ grunt . option ( 'behaviors' , { } ) ;
162+ result = src + '\n"behaviors" : {},' ;
127163 } 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 ) ;
130171 }
131172 return result ;
132173 }
133174 } ,
134175 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' ]
136177 }
137178 } ,
138179 systemSchemas : {
139180 options : {
140181 process : function ( src , filepath ) {
141182 var result = '' ,
142- uid = '' ;
183+ uuid = '' ,
184+ schemas = { } ;
143185
144186 if ( filepath . indexOf ( 'build' ) !== - 1 ) {
145- result = src + '},\n"schemas" : {' ;
187+ grunt . option ( 'schemas' , { } ) ;
188+ result = src + '\n"schemas" : {},' ;
146189 } 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 ) ;
149193 }
150194 return result ;
151195 }
@@ -158,13 +202,16 @@ module.exports = function (grunt) {
158202 options : {
159203 process : function ( src , filepath ) {
160204 var result = '' ,
161- uid = '' ;
205+ uuid = '' ,
206+ types = { } ;
162207
163208 if ( filepath . indexOf ( 'build' ) !== - 1 ) {
164- result = src + '},\n"types" : {' ;
209+ grunt . option ( 'types' , { } ) ;
210+ result = src + '\n"types" : {},' ;
165211 } 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 ) ;
168215 }
169216 return result ;
170217 }
@@ -177,18 +224,28 @@ module.exports = function (grunt) {
177224 options : {
178225 process : function ( src , filepath ) {
179226 var result = '' ,
180- uid = '' ,
181- collectionName = '' ;
227+ uuid = '' ,
228+ collectionName = '' ,
229+ components = { } ;
182230
183231 if ( filepath . indexOf ( 'build' ) !== - 1 ) {
184- result = src + '},\n"components" : {' ;
232+ result = src + '\n"components" : {}\n}' ;
233+ grunt . option ( 'components' , { } ) ;
185234 } else {
186- uid = JSON . parse ( src ) . _id ;
235+ components = grunt . option ( 'components' ) ;
236+
237+ uuid = JSON . parse ( src ) . _id ;
187238
188239 collectionName = filepath . split ( 'components/' ) [ 1 ] ;
189240 collectionName = collectionName . split ( '/' ) [ 0 ] ;
241+
190242 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 ) ;
192249 }
193250 return result ;
194251 }
@@ -197,23 +254,22 @@ module.exports = function (grunt) {
197254 'build/monoco.json' : [ 'build/monoco.json' , 'src/system/components/*/*.json' ]
198255 }
199256 } ,
200- systemClean : {
257+ systemFill : {
201258 options : {
202259 process : function ( src , filepath ) {
203- var result = '' ;
260+ var system = { } ;
204261
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' ) ;
211267
212- return result ;
268+ return JSON . stringify ( system ) ;
213269 }
214270 } ,
215271 files : {
216- 'build/monoco.json' : [ 'build/monoco.json' , 'src/template/footer/system.txt' ]
272+ 'build/monoco.json' : [ 'build/monoco.json' ]
217273 }
218274 } ,
219275 systemModule : {
@@ -235,6 +291,7 @@ module.exports = function (grunt) {
235291
236292 } ) ;
237293
294+
238295 // default tasks
239296 grunt . loadNpmTasks ( 'grunt-browserify' ) ;
240297 grunt . loadNpmTasks ( 'grunt-contrib-watch' ) ;
@@ -251,11 +308,12 @@ module.exports = function (grunt) {
251308
252309 // system JSON task
253310 grunt . registerTask ( 'system-json' , [
311+ 'concat:systemInfos' ,
254312 'concat:systemBehaviors' ,
255313 'concat:systemSchemas' ,
256- 'concat:systemComponents' ,
257314 'concat:systemTypes' ,
258- 'concat:systemClean'
315+ 'concat:systemComponents' ,
316+ 'concat:systemFill'
259317 ] ) ;
260318
261319 // system node task
@@ -298,4 +356,4 @@ module.exports = function (grunt) {
298356 'concat:licence' ,
299357 'karma:monoco'
300358 ] ) ;
301- } ;
359+ } ;
0 commit comments