Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Upload to all profiles #313

Merged
merged 1 commit into from
Jun 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,36 @@
"title": "Force Upload",
"category": "SFTP"
},
{
"command": "sftp.upload.file.to.allProfiles",
"title": "Upload File To All Profiles",
"category": "SFTP"
},
{
"command": "sftp.upload.activeFile.to.allProfiles",
"title": "Upload Active File To All Profiles",
"category": "SFTP"
},
{
"command": "sftp.upload.folder.to.allProfiles",
"title": "Upload Folder To All Profiles",
"category": "SFTP"
},
{
"command": "sftp.upload.activeFolder.to.allProfiles",
"title": "Upload Active Folder To All Profiles",
"category": "SFTP"
},
{
"command": "sftp.upload.project.to.allProfiles",
"title": "Upload Project To All Profiles",
"category": "SFTP"
},
{
"command": "sftp.forceUpload.to.allProfiles",
"title": "Force Upload To All Profiles",
"category": "SFTP"
},
{
"command": "sftp.download.file",
"title": "Download File",
Expand Down Expand Up @@ -273,6 +303,18 @@
"command": "sftp.upload.project",
"when": "sftp.enabled"
},
{
"command": "sftp.upload.activeFile.to.allProfiles",
"when": "sftp.enabled"
},
{
"command": "sftp.upload.activeFolder.to.allProfiles",
"when": "sftp.enabled"
},
{
"command": "sftp.upload.project.to.allProfiles",
"when": "sftp.enabled"
},
{
"command": "sftp.download.activeFile",
"when": "sftp.enabled"
Expand Down Expand Up @@ -329,6 +371,18 @@
"command": "sftp.forceUpload",
"when": "false"
},
{
"command": "sftp.upload.file.to.allProfiles",
"when": "false"
},
{
"command": "sftp.upload.folder.to.allProfiles",
"when": "false"
},
{
"command": "sftp.forceUpload.to.allProfiles",
"when": "false"
},
{
"command": "sftp.download.file",
"when": "false"
Expand Down Expand Up @@ -415,6 +469,17 @@
"alt": "sftp.forceUpload",
"when": "sftp.enabled && explorerResourceIsFolder"
},
{
"command": "sftp.upload.file.to.allProfiles",
"group": "sftp.trans@1",
"when": "sftp.enabled && !explorerResourceIsRoot && !explorerResourceIsFolder"
},
{
"command": "sftp.upload.folder.to.allProfiles",
"group": "sftp.trans@1",
"alt": "sftp.forceUpload.to.allProfiles",
"when": "sftp.enabled && explorerResourceIsFolder"
},
{
"command": "sftp.download.file",
"group": "sftp.trans@2",
Expand All @@ -433,6 +498,11 @@
"group": "sftp.trans@1",
"when": "sftp.enabled && resourceScheme == file"
},
{
"command": "sftp.upload.file.to.allProfiles",
"group": "sftp.trans@1",
"when": "sftp.enabled && resourceScheme == file"
},
{
"command": "sftp.download.file",
"group": "sftp.trans@2",
Expand Down Expand Up @@ -500,6 +570,17 @@
"group": "3_trans@1",
"when": "view == remoteExplorer && viewItem != root && viewItem == file"
},
{
"command": "sftp.upload.folder.to.allProfiles",
"group": "3_trans@1",
"alt": "sftp.forceUpload.to.allProfiles",
"when": "sftp.enabled && viewItem != file"
},
{
"command": "sftp.upload.file.to.allProfiles",
"group": "3_trans@1",
"when": "view == remoteExplorer && viewItem != root && viewItem == file"
},
{
"command": "sftp.download.folder",
"group": "3_trans@2",
Expand Down
31 changes: 30 additions & 1 deletion src/commands/abstract/createCommand.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Uri } from 'vscode';
import logger from '../../logger';
import { reportError } from '../../helper';
import { handleCtxFromUri, FileHandlerContext } from '../../fileHandlers';
import { handleCtxFromUri, allHandleCtxFromUri, FileHandlerContext } from '../../fileHandlers';
import Command from './command';

interface BaseCommandOption {
Expand Down Expand Up @@ -67,3 +67,32 @@ export function createFileCommand(commandOption: FileCommandOption & { name: str
}
};
}

export function createFileMultiCommand(commandOption: FileCommandOption & { name: string }) {
return class FileCommand extends Command {
constructor() {
super();
this.id = commandOption.id;
this.name = commandOption.name;
}

protected async doCommandRun(...args) {
const target = await commandOption.getFileTarget(...args);
if (!target) {
logger.warn(`The "${this.name}" command get canceled because of missing targets.`);
return;
}

const targetList: Uri[] = Array.isArray(target) ? target : [target];
const pendingTasks = targetList.map(async uri => {
try {
await Promise.all(allHandleCtxFromUri(uri).map(commandOption.handleFile));
} catch (error) {
reportError(error);
}
});

await Promise.all(pendingTasks);
}
};
}
9 changes: 9 additions & 0 deletions src/commands/fileMultiCommandUploadActiveFileToAllProfiles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { COMMAND_UPLOAD_ACTIVEFILE_TO_ALL_PROFILES } from '../constants';
import { checkFileCommand } from './abstract/createCommand';
import fileCommandUploadActiveFile from './fileCommandUploadActiveFile';


export default checkFileCommand({
...fileCommandUploadActiveFile,
id: COMMAND_UPLOAD_ACTIVEFILE_TO_ALL_PROFILES
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { COMMAND_UPLOAD_ACTIVEFOLDER_TO_ALL_PROFILES } from '../constants';
import { checkFileCommand } from './abstract/createCommand';
import fileCommandUploadActiveFolder from './fileCommandUploadActiveFolder';

export default checkFileCommand({
...fileCommandUploadActiveFolder,
id: COMMAND_UPLOAD_ACTIVEFOLDER_TO_ALL_PROFILES,
});
8 changes: 8 additions & 0 deletions src/commands/fileMultiCommandUploadFileToAllProfiles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { COMMAND_UPLOAD_FILE_TO_ALL_PROFILES } from '../constants';
import { checkFileCommand } from './abstract/createCommand';
import fileCommandUploadFile from './fileCommandUploadFile';

export default checkFileCommand({
...fileCommandUploadFile,
id: COMMAND_UPLOAD_FILE_TO_ALL_PROFILES
});
8 changes: 8 additions & 0 deletions src/commands/fileMultiCommandUploadFolderToAllProfiles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { COMMAND_UPLOAD_FOLDER_TO_ALL_PROFILES } from '../constants';
import { checkFileCommand } from './abstract/createCommand';
import fileCommandUploadFolder from './fileCommandUploadFolder';

export default checkFileCommand({
...fileCommandUploadFolder,
id: COMMAND_UPLOAD_FOLDER_TO_ALL_PROFILES
});
8 changes: 8 additions & 0 deletions src/commands/fileMultiCommandUploadForceToAllProfiles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { COMMAND_FORCE_UPLOAD_TO_ALL_PROFILES } from '../constants';
import { checkFileCommand } from './abstract/createCommand';
import fileCommandUploadForce from './fileCommandUploadForce';

export default checkFileCommand({
...fileCommandUploadForce,
id: COMMAND_FORCE_UPLOAD_TO_ALL_PROFILES
});
8 changes: 8 additions & 0 deletions src/commands/fileMultiCommandUploadProjectToAllProfiles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { COMMAND_UPLOAD_PROJECT_TO_ALL_PROFILES } from '../constants';
import { checkFileCommand } from './abstract/createCommand';
import fileCommandUploadProject from './fileCommandUploadProject';

export default checkFileCommand({
...fileCommandUploadProject,
id: COMMAND_UPLOAD_PROJECT_TO_ALL_PROFILES
});
8 changes: 8 additions & 0 deletions src/commands/fileMultiCommandUploadToAllProfiles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { COMMAND_UPLOAD_TO_ALL_PROFILES } from '../constants';
import { checkFileCommand } from './abstract/createCommand';
import fileCommandUpload from './fileCommandUpload';

export default checkFileCommand({
...fileCommandUpload,
id: COMMAND_UPLOAD_TO_ALL_PROFILES
});
9 changes: 9 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ export const COMMAND_UPLOAD_ACTIVEFILE = 'sftp.upload.activeFile';
export const COMMAND_UPLOAD_FOLDER = 'sftp.upload.folder';
export const COMMAND_UPLOAD_ACTIVEFOLDER = 'sftp.upload.activeFolder';
export const COMMAND_UPLOAD_PROJECT = 'sftp.upload.project';

export const COMMAND_FORCE_UPLOAD_TO_ALL_PROFILES = 'sftp.forceUpload.to.allProfiles';
export const COMMAND_UPLOAD_TO_ALL_PROFILES = 'sftp.upload.to.allProfiles';
export const COMMAND_UPLOAD_FILE_TO_ALL_PROFILES = 'sftp.upload.file.to.allProfiles';
export const COMMAND_UPLOAD_ACTIVEFILE_TO_ALL_PROFILES = 'sftp.upload.activeFile.to.allProfiles';
export const COMMAND_UPLOAD_FOLDER_TO_ALL_PROFILES = 'sftp.upload.folder.to.allProfiles';
export const COMMAND_UPLOAD_ACTIVEFOLDER_TO_ALL_PROFILES = 'sftp.upload.activeFolder.to.allProfiles';
export const COMMAND_UPLOAD_PROJECT_TO_ALL_PROFILES = 'sftp.upload.project.to.allProfiles';

export const COMMAND_FORCE_DOWNLOAD = 'sftp.forceDownload';
export const COMMAND_DOWNLOAD = 'sftp.download';
export const COMMAND_DOWNLOAD_FILE = 'sftp.download.file';
Expand Down
15 changes: 10 additions & 5 deletions src/core/fileService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -517,16 +517,16 @@ export default class FileService {
return createRemoteIfNoneExist(getHostInfo(config));
}

getConfig(): ServiceConfig {
getConfig(useProfile = app.state.profile): ServiceConfig {
let config = this._config;
const hasProfile =
config.profiles && Object.keys(config.profiles).length > 0;
if (hasProfile && app.state.profile) {
logger.info(`Using profile: ${app.state.profile}`);
const profile = config.profiles![app.state.profile];
if (hasProfile && useProfile) {
logger.info(`Using profile: ${useProfile}`);
const profile = config.profiles![useProfile];
if (!profile) {
throw new Error(
`Unkown Profile "${app.state.profile}".` +
`Unkown Profile "${useProfile}".` +
' Please check your profile setting.' +
' You can set a profile by running command `SFTP: Set Profile`.'
);
Expand All @@ -549,6 +549,11 @@ export default class FileService {
return this._resolveServiceConfig(completeConfig);
}

getAllConfig(): Array<ServiceConfig> {
const profiles = this._config.profiles;
return profiles ? Object.keys(profiles).map(p => this.getConfig(p)) : [];
}

dispose() {
this._disposeWatcher();
this._disposeFileSystem();
Expand Down
31 changes: 31 additions & 0 deletions src/fileHandlers/createFileHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,37 @@ export function handleCtxFromUri(uri: Uri): FileHandlerContext {
};
}

export function allHandleCtxFromUri(uri: Uri): Array<FileHandlerContext> {
const fileService = getFileService(uri);
if (!fileService) {
if (uri.toString(true) == "file:///${command:sftp.sync.remoteToLocal}") {
throw '';
} else {
throw new Error(`Config Not Found. (${uri.toString(true)})`);
}
}

const configArr = fileService.getAllConfig();

return configArr.map(config => {
const target = UResource.from(uri, {
localBasePath: fileService.baseDir,
remoteBasePath: config.remotePath,
remoteId: fileService.id,
remote: {
host: config.host,
port: config.port,
},
});

return {
fileService,
config,
target,
};
})
}

export default function createFileHandler<T>(
handlerOption: FileHandlerOption<T>
): (ctx: FileHandlerContext | Uri, option?: Partial<T>) => Promise<void> {
Expand Down
2 changes: 1 addition & 1 deletion src/fileHandlers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ export * from './remove';
export * from './diff';
export * from './rename';
export * from './create';
export { handleCtxFromUri, FileHandlerContext } from './createFileHandler';
export { handleCtxFromUri, allHandleCtxFromUri, FileHandlerContext } from './createFileHandler';
15 changes: 14 additions & 1 deletion src/initCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ExtensionContext } from 'vscode';
import logger from './logger';
import { registerCommand } from './host';
import Command from './commands/abstract/command';
import { createCommand, createFileCommand } from './commands/abstract/createCommand';
import { createCommand, createFileCommand, createFileMultiCommand } from './commands/abstract/createCommand';

export default function init(context: ExtensionContext) {
loadCommands(
Expand Down Expand Up @@ -31,6 +31,19 @@ export default function init(context: ExtensionContext) {
createFileCommand,
context
);
loadCommands(
require.context(
// Look for files in the current directory
'./commands',
// Do not look in subdirectories
false,
// Only include "_base-" prefixed .vue files
/fileMultiCommand.*.ts$/
),
/fileMultiCommand(.*)/,
createFileMultiCommand,
context
);
}

function nomalizeCommandName(rawName) {
Expand Down