-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathpatch_wordpress.js
More file actions
354 lines (296 loc) · 11 KB
/
patch_wordpress.js
File metadata and controls
354 lines (296 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*
* grunt-patch-wordpress
* https://github.com/aaronjorbin/grunt-patch-wordpress
* Based on https://gist.github.com/markjaquith/4219135
*
*
* Copyright (c) 2013 Aaron Jorbin
* Licensed under the MIT license.
*/
var request = require( 'request' )
, exec = require( 'child_process' ).exec
, spawn = require( 'child_process' ).spawn
, inquirer = require( 'inquirer' )
, url = require( 'url' )
, fs = require( 'fs' )
, _ = require( 'underscore' )
, trac = require( '../lib/trac.js' )
, patch = require( '../lib/patch.js' )
, regex = require( '../lib/regex.js' )
, xmlrpc = require('xmlrpc')
, clortho = require( 'clortho' )
_.str = _.str = require('underscore.string')
_.mixin( _.str.exports() )
module.exports = function(grunt) {
var temp_file = 'wppatch.diff'
, defaults = {
tracUrl : 'core.trac.wordpress.org'
}
function is_svn(){
return fs.existsSync('.svn')
}
function working_dir(){
return process.cwd()
}
function apply_patch( patch_url , done , options ){
grunt.verbose.write( patch_url )
parsed_url = url.parse( patch_url )
// What to do when either our patch is ready
grunt.event.once('fileReady', function(level, move_to_src){
var patchOptions = {}
, patchArgs = []
, patchProcess
// Set patch process to use the existing I/O streams, which will output
// the command's results and allow for user input on patch error
patchOptions.stdio = 'inherit'
// Decide if we need to be in src
if ( move_to_src ) {
patchOptions.cwd = working_dir() + '/src'
temp_file = working_dir() + '/' + temp_file
}
// Set the patch command's arguments
patchArgs.push( '-p' + level )
patchArgs.push( '--input=' + temp_file )
grunt.log.debug( 'patch options: ' + JSON.stringify( patchOptions ) )
grunt.log.debug( 'patch arguments: ' + JSON.stringify( patchArgs ) )
grunt.log.debug( 'patch temp_file: ' + JSON.stringify( temp_file ) )
patchProcess = spawn( 'patch', patchArgs, patchOptions )
patchProcess.on('exit', function( code, signal ) {
if ( signal ) {
grunt.log.debug( 'error signal: ' + signal )
}
// if debug is enabled, don't delete the file
if ( grunt.option( 'debug' ) ) {
grunt.log.debug( 'File Saved' )
} else {
grunt.file.delete(temp_file)
}
done( code )
})
})
// or we know we have failed
grunt.event.once('fileFail', function(msg){
if (typeof msg === 'string') {
grunt.log.errorlns(msg)
}
done(false)
})
// if patch_url is a github url
if ( parsed_url.hostname === 'github.com' ){
grunt.log.debug( 'github url detected: ' + patch_url )
if ( patch_url.slice( -5 ) !== '.diff' && patch_url.slice( -6 ) !== '.patch' ){
patch_url += '.diff';
}
get_patch( patch_url, options )
// if patch_url is a full url and is a raw-attachement, just apply it
} else if( parsed_url.hostname === options.tracUrl && parsed_url.pathname.match(/raw-attachment/) ) {
get_patch( patch_url, options )
// if patch_url is full url and is an attachment, convert it to a raw attachment
} else if ( parsed_url.hostname === options.tracUrl && parsed_url.pathname.match(/attachment/) && parsed_url.pathname.match(/(patch|diff)/ ) ) {
get_patch( trac.convert_to_raw ( parsed_url, options ) )
// if patch_url is just a ticket number, get a list of patches on that ticket and allow user to choose one
} else if ( parsed_url.hostname === options.tracUrl && parsed_url.pathname.match(/ticket/) ) {
get_patch_from_ticket( patch_url, options )
// if we just enter a number, assume it is a ticket number
} else if ( parsed_url.hostname === null && ! parsed_url.pathname.match(/\./) ) {
get_patch_from_ticket_number( patch_url, options )
// if patch_url is a local file, just use that
} else if ( parsed_url.hostname === null ) {
get_local_patch( patch_url, options )
// We've failed in our mission
} else {
grunt.event.emit('fileFile', 'All matching failed. Please enter a ticket url, ticket number, patch url')
}
}
function get_patch_from_ticket_number( patch_url, options ){
grunt.log.debug( 'get_patch_from_ticket_number: ' + patch_url )
get_patch_from_ticket( 'https://' + options.tracUrl + '/attachment/ticket/' + patch_url + '/', options )
}
function get_patch_from_ticket( patch_url, options ){
var matches
, long_matches
, match_url
, possible_patches
grunt.log.debug( 'get_patch_from_ticket: ' + patch_url )
request( patch_url, function(error, response, body) {
if ( !error && response.statusCode == 200 ) {
matches = regex.patch_attachments( body )
grunt.log.debug( 'matches: ' + JSON.stringify( matches ) )
if (matches == null) {
grunt.event.emit('fileFail', patch_url + '\ncontains no attachments')
} else if (matches.length === 1){
match_url = options.tracUrl + regex.urls_from_attachment_list( matches[0] )[1]
get_patch( trac.convert_to_raw ( url.parse( 'https://' + match_url ) ), options )
} else {
long_matches = regex.long_matches( body )
possible_patches = regex.possible_patches( long_matches )
grunt.log.debug( 'possible_patches: ' + JSON.stringify( possible_patches ) )
grunt.log.debug( 'long_matches: ' + JSON.stringify( long_matches ) )
inquirer.prompt([
{ type: 'list',
name: 'patch_name',
message: 'Please select a patch to apply',
choices: possible_patches,
// preselect the most recent patch
default: possible_patches.length - 1
}
], function ( answers ) {
grunt.log.debug( 'answers:' + JSON.stringify(answers) )
match_url = options.tracUrl
+ regex.urls_from_attachment_list( matches[ _.indexOf( possible_patches, answers.patch_name) ])[1]
get_patch( trac.convert_to_raw ( url.parse( 'https://' + match_url ) ), options )
})
}
} else {
// something went wrong
grunt.event.emit( 'fileFail', 'get_patch_from_ticket fail \n status: ' + response.statusCode )
}
})
grunt.event.emit('fileFile', 'method not available yet')
}
function get_local_patch(patch_url) {
var body = grunt.file.read(patch_url)
, level = patch.is_ab(body) ? 1 : 0
, move_to_src = patch.move_to_src( body )
grunt.file.copy(patch_url, temp_file)
grunt.event.emit( 'fileReady', level, move_to_src )
}
function get_patch( patch_url ){
grunt.log.debug( 'getting patch: ' + patch_url )
request(patch_url, function(error, response, body) {
if (!error && response.statusCode == 200) {
var level = patch.is_ab(body) ? 1 : 0
, move_to_src = patch.move_to_src( body )
grunt.file.write( temp_file, body)
grunt.event.emit( 'fileReady', level, move_to_src )
} else {
// something went wrong
grunt.event.emit('fileFail', 'get_patch_fail \n status: ' + response.statusCode )
}
})
}
function file_fail( done, msg ) {
grunt.log.errorlns( 'Nothing to patch.' )
grunt.log.errorlns( '' )
grunt.log.errorlns( '' )
grunt.log.errorlns( 'To use this command, please:' )
grunt.log.errorlns( '' )
grunt.log.errorlns( '1) have a diff or patch in your WordPress Directory' )
grunt.log.errorlns( '' )
grunt.log.errorlns( '2) enter a ticket number, e.g. grunt patch:15705' )
grunt.log.errorlns( '' )
grunt.log.errorlns( '3) enter a ticket url, e.g. grunt patch:https://core.trac.wordpress.org/ticket/15705' )
grunt.log.errorlns( '' )
grunt.log.errorlns( '4) enter a patch url, e.g. grunt patch:https://core.trac.wordpress.org/attachment/ticket/11817/13711.diff' )
grunt.log.errorlns( '' )
if ( typeof( msg ) === 'string' ) {
grunt.verbose.errorlns( 'msg: ' + msg )
}
done( false )
}
function local_file( error, result, code, done, options ) {
if ( ! error ){
files = _.filter( result.split( "\n" ) , function( file ) {
return ( _.str.include( file, 'patch' ) || _.str.include( file, 'diff') )
})
grunt.log.debug( 'files: ' + JSON.stringify( files ) )
if ( files.length === 0 ) {
file_fail( done )
} else if ( files.length === 1 ) {
apply_patch( regex.local_file_clean( files[0] ), done, options )
} else {
inquirer.prompt([
{ type: 'list',
name: 'file',
message: 'Please select a file to apply',
choices: files
}
], function ( answers ) {
var file = regex.local_file_clean( answers.file )
apply_patch( file , done, options )
})
}
} else {
file_fail( done , 'local file fail' )
}
}
grunt.registerTask( 'patch_wordpress', 'Patch your develop-wordpress directory like a boss', function( ticket, afterProtocal ) {
var done = this.async()
var options = this.options(defaults)
// since URLs contain a : which is the seperator for grunt, we
// need to reassemble the url.
if (typeof afterProtocal !== 'undefined') {
ticket = ticket + ':' + afterProtocal
}
grunt.log.debug( 'ticket: ' + ticket )
grunt.log.debug( 'options: ' + JSON.stringify( options ) )
if (typeof ticket === 'undefined'){
// look for diffs and patches in the root of the checkout and
// prompt using inquirer to pick one
var fileFinderCommand = is_svn() ? "svn status " : 'git ls-files --other --exclude-standard'
, files
exec(fileFinderCommand , function(error, result, code) {
local_file( error, result, code, done, options)
})
} else {
apply_patch( ticket , done, options )
}
})
grunt.registerTask( 'upload_patch', 'Upload the current diff of your develop-wordpress directory to Trac', function( ticketNumber ) {
var done = this.async()
var options = this.options(defaults)
grunt.log.debug( 'ticketNumber: ' + ticketNumber )
grunt.log.debug( 'options: ' + JSON.stringify( options ) )
ticketNumber = parseInt( ticketNumber, 10 )
if ( typeof ticketNumber != 'number' ) {
grunt.fail.warn( 'A ticket number is required to upload a patch.' )
}
var uploadPatchWithCredentials = function( username, password ) {
var diffCommand = is_svn() ? 'svn diff --diff-cmd diff' : 'git diff'
exec( diffCommand, function(error, result, code) {
var client = xmlrpc.createSecureClient({
hostname: options.tracUrl,
port: 443,
path: '/login/xmlrpc',
basic_auth: {
user: username,
pass: password
}
})
client.methodCall(
'ticket.putAttachment',
[
ticketNumber,
ticketNumber + '.diff',
'', // description. empty for now.
new Buffer( new Buffer(result).toString('base64'), 'base64'),
false // never overwrite the old file
], function( err, value ) {
if ( err === null ) {
grunt.log.writeln( 'Uploaded patch.' )
done()
} else {
grunt.fail.warn( 'Something went wrong when attempting to upload the patch. Please confirm your credentials and the ticket number. ' + err )
}
}
)
})
}
inquirer.prompt(
[{
type: 'input',
name: 'username',
message: 'Enter your WordPress.org username'
}],
function( answers ) {
clortho({
service: 'WordPress.org Trac',
username: answers.username,
message: 'Enter your WordPress.org password:'
}).then( function( credential ) {
uploadPatchWithCredentials( credential.username, credential.password )
});
}
)
})
}