Skip to content

Commit

Permalink
Merge pull request #152 from LarsinYousif/master
Browse files Browse the repository at this point in the history
Added the ability to download a single file using the context menu
  • Loading branch information
Lino authored Apr 28, 2017
2 parents 59be0ee + e115260 commit ae556cc
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ This extension allows you to easily synchronise your local workspace (project fi
## Usage
There are four commands available. You can access them from the command palette (Ctrl+Shift+P on Windows/Linux).

You can also upload a single file by right-clicking on it in the left menu and choosing the "Ftp-sync: Upload File" command.
You can also sync a single file by right-clicking on it in the left menu and using the "Ftp-sync: Upload File" and "Ftp-sync: Download File" commands.

### Ftp-sync: Init
Initializes a default FTP-Sync configuration file in the `.vscode` directory. Options can be customised as follows:
Expand Down
2 changes: 2 additions & 0 deletions extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ function activate(context) {
var commitCommand = vscode.commands.registerCommand('extension.ftpsynccommit', function() { require('./modules/commit-command')(getSyncHelper) });
var singleCommand = vscode.commands.registerTextEditorCommand('extension.ftpsyncsingle', function(editor) { require('./modules/sync-single-command')(editor, getSyncHelper) });
var uploadcurrentCommand = vscode.commands.registerCommand("extension.ftpsyncuploadselected", function(fileUrl) { require('./modules/uploadcurrent-command')(fileUrl, getSyncHelper) });
var downloadcurrentCommand = vscode.commands.registerCommand("extension.ftpsyncdownloadselected", function(fileUrl) { require('./modules/downloadcurrent-command')(fileUrl, getSyncHelper) });
var onSave = require('./modules/on-save');

var currentConfig = getSyncHelper().getConfig();
Expand Down Expand Up @@ -59,6 +60,7 @@ function activate(context) {
context.subscriptions.push(commitCommand);
context.subscriptions.push(singleCommand);
context.subscriptions.push(uploadcurrentCommand);
context.subscriptions.push(downloadcurrentCommand);
}

exports.activate = activate;
Expand Down
33 changes: 33 additions & 0 deletions modules/downloadcurrent-command.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/* global STATUS_TIMEOUT */
var vscode = require("vscode");
var ftpconfig = require("./ftp-config");
var path = require("path");
var isIgnored = require("./is-ignored");

module.exports = function(fileUrl, getFtpSync) {
if(!vscode.workspace.rootPath) {
vscode.window.showErrorMessage("Ftp-sync: Cannot init ftp-sync without opened folder");
return;
}

if(fileUrl.fsPath.indexOf(vscode.workspace.rootPath) < 0) {
vscode.window.showErrorMessage("Ftp-sync: Selected file is not a part of the workspace.");
return;
}

var config = ftpconfig.getConfig();
if(isIgnored(config.ignore, fileUrl.fsPath)) {
vscode.window.showErrorMessage("Ftp-sync: Selected file is ignored.");
return;
}

var fileName = path.basename(fileUrl.fsPath);
var downloadStatus = vscode.window.setStatusBarMessage("Ftp-sync: Downloading " + fileName + " from FTP server...", STATUS_TIMEOUT);
getFtpSync().downloadFile(fileUrl.fsPath, vscode.workspace.rootPath, function(err) {
downloadStatus.dispose();
if(err)
vscode.window.showErrorMessage("Ftp-sync: Downloading " + fileName + " failed: " + err);
else
vscode.window.setStatusBarMessage("Ftp-sync: " + fileName + " downloaded successfully!", STATUS_TIMEOUT);
})
}
22 changes: 22 additions & 0 deletions modules/sync-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,27 @@ var uploadFile = function(localPath, rootPath, callback) {
})
}

var downloadFile = function(localPath, rootPath, callback) {
output("[sync-helper] downloadFile");
var remotePath = upath.toUnix(path.join(ftpConfig.remote, localPath.replace(rootPath, '')));
var remoteDir = upath.toUnix(path.dirname(remotePath));
connect(function(err) {
if(err) callback(err);
var getFile = function() {
ftp.get(remotePath, localPath, function(err) {
callback(err);
})
}
if(remoteDir != ".")
ensureDirExists(remoteDir, function(err) {
if(err) callback(err);
else getFile();
})
else
getFile();
})
}

var executeSync = function(sync, options, callback) {
output("[sync-helper] executeSync");
sync.startTotal = totalOperations(sync);
Expand All @@ -386,6 +407,7 @@ var helper = {
executeSync: executeSync,
totalOperations: totalOperations,
uploadFile: uploadFile,
downloadFile: downloadFile,
disconnect: function() {
ftp.end();
},
Expand Down
11 changes: 10 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"onCommand:extension.ftpsynccommit",
"onCommand:extension.ftpsyncsingle",
"onCommand:extension.ftpsyncuploadselected",
"onCommand:extension.ftpsyncdownloadselected",
"workspaceContains:.vscode/ftp-sync.json"
],
"main": "./extension",
Expand Down Expand Up @@ -54,13 +55,21 @@
{
"command": "extension.ftpsyncuploadselected",
"title": "Ftp-sync: Upload File"
}
},
{
"command": "extension.ftpsyncdownloadselected",
"title": "Ftp-sync: Download File"
}
],
"menus": {
"explorer/context": [
{
"command": "extension.ftpsyncuploadselected",
"group": "extension"
},
{
"command": "extension.ftpsyncdownloadselected",
"group": "extension"
}
]
}
Expand Down

0 comments on commit ae556cc

Please sign in to comment.