Skip to content

Commit

Permalink
Merge pull request #44 from adamchalmers/achalmers/arm64-support
Browse files Browse the repository at this point in the history
fix #25: Support ARM64 and other platforms
  • Loading branch information
alessio-perugini authored May 24, 2023
2 parents f9aa72c + f92bd14 commit 51cd93c
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 17 deletions.
20 changes: 20 additions & 0 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,26 @@ process.env["RUNNER_TEMP"] = tempDir;
process.env["RUNNER_TOOL_CACHE"] = toolDir;
import * as installer from "../src/installer";

describe("filename tests", () => {
const tests = [
["protoc-3.20.2-linux-x86_32.zip", "linux", ""],
["protoc-3.20.2-linux-x86_64.zip", "linux", "x64"],
["protoc-3.20.2-linux-aarch_64.zip", "linux", "arm64"],
["protoc-3.20.2-linux-ppcle_64.zip", "linux", "ppc64"],
["protoc-3.20.2-linux-s390_64.zip", "linux", "s390x"],
["protoc-3.20.2-osx-aarch_64.zip", "darwin", "arm64"],
["protoc-3.20.2-osx-x86_64.zip", "darwin", "x64"],
["protoc-3.20.2-win64.zip", "win32", "x64"],
["protoc-3.20.2-win32.zip", "win32", "x32"]
];
for (const [expected, plat, arch] of tests) {
it(`downloads ${expected} correctly`, () => {
const actual = installer.getFileName("3.20.2", plat, arch);
expect(expected).toBe(actual);
});
}
});

describe("installer tests", () => {
beforeEach(async function() {
await io.rmRF(toolDir);
Expand Down
52 changes: 45 additions & 7 deletions lib/installer.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ exports.getProtoc = getProtoc;
function downloadRelease(version) {
return __awaiter(this, void 0, void 0, function* () {
// Download
let fileName = getFileName(version);
let fileName = getFileName(version, osPlat, osArch);
let downloadUrl = util.format("https://github.com/protocolbuffers/protobuf/releases/download/%s/%s", version, fileName);
process.stdout.write("Downloading archive: " + downloadUrl + os.EOL);
let downloadPath = null;
Expand All @@ -126,7 +126,43 @@ function downloadRelease(version) {
return yield tc.cacheDir(extPath, "protoc", version);
});
}
function getFileName(version) {
/**
*
* @param osArch - A string identifying operating system CPU architecture for which the Node.js binary was compiled.
* See https://nodejs.org/api/os.html#osarch for possible values.
* @returns Suffix for the protoc filename.
*/
function fileNameSuffix(osArch) {
switch (osArch) {
case "x64": {
return "x86_64";
}
case "arm64": {
return "aarch_64";
}
case "s390x": {
return "s390_64";
}
case "ppc64": {
return "ppcle_64";
}
default: {
return "x86_32";
}
}
}
/**
* Returns the filename of the protobuf compiler.
*
* @param version - The version to download
* @param osPlat - The operating system platform for which the Node.js binary was compiled.
* See https://nodejs.org/api/os.html#osplatform for more.
* @param osArch - The operating system CPU architecture for which the Node.js binary was compiled.
* See https://nodejs.org/api/os.html#osarch for more.
* @returns The filename of the protocol buffer for the given release, platform and architecture.
*
*/
function getFileName(version, osPlat, osArch) {
// to compose the file name, strip the leading `v` char
if (version.startsWith("v")) {
version = version.slice(1, version.length);
Expand All @@ -136,12 +172,13 @@ function getFileName(version) {
const arch = osArch == "x64" ? "64" : "32";
return util.format("protoc-%s-win%s.zip", version, arch);
}
const arch = osArch == "x64" ? "x86_64" : "x86_32";
const suffix = fileNameSuffix(osArch);
if (osPlat == "darwin") {
return util.format("protoc-%s-osx-%s.zip", version, arch);
return util.format("protoc-%s-osx-%s.zip", version, suffix);
}
return util.format("protoc-%s-linux-%s.zip", version, arch);
return util.format("protoc-%s-linux-%s.zip", version, suffix);
}
exports.getFileName = getFileName;
// Retrieve a list of versions scraping tags from the Github API
function fetchVersions(includePreReleases, repoToken) {
return __awaiter(this, void 0, void 0, function* () {
Expand All @@ -156,8 +193,9 @@ function fetchVersions(includePreReleases, repoToken) {
}
let tags = [];
for (let pageNum = 1, morePages = true; morePages; pageNum++) {
let nextPage = (yield rest.get("https://api.github.com/repos/protocolbuffers/protobuf/releases?page=" +
pageNum)).result || [];
let p = yield rest.get("https://api.github.com/repos/protocolbuffers/protobuf/releases?page=" +
pageNum);
let nextPage = p.result || [];
if (nextPage.length > 0) {
tags = tags.concat(nextPage);
}
Expand Down
61 changes: 51 additions & 10 deletions src/installer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export async function getProtoc(

async function downloadRelease(version: string): Promise<string> {
// Download
let fileName: string = getFileName(version);
let fileName: string = getFileName(version, osPlat, osArch);
let downloadUrl: string = util.format(
"https://github.com/protocolbuffers/protobuf/releases/download/%s/%s",
version,
Expand All @@ -117,7 +117,48 @@ async function downloadRelease(version: string): Promise<string> {
return await tc.cacheDir(extPath, "protoc", version);
}

function getFileName(version: string): string {
/**
*
* @param osArch - A string identifying operating system CPU architecture for which the Node.js binary was compiled.
* See https://nodejs.org/api/os.html#osarch for possible values.
* @returns Suffix for the protoc filename.
*/
function fileNameSuffix(osArch: string): string {
switch (osArch) {
case "x64": {
return "x86_64";
}
case "arm64": {
return "aarch_64";
}
case "s390x": {
return "s390_64";
}
case "ppc64": {
return "ppcle_64";
}
default: {
return "x86_32";
}
}
}

/**
* Returns the filename of the protobuf compiler.
*
* @param version - The version to download
* @param osPlat - The operating system platform for which the Node.js binary was compiled.
* See https://nodejs.org/api/os.html#osplatform for more.
* @param osArch - The operating system CPU architecture for which the Node.js binary was compiled.
* See https://nodejs.org/api/os.html#osarch for more.
* @returns The filename of the protocol buffer for the given release, platform and architecture.
*
*/
export function getFileName(
version: string,
osPlat: string,
osArch: string
): string {
// to compose the file name, strip the leading `v` char
if (version.startsWith("v")) {
version = version.slice(1, version.length);
Expand All @@ -129,13 +170,13 @@ function getFileName(version: string): string {
return util.format("protoc-%s-win%s.zip", version, arch);
}

const arch: string = osArch == "x64" ? "x86_64" : "x86_32";
const suffix = fileNameSuffix(osArch);

if (osPlat == "darwin") {
return util.format("protoc-%s-osx-%s.zip", version, arch);
return util.format("protoc-%s-osx-%s.zip", version, suffix);
}

return util.format("protoc-%s-linux-%s.zip", version, arch);
return util.format("protoc-%s-linux-%s.zip", version, suffix);
}

// Retrieve a list of versions scraping tags from the Github API
Expand All @@ -154,11 +195,11 @@ async function fetchVersions(

let tags: IProtocRelease[] = [];
for (let pageNum = 1, morePages = true; morePages; pageNum++) {
let nextPage: IProtocRelease[] =
(await rest.get<IProtocRelease[]>(
"https://api.github.com/repos/protocolbuffers/protobuf/releases?page=" +
pageNum
)).result || [];
let p = await rest.get<IProtocRelease[]>(
"https://api.github.com/repos/protocolbuffers/protobuf/releases?page=" +
pageNum
);
let nextPage: IProtocRelease[] = p.result || [];
if (nextPage.length > 0) {
tags = tags.concat(nextPage);
} else {
Expand Down

0 comments on commit 51cd93c

Please sign in to comment.