Skip to content

Commit e11ec85

Browse files
authored
Merge pull request #590 from Zemnmez/deploy
try to get deploy into a more testable state
2 parents 38079c9 + 3bbef03 commit e11ec85

File tree

6 files changed

+103
-99
lines changed

6 files changed

+103
-99
lines changed

csv/format/BUILD

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
# empty
1+
# empty

csv/format/rules.bzl

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
1-
21
def csv_format(name, src = None, out = None, separator = ",", **kwargs):
32
if out == None:
43
out = src + "_formatted.csv"
54

65
native.genrule(
76
name = name,
8-
exec_tools = [ "//go/cmd/csvpretty:csvpretty" ],
7+
exec_tools = ["//go/cmd/csvpretty:csvpretty"],
98
cmd_bash = """
109
$(execpath //go/cmd/csvpretty:csvpretty) \\
1110
--input "$<" \\
1211
--output "$@" \\
1312
--comma \"""" + separator + """\"
1413
""",
15-
outs = [ out ],
16-
srcs = [ src ],
14+
outs = [out],
15+
srcs = [src],
1716
**kwargs
18-
)
17+
)

csv/lint/rules.bzl

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@ def csv_lint_test(name, srcs = [], **kwargs):
1414
file1 = name + src + "_format",
1515
file2 = src,
1616
)
17-
17+
1818
native.test_suite(
1919
name = name,
20-
tests = [name + src + "_test" for src in srcs ]
20+
tests = [name + src + "_test" for src in srcs],
2121
)
22-

csv/lint/testing/BUILD

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ load("//csv/lint:rules.bzl", "csv_lint_test")
22

33
csv_lint_test(
44
name = "test",
5-
srcs = [ "test_out.csv" ]
6-
)
5+
srcs = ["test_out.csv"],
6+
)

deploy/deploy_test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ import program from './program';
22

33
test('deploy', async () => {
44
process.env.NPM_TOKEN = '123fake';
5-
await program.parseAsync(['--dryRun', 'true']);
5+
await program.parseAsync(['xxx', 'ok', '--dryRun', 'true']);
66
});

deploy/program.ts

Lines changed: 93 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -20,48 +20,52 @@ interface ArtifactInfo {
2020
kind: 'artifact';
2121
filename: string;
2222
buildTag: string;
23-
published: Promise<void>;
23+
publish: (c: Context) => Promise<void>;
2424
}
2525

2626
const artifact =
27-
(filename: string, buildTag: string) =>
28-
async ({ publish }: Context): Promise<ArtifactInfo> => {
27+
(filename: string, buildTag: string) => async (): Promise<ArtifactInfo> => {
2928
return {
3029
kind: 'artifact',
3130
filename,
3231
buildTag,
33-
published: publish(
34-
filename,
35-
await fs.readFile(runfiles.resolveWorkspaceRelative(buildTag))
36-
),
32+
publish: async ({ publish }: Context) =>
33+
publish(
34+
filename,
35+
await fs.readFile(
36+
runfiles.resolveWorkspaceRelative(buildTag)
37+
)
38+
),
3739
};
3840
};
3941

4042
interface NpmPackageInfo {
4143
kind: 'npm_publication';
4244
package_name: string;
4345
buildTag: string;
44-
published: Promise<void>;
46+
publish: (c: Context) => Promise<void>;
4547
}
4648

4749
const npmPackage =
4850
(packageName: string, buildTag: string) =>
49-
async ({ exec }: Context): Promise<NpmPackageInfo> => {
50-
if (!('NPM_TOKEN' in process.env))
51-
throw new Error(
52-
"Missing NPM_TOKEN. We won't be able to publish any NPM packages."
53-
);
51+
async (): Promise<NpmPackageInfo> => {
5452
return {
5553
buildTag,
5654
kind: 'npm_publication',
5755
package_name: packageName,
58-
published: exec(runfiles.resolveWorkspaceRelative(buildTag)),
56+
async publish({ exec }: Context) {
57+
if (!('NPM_TOKEN' in process.env))
58+
throw new Error(
59+
"Missing NPM_TOKEN. We won't be able to publish any NPM packages."
60+
);
61+
exec(runfiles.resolveWorkspaceRelative(buildTag));
62+
},
5963
};
6064
};
6165

62-
interface ReleaseProps<T> {
66+
interface ReleaseProps {
6367
dryRun: boolean;
64-
releaseNotes: (items: T[]) => string;
68+
releaseNotes: (items: (ArtifactInfo | NpmPackageInfo)[]) => string;
6569
createRelease(data: { body: string }): Promise<{ release_id: number }>;
6670
uploadReleaseAsset(data: {
6771
release_id: number;
@@ -71,30 +75,29 @@ interface ReleaseProps<T> {
7175
}
7276

7377
const release =
74-
<T extends { published: Promise<void> }>(
75-
...fns: ((c: Context) => Promise<T>)[]
76-
) =>
78+
(...fns: (() => Promise<ArtifactInfo | NpmPackageInfo>)[]) =>
7779
async ({
7880
dryRun,
7981
createRelease,
8082
releaseNotes,
8183
uploadReleaseAsset,
82-
}: ReleaseProps<T>) => {
83-
let set_release_id: ((release_id: number) => void) | undefined;
84-
const release_id = new Promise<number>(ok => (set_release_id = ok));
85-
86-
const publish: Context['publish'] = dryRun
87-
? async (filename: string, content: Buffer) => {
88-
if (!filename) throw new Error('Empty filename.');
89-
if (content.length == 0)
90-
throw new Error(`Empty content for ${filename}`);
91-
}
92-
: async (file: string, content: Buffer) =>
93-
uploadReleaseAsset({
94-
release_id: await release_id,
95-
name: file,
96-
data: content,
97-
});
84+
}: ReleaseProps) => {
85+
const logInfo = await Promise.all(fns.map(f => f()));
86+
87+
const { release_id } = await createRelease({
88+
body: releaseNotes(logInfo),
89+
});
90+
91+
const publish: Context['publish'] = async (
92+
file: string,
93+
content: Buffer
94+
) =>
95+
uploadReleaseAsset({
96+
release_id,
97+
name: file,
98+
data: content,
99+
});
100+
98101
const exec: Context['exec'] = dryRun
99102
? async (filename: string) => {
100103
if (filename == '')
@@ -103,17 +106,9 @@ const release =
103106
: async (filename: string) =>
104107
void (await promisify(child_process.execFile)(filename));
105108

106-
const logInfo = await Promise.all(fns.map(f => f({ publish, exec })));
107-
108-
const { release_id: concreteReleaseId } = await createRelease({
109-
body: releaseNotes(logInfo),
110-
});
111-
112-
if (!set_release_id) throw new Error();
113-
114-
set_release_id(concreteReleaseId);
115-
116-
await Promise.all(logInfo.map(itm => itm.published));
109+
await Promise.all(
110+
logInfo.map(({ publish: p }) => p({ publish, exec }))
111+
);
117112
};
118113

119114
export const program = Program.name('release')
@@ -130,17 +125,17 @@ export const program = Program.name('release')
130125
? getOctokit(process.env['GITHUB_TOKEN']!)
131126
: undefined;
132127

133-
const releaser = release<NpmPackageInfo | ArtifactInfo>(
128+
const releaser = release(
134129
artifact(
135130
'recursive_vassals.zip',
136-
'project/ck3/recursive-vassals/mod_zip.zip'
131+
'//project/ck3/recursive-vassals/mod_zip.zip'
137132
),
138133
artifact(
139134
'recursive_vassals.patch',
140135
'//project/ck3/recursive-vassals/mod.patch'
141136
),
142-
artifact('svgshot.tar.gz', 'ts/cmd/svgshot/svgshot.tgz'),
143-
npmPackage('svgshot', 'ts/cmd/svgshot/npm_pkg.publish.sh')
137+
artifact('svgshot.tar.gz', '//ts/cmd/svgshot/svgshot.tgz'),
138+
npmPackage('svgshot', '//ts/cmd/svgshot/npm_pkg.publish.sh')
144139
);
145140

146141
releaser({
@@ -160,57 +155,68 @@ export const program = Program.name('release')
160155
if (!data) throw new Error('data is empty');
161156
},
162157

163-
createRelease: Github
164-
? async ({ body }) => ({
165-
release_id: (
166-
await Github.rest.repos.createRelease({
167-
// could probably use a spread operator here
168-
// but i also think that would be uglier...
169-
owner: context.repo.owner,
170-
repo: context.repo.repo,
171-
172-
tag_name: syntheticVersion,
173-
174-
body,
175-
176-
generate_release_notes: true,
177-
178-
name: syntheticVersion,
179-
180-
target_commitish: context.ref,
181-
})
182-
).data.id,
183-
})
184-
: async ({ body }) => {
185-
if (body === '')
186-
throw new Error('Release body is empty.');
187-
return { release_id: 0 };
188-
},
158+
createRelease:
159+
Github !== undefined
160+
? async ({ body }) => ({
161+
release_id: (
162+
await Github.rest.repos.createRelease({
163+
// could probably use a spread operator here
164+
// but i also think that would be uglier...
165+
owner: context.repo.owner,
166+
repo: context.repo.repo,
167+
168+
tag_name: syntheticVersion,
169+
170+
body,
171+
172+
generate_release_notes: true,
173+
174+
name: syntheticVersion,
175+
176+
target_commitish: context.ref,
177+
})
178+
).data.id,
179+
})
180+
: async ({ body }) => {
181+
if (body === '')
182+
throw new Error(`Release body is empty.`);
183+
return { release_id: 0 };
184+
},
189185
dryRun: dryRun,
190186
releaseNotes: (notes: (NpmPackageInfo | ArtifactInfo)[]) => {
191187
const artifacts: ArtifactInfo[] = [];
192188
const npmPackages: NpmPackageInfo[] = [];
193189

194190
for (const note of notes) {
195-
if (note.kind === 'artifact') artifacts.push(note);
196-
if (note.kind === 'npm_publication') npmPackages.push(note);
197-
throw new Error();
191+
if (note.kind === 'artifact') {
192+
artifacts.push(note);
193+
continue;
194+
}
195+
if (note.kind === 'npm_publication') {
196+
npmPackages.push(note);
197+
continue;
198+
}
199+
throw new Error(`Unknown kind ${(note as any).kind}`);
198200
}
199201

200202
return `${
201203
artifacts.length
202-
? `This release contains the following artifacts:\n ${artifacts.map(
203-
artifact =>
204-
` - ${artifact.buildTag}${artifact.filename}`
205-
)}`
204+
? `This release contains the following artifacts:\n ${artifacts
205+
.map(
206+
artifact =>
207+
` - ${artifact.buildTag}${artifact.filename}`
208+
)
209+
.join('\n')}`
206210
: ''
207211
}
208212
${
209213
npmPackages.length
210-
? `This release contains the following NPM packages:\n: ${npmPackages.map(
211-
pkg =>
212-
` - ${pkg.buildTag} → [${pkg.package_name}](https://npmjs.com/package/svgshot)`
213-
)}`
214+
? `This release contains the following NPM packages:\n ${npmPackages
215+
.map(
216+
pkg =>
217+
` - ${pkg.buildTag} → [${pkg.package_name}](https://npmjs.com/package/svgshot)`
218+
)
219+
.join('\n')}`
214220
: ''
215221
}
216222
`;

0 commit comments

Comments
 (0)