Skip to content

Commit 51cd93c

Browse files
Merge pull request #44 from adamchalmers/achalmers/arm64-support
fix #25: Support ARM64 and other platforms
2 parents f9aa72c + f92bd14 commit 51cd93c

File tree

3 files changed

+116
-17
lines changed

3 files changed

+116
-17
lines changed

__tests__/main.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,26 @@ process.env["RUNNER_TEMP"] = tempDir;
1414
process.env["RUNNER_TOOL_CACHE"] = toolDir;
1515
import * as installer from "../src/installer";
1616

17+
describe("filename tests", () => {
18+
const tests = [
19+
["protoc-3.20.2-linux-x86_32.zip", "linux", ""],
20+
["protoc-3.20.2-linux-x86_64.zip", "linux", "x64"],
21+
["protoc-3.20.2-linux-aarch_64.zip", "linux", "arm64"],
22+
["protoc-3.20.2-linux-ppcle_64.zip", "linux", "ppc64"],
23+
["protoc-3.20.2-linux-s390_64.zip", "linux", "s390x"],
24+
["protoc-3.20.2-osx-aarch_64.zip", "darwin", "arm64"],
25+
["protoc-3.20.2-osx-x86_64.zip", "darwin", "x64"],
26+
["protoc-3.20.2-win64.zip", "win32", "x64"],
27+
["protoc-3.20.2-win32.zip", "win32", "x32"]
28+
];
29+
for (const [expected, plat, arch] of tests) {
30+
it(`downloads ${expected} correctly`, () => {
31+
const actual = installer.getFileName("3.20.2", plat, arch);
32+
expect(expected).toBe(actual);
33+
});
34+
}
35+
});
36+
1737
describe("installer tests", () => {
1838
beforeEach(async function() {
1939
await io.rmRF(toolDir);

lib/installer.js

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ exports.getProtoc = getProtoc;
106106
function downloadRelease(version) {
107107
return __awaiter(this, void 0, void 0, function* () {
108108
// Download
109-
let fileName = getFileName(version);
109+
let fileName = getFileName(version, osPlat, osArch);
110110
let downloadUrl = util.format("https://github.com/protocolbuffers/protobuf/releases/download/%s/%s", version, fileName);
111111
process.stdout.write("Downloading archive: " + downloadUrl + os.EOL);
112112
let downloadPath = null;
@@ -126,7 +126,43 @@ function downloadRelease(version) {
126126
return yield tc.cacheDir(extPath, "protoc", version);
127127
});
128128
}
129-
function getFileName(version) {
129+
/**
130+
*
131+
* @param osArch - A string identifying operating system CPU architecture for which the Node.js binary was compiled.
132+
* See https://nodejs.org/api/os.html#osarch for possible values.
133+
* @returns Suffix for the protoc filename.
134+
*/
135+
function fileNameSuffix(osArch) {
136+
switch (osArch) {
137+
case "x64": {
138+
return "x86_64";
139+
}
140+
case "arm64": {
141+
return "aarch_64";
142+
}
143+
case "s390x": {
144+
return "s390_64";
145+
}
146+
case "ppc64": {
147+
return "ppcle_64";
148+
}
149+
default: {
150+
return "x86_32";
151+
}
152+
}
153+
}
154+
/**
155+
* Returns the filename of the protobuf compiler.
156+
*
157+
* @param version - The version to download
158+
* @param osPlat - The operating system platform for which the Node.js binary was compiled.
159+
* See https://nodejs.org/api/os.html#osplatform for more.
160+
* @param osArch - The operating system CPU architecture for which the Node.js binary was compiled.
161+
* See https://nodejs.org/api/os.html#osarch for more.
162+
* @returns The filename of the protocol buffer for the given release, platform and architecture.
163+
*
164+
*/
165+
function getFileName(version, osPlat, osArch) {
130166
// to compose the file name, strip the leading `v` char
131167
if (version.startsWith("v")) {
132168
version = version.slice(1, version.length);
@@ -136,12 +172,13 @@ function getFileName(version) {
136172
const arch = osArch == "x64" ? "64" : "32";
137173
return util.format("protoc-%s-win%s.zip", version, arch);
138174
}
139-
const arch = osArch == "x64" ? "x86_64" : "x86_32";
175+
const suffix = fileNameSuffix(osArch);
140176
if (osPlat == "darwin") {
141-
return util.format("protoc-%s-osx-%s.zip", version, arch);
177+
return util.format("protoc-%s-osx-%s.zip", version, suffix);
142178
}
143-
return util.format("protoc-%s-linux-%s.zip", version, arch);
179+
return util.format("protoc-%s-linux-%s.zip", version, suffix);
144180
}
181+
exports.getFileName = getFileName;
145182
// Retrieve a list of versions scraping tags from the Github API
146183
function fetchVersions(includePreReleases, repoToken) {
147184
return __awaiter(this, void 0, void 0, function* () {
@@ -156,8 +193,9 @@ function fetchVersions(includePreReleases, repoToken) {
156193
}
157194
let tags = [];
158195
for (let pageNum = 1, morePages = true; morePages; pageNum++) {
159-
let nextPage = (yield rest.get("https://api.github.com/repos/protocolbuffers/protobuf/releases?page=" +
160-
pageNum)).result || [];
196+
let p = yield rest.get("https://api.github.com/repos/protocolbuffers/protobuf/releases?page=" +
197+
pageNum);
198+
let nextPage = p.result || [];
161199
if (nextPage.length > 0) {
162200
tags = tags.concat(nextPage);
163201
}

src/installer.ts

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ export async function getProtoc(
9191

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

120-
function getFileName(version: string): string {
120+
/**
121+
*
122+
* @param osArch - A string identifying operating system CPU architecture for which the Node.js binary was compiled.
123+
* See https://nodejs.org/api/os.html#osarch for possible values.
124+
* @returns Suffix for the protoc filename.
125+
*/
126+
function fileNameSuffix(osArch: string): string {
127+
switch (osArch) {
128+
case "x64": {
129+
return "x86_64";
130+
}
131+
case "arm64": {
132+
return "aarch_64";
133+
}
134+
case "s390x": {
135+
return "s390_64";
136+
}
137+
case "ppc64": {
138+
return "ppcle_64";
139+
}
140+
default: {
141+
return "x86_32";
142+
}
143+
}
144+
}
145+
146+
/**
147+
* Returns the filename of the protobuf compiler.
148+
*
149+
* @param version - The version to download
150+
* @param osPlat - The operating system platform for which the Node.js binary was compiled.
151+
* See https://nodejs.org/api/os.html#osplatform for more.
152+
* @param osArch - The operating system CPU architecture for which the Node.js binary was compiled.
153+
* See https://nodejs.org/api/os.html#osarch for more.
154+
* @returns The filename of the protocol buffer for the given release, platform and architecture.
155+
*
156+
*/
157+
export function getFileName(
158+
version: string,
159+
osPlat: string,
160+
osArch: string
161+
): string {
121162
// to compose the file name, strip the leading `v` char
122163
if (version.startsWith("v")) {
123164
version = version.slice(1, version.length);
@@ -129,13 +170,13 @@ function getFileName(version: string): string {
129170
return util.format("protoc-%s-win%s.zip", version, arch);
130171
}
131172

132-
const arch: string = osArch == "x64" ? "x86_64" : "x86_32";
173+
const suffix = fileNameSuffix(osArch);
133174

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

138-
return util.format("protoc-%s-linux-%s.zip", version, arch);
179+
return util.format("protoc-%s-linux-%s.zip", version, suffix);
139180
}
140181

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

155196
let tags: IProtocRelease[] = [];
156197
for (let pageNum = 1, morePages = true; morePages; pageNum++) {
157-
let nextPage: IProtocRelease[] =
158-
(await rest.get<IProtocRelease[]>(
159-
"https://api.github.com/repos/protocolbuffers/protobuf/releases?page=" +
160-
pageNum
161-
)).result || [];
198+
let p = await rest.get<IProtocRelease[]>(
199+
"https://api.github.com/repos/protocolbuffers/protobuf/releases?page=" +
200+
pageNum
201+
);
202+
let nextPage: IProtocRelease[] = p.result || [];
162203
if (nextPage.length > 0) {
163204
tags = tags.concat(nextPage);
164205
} else {

0 commit comments

Comments
 (0)