Skip to content

Commit

Permalink
fix: fix parsing of gcc version on macos + sort gcc exes
Browse files Browse the repository at this point in the history
  • Loading branch information
aminya committed Sep 3, 2024
1 parent c2e0936 commit dfe5373
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 71 deletions.
34 changes: 17 additions & 17 deletions dist/actions/setup-cpp.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/actions/setup-cpp.js.map

Large diffs are not rendered by default.

34 changes: 17 additions & 17 deletions dist/legacy/setup-cpp.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/legacy/setup-cpp.js.map

Large diffs are not rendered by default.

34 changes: 17 additions & 17 deletions dist/modern/setup-cpp.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/modern/setup-cpp.js.map

Large diffs are not rendered by default.

11 changes: 9 additions & 2 deletions src/gcc/gcc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { type InstallationInfo, type PackageInfo, setupBin } from "../utils/setu
import { setupChocoPack } from "../utils/setup/setupChocoPack.js"
import { setupDnfPack } from "../utils/setup/setupDnfPack.js"
import { setupPacmanPack } from "../utils/setup/setupPacmanPack.js"
import { compareVersion } from "../utils/setup/version.js"

async function getGccPackageInfo(version: string, platform: NodeJS.Platform, arch: string): Promise<PackageInfo> {
switch (platform) {
Expand Down Expand Up @@ -284,7 +285,9 @@ async function getGccCmdVersion(binDir: string, givenVersion: string) {
gccExe = `${binDir}/gcc`
} else {
// try to find the gcc exe in the bin dir
const files = await readdir(binDir)
const files = (await readdir(binDir)).sort(
compareVersion,
)
for (const file of files) {
if (file.startsWith("gcc")) {
gccExe = `${binDir}/${file}`
Expand All @@ -295,7 +298,11 @@ async function getGccCmdVersion(binDir: string, givenVersion: string) {

const { stdout: versionStdout } = await execa(gccExe, ["--version"], { stdio: "pipe" })

const versionMatch = (versionStdout as string).match(/gcc \(.*\) ([\d.]+)/)
// gcc-11 (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
// gcc-12 (Homebrew GCC 12.4.0) 12.4.0
// gcc (Ubuntu 13.1.0-8ubuntu1~22.04) 13.1.0

const versionMatch = (versionStdout as string).match(/gcc.* \(.*\) ([\d.]+)/)

if (versionMatch !== null) {
return versionMatch[1]
Expand Down
5 changes: 3 additions & 2 deletions src/utils/github/fetch-assets.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Octokit } from "@octokit/rest"
import { writeFile } from "fs/promises"
import JsonStringify from "safe-stable-stringify"
import { type Assets, compareTag } from "./load-assets.ts"
import { compareVersion } from "../setup/version.ts"
import type { Assets } from "./load-assets.ts"

/**
* Get the list of all releases of a GitHub repository
Expand Down Expand Up @@ -70,7 +71,7 @@ export async function saveGitHubAssetList(
const assets = await fetchGitHubAssetList(owner, repo)

const jsonStringify = JsonStringify.configure({
deterministic: compareTag,
deterministic: compareVersion,
})
const data = jsonStringify(assets)

Expand Down
13 changes: 0 additions & 13 deletions src/utils/github/load-assets.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { readFile } from "fs/promises"
import coerce from "semver/functions/coerce.js"

/**
* The list of assets of a GitHub release
Expand All @@ -8,18 +7,6 @@ import coerce from "semver/functions/coerce.js"
*/
export type Assets = Record<string, string[]>

export function compareTag(tag1: string, tag2: string) {
const v1 = coerce(tag1)
const v2 = coerce(tag2)
if (v1 !== null && v2 !== null) {
// put the latest version first
return v2.compare(v1)
}

// if the tags are not semver, compare them as strings, putting the latest tag first
return tag2.localeCompare(tag1)
}

export async function loadGitHubAssetList(path: string): Promise<Assets> {
const data = await readFile(path, "utf-8")
return JSON.parse(data)
Expand Down
12 changes: 12 additions & 0 deletions src/utils/setup/version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,15 @@ export function addVPrefix(version: string) {
}
return version
}

export function compareVersion(tag1: string, tag2: string) {
const v1 = semverCoerce(tag1)
const v2 = semverCoerce(tag2)
if (v1 !== null && v2 !== null) {
// put the latest version first
return v2.compare(v1)
}

// if the tags are not semver, compare them as strings, putting the latest tag first
return tag2.localeCompare(tag1)
}

0 comments on commit dfe5373

Please sign in to comment.