From f229477e43fee9f6b09a9ed7fcb9072204de6399 Mon Sep 17 00:00:00 2001 From: Tom van de Velde Date: Tue, 11 Feb 2014 00:12:03 +0000 Subject: [PATCH 1/6] Trim warnings from SQL dump --- tasks/lib/util.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/tasks/lib/util.js b/tasks/lib/util.js index b0d2887..ccc8e4f 100644 --- a/tasks/lib/util.js +++ b/tasks/lib/util.js @@ -83,14 +83,22 @@ exports.init = function (grunt) { }; exports.db_adapt = function(old_url, new_url, file) { - grunt.log.oklns("Adapt the database: set the correct urls for the destination in the database."); + var content = grunt.file.read(file); + + grunt.log.oklns("Trim warnings from SQL dump."); + content = exports.trim_warnings_from_sql_dump(content); + grunt.log.oklns("Adapt the database: set the correct urls for the destination in the database."); var output = exports.replace_urls(old_url, new_url, content); grunt.file.write(file, output); }; + exports.trim_warnings_from_sql_dump = function (sql_dump) { + return sql_dump.replace(/^Warning.*\n?/gm, ''); + }; + exports.replace_urls = function(search, replace, content) { content = exports.replace_urls_in_serialized(search, replace, content); content = exports.replace_urls_in_string(search, replace, content); @@ -226,4 +234,4 @@ exports.init = function (grunt) { }; return exports; -}; +}; \ No newline at end of file From 7e40e1c7d1f864b9205f8e1a9078e3a1d409da9b Mon Sep 17 00:00:00 2001 From: Benjamin Bojko Date: Thu, 14 May 2015 16:18:56 -0400 Subject: [PATCH 2/6] Added support for 'filter' function in target options --- package.json | 2 +- tasks/lib/util.js | 13 +++++++++---- tasks/wordpressdeploy.js | 2 +- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index bf1584a..5272ee3 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "grunt-wordpress-deploy", "description": "Deploy Wordpress without pain using Grunt.", - "version": "0.0.5", + "version": "0.0.6", "homepage": "https://github.com/webrain/grunt-wordpress-deploy", "author": { "name": "Dario Ghilardi", diff --git a/tasks/lib/util.js b/tasks/lib/util.js index ccc8e4f..1886d0c 100644 --- a/tasks/lib/util.js +++ b/tasks/lib/util.js @@ -82,16 +82,21 @@ exports.init = function (grunt) { return exclusions; }; - exports.db_adapt = function(old_url, new_url, file) { + exports.db_adapt = function(old_url, new_url, file, fn_custom_filter) { var content = grunt.file.read(file); - + grunt.log.oklns("Trim warnings from SQL dump."); - content = exports.trim_warnings_from_sql_dump(content); + content = exports.trim_warnings_from_sql_dump(content); grunt.log.oklns("Adapt the database: set the correct urls for the destination in the database."); var output = exports.replace_urls(old_url, new_url, content); + if (fn_custom_filter) { + grunt.log.oklns("Applying custom filter to database."); + output = fn_custom_filter(output); + } + grunt.file.write(file, output); }; @@ -234,4 +239,4 @@ exports.init = function (grunt) { }; return exports; -}; \ No newline at end of file +}; diff --git a/tasks/wordpressdeploy.js b/tasks/wordpressdeploy.js index c3ef240..b0b932f 100644 --- a/tasks/wordpressdeploy.js +++ b/tasks/wordpressdeploy.js @@ -41,7 +41,7 @@ module.exports = function(grunt) { util.db_dump(local_options, local_backup_paths); // Search and Replace database refs - util.db_adapt(local_options.url, target_options.url, local_backup_paths.file); + util.db_adapt(local_options.url, target_options.url, local_backup_paths.file, target_options.filter); // Dump target DB util.db_dump(target_options, target_backup_paths); From 22adf750f5121691850f5ba2af1d7561ae48f931 Mon Sep 17 00:00:00 2001 From: Benjamin Bojko Date: Mon, 6 Jul 2015 12:41:09 -0400 Subject: [PATCH 3/6] Added filter support for pull_db task to local env --- tasks/wordpressdeploy.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tasks/wordpressdeploy.js b/tasks/wordpressdeploy.js index b0b932f..ac148eb 100644 --- a/tasks/wordpressdeploy.js +++ b/tasks/wordpressdeploy.js @@ -77,15 +77,15 @@ module.exports = function(grunt) { grunt.log.subhead("Pulling database from '" + target_options.title + "' into Local"); // Dump Target DB - util.db_dump(target_options, target_backup_paths ); + util.db_dump(target_options, target_backup_paths); - util.db_adapt(target_options.url,local_options.url,target_backup_paths.file); + util.db_adapt(target_options.url, local_options.url, target_backup_paths.file, local_options.filter); // Backup Local DB util.db_dump(local_options, local_backup_paths); // Import dump into Local - util.db_import(local_options,target_backup_paths.file); + util.db_import(local_options, target_backup_paths.file); grunt.log.subhead("Operations completed"); }); From 05a4976523ed5ddf1aabcf832f6cd7690cd4b471 Mon Sep 17 00:00:00 2001 From: Benjamin Bojko Date: Mon, 6 Jul 2015 12:47:17 -0400 Subject: [PATCH 4/6] Removed trim_warnings_from_sql_dump from utils This can be achieved with custom filters now. --- tasks/wordpressdeploy.js | 309 +++++++++++++++++++++++++-------------- 1 file changed, 196 insertions(+), 113 deletions(-) diff --git a/tasks/wordpressdeploy.js b/tasks/wordpressdeploy.js index ac148eb..4f37c78 100644 --- a/tasks/wordpressdeploy.js +++ b/tasks/wordpressdeploy.js @@ -1,152 +1,235 @@ -/* - * grunt-wordpress-deploy - * https://github.com/webrain/grunt-wordpress-deploy - * - * Copyright (c) 2013 Webrain - * Licensed under the MIT license. - */ - 'use strict'; -var grunt = require('grunt'); -var util = require('../tasks/lib/util.js').init(grunt); +exports.init = function (grunt) { + var shell = require('shelljs'); + var lineReader = require("line-reader"); -module.exports = function(grunt) { + var exports = {}; - /** - * DB PUSH - * pushes local database to remote database - */ - grunt.registerTask('push_db', 'Push to Database', function() { + exports.db_dump = function(config, output_paths) { + grunt.file.mkdir(output_paths.dir); - var task_options = grunt.config.get('wordpressdeploy')['options']; + var cmd = exports.mysqldump_cmd(config); - var target = grunt.option('target') || task_options['target']; + var output = shell.exec(cmd, {silent: true}).output; - if ( typeof target === "undefined" || typeof grunt.config.get('wordpressdeploy')[target] === "undefined") { - grunt.fail.warn("Invalid target specified. Did you pass the wrong argument? Please check your task configuration.", 6); - } + grunt.file.write(output_paths.file, output); + grunt.log.oklns("Database DUMP succesfully exported to: " + output_paths.file); + }; - // Grab the options - var target_options = grunt.config.get('wordpressdeploy')[target]; - var local_options = grunt.config.get('wordpressdeploy').local; + exports.db_import = function(config, src) { + shell.exec(exports.mysql_cmd(config, src)); + grunt.log.oklns("Database imported succesfully"); + }; - // Generate required backup directories and paths - var local_backup_paths = util.generate_backup_paths("local", task_options); - var target_backup_paths = util.generate_backup_paths(target, task_options); + exports.rsync_push = function(config) { + grunt.log.oklns("Syncing data from '" + config.from + "' to '" + config.to + "' with rsync."); - grunt.log.subhead("Pushing database from 'Local' to '" + target_options.title + "'"); + var cmd = exports.rsync_push_cmd(config); + grunt.log.writeln(cmd); - // Dump local DB - util.db_dump(local_options, local_backup_paths); + shell.exec(cmd); - // Search and Replace database refs - util.db_adapt(local_options.url, target_options.url, local_backup_paths.file, target_options.filter); + grunt.log.oklns("Sync completed successfully."); + }; - // Dump target DB - util.db_dump(target_options, target_backup_paths); + exports.rsync_pull = function(config) { + grunt.log.oklns("Syncing data from '" + config.from + "' to '" + config.to + "' with rsync."); - // Import dump to target DB - util.db_import(target_options, local_backup_paths.file); + var cmd = exports.rsync_pull_cmd(config); + shell.exec(cmd); - grunt.log.subhead("Operations completed"); - }); + grunt.log.oklns("Sync completed successfully."); + }; - /** - * DB PULL - * pulls remote database into local database - */ - grunt.registerTask('pull_db', 'Pull from Database', function() { + exports.generate_backup_paths = function(target, task_options) { - var task_options = grunt.config.get('wordpressdeploy')['options']; - var target = grunt.option('target') || task_options['target']; + var backups_dir = task_options['backups_dir'] || "backups"; - if ( typeof target === "undefined" || typeof grunt.config.get('wordpressdeploy')[target] === "undefined") { - grunt.fail.warn("Invalid target provided. I cannot pull a database from nowhere! Please checked your configuration and provide a valid target.", 6); - } + var directory = grunt.template.process(tpls.backup_path, { + data: { + backups_dir: backups_dir, + env: target, + date: grunt.template.today('yyyymmdd'), + time: grunt.template.today('HH-MM-ss'), + } + }); + + var filepath = directory + '/db_backup.sql'; - // Grab the options - var target_options = grunt.config.get('wordpressdeploy')[target]; - var local_options = grunt.config.get('wordpressdeploy').local; + return { + dir: directory, + file: filepath + }; + }; - // Generate required backup directories and paths - var local_backup_paths = util.generate_backup_paths("local", task_options); - var target_backup_paths = util.generate_backup_paths(target, task_options); + exports.compose_rsync_options = function(options) { + var args = options.join(' '); - // Start execution - grunt.log.subhead("Pulling database from '" + target_options.title + "' into Local"); + return args; + }; - // Dump Target DB - util.db_dump(target_options, target_backup_paths); + exports.compose_rsync_exclusions = function(options) { + var exclusions = ''; + var i = 0; - util.db_adapt(target_options.url, local_options.url, target_backup_paths.file, local_options.filter); + for(i = 0;i < options.length; i++) { + exclusions += "--exclude '" + options[i] + "' "; + } - // Backup Local DB - util.db_dump(local_options, local_backup_paths); + exclusions = exclusions.trim(); - // Import dump into Local - util.db_import(local_options, target_backup_paths.file); + return exclusions; + }; - grunt.log.subhead("Operations completed"); - }); + exports.db_adapt = function(old_url, new_url, file, fn_custom_filter) { - /** - * Push files - * Sync all local files with the remote location - */ - grunt.registerTask("push_files", "Transfer files to a remote host with rsync.", function () { + var content = grunt.file.read(file); - var task_options = grunt.config.get('wordpressdeploy')['options']; - var target = grunt.option('target') || task_options['target']; + grunt.log.oklns("Adapt the database: set the correct urls for the destination in the database."); + var output = exports.replace_urls(old_url, new_url, content); - if ( typeof target === "undefined" || typeof grunt.config.get('wordpressdeploy')[target] === "undefined") { - grunt.fail.warn("Invalid target provided. I cannot push files from nowhere! Please checked your configuration and provide a valid target.", 6); + if (fn_custom_filter) { + grunt.log.oklns("Applying custom filter to database."); + output = fn_custom_filter(output); } - // Grab the options - var target_options = grunt.config.get('wordpressdeploy')[target]; - var local_options = grunt.config.get('wordpressdeploy').local; - var rsync_args = util.compose_rsync_options(task_options.rsync_args); - var exclusions = util.compose_rsync_exclusions(task_options.exclusions); - - var config = { - rsync_args: task_options.rsync_args.join(' '), - ssh_host: target_options.ssh_host, - from: local_options.path, - to: target_options.path, - exclusions: exclusions - }; + grunt.file.write(file, output); + }; + + exports.replace_urls = function(search, replace, content) { + content = exports.replace_urls_in_serialized(search, replace, content); + content = exports.replace_urls_in_string(search, replace, content); - util.rsync_push(config); - }); + return content; + }; - /** - * Pull files - * Sync all target files with the local location - */ - grunt.registerTask("pull_files", "Transfer files to a remote host with rsync.", function () { + exports.replace_urls_in_serialized = function(search, replace, string) { + var length_delta = search.length - replace.length; - var task_options = grunt.config.get('wordpressdeploy')['options']; - var target = grunt.option('target') || task_options['target']; + // Replace for serialized data + var matches, length, delimiter, old_serialized_data, target_string, new_url; + var regexp = /s:(\d+):([\\]*['"])(.*?)\2;/g; - if ( typeof target === "undefined" || typeof grunt.config.get('wordpressdeploy')[target] === "undefined") { - grunt.fail.warn("Invalid target provided. I cannot push files from nowhere! Please checked your configuration and provide a valid target.", 6); + while (matches = regexp.exec(string)) { + old_serialized_data = matches[0]; + target_string = matches[3]; + + // If the string contains the url make the substitution + if (target_string.indexOf(search) !== -1) { + length = matches[1]; + delimiter = matches[2]; + + // Replace the url + new_url = target_string.replace(search, replace); + length -= length_delta; + + // Compose the new serialized data + var new_serialized_data = 's:' + length + ':' + delimiter + new_url + delimiter + ';'; + + // Replace the new serialized data into the dump + string = string.replace(old_serialized_data, new_serialized_data); + } } - // Grab the options - var target_options = grunt.config.get('wordpressdeploy')[target]; - var local_options = grunt.config.get('wordpressdeploy').local; - var rsync_args = util.compose_rsync_options(task_options.rsync_args); - var exclusions = util.compose_rsync_exclusions(task_options.exclusions); - - var config = { - rsync_args: rsync_args, - ssh_host: target_options.ssh_host, - from: target_options.path, - to: local_options.path, - exclusions: exclusions - }; + return string; + }; + + exports.replace_urls_in_string = function (search, replace, string) { + var regexp = new RegExp('(?!' + replace + ')(' + search + ')', 'g'); + return string.replace(regexp, replace); + }; + + /* Commands generators */ + exports.mysqldump_cmd = function(config) { + var cmd = grunt.template.process(tpls.mysqldump, { + data: { + user: config.user, + pass: config.pass, + database: config.database, + host: config.host + } + }); + + if (typeof config.ssh_host === "undefined") { + grunt.log.oklns("Creating DUMP of local database"); + } else { + grunt.log.oklns("Creating DUMP of remote database"); + var tpl_ssh = grunt.template.process(tpls.ssh, { + data: { + host: config.ssh_host + } + }); + cmd = tpl_ssh + " '" + cmd + "'"; + } + + return cmd; + }; + + exports.mysql_cmd = function(config, src) { + var cmd = grunt.template.process(tpls.mysql, { + data: { + host: config.host, + user: config.user, + pass: config.pass, + database: config.database, + path: src + } + }); + + if (typeof config.ssh_host === "undefined") { + grunt.log.oklns("Importing DUMP into local database"); + cmd = cmd + " < " + src; + } else { + var tpl_ssh = grunt.template.process(tpls.ssh, { + data: { + host: config.ssh_host + } + }); + + grunt.log.oklns("Importing DUMP into remote database"); + cmd = tpl_ssh + " '" + cmd + "' < " + src; + } - util.rsync_pull(config); - }); + return cmd; + }; + + exports.rsync_push_cmd = function(config) { + var cmd = grunt.template.process(tpls.rsync_push, { + data: { + rsync_args: config.rsync_args, + ssh_host: config.ssh_host, + from: config.from, + to: config.to, + exclusions: config.exclusions + } + }); + + return cmd; + }; + + exports.rsync_pull_cmd = function(config) { + var cmd = grunt.template.process(tpls.rsync_pull, { + data: { + rsync_args: config.rsync_args, + ssh_host: config.ssh_host, + from: config.from, + to: config.to, + exclusions: config.exclusions + } + }); + + return cmd; + }; + + var tpls = { + backup_path: "<%= backups_dir %>/<%= env %>/<%= date %>/<%= time %>", + mysqldump: "mysqldump -h <%= host %> -u<%= user %> -p<%= pass %> <%= database %>", + mysql: "mysql -h <%= host %> -u <%= user %> -p<%= pass %> <%= database %>", + rsync_push: "rsync <%= rsync_args %> --delete -e 'ssh <%= ssh_host %>' <%= exclusions %> <%= from %> :<%= to %>", + rsync_pull: "rsync <%= rsync_args %> -e 'ssh <%= ssh_host %>' <%= exclusions %> :<%= from %> <%= to %>", + ssh: "ssh <%= host %>", + }; + + return exports; }; From 724087e965055dab14208098beffa8d783f870ab Mon Sep 17 00:00:00 2001 From: Benjamin Bojko Date: Mon, 6 Jul 2015 12:56:38 -0400 Subject: [PATCH 5/6] Revered to webrain's wordpressdeploy.js + filters --- tasks/wordpressdeploy.js | 309 ++++++++++++++------------------------- 1 file changed, 113 insertions(+), 196 deletions(-) diff --git a/tasks/wordpressdeploy.js b/tasks/wordpressdeploy.js index 4f37c78..afa692e 100644 --- a/tasks/wordpressdeploy.js +++ b/tasks/wordpressdeploy.js @@ -1,235 +1,152 @@ -'use strict'; - -exports.init = function (grunt) { - var shell = require('shelljs'); - var lineReader = require("line-reader"); - - var exports = {}; - - exports.db_dump = function(config, output_paths) { - grunt.file.mkdir(output_paths.dir); +/* + * grunt-wordpress-deploy + * https://github.com/webrain/grunt-wordpress-deploy + * + * Copyright (c) 2013 Webrain + * Licensed under the MIT license. + */ - var cmd = exports.mysqldump_cmd(config); - - var output = shell.exec(cmd, {silent: true}).output; - - grunt.file.write(output_paths.file, output); - grunt.log.oklns("Database DUMP succesfully exported to: " + output_paths.file); - }; +'use strict'; - exports.db_import = function(config, src) { - shell.exec(exports.mysql_cmd(config, src)); - grunt.log.oklns("Database imported succesfully"); - }; +var grunt = require('grunt'); +var util = require('../tasks/lib/util.js').init(grunt); - exports.rsync_push = function(config) { - grunt.log.oklns("Syncing data from '" + config.from + "' to '" + config.to + "' with rsync."); +module.exports = function(grunt) { - var cmd = exports.rsync_push_cmd(config); - grunt.log.writeln(cmd); + /** + * DB PUSH + * pushes local database to remote database + */ + grunt.registerTask('push_db', 'Push to Database', function() { - shell.exec(cmd); + var task_options = grunt.config.get('wordpressdeploy')['options']; - grunt.log.oklns("Sync completed successfully."); - }; + var target = grunt.option('target') || task_options['target']; - exports.rsync_pull = function(config) { - grunt.log.oklns("Syncing data from '" + config.from + "' to '" + config.to + "' with rsync."); + if ( typeof target === "undefined" || typeof grunt.config.get('wordpressdeploy')[target] === "undefined") { + grunt.fail.warn("Invalid target specified. Did you pass the wrong argument? Please check your task configuration.", 6); + } - var cmd = exports.rsync_pull_cmd(config); - shell.exec(cmd); + // Grab the options + var target_options = grunt.config.get('wordpressdeploy')[target]; + var local_options = grunt.config.get('wordpressdeploy').local; - grunt.log.oklns("Sync completed successfully."); - }; + // Generate required backup directories and paths + var local_backup_paths = util.generate_backup_paths("local", task_options); + var target_backup_paths = util.generate_backup_paths(target, task_options); - exports.generate_backup_paths = function(target, task_options) { + grunt.log.subhead("Pushing database from 'Local' to '" + target_options.title + "'"); - var backups_dir = task_options['backups_dir'] || "backups"; + // Dump local DB + util.db_dump(local_options, local_backup_paths); - var directory = grunt.template.process(tpls.backup_path, { - data: { - backups_dir: backups_dir, - env: target, - date: grunt.template.today('yyyymmdd'), - time: grunt.template.today('HH-MM-ss'), - } - }); + // Search and Replace database refs + util.db_adapt(local_options.url, target_options.url, local_backup_paths.file, target_options.filter); - var filepath = directory + '/db_backup.sql'; + // Dump target DB + util.db_dump(target_options, target_backup_paths); - return { - dir: directory, - file: filepath - }; - }; + // Import dump to target DB + util.db_import(target_options, local_backup_paths.file); - exports.compose_rsync_options = function(options) { - var args = options.join(' '); + grunt.log.subhead("Operations completed"); + }); - return args; - }; + /** + * DB PULL + * pulls remote database into local database + */ + grunt.registerTask('pull_db', 'Pull from Database', function() { - exports.compose_rsync_exclusions = function(options) { - var exclusions = ''; - var i = 0; + var task_options = grunt.config.get('wordpressdeploy')['options']; + var target = grunt.option('target') || task_options['target']; - for(i = 0;i < options.length; i++) { - exclusions += "--exclude '" + options[i] + "' "; + if ( typeof target === "undefined" || typeof grunt.config.get('wordpressdeploy')[target] === "undefined") { + grunt.fail.warn("Invalid target provided. I cannot pull a database from nowhere! Please checked your configuration and provide a valid target.", 6); } - exclusions = exclusions.trim(); - - return exclusions; - }; + // Grab the options + var target_options = grunt.config.get('wordpressdeploy')[target]; + var local_options = grunt.config.get('wordpressdeploy').local; - exports.db_adapt = function(old_url, new_url, file, fn_custom_filter) { + // Generate required backup directories and paths + var local_backup_paths = util.generate_backup_paths("local", task_options); + var target_backup_paths = util.generate_backup_paths(target, task_options); - var content = grunt.file.read(file); + // Start execution + grunt.log.subhead("Pulling database from '" + target_options.title + "' into Local"); - grunt.log.oklns("Adapt the database: set the correct urls for the destination in the database."); - var output = exports.replace_urls(old_url, new_url, content); + // Dump Target DB + util.db_dump(target_options, target_backup_paths ); - if (fn_custom_filter) { - grunt.log.oklns("Applying custom filter to database."); - output = fn_custom_filter(output); - } + util.db_adapt(target_options.url,local_options.url,target_backup_paths.file, local_options.filter); - grunt.file.write(file, output); - }; + // Backup Local DB + util.db_dump(local_options, local_backup_paths); - exports.replace_urls = function(search, replace, content) { - content = exports.replace_urls_in_serialized(search, replace, content); - content = exports.replace_urls_in_string(search, replace, content); + // Import dump into Local + util.db_import(local_options,target_backup_paths.file); - return content; - }; + grunt.log.subhead("Operations completed"); + }); - exports.replace_urls_in_serialized = function(search, replace, string) { - var length_delta = search.length - replace.length; + /** + * Push files + * Sync all local files with the remote location + */ + grunt.registerTask("push_files", "Transfer files to a remote host with rsync.", function () { - // Replace for serialized data - var matches, length, delimiter, old_serialized_data, target_string, new_url; - var regexp = /s:(\d+):([\\]*['"])(.*?)\2;/g; + var task_options = grunt.config.get('wordpressdeploy')['options']; + var target = grunt.option('target') || task_options['target']; - while (matches = regexp.exec(string)) { - old_serialized_data = matches[0]; - target_string = matches[3]; + if ( typeof target === "undefined" || typeof grunt.config.get('wordpressdeploy')[target] === "undefined") { + grunt.fail.warn("Invalid target provided. I cannot push files from nowhere! Please checked your configuration and provide a valid target.", 6); + } - // If the string contains the url make the substitution - if (target_string.indexOf(search) !== -1) { - length = matches[1]; - delimiter = matches[2]; + // Grab the options + var target_options = grunt.config.get('wordpressdeploy')[target]; + var local_options = grunt.config.get('wordpressdeploy').local; + var rsync_args = util.compose_rsync_options(task_options.rsync_args); + var exclusions = util.compose_rsync_exclusions(task_options.exclusions); + + var config = { + rsync_args: task_options.rsync_args.join(' '), + ssh_host: target_options.ssh_host, + from: local_options.path, + to: target_options.path, + exclusions: exclusions + }; - // Replace the url - new_url = target_string.replace(search, replace); - length -= length_delta; + util.rsync_push(config); + }); - // Compose the new serialized data - var new_serialized_data = 's:' + length + ':' + delimiter + new_url + delimiter + ';'; + /** + * Pull files + * Sync all target files with the local location + */ + grunt.registerTask("pull_files", "Transfer files to a remote host with rsync.", function () { - // Replace the new serialized data into the dump - string = string.replace(old_serialized_data, new_serialized_data); - } - } + var task_options = grunt.config.get('wordpressdeploy')['options']; + var target = grunt.option('target') || task_options['target']; - return string; - }; - - exports.replace_urls_in_string = function (search, replace, string) { - var regexp = new RegExp('(?!' + replace + ')(' + search + ')', 'g'); - return string.replace(regexp, replace); - }; - - /* Commands generators */ - exports.mysqldump_cmd = function(config) { - var cmd = grunt.template.process(tpls.mysqldump, { - data: { - user: config.user, - pass: config.pass, - database: config.database, - host: config.host - } - }); - - if (typeof config.ssh_host === "undefined") { - grunt.log.oklns("Creating DUMP of local database"); - } else { - grunt.log.oklns("Creating DUMP of remote database"); - var tpl_ssh = grunt.template.process(tpls.ssh, { - data: { - host: config.ssh_host - } - }); - cmd = tpl_ssh + " '" + cmd + "'"; + if ( typeof target === "undefined" || typeof grunt.config.get('wordpressdeploy')[target] === "undefined") { + grunt.fail.warn("Invalid target provided. I cannot push files from nowhere! Please checked your configuration and provide a valid target.", 6); } - return cmd; - }; - - exports.mysql_cmd = function(config, src) { - var cmd = grunt.template.process(tpls.mysql, { - data: { - host: config.host, - user: config.user, - pass: config.pass, - database: config.database, - path: src - } - }); - - if (typeof config.ssh_host === "undefined") { - grunt.log.oklns("Importing DUMP into local database"); - cmd = cmd + " < " + src; - } else { - var tpl_ssh = grunt.template.process(tpls.ssh, { - data: { - host: config.ssh_host - } - }); - - grunt.log.oklns("Importing DUMP into remote database"); - cmd = tpl_ssh + " '" + cmd + "' < " + src; - } + // Grab the options + var target_options = grunt.config.get('wordpressdeploy')[target]; + var local_options = grunt.config.get('wordpressdeploy').local; + var rsync_args = util.compose_rsync_options(task_options.rsync_args); + var exclusions = util.compose_rsync_exclusions(task_options.exclusions); + + var config = { + rsync_args: rsync_args, + ssh_host: target_options.ssh_host, + from: target_options.path, + to: local_options.path, + exclusions: exclusions + }; - return cmd; - }; - - exports.rsync_push_cmd = function(config) { - var cmd = grunt.template.process(tpls.rsync_push, { - data: { - rsync_args: config.rsync_args, - ssh_host: config.ssh_host, - from: config.from, - to: config.to, - exclusions: config.exclusions - } - }); - - return cmd; - }; - - exports.rsync_pull_cmd = function(config) { - var cmd = grunt.template.process(tpls.rsync_pull, { - data: { - rsync_args: config.rsync_args, - ssh_host: config.ssh_host, - from: config.from, - to: config.to, - exclusions: config.exclusions - } - }); - - return cmd; - }; - - var tpls = { - backup_path: "<%= backups_dir %>/<%= env %>/<%= date %>/<%= time %>", - mysqldump: "mysqldump -h <%= host %> -u<%= user %> -p<%= pass %> <%= database %>", - mysql: "mysql -h <%= host %> -u <%= user %> -p<%= pass %> <%= database %>", - rsync_push: "rsync <%= rsync_args %> --delete -e 'ssh <%= ssh_host %>' <%= exclusions %> <%= from %> :<%= to %>", - rsync_pull: "rsync <%= rsync_args %> -e 'ssh <%= ssh_host %>' <%= exclusions %> :<%= from %> <%= to %>", - ssh: "ssh <%= host %>", - }; - - return exports; + util.rsync_pull(config); + }); }; From 869a034da80bb39822332f25cb4d82b7ea20db42 Mon Sep 17 00:00:00 2001 From: Benjamin Bojko Date: Mon, 6 Jul 2015 12:59:26 -0400 Subject: [PATCH 6/6] Reverted to webrain's base util.js + filters --- tasks/lib/util.js | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/tasks/lib/util.js b/tasks/lib/util.js index 1886d0c..e48b4eb 100644 --- a/tasks/lib/util.js +++ b/tasks/lib/util.js @@ -83,27 +83,19 @@ exports.init = function (grunt) { }; exports.db_adapt = function(old_url, new_url, file, fn_custom_filter) { - + grunt.log.oklns("Adapt the database: set the correct urls for the destination in the database."); var content = grunt.file.read(file); - grunt.log.oklns("Trim warnings from SQL dump."); - content = exports.trim_warnings_from_sql_dump(content); - - grunt.log.oklns("Adapt the database: set the correct urls for the destination in the database."); var output = exports.replace_urls(old_url, new_url, content); - + if (fn_custom_filter) { grunt.log.oklns("Applying custom filter to database."); output = fn_custom_filter(output); } - + grunt.file.write(file, output); }; - exports.trim_warnings_from_sql_dump = function (sql_dump) { - return sql_dump.replace(/^Warning.*\n?/gm, ''); - }; - exports.replace_urls = function(search, replace, content) { content = exports.replace_urls_in_serialized(search, replace, content); content = exports.replace_urls_in_string(search, replace, content); @@ -113,9 +105,10 @@ exports.init = function (grunt) { exports.replace_urls_in_serialized = function(search, replace, string) { var length_delta = search.length - replace.length; + var search_regexp = new RegExp(search, 'g'); // Replace for serialized data - var matches, length, delimiter, old_serialized_data, target_string, new_url; + var matches, length, delimiter, old_serialized_data, target_string, new_url, occurences; var regexp = /s:(\d+):([\\]*['"])(.*?)\2;/g; while (matches = regexp.exec(string)) { @@ -124,12 +117,13 @@ exports.init = function (grunt) { // If the string contains the url make the substitution if (target_string.indexOf(search) !== -1) { + occurences = target_string.match(search_regexp).length; length = matches[1]; delimiter = matches[2]; // Replace the url - new_url = target_string.replace(search, replace); - length -= length_delta; + new_url = target_string.replace(search_regexp, replace); + length -= length_delta * occurences; // Compose the new serialized data var new_serialized_data = 's:' + length + ':' + delimiter + new_url + delimiter + ';';