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