Skip to content

Commit 8673929

Browse files
committed
Fix git download on first use
Was failing to download git if the first action was to create an example project Signed-off-by: William Vinnicombe <[email protected]>
1 parent 8ba4562 commit 8673929

File tree

4 files changed

+75
-42
lines changed

4 files changed

+75
-42
lines changed

src/utils/download.mts

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { join as joinPosix } from "path/posix";
66
import Logger from "../logger.mjs";
77
import { get, type RequestOptions } from "https";
88
import type { SupportedToolchainVersion } from "./toolchainUtil.mjs";
9-
import { cloneRepository, initSubmodules } from "./gitUtil.mjs";
9+
import { cloneRepository, initSubmodules, getGit } from "./gitUtil.mjs";
1010
import { checkForInstallationRequirements } from "./requirementsUtil.mjs";
1111
import { HOME_VAR, SettingsKey } from "../settings.mjs";
1212
import Settings from "../settings.mjs";
@@ -136,15 +136,9 @@ export async function downloadAndInstallSDK(
136136
return false;
137137
}
138138

139-
let gitExecutable: string | undefined =
140-
settings
141-
.getString(SettingsKey.gitPath)
142-
?.replace(HOME_VAR, homedir().replaceAll("\\", "/")) || "git";
143-
144139
// TODO: this does take about 2s - may be reduced
145140
const requirementsCheck = await checkForInstallationRequirements(
146-
settings,
147-
gitExecutable
141+
settings
148142
);
149143
if (!requirementsCheck) {
150144
return false;
@@ -161,33 +155,15 @@ export async function downloadAndInstallSDK(
161155

162156
// Ensure the target directory exists
163157
//await mkdir(targetDirectory, { recursive: true });
164-
const gitPath = await which(gitExecutable, { nothrow: true });
165-
if (gitPath === null) {
166-
// if git is not in path then checkForInstallationRequirements
167-
// maye downloaded it, so reload
168-
//settings.reload();
169-
gitExecutable = settings
170-
.getString(SettingsKey.gitPath)
171-
?.replace(HOME_VAR, homedir().replaceAll("\\", "/"));
172-
if (gitExecutable === null) {
173-
Logger.log("Error: Git not found.");
174-
175-
await window.showErrorMessage(
176-
"Git not found. Please install and add to PATH or " +
177-
"set the path to the git executable in global settings."
178-
);
179-
180-
return false;
181-
}
182-
}
158+
const gitPath = await getGit(settings);
183159
// using deferred execution to avoid git clone if git is not available
184160
if (
185-
gitPath !== null &&
161+
gitPath !== undefined &&
186162
(await cloneRepository(
187163
repositoryUrl,
188164
version,
189165
targetDirectory,
190-
gitExecutable
166+
gitPath
191167
))
192168
) {
193169
settings.reload();
@@ -210,7 +186,7 @@ export async function downloadAndInstallSDK(
210186
return false;
211187
}
212188

213-
return initSubmodules(targetDirectory, gitExecutable);
189+
return initSubmodules(targetDirectory, gitPath);
214190
}
215191

216192
return false;

src/utils/examplesUtil.mts

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ import { fileURLToPath } from "url";
44
import Logger from "../logger.mjs";
55
import { existsSync, readFileSync } from "fs";
66
import { homedir } from "os";
7-
import { sparseCheckout, sparseCloneRepository } from "./gitUtil.mjs";
7+
import { getGit, sparseCheckout, sparseCloneRepository } from "./gitUtil.mjs";
88
import { EXAMPLES_REPOSITORY_URL } from "./githubREST.mjs";
9-
import Settings, { HOME_VAR, SettingsKey } from "../settings.mjs";
9+
import Settings from "../settings.mjs";
10+
import { checkForInstallationRequirements } from "./requirementsUtil.mjs";
1011
import { cp } from "fs/promises";
1112
import { get } from "https";
1213
import { isInternetConnected } from "./downloadHelpers.mjs";
@@ -125,17 +126,30 @@ export async function setupExample(
125126
): Promise<boolean> {
126127
const examplesRepoPath = buildExamplesPath();
127128
const absoluteExamplePath = joinPosix(examplesRepoPath, example.path);
128-
const gitExecutable: string | undefined =
129-
Settings.getInstance()
130-
?.getString(SettingsKey.gitPath)
131-
?.replace(HOME_VAR, homedir().replaceAll("\\", "/")) || "git";
129+
130+
const settings = Settings.getInstance();
131+
if (settings === undefined) {
132+
Logger.log("Error: Settings not initialized.");
133+
134+
return false;
135+
}
136+
137+
// TODO: this does take about 2s - may be reduced
138+
const requirementsCheck = await checkForInstallationRequirements(
139+
settings
140+
);
141+
if (!requirementsCheck) {
142+
return false;
143+
}
144+
145+
const gitPath = await getGit(settings);
132146

133147
if (!existsSync(examplesRepoPath)) {
134148
const result = await sparseCloneRepository(
135149
EXAMPLES_REPOSITORY_URL,
136150
"master",
137151
examplesRepoPath,
138-
gitExecutable
152+
gitPath
139153
);
140154

141155
if (!result) {
@@ -147,7 +161,7 @@ export async function setupExample(
147161
const result = await sparseCheckout(
148162
examplesRepoPath,
149163
example.path,
150-
gitExecutable
164+
gitPath
151165
);
152166
if (!result) {
153167
return result;

src/utils/gitUtil.mts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,49 @@ import { promisify } from "util";
22
import { exec } from "child_process";
33
import Logger from "../logger.mjs";
44
import { unlink } from "fs/promises";
5+
import type Settings from "../settings.mjs";
6+
import { SettingsKey, HOME_VAR } from "../settings.mjs";
7+
import { homedir } from "os";
8+
import which from "which";
9+
import { window } from "vscode";
510

611
const execAsync = promisify(exec);
712

13+
/**
14+
* Get installed version of git, and install it if it isn't already
15+
*/
16+
export async function getGit(
17+
settings: Settings
18+
): Promise<string | undefined> {
19+
let gitExecutable: string | undefined =
20+
settings
21+
.getString(SettingsKey.gitPath)
22+
?.replace(HOME_VAR, homedir().replaceAll("\\", "/")) || "git";
23+
let gitPath = await which(gitExecutable, { nothrow: true });
24+
if (gitPath === null) {
25+
// if git is not in path then checkForInstallationRequirements
26+
// maye downloaded it, so reload
27+
settings.reload();
28+
gitExecutable = settings
29+
.getString(SettingsKey.gitPath)
30+
?.replace(HOME_VAR, homedir().replaceAll("\\", "/"));
31+
if (gitExecutable === null || gitExecutable === undefined) {
32+
Logger.log("Error: Git not found.");
33+
34+
await window.showErrorMessage(
35+
"Git not found. Please install and add to PATH or " +
36+
"set the path to the git executable in global settings."
37+
);
38+
39+
return undefined;
40+
} else {
41+
gitPath = await which(gitExecutable, { nothrow: true });
42+
}
43+
}
44+
45+
return gitPath;
46+
}
47+
848
/**
949
* Initialize git submodules in downloaded Pico-SDK.
1050
*

src/utils/requirementsUtil.mts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { window } from "vscode";
22
import which from "which";
33
import type Settings from "../settings.mjs";
4-
import { SettingsKey } from "../settings.mjs";
4+
import { SettingsKey, HOME_VAR } from "../settings.mjs";
5+
import { homedir } from "os";
56
import { downloadGit } from "./downloadGit.mjs";
67
import Logger from "../logger.mjs";
78

@@ -23,10 +24,12 @@ export async function showRequirementsNotMetErrorMessage(
2324
* @returns true if all requirements are met, false otherwise
2425
*/
2526
export async function checkForInstallationRequirements(
26-
settings: Settings,
27-
gitPath?: string
27+
settings: Settings
2828
): Promise<boolean> {
29-
const gitExe: string = gitPath || "git";
29+
const gitExe: string =
30+
settings
31+
.getString(SettingsKey.gitPath)
32+
?.replace(HOME_VAR, homedir().replaceAll("\\", "/")) || "git";
3033
const compilerExe: string[] = ["clang", "gcc", "cl"];
3134

3235
const git: string | null = await which(gitExe, { nothrow: true });

0 commit comments

Comments
 (0)