Skip to content

Commit 9374721

Browse files
authoredJun 24, 2022
refactor makefile and add CI (#35)
1 parent 913dfe2 commit 9374721

12 files changed

+3892
-3787
lines changed
 

‎.eslintrc.js

+9-24
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2020-2021 Buf Technologies, Inc.
1+
// Copyright 2020-2022 Buf Technologies, Inc.
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -12,39 +12,24 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
const ignoreFiles = [".eslintrc.js", "dist/**/*"];
16+
1517
module.exports = {
1618
env: {
17-
es2021: true
19+
es2022: true,
1820
},
19-
extends: [
20-
"eslint:recommended",
21-
"plugin:@typescript-eslint/recommended",
22-
"plugin:@typescript-eslint/recommended-requiring-type-checking",
23-
],
21+
ignorePatterns: ignoreFiles,
22+
extends: ["eslint:recommended", "plugin:@typescript-eslint/recommended"],
2423
parser: "@typescript-eslint/parser",
2524
parserOptions: {
2625
project: "tsconfig.json",
2726
tsconfigRootDir: __dirname,
2827
ecmaFeatures: {
29-
jsx: true
28+
jsx: true,
3029
},
3130
ecmaVersion: 12,
3231
sourceType: "module",
3332
},
34-
plugins: [
35-
"@typescript-eslint",
36-
],
37-
ignorePatterns: [
38-
".eslintrc.js",
39-
],
40-
rules: {
41-
"@typescript-eslint/strict-boolean-expressions": "error",
42-
"@typescript-eslint/no-unnecessary-condition": "error",
43-
"@typescript-eslint/array-type": "error",
44-
"@typescript-eslint/switch-exhaustiveness-check": "error",
45-
"@typescript-eslint/prefer-nullish-coalescing": "error",
46-
"@typescript-eslint/no-unnecessary-boolean-literal-compare": "error",
47-
"@typescript-eslint/no-invalid-void-type": "error",
48-
"@typescript-eslint/no-base-to-string": "error"
49-
},
33+
plugins: ["@typescript-eslint"],
34+
rules: {},
5035
};

‎.github/workflows/ci.yaml

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: ci
2+
on:
3+
push:
4+
branches: [main]
5+
pull_request:
6+
branches: [main]
7+
workflow_dispatch: {} # support manual runs
8+
# Prevent writing to the repository using the CI token.
9+
# Ref: https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#permissions
10+
permissions: read-all
11+
jobs:
12+
ci:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- name: Checkout Code
16+
uses: actions/checkout@v3
17+
with:
18+
fetch-depth: 1
19+
- name: Install Go
20+
uses: actions/setup-go@v3
21+
with:
22+
go-version: 1.18
23+
- name: Install Node
24+
uses: actions/setup-node@v3
25+
with:
26+
node-version: 16
27+
- name: Generate
28+
run: make generate && make checkgenerate
29+
- name: Build
30+
run: make build && make checkgenerate

‎.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
/.tmp/
12
node_modules/

‎.prettierignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/dist

‎Makefile

+53-14
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
1-
SHELL := /usr/bin/env bash -o pipefail
1+
# See https://tech.davis-hansson.com/p/make/
2+
SHELL := bash
3+
.DELETE_ON_ERROR:
4+
.SHELLFLAGS := -eu -o pipefail -c
5+
.DEFAULT_GOAL := all
6+
MAKEFLAGS += --warn-undefined-variables
7+
MAKEFLAGS += --no-builtin-rules
8+
MAKEFLAGS += --no-print-directory
9+
BIN := .tmp/bin
10+
COPYRIGHT_YEARS := 2020-2022
11+
LICENSE_IGNORE := -e dist\/
212

313
UNAME_OS := $(shell uname -s)
414

@@ -8,25 +18,54 @@ else
818
SED_I := sed -i
919
endif
1020

11-
.DEFAULT_GOAL := build
21+
PHONY: all
22+
all: ## Format, Lint and build (default)
23+
$(MAKE) build
24+
25+
.PHONY: format
26+
format: node_modules
27+
npm run format
28+
29+
.PHONY: lint
30+
lint: node_modules
31+
npm run lint
1232

1333
.PHONY: build
14-
build:
15-
@tsc
16-
@eslint ./src
17-
@esbuild \
18-
--minify \
19-
--bundle \
20-
--sourcemap \
21-
'--define:process.env.NODE_ENV="production"' \
22-
--outdir=dist \
23-
--platform=node \
24-
--target=node12 \
25-
./src/main.ts
34+
build: node_modules format lint
35+
npm run build
2636

2737
.PHONY: updateversion
2838
updateversion:
2939
ifndef VERSION
3040
$(error "VERSION must be set")
3141
endif
3242
$(SED_I) "s/default: '[0-9].[0-9][0-9]*\.[0-9][0-9]*[-rc0-9]*'/default: '$(VERSION)'/g" action.yml
43+
44+
.PHONY: generate
45+
generate: $(BIN)/license-header ## Regenerate licenses
46+
@# We want to operate on a list of modified and new files, excluding
47+
@# deleted and ignored files. git-ls-files can't do this alone. comm -23 takes
48+
@# two files and prints the union, dropping lines common to both (-3) and
49+
@# those only in the second file (-2). We make one git-ls-files call for
50+
@# the modified, cached, and new (--others) files, and a second for the
51+
@# deleted files.
52+
comm -23 \
53+
<(git ls-files --cached --modified --others --no-empty-directory --exclude-standard | sort -u | grep -v $(LICENSE_IGNORE) ) \
54+
<(git ls-files --deleted | sort -u) | \
55+
xargs $(BIN)/license-header \
56+
--license-type apache \
57+
--copyright-holder "Buf Technologies, Inc." \
58+
--year-range "$(COPYRIGHT_YEARS)"
59+
60+
.PHONY: checkgenerate
61+
checkgenerate:
62+
@# Used in CI to verify that `make generate` doesn't produce a diff.
63+
test -z "$$(git status --porcelain | tee /dev/stderr)"
64+
65+
node_modules: package-lock.json
66+
npm ci
67+
68+
$(BIN)/license-header: Makefile
69+
@mkdir -p $(@D)
70+
GOBIN=$(abspath $(@D)) go install \
71+
github.com/bufbuild/buf/private/pkg/licenseheader/cmd/license-header@v1.6.0

‎dist/main.js.map

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package-lock.json

+3,637-3,616
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

+28-19
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,30 @@
11
{
2-
"name": "buf-setup",
3-
"version": "1.6.0",
4-
"engineStrict": true,
5-
"engines": {
6-
"node": ">=16",
7-
"npm": ">=8"
8-
},
9-
"dependencies": {
10-
"@actions/core": "^1.9.0",
11-
"@actions/github": "^5.0.3",
12-
"@actions/io": "^1.1.2",
13-
"@actions/tool-cache": "^2.0.1",
14-
"@types/node": "^18.0.0",
15-
"esbuild": "^0.14.47",
16-
"typescript": "^4.7.4"
17-
},
18-
"devDependencies": {
19-
"@typescript-eslint/eslint-plugin": "^5.29.0"
20-
}
2+
"name": "buf-setup",
3+
"version": "1.6.0",
4+
"scripts": {
5+
"build": "esbuild --minify --bundle --sourcemap '--define:process.env.NODE_ENV=\"production\"' --outdir=dist --platform=node --target=node16 ./src/main.ts",
6+
"eslint": "eslint --max-warnings 0 .",
7+
"format": "prettier --write '**/*.{json,js,jsx,ts,tsx,css}' --loglevel error",
8+
"lint": "npm run eslint && npm run types-check",
9+
"types-check": "tsc --noemit"
10+
},
11+
"engineStrict": true,
12+
"engines": {
13+
"node": ">=16",
14+
"npm": ">=8"
15+
},
16+
"dependencies": {
17+
"@actions/core": "^1.9.0",
18+
"@actions/github": "^5.0.3",
19+
"@actions/io": "^1.1.2",
20+
"@actions/tool-cache": "^2.0.1",
21+
"@types/node": "^18.0.0",
22+
"esbuild": "^0.14.47",
23+
"typescript": "^4.7.4"
24+
},
25+
"devDependencies": {
26+
"@typescript-eslint/eslint-plugin": "^5.29.0",
27+
"@typescript-eslint/parser": "^5.29.0",
28+
"prettier": "^2.7.1"
29+
}
2130
}

‎src/buf.ts

+71-53
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2020-2021 Buf Technologies, Inc.
1+
// Copyright 2020-2022 Buf Technologies, Inc.
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -12,112 +12,130 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
import * as os from 'os';
16-
import * as path from 'path';
17-
import * as core from '@actions/core';
18-
import * as tc from '@actions/tool-cache';
19-
import {Octokit} from '@octokit/core';
20-
import { Error, isError } from './error';
15+
import * as os from "os";
16+
import * as path from "path";
17+
import * as core from "@actions/core";
18+
import * as tc from "@actions/tool-cache";
19+
import { Octokit } from "@octokit/core";
20+
import { Error, isError } from "./error";
2121

2222
// versionPrefix is used in Github release names, and can
2323
// optionally be specified in the action's version parameter.
2424
const versionPrefix = "v";
2525

26-
export async function getBuf(version: string, githubToken: string): Promise<string|Error> {
27-
const binaryPath = tc.find('buf', version, os.arch());
28-
if (binaryPath !== '') {
26+
export async function getBuf(
27+
version: string,
28+
githubToken: string
29+
): Promise<string | Error> {
30+
const binaryPath = tc.find("buf", version, os.arch());
31+
if (binaryPath !== "") {
2932
core.info(`Found in cache @ ${binaryPath}`);
3033
return binaryPath;
3134
}
3235

3336
core.info(`Resolving the download URL for the current platform...`);
3437
const downloadURL = await getDownloadURL(version, githubToken);
3538
if (isError(downloadURL)) {
36-
return downloadURL
39+
return downloadURL;
3740
}
3841

3942
let cacheDir = "";
4043
core.info(`Downloading buf version "${version}" from ${downloadURL}`);
41-
if (downloadURL.endsWith('.tar.gz')){
44+
if (downloadURL.endsWith(".tar.gz")) {
4245
const downloadPath = await tc.downloadTool(downloadURL);
43-
core.info(`Successfully downloaded buf version "${version}" from ${downloadURL}`);
46+
core.info(
47+
`Successfully downloaded buf version "${version}" from ${downloadURL}`
48+
);
4449

45-
core.info('Extracting buf...');
50+
core.info("Extracting buf...");
4651
const extractPath = await tc.extractTar(downloadPath);
4752
core.info(`Successfully extracted buf to ${extractPath}`);
4853

49-
core.info('Adding buf to the cache...');
54+
core.info("Adding buf to the cache...");
5055
cacheDir = await tc.cacheDir(
51-
path.join(extractPath, 'buf'),
52-
'buf',
56+
path.join(extractPath, "buf"),
57+
"buf",
5358
version,
5459
os.arch()
5560
);
5661
} else {
5762
// For Windows, we only download the .exe for `buf` CLI becasue we do not create `.tar.gz`
5863
// bundles for Windows releases.
59-
const downloadPath = await tc.downloadTool(downloadURL, 'C:\\Users\\runneradmin\\buf-download\\buf.exe');
60-
core.info(`Successfully downloaded buf version "${version}" from ${downloadURL} to ${downloadPath}`);
64+
const downloadPath = await tc.downloadTool(
65+
downloadURL,
66+
"C:\\Users\\runneradmin\\buf-download\\buf.exe"
67+
);
68+
core.info(
69+
`Successfully downloaded buf version "${version}" from ${downloadURL} to ${downloadPath}`
70+
);
6171

62-
core.info('Adding buf to the cache...');
63-
cacheDir = await tc.cacheDir(path.dirname(downloadPath), 'buf', version, os.arch());
72+
core.info("Adding buf to the cache...");
73+
cacheDir = await tc.cacheDir(
74+
path.dirname(downloadPath),
75+
"buf",
76+
version,
77+
os.arch()
78+
);
6479
}
6580
core.info(`Successfully cached buf to ${cacheDir}`);
6681
return cacheDir;
6782
}
6883

6984
// getDownloadURL resolves Buf's Github download URL for the
7085
// current architecture and platform.
71-
async function getDownloadURL(version: string, githubToken: string): Promise<string|Error> {
72-
let architecture = '';
86+
async function getDownloadURL(
87+
version: string,
88+
githubToken: string
89+
): Promise<string | Error> {
90+
let architecture = "";
7391
switch (os.arch()) {
7492
// The available architectures can be found at:
7593
// https://nodejs.org/api/process.html#process_process_arch
76-
case 'x64':
77-
architecture = 'x86_64';
94+
case "x64":
95+
architecture = "x86_64";
7896
break;
79-
case 'arm64':
80-
architecture = 'arm64';
97+
case "arm64":
98+
architecture = "arm64";
8199
break;
82100
default:
83101
return {
84-
message: `The "${os.arch()}" architecture is not supported with a Buf release.`
102+
message: `The "${os.arch()}" architecture is not supported with a Buf release.`,
85103
};
86104
}
87-
let platform = '';
105+
let platform = "";
88106
switch (os.platform()) {
89107
// The available platforms can be found at:
90108
// https://nodejs.org/api/process.html#process_process_platform
91-
case 'linux':
92-
platform = 'Linux';
109+
case "linux":
110+
platform = "Linux";
93111
break;
94-
case 'darwin':
95-
platform = 'Darwin';
112+
case "darwin":
113+
platform = "Darwin";
96114
break;
97-
case 'win32':
98-
platform = 'Windows';
115+
case "win32":
116+
platform = "Windows";
99117
break;
100118
default:
101119
return {
102-
message: `The "${os.platform()}" platform is not supported with a Buf release.`
120+
message: `The "${os.platform()}" platform is not supported with a Buf release.`,
103121
};
104122
}
105123
// The asset name is determined by the buf release structure found at:
106124
// https://github.com/bufbuild/buf/blob/8255257bd94c9f1b5faa27242211c5caad05be79/make/buf/scripts/release.bash#L102
107-
let assetName = '';
125+
let assetName = "";
108126
// For Windows, we only download the .exe for `buf` CLI
109-
if (platform === 'Windows') {
110-
assetName = `buf-${platform}-${architecture}.exe`
127+
if (platform === "Windows") {
128+
assetName = `buf-${platform}-${architecture}.exe`;
111129
} else {
112-
assetName = `buf-${platform}-${architecture}.tar.gz`
130+
assetName = `buf-${platform}-${architecture}.tar.gz`;
113131
}
114132
const octokit = new Octokit({ auth: githubToken });
115-
if (version === 'latest') {
116-
const {data: releases} = await octokit.request(
117-
'GET /repos/{owner}/{repo}/releases',
133+
if (version === "latest") {
134+
const { data: releases } = await octokit.request(
135+
"GET /repos/{owner}/{repo}/releases",
118136
{
119-
owner: 'bufbuild',
120-
repo: 'buf',
137+
owner: "bufbuild",
138+
repo: "buf",
121139
per_page: 1,
122140
}
123141
);
@@ -127,15 +145,15 @@ async function getDownloadURL(version: string, githubToken: string): Promise<str
127145
}
128146
}
129147
return {
130-
message: `Unable to find Buf version "${version}" for platform "${platform}" and architecture "${architecture}".`
148+
message: `Unable to find Buf version "${version}" for platform "${platform}" and architecture "${architecture}".`,
131149
};
132150
}
133151
const tag = releaseTagForVersion(version);
134-
const {data: release} = await octokit.request(
135-
'GET /repos/{owner}/{repo}/releases/tags/{tag}',
152+
const { data: release } = await octokit.request(
153+
"GET /repos/{owner}/{repo}/releases/tags/{tag}",
136154
{
137-
owner: 'bufbuild',
138-
repo: 'buf',
155+
owner: "bufbuild",
156+
repo: "buf",
139157
tag: tag,
140158
}
141159
);
@@ -145,7 +163,7 @@ async function getDownloadURL(version: string, githubToken: string): Promise<str
145163
}
146164
}
147165
return {
148-
message: `Unable to find Buf version "${version}" for platform "${platform}" and architecture "${architecture}".`
166+
message: `Unable to find Buf version "${version}" for platform "${platform}" and architecture "${architecture}".`,
149167
};
150168
}
151169

@@ -154,7 +172,7 @@ async function getDownloadURL(version: string, githubToken: string): Promise<str
154172
// both versions, e.g. v0.38.0 and 0.38.0.
155173
function releaseTagForVersion(version: string): string {
156174
if (version.indexOf(versionPrefix) === 0) {
157-
return version
175+
return version;
158176
}
159-
return versionPrefix + version
177+
return versionPrefix + version;
160178
}

‎src/error.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2020-2021 Buf Technologies, Inc.
1+
// Copyright 2020-2022 Buf Technologies, Inc.
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.

‎src/main.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2020-2021 Buf Technologies, Inc.
1+
// Copyright 2020-2022 Buf Technologies, Inc.
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -12,6 +12,6 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
import {run} from './run';
15+
import { run } from "./run";
1616

1717
void run();

‎src/run.ts

+57-56
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2020-2021 Buf Technologies, Inc.
1+
// Copyright 2020-2022 Buf Technologies, Inc.
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -12,71 +12,72 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
import cp from 'child_process';
16-
import * as os from 'os';
17-
import * as path from 'path';
18-
import * as core from '@actions/core';
19-
import * as io from '@actions/io';
20-
import { getBuf } from './buf';
21-
import { Error, isError } from './error';
15+
import cp from "child_process";
16+
import * as os from "os";
17+
import * as path from "path";
18+
import * as core from "@actions/core";
19+
import * as io from "@actions/io";
20+
import { getBuf } from "./buf";
21+
import { Error, isError } from "./error";
2222

2323
export async function run(): Promise<void> {
24-
try {
25-
const result = await runSetup()
26-
if (result !== null && isError(result)) {
27-
core.setFailed(result.message);
28-
}
29-
} catch (error) {
30-
// In case we ever fail to catch an error
31-
// in the call chain, we catch the error
32-
// and mark the build as a failure. The
33-
// user is otherwise prone to false positives.
34-
if (isError(error)) {
35-
core.setFailed(error.message);
36-
return;
37-
}
38-
core.setFailed('Internal error');
24+
try {
25+
const result = await runSetup();
26+
if (result !== null && isError(result)) {
27+
core.setFailed(result.message);
3928
}
29+
} catch (error) {
30+
// In case we ever fail to catch an error
31+
// in the call chain, we catch the error
32+
// and mark the build as a failure. The
33+
// user is otherwise prone to false positives.
34+
if (isError(error)) {
35+
core.setFailed(error.message);
36+
return;
37+
}
38+
core.setFailed("Internal error");
39+
}
4040
}
4141

4242
// runSetup runs the buf-setup action, and returns
4343
// a non-empty error if it fails.
44-
async function runSetup(): Promise<null|Error> {
45-
const version = core.getInput('version');
46-
if (version === '') {
47-
return {
48-
message: 'a version was not provided'
49-
};
50-
}
44+
async function runSetup(): Promise<null | Error> {
45+
const version = core.getInput("version");
46+
if (version === "") {
47+
return {
48+
message: "a version was not provided",
49+
};
50+
}
5151

52-
const githubToken = core.getInput('github_token');
53-
if (githubToken === '') {
54-
core.warning('No github_token supplied, API requests will be subject to stricter rate limiting');
55-
}
52+
const githubToken = core.getInput("github_token");
53+
if (githubToken === "") {
54+
core.warning(
55+
"No github_token supplied, API requests will be subject to stricter rate limiting"
56+
);
57+
}
5658

59+
core.info(`Setting up buf version "${version}"`);
60+
const installDir = await getBuf(version, githubToken);
61+
if (isError(installDir)) {
62+
return installDir;
63+
}
5764

58-
core.info(`Setting up buf version "${version}"`);
59-
const installDir = await getBuf(version, githubToken);
60-
if (isError(installDir)) {
61-
return installDir
62-
}
63-
64-
core.info('Adding buf binary to PATH');
65-
let binaryPath = '';
66-
if (os.platform() === 'win32') {
67-
core.addPath(installDir);
68-
} else {
69-
core.addPath(path.join(installDir, 'bin'));
70-
}
71-
binaryPath = await io.which('buf', true);
72-
if (binaryPath === '') {
73-
return {
74-
message: 'buf was not found on PATH'
75-
};
76-
}
65+
core.info("Adding buf binary to PATH");
66+
let binaryPath = "";
67+
if (os.platform() === "win32") {
68+
core.addPath(installDir);
69+
} else {
70+
core.addPath(path.join(installDir, "bin"));
71+
}
72+
binaryPath = await io.which("buf", true);
73+
if (binaryPath === "") {
74+
return {
75+
message: "buf was not found on PATH",
76+
};
77+
}
7778

78-
core.info(`Successfully setup buf version ${version}`);
79-
core.info(cp.execSync(`${binaryPath} --version`).toString());
79+
core.info(`Successfully setup buf version ${version}`);
80+
core.info(cp.execSync(`${binaryPath} --version`).toString());
8081

81-
return null;
82+
return null;
8283
}

0 commit comments

Comments
 (0)
Please sign in to comment.