1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | import {
|
18 | GitProject,
|
19 | guid,
|
20 | logger,
|
21 | } from "@atomist/automation-client";
|
22 | import { resolvePlaceholders } from "@atomist/automation-client/lib/configuration";
|
23 | import {
|
24 | GoalInvocation,
|
25 | spawnLog,
|
26 | } from "@atomist/sdm";
|
27 | import * as fs from "fs-extra";
|
28 | import * as os from "os";
|
29 | import * as path from "path";
|
30 | import { resolvePlaceholder } from "../../machine/yaml/resolvePlaceholder";
|
31 | import {
|
32 | FileSystemGoalCacheArchiveStore,
|
33 | } from "./FileSystemGoalCacheArchiveStore";
|
34 | import { GoalCache } from "./goalCaching";
|
35 |
|
36 | export interface GoalCacheArchiveStore {
|
37 | |
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 | store(gi: GoalInvocation, classifier: string, archivePath: string): Promise<void>;
|
44 | |
45 |
|
46 |
|
47 |
|
48 |
|
49 | delete(gi: GoalInvocation, classifier: string): Promise<void>;
|
50 | |
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 | retrieve(gi: GoalInvocation, classifier: string, targetArchivePath: string): Promise<void>;
|
57 | }
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 | export class CompressingGoalCache implements GoalCache {
|
64 | private readonly store: GoalCacheArchiveStore;
|
65 |
|
66 | public constructor(store: GoalCacheArchiveStore = new FileSystemGoalCacheArchiveStore()) {
|
67 | this.store = store;
|
68 | }
|
69 |
|
70 | public async put(gi: GoalInvocation, project: GitProject, files: string[], classifier?: string): Promise<void> {
|
71 | const archiveName = "atomist-cache";
|
72 | const teamArchiveFileName = path.join(os.tmpdir(), `${archiveName}.${guid().slice(0, 7)}`);
|
73 | const slug = `${gi.id.owner}/${gi.id.repo}`;
|
74 |
|
75 | const tarResult = await spawnLog("tar", ["-cf", teamArchiveFileName, ...files], {
|
76 | log: gi.progressLog,
|
77 | cwd: project.baseDir,
|
78 | });
|
79 | if (tarResult.code) {
|
80 | const message = `Failed to create tar archive '${teamArchiveFileName}' for ${slug}`;
|
81 | logger.error(message);
|
82 | gi.progressLog.write(message);
|
83 | return;
|
84 | }
|
85 | const gzipResult = await spawnLog("gzip", ["-3", teamArchiveFileName], {
|
86 | log: gi.progressLog,
|
87 | cwd: project.baseDir,
|
88 | });
|
89 | if (gzipResult.code) {
|
90 | const message = `Failed to gzip tar archive '${teamArchiveFileName}' for ${slug}`;
|
91 | logger.error(message);
|
92 | gi.progressLog.write(message);
|
93 | return;
|
94 | }
|
95 | const resolvedClassifier = await resolveClassifierPath(classifier, gi);
|
96 | await this.store.store(gi, resolvedClassifier, teamArchiveFileName + ".gz");
|
97 | }
|
98 |
|
99 | public async remove(gi: GoalInvocation, classifier?: string): Promise<void> {
|
100 | const resolvedClassifier = await resolveClassifierPath(classifier, gi);
|
101 | await this.store.delete(gi, resolvedClassifier);
|
102 | }
|
103 |
|
104 | public async retrieve(gi: GoalInvocation, project: GitProject, classifier?: string): Promise<void> {
|
105 | const archiveName = "atomist-cache";
|
106 | const teamArchiveFileName = path.join(os.tmpdir(), `${archiveName}.${guid().slice(0, 7)}`);
|
107 | const resolvedClassifier = await resolveClassifierPath(classifier, gi);
|
108 | await this.store.retrieve(gi, resolvedClassifier, teamArchiveFileName);
|
109 | if (fs.existsSync(teamArchiveFileName)) {
|
110 | await spawnLog("tar", ["-xzf", teamArchiveFileName], {
|
111 | log: gi.progressLog,
|
112 | cwd: project.baseDir,
|
113 | });
|
114 | } else {
|
115 | throw Error("No cache entry");
|
116 | }
|
117 | }
|
118 |
|
119 | }
|
120 |
|
121 |
|
122 |
|
123 |
|
124 | export async function resolveClassifierPath(classifier: string | undefined, gi: GoalInvocation): Promise<string> {
|
125 | if (!classifier) {
|
126 | return gi.context.workspaceId;
|
127 | }
|
128 | const wrapper = { classifier };
|
129 | await resolvePlaceholders(wrapper, v => resolvePlaceholder(v, gi.goalEvent, gi, {}));
|
130 | return gi.context.workspaceId + "/" + sanitizeClassifier(wrapper.classifier);
|
131 | }
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 | export function sanitizeClassifier(classifier: string): string {
|
139 | return classifier.replace(/[^-.0-9A-Za-z_+]/g, "_")
|
140 | .replace(/^\.+/, "");
|
141 | }
|