Skip to content

Commit

Permalink
added download command with backup
Browse files Browse the repository at this point in the history
  • Loading branch information
PhotoNomad0 committed Nov 5, 2024
1 parent 44b3c6c commit 1a42cd5
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 40 deletions.
86 changes: 52 additions & 34 deletions src/CheckingProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import {
initProject,
isRepoInitialized,
loadResources,
removeHomePath,
resourcesPath,
saveCatalog,
} from "./utilities/resourceUtils";
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -1144,7 +1126,9 @@ export class CheckingProvider implements CustomTextEditorProvider {
return { targetLanguagePick, targetOwnerPick, targetBibleIdPick };
}

private static async getRepoSelection(): Promise<RepoSelection> {
private static async downloadCheckingProjectFromDCS(): Promise<string> {
await showInformationMessage(`Searching for Checking Projects on server`);

const server = getServer();
const results = await getCheckingRepos(server)
const repos = results?.repos || [];
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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) {
Expand Down
14 changes: 9 additions & 5 deletions src/utilities/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,8 @@ async function updateFilesInBranch(localFiles: string[], localRepoPath: string,

export async function downloadRepoFromDCS(server: string, owner: string, repo: string, backup = false): Promise<GeneralObject> {
const localRepoPath = path.join(projectsBasePath, repo)
let backupRepoPath = ''

if (fs.existsSync(localRepoPath)) {
if (!backup) {
return {
Expand All @@ -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}`,
Expand All @@ -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<GeneralObject> {
Expand Down Expand Up @@ -312,6 +315,7 @@ export async function downloadPublicRepoFromBranch(localRepoPath: string, server
},
};
fs.outputJsonSync(path.join(localRepoPath, dcsStatusFile, owner), newStatus);
results.localRepoPath = localRepoPath
return results
}

Expand Down
2 changes: 1 addition & 1 deletion src/utilities/resourceUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1a42cd5

Please sign in to comment.