From 1a42cd5da2c3581d455efb1fac609a3e9406b57b Mon Sep 17 00:00:00 2001 From: PhotoNomad0 Date: Tue, 5 Nov 2024 07:43:02 -0500 Subject: [PATCH] added download command with backup --- src/CheckingProvider.ts | 86 ++++++++++++++++++++-------------- src/utilities/network.ts | 14 ++++-- src/utilities/resourceUtils.ts | 2 +- 3 files changed, 62 insertions(+), 40 deletions(-) diff --git a/src/CheckingProvider.ts b/src/CheckingProvider.ts index 36527f5..b69a3b0 100644 --- a/src/CheckingProvider.ts +++ b/src/CheckingProvider.ts @@ -38,6 +38,7 @@ import { initProject, isRepoInitialized, loadResources, + removeHomePath, resourcesPath, saveCatalog, } from "./utilities/resourceUtils"; @@ -346,32 +347,13 @@ export class CheckingProvider implements CustomTextEditorProvider { "checking-extension.downloadProject", executeWithRedirecting(async () => { console.log(`starting "checking-extension.downloadProject"`) - const { - owner: ownerPick, - repoName: repoPick, - server, - } = await this.getRepoSelection() - let success = false - - if (ownerPick && repoPick) { - const results = await downloadRepoFromDCS(server || '', ownerPick || '', repoPick || '', false) - if (results.error) { - if (results.errorLocalProjectExists) { - const results = await downloadRepoFromDCS(server || '', ownerPick || '', repoPick || '', true) - if (!results.error) { - success = true - } - } - } else { - success = true - } - } - - if (!success) { - await showErrorMessage(`Could not download repo ${ownerPick}/${repoPick}`, true) + const localRepoPath = await this.downloadCheckingProjectFromDCS() + if (localRepoPath) { + // navigate to new folder + const repoPathUri = vscode.Uri.file(localRepoPath); + vscode.commands.executeCommand("vscode.openFolder", repoPathUri); } - - console.log(`finished "checking-extension.downloadProject"`) + console.log(`finished "checking-extension.downloadProject success=${!!localRepoPath}"`) }) ); subscriptions.push(commandRegistration) @@ -1144,7 +1126,9 @@ export class CheckingProvider implements CustomTextEditorProvider { return { targetLanguagePick, targetOwnerPick, targetBibleIdPick }; } - private static async getRepoSelection(): Promise { + private static async downloadCheckingProjectFromDCS(): Promise { + await showInformationMessage(`Searching for Checking Projects on server`); + const server = getServer(); const results = await getCheckingRepos(server) const repos = results?.repos || []; @@ -1156,9 +1140,7 @@ export class CheckingProvider implements CustomTextEditorProvider { if (!ownerNames?.length) { await showInformationMessage(`No Owners found on ${server}`, true, `No Owners found on ${server}. Check with your network Administrator`); - return { - error: `No Owners found on ${server}` - } + return '' } let ownerPick = await vscode.window.showQuickPick( @@ -1178,9 +1160,7 @@ export class CheckingProvider implements CustomTextEditorProvider { if (!repoNames?.length) { await showInformationMessage(`No Checking repos found on ${server}/${ownerPick}`, true, `No Checking repos found on ${server}/${ownerPick}. Try a different owner`); - return { - error: `No Checking repos found on ${server}/${ownerPick}` - } + return '' } repoPick = await vscode.window.showQuickPick( @@ -1192,11 +1172,49 @@ export class CheckingProvider implements CustomTextEditorProvider { if (repoPick) { await showInformationMessage(`Repo selected ${repoPick}`); + let success = false + let madeBackup = false + + let results = await downloadRepoFromDCS(server || '', ownerPick || '', repoPick || '', false) + if (results.error) { + if (results.errorLocalProjectExists) { + const backupOption = 'Backup Current Repo and Download' + const response = await vscode.window.showWarningMessage( + 'There is already a project with the same name on your computer. What do you want to do?', + { modal: true }, + backupOption, + ); + console.log('User selected:', response); + + if (response !== backupOption) { + return '' + } + + madeBackup = true + await showInformationMessage(`Downloading Checking Project ${ownerPick}/${repoPick} from server`); + + results = await downloadRepoFromDCS(server || '', ownerPick || '', repoPick || '', true) + if (!results.error) { + success = true + } + } + } else { + success = true + } + + if (!success) { + await showErrorMessage(`Could not download repo ${ownerPick}/${repoPick}`, true) + } else { + const _backupRepoPath = removeHomePath(results?.backupRepoPath); + const _localRepoPath = removeHomePath(results?.localRepoPath); + const detail = madeBackup ? `The existing project was moved to ${_backupRepoPath}.` : '' + await showInformationMessage(`Project successfully downloaded to ${_localRepoPath}.`, true, detail); + return results?.localRepoPath || '' + } } } - - return { owner: ownerPick, repoName: repoPick, server }; + return ''; } private static getDoor43ResourcesCatalogWithProgress(resourcesPath:string, preRelease = false) { diff --git a/src/utilities/network.ts b/src/utilities/network.ts index 7f6aa00..63e557d 100644 --- a/src/utilities/network.ts +++ b/src/utilities/network.ts @@ -263,6 +263,8 @@ async function updateFilesInBranch(localFiles: string[], localRepoPath: string, export async function downloadRepoFromDCS(server: string, owner: string, repo: string, backup = false): Promise { const localRepoPath = path.join(projectsBasePath, repo) + let backupRepoPath = '' + if (fs.existsSync(localRepoPath)) { if (!backup) { return { @@ -272,9 +274,8 @@ export async function downloadRepoFromDCS(server: string, owner: string, repo: s } try { - const newFolder = localRepoPath + '.OLD_' + getTimeStamp() - fs.moveSync(localRepoPath, newFolder) - // TODO: move original project + backupRepoPath = localRepoPath + '.OLD_' + getTimeStamp() + fs.moveSync(localRepoPath, backupRepoPath) } catch (e:any) { return { error: `Could not backup local project ${localRepoPath}`, @@ -283,8 +284,10 @@ export async function downloadRepoFromDCS(server: string, owner: string, repo: s } } } - - return await downloadPublicRepoFromBranch(localRepoPath, server, owner, repo, 'master') + + const results = await downloadPublicRepoFromBranch(localRepoPath, server, owner, repo, 'master') + results.backupRepoPath = backupRepoPath + return results } export async function downloadPublicRepoFromBranch(localRepoPath: string, server: string, owner: string, repo: string, branch: string): Promise { @@ -312,6 +315,7 @@ export async function downloadPublicRepoFromBranch(localRepoPath: string, server }, }; fs.outputJsonSync(path.join(localRepoPath, dcsStatusFile, owner), newStatus); + results.localRepoPath = localRepoPath return results } diff --git a/src/utilities/resourceUtils.ts b/src/utilities/resourceUtils.ts index 1e3e35a..e7ac717 100644 --- a/src/utilities/resourceUtils.ts +++ b/src/utilities/resourceUtils.ts @@ -1414,7 +1414,7 @@ export function getBibleBookFolders(repoPath: string, bookId:string | null = nul * replace the home path with '~' * @param filePath */ -function removeHomePath(filePath:string) { +export function removeHomePath(filePath:string) { if (filePath && (filePath.indexOf(ospath.home()) === 0)) { const newPath = filePath.replace(ospath.home(), '~') return newPath