1
+ module . exports = function ( grunt ) {
2
+
3
+ var path = require ( "path" ) ;
4
+
5
+ grunt . registerMultiTask ( "copy" , "Copy files to destination folder and replace @VERSION with pkg.version" , function ( ) {
6
+ function replaceVersion ( source ) {
7
+ return source . replace ( / @ V E R S I O N / g, grunt . config ( "pkg.version" ) ) ;
8
+ }
9
+ function copyFile ( src , dest ) {
10
+ if ( / ( j s | c s s ) $ / . test ( src ) ) {
11
+ grunt . file . copy ( src , dest , {
12
+ process : replaceVersion
13
+ } ) ;
14
+ } else {
15
+ grunt . file . copy ( src , dest ) ;
16
+ }
17
+ }
18
+ var files = grunt . file . expandFiles ( this . file . src ) ,
19
+ target = this . file . dest + "/" ,
20
+ strip = this . data . strip ,
21
+ renameCount = 0 ,
22
+ fileName ;
23
+ if ( typeof strip === "string" ) {
24
+ strip = new RegExp ( "^" + grunt . template . process ( strip , grunt . config ( ) ) . replace ( / [ \- \[ \] { } ( ) * + ? . , \\ \^ $ | # \s ] / g, "\\$&" ) ) ;
25
+ }
26
+ files . forEach ( function ( fileName ) {
27
+ var targetFile = strip ? fileName . replace ( strip , "" ) : fileName ;
28
+ copyFile ( fileName , target + targetFile ) ;
29
+ } ) ;
30
+ grunt . log . writeln ( "Copied " + files . length + " files." ) ;
31
+ for ( fileName in this . data . renames ) {
32
+ renameCount += 1 ;
33
+ copyFile ( fileName , target + grunt . template . process ( this . data . renames [ fileName ] , grunt . config ( ) ) ) ;
34
+ }
35
+ if ( renameCount ) {
36
+ grunt . log . writeln ( "Renamed " + renameCount + " files." ) ;
37
+ }
38
+ } ) ;
39
+
40
+
41
+ grunt . registerMultiTask ( "zip" , "Create a zip file for release" , function ( ) {
42
+ // TODO switch back to adm-zip for better cross-platform compability once it actually works
43
+ // 0.1.3 works, but result can't be unzipped
44
+ // its also a lot slower then zip program, probably due to how its used...
45
+ // var files = grunt.file.expandFiles( "dist/" + this.file.src + "/**/*" );
46
+ // grunt.log.writeln( "Creating zip file " + this.file.dest );
47
+
48
+ //var AdmZip = require( "adm-zip" );
49
+ //var zip = new AdmZip();
50
+ //files.forEach(function( file ) {
51
+ // grunt.verbose.writeln( "Zipping " + file );
52
+ // // rewrite file names from dist folder (created by build), drop the /dist part
53
+ // zip.addFile(file.replace(/^dist/, "" ), fs.readFileSync( file ) );
54
+ //});
55
+ //zip.writeZip( "dist/" + this.file.dest );
56
+ //grunt.log.writeln( "Wrote " + files.length + " files to " + this.file.dest );
57
+
58
+ var done = this . async ( ) ,
59
+ dest = this . file . dest ,
60
+ src = grunt . template . process ( this . file . src , grunt . config ( ) ) ;
61
+ grunt . utils . spawn ( {
62
+ cmd : "zip" ,
63
+ args : [ "-r" , dest , src ] ,
64
+ opts : {
65
+ cwd : 'dist'
66
+ }
67
+ } , function ( err , result ) {
68
+ if ( err ) {
69
+ grunt . log . error ( err ) ;
70
+ done ( ) ;
71
+ return ;
72
+ }
73
+ grunt . log . writeln ( "Zipped " + dest ) ;
74
+ done ( ) ;
75
+ } ) ;
76
+ } ) ;
77
+
78
+ grunt . registerMultiTask ( "md5" , "Create list of md5 hashes for CDN uploads" , function ( ) {
79
+ // remove dest file before creating it, to make sure itself is not included
80
+ if ( path . existsSync ( this . file . dest ) ) {
81
+ fs . unlinkSync ( this . file . dest ) ;
82
+ }
83
+ var crypto = require ( "crypto" ) ,
84
+ dir = this . file . src + "/" ,
85
+ hashes = [ ] ;
86
+ grunt . file . expandFiles ( dir + "**/*" ) . forEach ( function ( fileName ) {
87
+ var hash = crypto . createHash ( "md5" ) ;
88
+ hash . update ( grunt . file . read ( fileName , "ascii" ) ) ;
89
+ hashes . push ( fileName . replace ( dir , "" ) + " " + hash . digest ( "hex" ) ) ;
90
+ } ) ;
91
+ grunt . file . write ( this . file . dest , hashes . join ( "\n" ) + "\n" ) ;
92
+ grunt . log . writeln ( "Wrote " + this . file . dest + " with " + hashes . length + " hashes" ) ;
93
+ } ) ;
94
+
95
+ // only needed for 1.8
96
+ grunt . registerTask ( "download_docs" , function ( ) {
97
+ function capitalize ( value ) {
98
+ return value [ 0 ] . toUpperCase ( ) + value . slice ( 1 ) ;
99
+ }
100
+ // should be grunt.config("pkg.version")?
101
+ var version = "1.8" ,
102
+ docsDir = "dist/docs" ,
103
+ files = "draggable droppable resizable selectable sortable accordion autocomplete button datepicker dialog progressbar slider tabs position"
104
+ . split ( " " ) . map ( function ( widget ) {
105
+ return {
106
+ url : "http://docs.jquery.com/action/render/UI/API/" + version + "/" + capitalize ( widget ) ,
107
+ dest : docsDir + '/' + widget + '.html'
108
+ } ;
109
+ } ) ;
110
+ files = files . concat ( "animate addClass effect hide removeClass show switchClass toggle toggleClass" . split ( " " ) . map ( function ( widget ) {
111
+ return {
112
+ url : "http://docs.jquery.com/action/render/UI/Effects/" + widget ,
113
+ dest : docsDir + '/' + widget + '.html'
114
+ } ;
115
+ } ) ) ;
116
+ files = files . concat ( "Blind Clip Drop Explode Fade Fold Puff Slide Scale Bounce Highlight Pulsate Shake Size Transfer" . split ( " " ) . map ( function ( widget ) {
117
+ return {
118
+ url : "http://docs.jquery.com/action/render/UI/Effects/" + widget ,
119
+ dest : docsDir + '/effect-' + widget . toLowerCase ( ) + '.html'
120
+ } ;
121
+ } ) ) ;
122
+ grunt . file . mkdir ( "dist/docs" ) ;
123
+ grunt . utils . async . forEach ( files , function ( file , done ) {
124
+ var out = fs . createWriteStream ( file . dest ) ;
125
+ out . on ( "close" , done ) ;
126
+ request ( file . url ) . pipe ( out ) ;
127
+ } , this . async ( ) ) ;
128
+ } ) ;
129
+
130
+ grunt . registerTask ( "download_themes" , function ( ) {
131
+ // var AdmZip = require('adm-zip');
132
+ var done = this . async ( ) ,
133
+ themes = grunt . file . read ( "build/themes" ) . split ( "," ) ,
134
+ requests = 0 ;
135
+ grunt . file . mkdir ( "dist/tmp" ) ;
136
+ themes . forEach ( function ( theme , index ) {
137
+ requests += 1 ;
138
+ grunt . file . mkdir ( "dist/tmp/" + index ) ;
139
+ var zipFileName = "dist/tmp/" + index + ".zip" ,
140
+ out = fs . createWriteStream ( zipFileName ) ;
141
+ out . on ( "close" , function ( ) {
142
+ grunt . log . writeln ( "done downloading " + zipFileName ) ;
143
+ // TODO AdmZip produces "crc32 checksum failed", need to figure out why
144
+ // var zip = new AdmZip(zipFileName);
145
+ // zip.extractAllTo('dist/tmp/' + index + '/');
146
+ // until then, using cli unzip...
147
+ grunt . utils . spawn ( {
148
+ cmd : "unzip" ,
149
+ args : [ "-d" , "dist/tmp/" + index , zipFileName ]
150
+ } , function ( err , result ) {
151
+ grunt . log . writeln ( "Unzipped " + zipFileName + ", deleting it now" ) ;
152
+ fs . unlinkSync ( zipFileName ) ;
153
+ requests -= 1 ;
154
+ if ( requests === 0 ) {
155
+ done ( ) ;
156
+ }
157
+ } ) ;
158
+ } ) ;
159
+ request ( "http://ui-dev.jquery.com/download/?" + theme ) . pipe ( out ) ;
160
+ } ) ;
161
+ } ) ;
162
+
163
+ grunt . registerTask ( "copy_themes" , function ( ) {
164
+ // each package includes the base theme, ignore that
165
+ var filter = / t h e m e s \/ b a s e / ,
166
+ files = grunt . file . expandFiles ( "dist/tmp/*/development-bundle/themes/**/*" ) . filter ( function ( file ) {
167
+ return ! filter . test ( file ) ;
168
+ } ) ,
169
+ // TODO the grunt.template.process call shouldn't be necessary
170
+ target = "dist/" + grunt . template . process ( grunt . config ( "files.themes" ) , grunt . config ( ) ) + "/" ,
171
+ distFolder = "dist/" + grunt . template . process ( grunt . config ( "files.dist" ) , grunt . config ( ) ) ;
172
+ files . forEach ( function ( fileName ) {
173
+ var targetFile = fileName . replace ( / d i s t \/ t m p \/ \d + \/ d e v e l o p m e n t - b u n d l e \/ / , "" ) . replace ( "jquery-ui-.custom" , "jquery-ui" ) ;
174
+ grunt . file . copy ( fileName , target + targetFile ) ;
175
+ } ) ;
176
+
177
+ // copy minified base theme from regular release
178
+ files = grunt . file . expandFiles ( distFolder + "/themes/base/**/*" ) ;
179
+ files . forEach ( function ( fileName ) {
180
+ grunt . file . copy ( fileName , target + fileName . replace ( distFolder , "" ) ) ;
181
+ } ) ;
182
+ } ) ;
183
+
184
+ grunt . registerTask ( "clean" , function ( ) {
185
+ require ( "rimraf" ) . sync ( "dist" ) ;
186
+ } ) ;
187
+
188
+ } ;
0 commit comments