From d3be5ea4118d5f6109d06899da6c2eae6cb5e88c Mon Sep 17 00:00:00 2001 From: WIZIOO Date: Wed, 12 Mar 2014 09:43:02 +0100 Subject: [PATCH 1/4] Change sed command by shelljs sed function in order to work w/ all platform - Also remove search_replace template as it is now unuses - dBReplace() function now return shell and not cmd anymore. Unit test will change as shell.sed returns the new string after replacement (But I'm not at ease w/ unit testing so I'll let this part to another). --- lib/dbReplace.js | 14 +++----------- lib/tpls.js | 2 -- 2 files changed, 3 insertions(+), 13 deletions(-) diff --git a/lib/dbReplace.js b/lib/dbReplace.js index 41d0c37..473f6ae 100644 --- a/lib/dbReplace.js +++ b/lib/dbReplace.js @@ -11,20 +11,12 @@ function dbReplace(search, replace, output_path, noExec) { // avoids overwriting original backup grunt.file.copy(output_path.file, output_path["file-tmp"]); - // Perform Search and replace on the temp file (not original) - var cmd = grunt.template.process( tpls.search_replace, { - data: { - search: search, - replace: replace, - path: output_path["file-tmp"] - } - }); - if ( grunt.file.exists(output_path["file-tmp"])) { // Execute cmd if (!noExec) { grunt.log.writeln("Replacing '" + search + "' with '" + replace + "' in the export (.sql) path.file."); - shell.exec(cmd); + //Execute SED + shell.sed("-i", search, replace, output_path["file-tmp"]); grunt.log.oklns("Database references succesfully updated."); } } else { @@ -32,7 +24,7 @@ function dbReplace(search, replace, output_path, noExec) { } // Return for reference and test suite purposes - return cmd; + return shell; } module.exports = dbReplace; \ No newline at end of file diff --git a/lib/tpls.js b/lib/tpls.js index ab12936..c00403c 100644 --- a/lib/tpls.js +++ b/lib/tpls.js @@ -5,8 +5,6 @@ */ var tpls = { - search_replace: "sed -i '' 's#<%= search %>#<%= replace %>#g' <%= path %>", - backup_path: "<%= backups_dir %>/<%= env %>/<%= date %>/<%= time %>", mysql: "mysql -h <%= host %> -u <%= user %> -p<%= pass %> -P<%= port %> <%= database %>", From 5f3fb38b05dc5c2d42724c3da2b64224d38a8c7c Mon Sep 17 00:00:00 2001 From: WIZIOO Date: Wed, 12 Mar 2014 09:47:46 +0100 Subject: [PATCH 2/4] Remove backslashes from dbDump cmd in order to work on windows platform - I don't know if it still work on other platforms --- lib/dbDump.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/dbDump.js b/lib/dbDump.js index 6e80483..2fb526d 100644 --- a/lib/dbDump.js +++ b/lib/dbDump.js @@ -50,7 +50,7 @@ function dbDump(config, output_paths, noExec) { } }); - cmd = tpl_ssh + " \\ " + tpl_mysqldump; + cmd = tpl_ssh + " " + tpl_mysqldump; } if (!noExec) { From 18a541b40bdd639fc1743be22c4e72c2a988c22d Mon Sep 17 00:00:00 2001 From: WIZIOO Date: Fri, 18 Apr 2014 16:09:05 +0200 Subject: [PATCH 3/4] Fix bug on backup path for db_replace --- tasks/deployments.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tasks/deployments.js b/tasks/deployments.js index 74cadb9..24b3ce2 100644 --- a/tasks/deployments.js +++ b/tasks/deployments.js @@ -55,7 +55,7 @@ module.exports = function(grunt) { dbDump(local_options, local_backup_paths); // Search and Replace database refs - dbReplace( local_options.url, target_options.url, local_backup_paths.file ); + dbReplace( local_options.url, target_options.url, local_backup_paths ); // Dump target DB dbDump(target_options, target_backup_paths); From 6f4c3cdaeb3ce2871214b7185f475030c144de69 Mon Sep 17 00:00:00 2001 From: WIZIOO Date: Fri, 18 Apr 2014 16:10:17 +0200 Subject: [PATCH 4/4] Replace sed command by js search and replace helped by grunt.file read and write --- lib/dbReplace.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/dbReplace.js b/lib/dbReplace.js index 473f6ae..0a090ac 100644 --- a/lib/dbReplace.js +++ b/lib/dbReplace.js @@ -15,8 +15,12 @@ function dbReplace(search, replace, output_path, noExec) { // Execute cmd if (!noExec) { grunt.log.writeln("Replacing '" + search + "' with '" + replace + "' in the export (.sql) path.file."); - //Execute SED - shell.sed("-i", search, replace, output_path["file-tmp"]); + + var reg= new RegExp(search,"g"); //Create a global regexp w/ search string + var myFile=grunt.file.read(output_path["file-tmp"]); //Get file as a string + var result = myFile.replace(reg, replace); //Replace search string by replace string + grunt.file.write(output_path["file-tmp"], result); //Write in file + grunt.log.oklns("Database references succesfully updated."); } } else {