Skip to content
This repository has been archived by the owner on Aug 16, 2024. It is now read-only.

Commit

Permalink
feat: add a public method generateDevfileContext
Browse files Browse the repository at this point in the history
Signed-off-by: Oleksii Orel <oorel@redhat.com>
  • Loading branch information
olexii4 committed Jan 22, 2023
1 parent 5c79f2d commit 0d2ce53
Show file tree
Hide file tree
Showing 5 changed files with 485 additions and 292 deletions.
2 changes: 1 addition & 1 deletion tools/devworkspace-generator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"prepare": "yarn run clean && yarn run build",
"clean": "rimraf lib",
"build": "yarn run format && yarn run compile && yarn run lint && yarn run test",
"compile": "tsc --project .",
"compile": "tsc --declaration --project .",
"format": "if-env SKIP_FORMAT=true && echo 'skip format check' || prettier --check '{src,tests}/**/*.ts' package.json",
"format:fix": "prettier --write '{src,tests}/**/*.ts' package.json",
"lint": "if-env SKIP_LINT=true && echo 'skip lint check' || eslint --cache=true --no-error-on-unmatched-pattern=true '{src,tests}/(!model|**)/*.ts'",
Expand Down
14 changes: 8 additions & 6 deletions tools/devworkspace-generator/src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,19 @@ export class Generate {
@inject(DevContainerComponentFinder)
private devContainerComponentFinder: DevContainerComponentFinder;

async generate(devfileContent: string, editorContent: string, outputFile: string): Promise<DevfileContext> {
async generate(devfileContent: string, editorContent: string, outputFile?: string): Promise<DevfileContext> {
const context = await this.generateContent(devfileContent, editorContent);

// write the result
// write templates and then DevWorkspace in a single file
const allContentArray = context.devWorkspaceTemplates.map(template => jsYaml.dump(template));
allContentArray.push(jsYaml.dump(context.devWorkspace));
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');
const generatedContent = allContentArray.join('---\n');

await fs.writeFile(outputFile, generatedContent, 'utf-8');
await fs.writeFile(outputFile, generatedContent, 'utf-8');
}

console.log(`DevWorkspace ${context.devWorkspaceTemplates[0].metadata.name} was generated.`);
return context;
Expand Down
119 changes: 74 additions & 45 deletions tools/devworkspace-generator/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,56 +20,29 @@ import { V1alpha2DevWorkspaceSpecTemplate } from '@devfile/api';
import { DevfileContext } from './api/devfile-context';

export class Main {
protected async doStart(): Promise<DevfileContext> {
let devfilePath: string | undefined;
let devfileUrl: string | undefined;
let outputFile: string | undefined;
let editorPath: string | undefined;
let pluginRegistryUrl: string | undefined;
let editorEntry: string | undefined;
const projects: { name: string; location: string }[] = [];

const args = process.argv.slice(2);
args.forEach(arg => {
if (arg.startsWith('--devfile-path:')) {
devfilePath = arg.substring('--devfile-path:'.length);
}
if (arg.startsWith('--devfile-url:')) {
devfileUrl = arg.substring('--devfile-url:'.length);
}
if (arg.startsWith('--plugin-registry-url:')) {
pluginRegistryUrl = arg.substring('--plugin-registry-url:'.length);
}
if (arg.startsWith('--editor-entry:')) {
editorEntry = arg.substring('--editor-entry:'.length);
}
if (arg.startsWith('--editor-path:')) {
editorPath = arg.substring('--editor-path:'.length);
}
if (arg.startsWith('--output-file:')) {
outputFile = arg.substring('--output-file:'.length);
}
if (arg.startsWith('--project.')) {
const name = arg.substring('--project.'.length, arg.indexOf('='));
let location = arg.substring(arg.indexOf('=') + 1);
location = location.replace('{{_INTERNAL_URL_}}', '{{ INTERNAL_URL }}');
// Generates a devfile context object based on params
public async generateDevfileContext(params: {
devfilePath?: string;
devfileUrl?: string;
devfileContent?: string;
outputFile?: string;
editorPath?: string;
pluginRegistryUrl?: string;
editorEntry?: string;
projects: { name: string; location: string }[];
}): Promise<DevfileContext> {
let { devfilePath, devfileUrl, outputFile, editorPath, pluginRegistryUrl, editorEntry, projects } = params;

projects.push({ name, location });
}
});
if (!editorPath && !editorEntry) {
throw new Error('missing --editor-path: or --editor-entry: parameter');
throw new Error('missing editorPath or editorEntry');
}
if (!devfilePath && !devfileUrl && !params.devfileContent) {
throw new Error('missing devfilePath or devfileUrl or devfileContent');
}
if (editorEntry && !pluginRegistryUrl) {
pluginRegistryUrl = 'https://eclipse-che.github.io/che-plugin-registry/main/v3';
console.log(`No plug-in registry url. Setting to ${pluginRegistryUrl}`);
}
if (!devfilePath && !devfileUrl) {
throw new Error('missing --devfile-path: or --devfile-url: parameter');
}
if (!outputFile) {
throw new Error('missing --output-file: parameter');
}

const axiosInstance = axios.default;
const inversifyBinbding = new InversifyBinding();
Expand Down Expand Up @@ -107,8 +80,10 @@ export class Main {
}
// get back the content
devfileContent = jsYaml.dump(devfileParsed);
} else {
} else if (devfilePath) {
devfileContent = await fs.readFile(devfilePath);
} else {
devfileContent = params.devfileContent;
}

// enhance projects
Expand Down Expand Up @@ -154,8 +129,62 @@ export class Main {
}

async start(): Promise<boolean> {
let devfilePath: string | undefined;
let devfileUrl: string | undefined;
let outputFile: string | undefined;
let editorPath: string | undefined;
let pluginRegistryUrl: string | undefined;
let editorEntry: string | undefined;
const projects: { name: string; location: string }[] = [];

const args = process.argv.slice(2);
args.forEach(arg => {
if (arg.startsWith('--devfile-path:')) {
devfilePath = arg.substring('--devfile-path:'.length);
}
if (arg.startsWith('--devfile-url:')) {
devfileUrl = arg.substring('--devfile-url:'.length);
}
if (arg.startsWith('--plugin-registry-url:')) {
pluginRegistryUrl = arg.substring('--plugin-registry-url:'.length);
}
if (arg.startsWith('--editor-entry:')) {
editorEntry = arg.substring('--editor-entry:'.length);
}
if (arg.startsWith('--editor-path:')) {
editorPath = arg.substring('--editor-path:'.length);
}
if (arg.startsWith('--output-file:')) {
outputFile = arg.substring('--output-file:'.length);
}
if (arg.startsWith('--project.')) {
const name = arg.substring('--project.'.length, arg.indexOf('='));
let location = arg.substring(arg.indexOf('=') + 1);
location = location.replace('{{_INTERNAL_URL_}}', '{{ INTERNAL_URL }}');

projects.push({ name, location });
}
});

try {
await this.doStart();
if (!editorPath && !editorEntry) {
throw new Error('missing --editor-path: or --editor-entry: parameter');
}
if (!devfilePath && !devfileUrl) {
throw new Error('missing --devfile-path: or --devfile-url: parameter');
}
if (!outputFile) {
throw new Error('missing --output-file: parameter');
}
await this.generateDevfileContext({
devfilePath,
devfileUrl,
editorPath,
outputFile,
pluginRegistryUrl,
editorEntry,
projects,
});
return true;
} catch (error) {
console.error('stack=' + error.stack);
Expand Down
Loading

0 comments on commit 0d2ce53

Please sign in to comment.