Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

bumped version

tidy up
added test-files
restructure exec with seperate callback function
starting doc
  • Loading branch information...
commit 54947025659961187243f08aea1f527099744a5b 1 parent 03e7845
@MaxDaten authored
View
10 README.md
@@ -1,17 +1,17 @@
-# grunt-scp
+# grunt-rsync
-Copy files to a (remote) machine running an SSH daemon.
+Copy files to a (remote) machine running an SSH daemon with 'rsync'.
## Getting Started
-*`scp` has to be installed on your system and be able to connect to the host without password (e.g. public key authentication)*
+*`rsync` has to be installed on the local and remote system. `rsync` must be able to connect to the host without password (e.g. public key authentication)*
-Install with: `npm install grunt-scp`
+Install with: `npm install grunt-rsyc`
Inside your `grunt.js` file add :
``` javascript
-grunt.loadNpmTasks('grunt-scp');
+grunt.loadNpmTasks('grunt-rsync');
```
and a task named `scp` (see Configuration)!
View
10 grunt.js
@@ -37,14 +37,18 @@ module.exports = function (grunt) {
}
},
- scp: {
+ rsync: {
deploy: {
- src: 'test/',
+ files: {
+ 'a/b/c/': 'test/test-files/',
+ 'a/': 'test/test-files/one.txt',
+ 'a/b/': ['test/test-files/one.txt', 'test/test-files/ddd/xyz.txt']
+ },
options: {
host: "test.mygnia.de",
port: "22",
user: "jloos",
- path: "~"
+ remoteBase: "~/grunt-rsync-test"
}
}
}
View
14 package.json
@@ -1,7 +1,7 @@
{
"name": "grunt-rsync",
"description": "Copy files to a (remote) machine with rsync",
- "version": "0.1.0",
+ "version": "0.1.1",
"homepage": "https://github.com/maxdaten/grunt-rsync",
"author": {
"name": "Jan-Philip Loos",
@@ -9,7 +9,7 @@
},
"repository": {
"type": "git",
- "url": "https://github.com/maxdaten/grunt-rsync"
+ "url": "https://github.com/maxdaten/grunt-rsync"
},
"bugs": {
"url": "https://github.com/maxdaten/grunt-rsync/issues"
@@ -35,5 +35,11 @@
"grunt-bump": "*",
"grunt-beautify": "*"
},
- "keywords": ["gruntplugin", "rsync", "copy", "remote", "scp"]
-}
+ "keywords": [
+ "gruntplugin",
+ "rsync",
+ "copy",
+ "remote",
+ "scp"
+ ]
+}
View
108 tasks/rsync.js
@@ -9,33 +9,64 @@
module.exports = function (grunt) {
'use strict';
+ function rsyncCallback(error, stdout, stderr) {
+ grunt.log.writeln('stdout: ' + stdout);
+ grunt.log.writeln('stderr: ' + stderr);
+ grunt.log.writeln('err: ' + error);
+
+ if (error) {
+ //done(false);
+ grunt.fail.fatal(error);
+ }
+ }
+
+
+ function doRsync(cmd, user, host, remoteBase, target, files) {
+ var exec = require('child_process').exec,
+ src = grunt.file.expand(files[target]),
+ dest = target;
+
+ cmd.push(src.join(' '));
+
+ // destination to copy
+ cmd.push(user + '@' + host + ':' + remoteBase + '/' + target); // TODO: normalize
+ cmd = cmd.join(' ');
+
+ grunt.log.writeln( 'executing: ' + cmd );
+ grunt.log.write( 'starting transfer... ' );
+
+ exec(cmd, rsyncCallback);
+ grunt.log.ok();
+ }
grunt.util = grunt.util || grunt.utils;
grunt.registerMultiTask('rsync', 'Copy files to a (remote) machine with rsync.', function () {
- var done = this.async(),
- exec = require('child_process').exec,
- src = this.file.src,
- dest = this.file.dest,
+ var done = this.async(),
+ files = grunt.helper('createFileMap', this.data.files),
+
+
// options
- dry = grunt.option('no-write'),
- host = this.data.options.host || 'localhost',
- user = this.data.options.user || 'getGitUser', // TODO system username or nothing?
- remoteBase = this.data.options.remoteBase || '~',
- verbose = grunt.option('verbose'),
- preserveTimes = this.data.options.preserveTimes,
+ dry = grunt.option('no-write'),
+ host = this.data.options.host || 'localhost',
+ user = this.data.options.user || 'getGitUser',
+
+
+ // TODO system username or nothing?
+ remoteBase = this.data.options.remoteBase || '~',
+ verbose = grunt.option('verbose'),
+ preserveTimes = this.data.options.preserveTimes,
preservePermissions = this.data.options.preservePermissions || true,
- compression = this.data.options.compression || true,
- recursive = this.data.options.recursive || true,
- additionalOptions = this.data.options.additionalOptions || '';
-
+ compression = this.data.options.compression || true,
+ recursive = this.data.options.recursive || true,
+ additionalOptions = this.data.options.additionalOptions || '';
+
// setup the cmd
var command = ['rsync'];
// these flags must be set before the src/dest args
-
if (recursive) {
command.push('-r');
}
@@ -47,7 +78,7 @@ module.exports = function (grunt) {
if (preserveTimes) {
command.push('-t');
}
-
+
if (preservePermissions) {
command.push('-p');
}
@@ -63,30 +94,27 @@ module.exports = function (grunt) {
command.push(additionalOptions);
// from this line on, the order of the args is relevant!
+ // files to copy
+ // save command before execute files-map wise
+ for (var target in files) {
+ // copy command
+ doRsync(command.slice(), user, host, remoteBase, target, files);
+ } // for in files
+ done(true);
+ });
- // files to copy
- command.push(grunt.file.expand(src).join(' '));
-
- // destination to copy
- command.push(user+'@' + host + ':' + remoteBase);
-
- command = command.join(' ');
-
- grunt.log.writeln('executing: ' + command );
- grunt.log.write('starting transfer...: ');
-
- exec(command, function (err, stdout, stderr) {
- if (stdout) {grunt.log.writeln(stdout);}
- if (stderr) {grunt.log.writeln(stderr);}
-
- if (err) {
- grunt.fail.fatal(err);
- done(false);
- } else {
- grunt.log.ok();
- done(true);
- }
- });
+ grunt.registerHelper('createFileMap', function (files) {
+ var map = {};
+
+ files = files instanceof Object ? files : {
+ '': files
+ };
+
+ for (var target in files) {
+
+ map[target] = files[target];
+ }
+ return map;
});
-};
+};
View
59 test/grunt-rsync_test.js
@@ -1,4 +1,5 @@
-var grunt = require('grunt');
+var grunt = require('grunt'),
+ rsync = require('../tasks/rsync');
/*
======== A Handy Little Nodeunit Reference ========
@@ -23,10 +24,64 @@ var grunt = require('grunt');
exports.rsync = {
'one': function (test) {
'use strict';
-
+
test.expect(1);
// tests here
test.equal(1, 1);
test.done();
+ },
+ 'Helper#createFileMap with one flat file <string> (no map)': function (test) {
+ 'use strict';
+ test.expect(1);
+
+ var files = 'a/b/c';
+ var fileMap = grunt.helper('createFileMap', files);
+
+ test.deepEqual(fileMap, {
+ '': 'a/b/c'
+ });
+ test.done();
+ },
+ 'Helper#createFileMap-identity with one element map <string:string>': function (test) {
+ 'use strict';
+ test.expect(1);
+
+ var files = {
+ 'cde/f': 'a/b/c'
+ };
+ var fileMap = grunt.helper('createFileMap', files);
+
+ test.deepEqual(fileMap, {
+ 'cde/f': 'a/b/c'
+ });
+ test.done();
+ },
+ 'Helper#createFileMap-identity with one element map <string:[string]>': function (test) {
+ 'use strict';
+ test.expect(1);
+
+ var files = {
+ 'cde/f': ['a/b/c', 'e/**/*.txt']
+ };
+ var fileMap = grunt.helper('createFileMap', files);
+
+ test.deepEqual(fileMap, {
+ 'cde/f': ['a/b/c', 'e/**/*.txt']
+ });
+ test.done();
+ },
+ 'Helper#createFileMap-identity with multiple element map <string:[string]> | <string:string>': function (test) {
+ 'use strict';
+ test.expect(1);
+
+ var files = {
+ 'cde/f': ['a/b/c', 'e/**/*.txt'],
+ 'ddd/': 'e/f',
+ 'rrr/': ['hh/*.txt']
+ };
+ var fileMap = grunt.helper('createFileMap', files);
+
+ test.deepEqual(fileMap, files);
+ test.done();
}
};
View
1  test/test-files/ddd/xyz.txt
@@ -0,0 +1 @@
+xyz
View
1  test/test-files/one.txt
@@ -0,0 +1 @@
+one
View
1  test/test-files/two.txt
@@ -0,0 +1 @@
+two
Please sign in to comment.
Something went wrong with that request. Please try again.