1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | import {
|
18 | CacheConfiguration,
|
19 | GoalInvocation,
|
20 | spawnLog,
|
21 | } from "@atomist/sdm";
|
22 | import * as fs from "fs-extra";
|
23 | import * as os from "os";
|
24 | import * as path from "path";
|
25 | import { GoalCacheArchiveStore } from "./CompressingGoalCache";
|
26 |
|
27 |
|
28 |
|
29 |
|
30 | export class FileSystemGoalCacheArchiveStore implements GoalCacheArchiveStore {
|
31 | private static readonly archiveName: string = "cache.tar.gz";
|
32 |
|
33 | public async store(gi: GoalInvocation, classifier: string, archivePath: string): Promise<void> {
|
34 | const cacheDir = await FileSystemGoalCacheArchiveStore.getCacheDirectory(gi, classifier);
|
35 | const archiveName = FileSystemGoalCacheArchiveStore.archiveName;
|
36 | const archiveFileName = path.join(cacheDir, archiveName);
|
37 | await spawnLog("mv", [archivePath, archiveFileName], {
|
38 | log: gi.progressLog,
|
39 | });
|
40 | }
|
41 |
|
42 | public async delete(gi: GoalInvocation, classifier: string): Promise<void> {
|
43 | const cacheDir = await FileSystemGoalCacheArchiveStore.getCacheDirectory(gi, classifier);
|
44 | const archiveName = FileSystemGoalCacheArchiveStore.archiveName;
|
45 | const archiveFileName = path.join(cacheDir, archiveName);
|
46 | await spawnLog("rm", ["-f", archiveFileName], {
|
47 | log: gi.progressLog,
|
48 | });
|
49 | }
|
50 |
|
51 | public async retrieve(gi: GoalInvocation, classifier: string, targetArchivePath: string): Promise<void> {
|
52 | const cacheDir = await FileSystemGoalCacheArchiveStore.getCacheDirectory(gi, classifier);
|
53 | const archiveName = FileSystemGoalCacheArchiveStore.archiveName;
|
54 | const archiveFileName = path.join(cacheDir, archiveName);
|
55 | await spawnLog("cp", [archiveFileName, targetArchivePath], {
|
56 | log: gi.progressLog,
|
57 | });
|
58 | }
|
59 |
|
60 | private static async getCacheDirectory(gi: GoalInvocation, classifier: string = "default"): Promise<string> {
|
61 | const defaultCachePath = path.join(os.homedir(), ".atomist", "cache");
|
62 | const possibleCacheConfiguration = gi.configuration.sdm.cache as (CacheConfiguration["cache"] | undefined);
|
63 | const sdmCacheDir = possibleCacheConfiguration ? (possibleCacheConfiguration.path || defaultCachePath) : defaultCachePath;
|
64 | const cacheDir = path.join(sdmCacheDir, classifier);
|
65 | await fs.mkdirs(cacheDir);
|
66 | return cacheDir;
|
67 | }
|
68 | }
|