1+ // Generated on 2015-11-02 using generator-mendix 1.0.0 :: http://github.com/JelteMX/generator-mendix
2+ 'use strict' ;
3+
4+ var path = require ( 'path' ) ,
5+ mendixApp = require ( 'node-mendix-modeler-path' ) ,
6+ base64 = require ( 'node-base64-image' ) ,
7+ fs = require ( 'fs' ) ,
8+ xml2js = require ( 'xml2js' ) ,
9+ parser = new xml2js . Parser ( ) ,
10+ builder = new xml2js . Builder ( ) ,
11+ shelljs = require ( 'shelljs' ) ;
12+
13+ // In case you seem to have trouble starting Mendix through `grunt start-mendix`, you might have to set the path to the Mendix application.
14+ // If it works, leave MODELER_PATH at null
15+ var MODELER_PATH = null ;
16+ var MODELER_ARGS = '/file:{path}' ;
17+
18+ var TEST_PATH = path . join ( shelljs . pwd ( ) , './test/[Test] DocumentViewer.mpr' ) ;
19+
20+ module . exports = function ( grunt ) {
21+ var pkg = grunt . file . readJSON ( "package.json" ) ;
22+ grunt . verbose ;
23+ grunt . initConfig ( {
24+ watch : {
25+ autoDeployUpdate : {
26+ "files" : [ "./src/**/*" ] ,
27+ "tasks" : [ "newer:copy" , "compress" ] ,
28+ options : {
29+ debounceDelay : 250 ,
30+ livereload : true
31+ }
32+ }
33+ } ,
34+ compress : {
35+ makezip : {
36+ options : {
37+ archive : "./dist/" + pkg . name + ".mpk" ,
38+ mode : "zip"
39+ } ,
40+ files : [ {
41+ expand : true ,
42+ date : new Date ( ) ,
43+ store : false ,
44+ cwd : "./src" ,
45+ src : [ "**/*" ]
46+ } ]
47+ }
48+ } ,
49+ copy : {
50+ deployment : {
51+ files : [
52+ { dest : "./test/deployment/web/widgets" , cwd : "./src/" , src : [ "**/*" ] , expand : true }
53+ ]
54+ } ,
55+ mpks : {
56+ files : [
57+ { dest : "./test/widgets" , cwd : "./dist/" , src : [ pkg . name + ".mpk" ] , expand : true }
58+ ]
59+ }
60+ } ,
61+ clean : {
62+ build : [
63+ "./dist/" + pkg . name + "/*" ,
64+ "./test/deployment/web/widgets/" + pkg . name + "/*" ,
65+ "./test/widgets/" + pkg . name + ".mpk"
66+ ]
67+ }
68+ } ) ;
69+
70+ grunt . loadNpmTasks ( "grunt-contrib-compress" ) ;
71+ grunt . loadNpmTasks ( "grunt-contrib-clean" ) ;
72+ grunt . loadNpmTasks ( "grunt-contrib-watch" ) ;
73+ grunt . loadNpmTasks ( "grunt-contrib-copy" ) ;
74+ grunt . loadNpmTasks ( "grunt-newer" ) ;
75+
76+ grunt . registerTask ( "start-mendix" , function ( ) {
77+ var done = this . async ( ) ,
78+ testProjectPath = TEST_PATH !== null ? TEST_PATH : path . join ( shelljs . pwd ( ) , '/test/Test.mpr' ) ;
79+
80+ if ( MODELER_PATH !== null || ( mendixApp . err === null && mendixApp . output !== null && mendixApp . output . cmd && mendixApp . output . arg ) ) {
81+ grunt . util . spawn ( {
82+ cmd : MODELER_PATH || mendixApp . output . cmd ,
83+ args : [
84+ ( MODELER_PATH !== null ? MODELER_ARGS : mendixApp . output . arg ) . replace ( '{path}' , testProjectPath )
85+ ]
86+ } , function ( ) {
87+ done ( ) ;
88+ } ) ;
89+ } else {
90+ console . error ( 'Cannot start Modeler, see error:' ) ;
91+ console . log ( mendixApp . err ) ;
92+ done ( ) ;
93+ }
94+ } ) ;
95+
96+ grunt . registerTask ( "generate-icon" , function ( ) {
97+ var iconPath = path . join ( shelljs . pwd ( ) , '/ico.png' ) ,
98+ widgetXml = path . join ( shelljs . pwd ( ) , '/src/' , pkg . name , '/' , pkg . name + '.xml' ) ,
99+ options = { localFile : true , string : true } ,
100+ done = this . async ( ) ;
101+
102+ grunt . log . writeln ( 'Processing icon' ) ;
103+
104+ if ( ! grunt . file . exists ( iconPath ) || ! grunt . file . exists ( widgetXml ) ) {
105+ grunt . log . error ( "can't generate icon" ) ;
106+ return done ( ) ;
107+ }
108+
109+ base64 . base64encoder ( iconPath , options , function ( err , image ) {
110+ if ( ! err ) {
111+ var xmlOld = grunt . file . read ( widgetXml ) ;
112+ parser . parseString ( xmlOld , function ( err , result ) {
113+ if ( ! err ) {
114+ if ( result && result . widget && result . widget . icon ) {
115+ result . widget . icon [ 0 ] = image ;
116+ }
117+ var xmlString = builder . buildObject ( result ) ;
118+ grunt . file . write ( widgetXml , xmlString ) ;
119+ done ( ) ;
120+ }
121+ } ) ;
122+ }
123+ } ) ;
124+ } ) ;
125+
126+ grunt . registerTask (
127+ "default" ,
128+ "Watches for changes and automatically creates an MPK file, as well as copying the changes to your deployment folder" ,
129+ [ "watch" ]
130+ ) ;
131+
132+ grunt . registerTask (
133+ "clean build" ,
134+ "Compiles all the assets and copies the files to the build directory." ,
135+ [ "clean" , "compress" , "copy" ]
136+ ) ;
137+
138+ grunt . registerTask (
139+ "build" ,
140+ [ "clean build" ]
141+ ) ;
142+ } ;
0 commit comments