UNPKG

3 kBPlain TextView Raw
1/*
2 * Copyright © 2019 Atomist, Inc.
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
17import { logger } from "@atomist/automation-client";
18import {
19 ArtifactStore,
20 DeployableArtifact,
21 StoredArtifact,
22} from "@atomist/sdm";
23import { AppInfo } from "@atomist/sdm/lib/spi/deploy/Deployment";
24
25/* tslint:disable:deprecation */
26
27/**
28 * Store the artifact on local disk, relying on in memory cache.
29 * **This is purely for demo and test use. It is NOT a production
30 * quality implementation. It uses fake artifact links in
31 * GitHub statuses that may not be honored after the present automation
32 * client is shut down.
33 * @deprecated Artifact storage should be done using project listeners.
34 */
35export class EphemeralLocalArtifactStore implements ArtifactStore {
36
37 private readonly entries: Array<StoredArtifact & { url: string }> = [];
38
39 public async storeFile(appInfo: AppInfo, what: string): Promise<string> {
40 const entry = {
41 appInfo,
42 deploymentUnitUrl: "http://" + what,
43 url: `http://${what}/x`,
44 };
45 this.entries.push(entry);
46 logger.info("EphemeralLocalArtifactStore: storing %j at %s", appInfo, entry.url);
47 return entry.url;
48 }
49
50 protected async retrieve(url: string): Promise<StoredArtifact> {
51 return this.entries.find(e => e.url === url);
52 }
53
54 public async checkout(url: string): Promise<DeployableArtifact> {
55 const storedArtifact = await this.retrieve(url);
56 if (!storedArtifact) {
57 logger.error("No stored artifact for [%s]: Known=%s", url,
58 this.entries.map(e => e.url).join(","));
59 return Promise.reject(new Error("No artifact found"));
60 }
61
62 const local: DeployableArtifact = {
63 ...storedArtifact.appInfo,
64 ...parseUrl(storedArtifact.deploymentUnitUrl),
65 };
66 logger.info("EphemeralLocalArtifactStore: checking out %s at %j", url, local);
67 return local;
68 }
69}
70
71function parseUrl(targetUrl: string): { cwd: string, filename: string } {
72 // Form is http:///var/folders/86/p817yp991bdddrqr_bdf20gh0000gp/T/tmp-20964EBUrRVIZ077a/target/losgatos1-0.1.0-SNAPSHOT.jar
73 const lastSlash = targetUrl.lastIndexOf("/");
74 const filename = targetUrl.substr(lastSlash + 1);
75 const cwd = targetUrl.substring(7, lastSlash);
76
77 logger.debug("Parsing results: url [%s]\n filename [%s]\n cwd [%s]", targetUrl, filename, cwd);
78
79 return { cwd, filename };
80}