Skip to content

Commit 87a4dd2

Browse files
authored
Download typegen binaries during postinstall
1 parent e654781 commit 87a4dd2

File tree

12 files changed

+186
-66
lines changed

12 files changed

+186
-66
lines changed

.github/workflows/release-create-kpt-functions.yaml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,15 @@ jobs:
2424
- name: Build, Test, Lint
2525
run: |
2626
cd go
27-
make publish-typegen
27+
make package-typegen
28+
- name: Set up gcloud
29+
uses: GoogleCloudPlatform/github-actions/setup-gcloud@master
30+
with:
31+
version: '275.0.0'
32+
service_account_email: ${{ secrets.GCP_SA_EMAIL }}
33+
service_account_key: ${{ secrets.GCP_SA_KEY }}
34+
- name: Upload binaries
35+
run: ./scripts/publish-bins.sh
2836
- name: Use Node.js 12.x
2937
uses: actions/setup-node@v1
3038
with:

go/Makefile

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
GOPATH := $(shell go env GOPATH)
44
OUT_DIR := .out
5-
CLI_BIN := ../ts/create-kpt-functions/bin/
65

76
all: fix fmt test test-e2e lint
87

@@ -29,12 +28,11 @@ publish-functions: all
2928
./scripts/publish-functions.sh
3029

3130
build-typegen: fix fmt test lint
32-
env GOOS=linux GOARCH=amd64 go build -o $(OUT_DIR)/typegen_linux_amd64 ./cmd/typegen
33-
env GOOS=darwin GOARCH=amd64 go build -o $(OUT_DIR)/typegen_darwin_amd64 ./cmd/typegen
34-
env GOOS=windows GOARCH=amd64 go build -o $(OUT_DIR)/typegen_windows_amd64 ./cmd/typegen
35-
36-
publish-typegen: build-typegen
37-
rm -rf $(CLI_BIN)
38-
mkdir -p $(CLI_BIN)
39-
cp $(OUT_DIR)/typegen* $(CLI_BIN)
40-
31+
env GOOS=linux GOARCH=amd64 go build -o $(OUT_DIR)/linux/typegen ./cmd/typegen
32+
env GOOS=darwin GOARCH=amd64 go build -o $(OUT_DIR)/darwin/typegen ./cmd/typegen
33+
env GOOS=windows GOARCH=amd64 go build -o $(OUT_DIR)/windows/typegen ./cmd/typegen
34+
35+
package-typegen: build-typegen
36+
tar -C $(OUT_DIR)/linux -czvf $(OUT_DIR)/typegen_linux_amd64.tar.gz typegen
37+
tar -C $(OUT_DIR)/darwin -czvf $(OUT_DIR)/typegen_darwin_amd64.tar.gz typegen
38+
tar -C $(OUT_DIR)/windows -czvf $(OUT_DIR)/typegen_windows_amd64.tar.gz typegen

scripts/publish-bins.sh

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/usr/bin/env bash
2+
# Copyright 2019 Google LLC
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
set -xeuo pipefail
17+
18+
VERSION=${1#*-v};
19+
20+
cd go/.out
21+
gsutil cp *.tar.gz gs://kpt-functions/v${VERSION}

ts/create-kpt-functions/package-lock.json

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

ts/create-kpt-functions/package.json

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@googlecontainertools/create-kpt-functions",
3-
"version": "0.13.1",
3+
"version": "0.14.3",
44
"description": "KPT functions CLI",
55
"author": "KPT Authors",
66
"license": "Apache-2.0",
@@ -13,16 +13,16 @@
1313
"kpt": "dist/cli.js"
1414
},
1515
"files": [
16-
"bin/",
1716
"dist/",
1817
"!**/*_test.*",
1918
"templates/"
2019
],
2120
"scripts": {
2221
"prepare": "npm run build",
22+
"postinstall": "go-npm install",
23+
"preuninstall": "go-npm uninstall",
2324
"build": "tsc",
2425
"watch": "tsc --watch",
25-
"prepublishOnly": "ls bin/typegen_darwin_amd64 bin/typegen_linux_amd64 bin/typegen_windows_amd64",
2626
"clean": "rm -Rf node_modules/ dist/",
2727
"lint": "tslint -p package.json; prettier \"src/**\" \"*.json\" --check",
2828
"lint-license": "license-checker --onlyAllow 'Apache-2.0;MIT;BSD;BSD-2-Clause;BSD-3-Clause;ISC;CC-BY-3.0;CC0-1.0;Unlicense'",
@@ -38,6 +38,7 @@
3838
"cli-interact": "^0.1.9",
3939
"fs-extra": "^8.1.0",
4040
"glob": "^7.1.4",
41+
"go-npm": "^0.1.9",
4142
"is-valid-npm-name": "0.0.4",
4243
"mustache": "^3.0.1",
4344
"request": "^2.88.0",
@@ -63,6 +64,11 @@
6364
"tslint-consistent-codestyle": "^1.16.0",
6465
"typescript": "^3.5.3"
6566
},
67+
"goBinary": {
68+
"name": "typegen",
69+
"path": "./bin",
70+
"url": "https://storage.googleapis.com/kpt-functions/v{{version}}/typegen_{{platform}}_{{arch}}.tar.gz"
71+
},
6672
"prettier": {
6773
"arrowParens": "always",
6874
"printWidth": 100,

ts/create-kpt-functions/src/cmd/docker_build.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ export function dockerBuild(packageDir: string, dockerTag: string) {
2525
stdio: 'inherit',
2626
});
2727
if (build.status !== 0) {
28-
process.exit(1);
28+
let msg = 'Failed to build docker image';
29+
if (build.error) {
30+
msg = `${msg}: ${build.error}`;
31+
}
32+
throw new Error(msg);
2933
}
3034
});
3135
}

ts/create-kpt-functions/src/cmd/docker_push.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ export function dockerPush(packageDir: string, dockerTag: string) {
2323
processDockerfile(packageDir, dockerTag, (dockerFile, functionName, image) => {
2424
const push = spawnSync('docker', ['push', image], { stdio: 'inherit' });
2525
if (push.status !== 0) {
26-
process.exit(1);
26+
let msg = 'Failed to build docker image';
27+
if (push.error) {
28+
msg = `${msg}: ${push.error}`;
29+
}
30+
throw new Error(msg);
2731
}
2832
});
2933
}

ts/create-kpt-functions/src/cmd/type_create.ts

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,17 @@
1616

1717
import { KubeConfig } from '@kubernetes/client-node';
1818
import { Context } from '@kubernetes/client-node/dist/config_types';
19-
import { execSync } from 'child_process';
2019
import { question } from 'cli-interact';
2120
import { mkdtempSync, unlinkSync, writeFileSync } from 'fs';
2221
import { tmpdir } from 'os';
23-
import { resolve } from 'path';
22+
import { resolve, delimiter } from 'path';
2423
import * as request from 'request-promise';
25-
import { CLI_PACKAGE } from '../paths';
2624
import * as format from '../utils/format';
2725
import { log } from '../utils/log';
2826
import * as validator from '../utils/validator';
2927
import { failure } from '../utils/format';
28+
import { spawnSync } from 'child_process';
29+
import { CLI_PACKAGE } from '../paths';
3030

3131
export async function typeCreate(packageDir: string) {
3232
const desc = 'Generating types from OpenAPI spec.';
@@ -60,16 +60,26 @@ export async function typeCreate(packageDir: string) {
6060
const tmp = mkdtempSync(resolve(tmpdir(), 'kpt-init'));
6161
const swaggerFile = resolve(tmp, 'swagger.json');
6262
const typegenOutDir = resolve(packageDir, 'src', 'gen');
63-
try {
64-
writeFileSync(swaggerFile, out);
65-
// Generate types.
66-
execSync(`${CLI_PACKAGE.typegen} ${swaggerFile} ${typegenOutDir}`);
67-
log(`Generated ${typegenOutDir}`);
68-
} finally {
69-
// Delete swagger.json.
70-
unlinkSync(swaggerFile);
63+
64+
writeFileSync(swaggerFile, out);
65+
const typegen = spawnSync('typegen', [swaggerFile, typegenOutDir], {
66+
env: {
67+
PATH: `${CLI_PACKAGE.binDir}${delimiter}${process.env.PATH}`,
68+
},
69+
stdio: 'inherit',
70+
});
71+
unlinkSync(swaggerFile);
72+
73+
if (typegen.status !== 0) {
74+
let msg = 'Failed to build docker image';
75+
if (typegen.error) {
76+
msg = `${msg}: ${typegen.error}`;
77+
}
78+
throw new Error(msg);
7179
}
7280

81+
log(`Generated ${typegenOutDir}`);
82+
7383
log(format.finishMarker(desc));
7484
}
7585

ts/create-kpt-functions/src/paths.ts

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,42 +15,13 @@
1515
*/
1616

1717
import { resolve } from 'path';
18-
import * as os from 'os';
1918

2019
// Absolute paths in this package.
2120
export const CLI_PACKAGE = {
22-
typegen: resolve(__dirname, '..', 'bin', typegenBin()),
21+
binDir: resolve(__dirname, '..', 'node_modules', '.bin'),
2322
templates: resolve(__dirname, '..', 'templates'),
2423
};
2524

26-
function typegenBin(): string {
27-
let arch;
28-
switch (os.arch()) {
29-
case 'x64':
30-
arch = 'amd64';
31-
break;
32-
default:
33-
throw new Error(`${os.arch()} architecture is currently not supported`);
34-
}
35-
36-
let platform;
37-
switch (os.platform()) {
38-
case 'linux':
39-
platform = 'linux';
40-
break;
41-
case 'darwin':
42-
platform = 'darwin';
43-
break;
44-
case 'win32':
45-
platform = 'windows';
46-
break;
47-
default:
48-
throw new Error(`${os.platform()} platform is currently not supported`);
49-
}
50-
51-
return `typegen_${platform}_${arch}`;
52-
}
53-
5425
// Paths relative to user package.
5526
export const USER_PACKAGE = {
5627
src: 'src',

ts/create-kpt-functions/templates/package.json.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"@googlecontainertools/kpt-functions": "^0.10.0"
1919
},
2020
"devDependencies": {
21-
"@googlecontainertools/create-kpt-functions": "^0.13.1",
21+
"@googlecontainertools/create-kpt-functions": "^0.14.3",
2222
"@types/jasmine": "^3.3.12",
2323
"@types/node": "^12.7.2",
2424
"jasmine": "^3.4.0",

0 commit comments

Comments
 (0)