Skip to content

Commit

Permalink
Add progress bar when downloading examples
Browse files Browse the repository at this point in the history
Also improves error handling with corrupted examples directories (#142)
  • Loading branch information
will-v-pi committed Jan 8, 2025
1 parent 6b31f8d commit 9beb4a3
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 16 deletions.
31 changes: 21 additions & 10 deletions src/utils/examplesUtil.mts
Original file line number Diff line number Diff line change
Expand Up @@ -170,18 +170,29 @@ export async function setupExample(
const gitPath = await getGit(settings);

if (existsSync(joinPosix(examplesRepoPath, ".git"))) {
const ref = await execAsync(
`cd ${
process.env.ComSpec?.endsWith("cmd.exe") ? "/d " : " "
}"${examplesRepoPath}" && ${
process.env.ComSpec === "powershell.exe" ? "&" : ""
}"${gitPath}" rev-parse HEAD`
);
Logger.log(`Examples git ref is ${ref.stdout}\n`);
if (ref.stdout.trim() !== EXAMPLES_GITREF) {
Logger.log(`Removing old examples repo\n`);
try {
const ref = await execAsync(
`cd ${
process.env.ComSpec?.endsWith("cmd.exe") ? "/d " : " "
}"${examplesRepoPath}" && ${
process.env.ComSpec === "powershell.exe" ? "&" : ""
}"${gitPath}" rev-parse HEAD`
);
Logger.log(`Examples git ref is ${ref.stdout}\n`);
if (ref.stdout.trim() !== EXAMPLES_GITREF) {
Logger.log(`Removing old examples repo\n`);
rmSync(examplesRepoPath, { recursive: true, force: true });
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
} catch (error) {
// Corrupted examples directory
Logger.log(`Removing corrupted examples repo\n`);
rmSync(examplesRepoPath, { recursive: true, force: true });
}
} else if (existsSync(examplesRepoPath)) {
// Examples path exists, but does not contain a git repository
Logger.log(`Removing empty examples repo\n`);
rmSync(examplesRepoPath, { recursive: true, force: true });
}

if (!existsSync(examplesRepoPath)) {
Expand Down
43 changes: 37 additions & 6 deletions src/webview/newProjectPanel.mts
Original file line number Diff line number Diff line change
Expand Up @@ -648,12 +648,43 @@ export class NewProjectPanel {
// close panel before generating project
this.dispose();

const result = await setupExample(
example,
// required to support backslashes in macOS/Linux folder names
process.platform !== "win32"
? this._projectRoot.fsPath
: this._projectRoot.fsPath.replaceAll("\\", "/")
// required to support backslashes in macOS/Linux folder names
const targetPath = process.platform !== "win32"
? this._projectRoot.fsPath
: this._projectRoot.fsPath.replaceAll("\\", "/");

const result = await window.withProgress(
{
location: ProgressLocation.Notification,
title: `Downloading ${example.name} example...`,
cancellable: false,
},
async progress => {
// download and install selected example
const result = await setupExample(
example,
targetPath
);

if (result) {
this._logger.debug(`Successfully downloaded ${example.name} example.`);

progress.report({
increment: 100,
message: `Successfully downloaded ${example.name} example.`,
});

return true;
}

this._logger.error(`Failed to download ${example.name} example.`);

progress.report({
increment: 100,
});

return false;
}
);

if (!result) {
Expand Down

0 comments on commit 9beb4a3

Please sign in to comment.