This repository was archived by the owner on Aug 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathgenerate.ts
More file actions
124 lines (105 loc) · 4.27 KB
/
generate.ts
File metadata and controls
124 lines (105 loc) · 4.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/**********************************************************************
* Copyright (c) 2022 Red Hat, Inc.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
***********************************************************************/
import {
V1alpha2DevWorkspace,
V1alpha2DevWorkspaceSpecContributions,
V1alpha2DevWorkspaceSpecTemplateComponents,
V1alpha2DevWorkspaceTemplate,
V1alpha2DevWorkspaceTemplateSpec,
} from '@devfile/api';
import { injectable, inject } from 'inversify';
import * as jsYaml from 'js-yaml';
import * as fs from 'fs-extra';
import { DevfileContext } from './api/devfile-context';
import { DevContainerComponentFinder } from './devfile/dev-container-component-finder';
@injectable()
export class Generate {
static readonly MERGE_CONTRIBUTION = 'controller.devfile.io/merge-contribution';
@inject(DevContainerComponentFinder)
private devContainerComponentFinder: DevContainerComponentFinder;
async generate(devfileContent: string, editorContent: string, outputFile?: string): Promise<DevfileContext> {
const context = await this.generateContent(devfileContent, editorContent);
// write the result
if (outputFile) {
// write templates and then DevWorkspace in a single file
const allContentArray = context.devWorkspaceTemplates.map(template => jsYaml.dump(template));
allContentArray.push(jsYaml.dump(context.devWorkspace));
const generatedContent = allContentArray.join('---\n');
await fs.writeFile(outputFile, generatedContent, 'utf-8');
}
console.log(`DevWorkspace ${context.devWorkspaceTemplates[0].metadata.name} was generated.`);
return context;
}
async generateContent(devfileContent: string, editorContent: string): Promise<DevfileContext> {
const devfile = jsYaml.load(devfileContent);
// const originalDevfile = Object.assign({}, devfile);
// sets the suffix to the devfile name
const suffix = devfile.metadata.name || '';
// devfile of the editor
let editorDevfile = jsYaml.load(editorContent);
// support for the inline format of the devfile
if (editorDevfile.inline) {
editorDevfile = editorDevfile.inline;
}
// transform it into a devWorkspace template
const metadata = editorDevfile.metadata;
// add sufix
metadata.name = `${metadata.name}-${suffix}`;
delete editorDevfile.metadata;
delete editorDevfile.schemaVersion;
const editorDevWorkspaceTemplate: V1alpha2DevWorkspaceTemplate = {
apiVersion: 'workspace.devfile.io/v1alpha2',
kind: 'DevWorkspaceTemplate',
metadata,
spec: editorDevfile as V1alpha2DevWorkspaceTemplateSpec,
};
// transform it into a devWorkspace
const devfileMetadata = devfile.metadata;
const devfileCopy = Object.assign({}, devfile);
delete devfileCopy.schemaVersion;
delete devfileCopy.metadata;
const editorSpecContribution: V1alpha2DevWorkspaceSpecContributions = {
name: 'editor',
kubernetes: {
name: editorDevWorkspaceTemplate.metadata.name,
},
};
const devWorkspace: V1alpha2DevWorkspace = {
apiVersion: 'workspace.devfile.io/v1alpha2',
kind: 'DevWorkspace',
metadata: devfileMetadata,
spec: {
started: true,
template: devfileCopy,
contributions: [editorSpecContribution],
},
};
// for now the list of devWorkspace templates is only the editor template
const devWorkspaceTemplates = [editorDevWorkspaceTemplate];
const context = {
devfile,
devWorkspace,
devWorkspaceTemplates,
suffix,
};
// grab container where to inject controller.devfile.io/merge-contribution attribute
let devContainer: V1alpha2DevWorkspaceSpecTemplateComponents = await this.devContainerComponentFinder.find(context);
// add attributes
let devContainerAttributes = devContainer.attributes;
if (!devContainerAttributes) {
devContainerAttributes = {};
devContainerAttributes[Generate.MERGE_CONTRIBUTION] = true;
devContainer.attributes = devContainerAttributes;
} else {
devContainerAttributes[Generate.MERGE_CONTRIBUTION] = true;
}
return context;
}
}