Skip to content

Commit a9d7008

Browse files
committed
fix: update if apt-cache fails + skip init deps if installed
1 parent 9ab878f commit a9d7008

File tree

8 files changed

+137
-106
lines changed

8 files changed

+137
-106
lines changed

dist/actions/setup-cpp.js

Lines changed: 27 additions & 27 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/actions/setup-cpp.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/legacy/setup-cpp.js

Lines changed: 27 additions & 27 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/legacy/setup-cpp.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/modern/setup-cpp.js

Lines changed: 27 additions & 27 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/modern/setup-cpp.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131
"build.packages": "pnpm run -r build",
3232
"build.parcel": "cross-env NODE_ENV=production parcel build && run-s build.babel && shx cp -r ./dist/actions/* ./dist/modern",
3333
"build.babel": "babel ./dist --out-dir dist --plugins @upleveled/babel-plugin-remove-node-prefix --plugins @babel/plugin-transform-private-methods --compact --no-babelrc --source-maps true",
34-
"bump": "ncu -u -x numerous,execa,prettier,@types/node,eslint,@types/eslint && pnpm update && pnpx typesync",
35-
"clean": "shx rm -rf ./dist ./exe ./packages/*/dist/ ./.parcel-cache && shx mkdir -p ./dist/legacy ./dist/actions ./dist/modern ",
34+
"bump": "ncu -u -x numerous,execa,prettier,@types/node,eslint,@types/eslint && pnpm update && pnpx typesync && pnpm run clean",
35+
"clean": "shx rm -rf ./dist ./exe ./packages/*/dist/ && shx mkdir -p ./dist/legacy ./dist/actions ./dist/modern ",
3636
"copy.matchers": "run-p copy.matchers.legacy copy.matchers.actions",
3737
"copy.matchers.legacy": "shx cp ./src/gcc/gcc_matcher.json ./dist/legacy/ && shx cp ./src/msvc/msvc_matcher.json ./dist/legacy/ && shx cp ./src/python/python_matcher.json ./dist/legacy/ && shx cp ./src/llvm/llvm_matcher.json ./dist/legacy/",
3838
"copy.matchers.actions": "shx cp ./src/gcc/gcc_matcher.json ./dist/actions/ && shx cp ./src/msvc/msvc_matcher.json ./dist/actions/ && shx cp ./src/python/python_matcher.json ./dist/actions/ && shx cp ./src/llvm/llvm_matcher.json ./dist/actions/",

src/utils/setup/setupAptPack.ts

Lines changed: 51 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -35,36 +35,33 @@ export async function setupAptPack(packages: AptPackage[], update = false): Prom
3535

3636
process.env.DEBIAN_FRONTEND = "noninteractive"
3737

38-
const allRepositories = [...new Set(packages.flatMap((pack) => pack.repositories ?? []))]
39-
40-
if (allRepositories.length !== 0) {
41-
for (const repo of allRepositories) {
42-
// eslint-disable-next-line no-await-in-loop
43-
execRootSync("add-apt-repository", ["-y", repo])
44-
}
45-
46-
updateRepos(apt)
47-
}
38+
// Add the repos if needed
39+
await addRepositories(apt, packages)
4840

41+
// Qualify the packages into full package name/version
4942
let qualifiedPacks = await Promise.all(packages.map((pack) => getAptArg(pack.name, pack.version)))
5043

5144
// find the packages that are not installed
5245
qualifiedPacks = await Promise.all(qualifiedPacks.filter(async (pack) => !(await isPackageInstalled(pack))))
5346

5447
if (qualifiedPacks.length === 0) {
48+
info("All packages are already installed")
5549
return { binDir: "/usr/bin/" }
5650
}
5751

52+
// Update the repos if needed
5853
if (!didUpdate || update) {
5954
updateRepos(apt)
6055
didUpdate = true
6156
}
6257

58+
// Initialize apt if needed
6359
if (!didInit) {
6460
await initApt(apt)
6561
didInit = true
6662
}
6763

64+
// Install
6865
try {
6966
execRootSync(apt, ["install", "--fix-broken", "-y", ...qualifiedPacks])
7067
} catch (err) {
@@ -89,6 +86,23 @@ export enum AptPackageType {
8986
None = 3,
9087
}
9188

89+
async function addRepositories(apt: string, packages: AptPackage[]) {
90+
const allRepositories = [...new Set(packages.flatMap((pack) => pack.repositories ?? []))]
91+
if (allRepositories.length !== 0) {
92+
if (!didInit) {
93+
await initApt(apt)
94+
didInit = true
95+
}
96+
await installAddAptRepo()
97+
for (const repo of allRepositories) {
98+
// eslint-disable-next-line no-await-in-loop
99+
execRootSync("add-apt-repository", ["-y", repo])
100+
}
101+
updateRepos(apt)
102+
didUpdate = true
103+
}
104+
}
105+
92106
export async function aptPackageType(name: string, version: string | undefined): Promise<AptPackageType> {
93107
if (version !== undefined && version !== "") {
94108
const { stdout } = await execa("apt-cache", [
@@ -121,6 +135,13 @@ export async function aptPackageType(name: string, version: string | undefined):
121135
// ignore
122136
}
123137

138+
// If apt-cache fails, update the repos and try again
139+
if (!didUpdate) {
140+
updateRepos(getApt())
141+
didUpdate = true
142+
return aptPackageType(name, version)
143+
}
144+
124145
return AptPackageType.None
125146
}
126147

@@ -156,17 +177,27 @@ function updateRepos(apt: string) {
156177
execRootSync(apt, apt !== "nala" ? ["update", "-y"] : ["update"])
157178
}
158179

159-
/** Install apt utils and certificates (usually missing from docker containers) */
180+
async function installAddAptRepo() {
181+
if (await isPackageInstalled("software-properties-common")) {
182+
return
183+
}
184+
execRootSync("apt-get", ["install", "-y", "software-properties-common"])
185+
}
186+
187+
/** Install gnupg and certificates (usually missing from docker containers) */
160188
async function initApt(apt: string) {
161-
execRootSync(apt, [
162-
"install",
163-
"--fix-broken",
164-
"-y",
165-
"software-properties-common",
166-
"apt-utils",
167-
"ca-certificates",
168-
"gnupg",
169-
])
189+
// Update the repos if needed
190+
if (!didUpdate) {
191+
updateRepos(apt)
192+
didUpdate = true
193+
}
194+
195+
if (!(await isPackageInstalled("ca-certificates"))) {
196+
execRootSync(apt, ["install", "--fix-broken", "-y", "ca-certificates"])
197+
}
198+
if (!(await isPackageInstalled("gnupg"))) {
199+
execRootSync(apt, ["install", "--fix-broken", "-y", "gnupg"])
200+
}
170201
const promises: Promise<string | void>[] = [
171202
addAptKeyViaServer(["3B4FE6ACC0B21F32", "40976EAF437D05B5"], "setup-cpp-ubuntu-archive.gpg"),
172203
addAptKeyViaServer(["1E9377A2BA9EF27F"], "launchpad-toolchain.gpg"),

0 commit comments

Comments
 (0)