Skip to content

Commit

Permalink
feat: Add component base path
Browse files Browse the repository at this point in the history
  • Loading branch information
doosuu committed Jun 8, 2024
1 parent 89837f0 commit 21c1741
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 19 deletions.
3 changes: 3 additions & 0 deletions src/modules/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ export interface ComponentManifest {
// Human readable description of the component, if any.
description?: string;

// A base path within the package where all files and programs are located to relatively.
basePath?: string;

// A list of files that need to be copied from source to target when running `velocitas sync`.
files?: FileSpec[];

Expand Down
2 changes: 1 addition & 1 deletion src/modules/exec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export async function runExecSpec(
throw new Error(`No program found for item '${execSpec.ref}' referenced in program list of '${componentId}'`);
}

const cwd = join(componentContext.packageConfig.getPackageDirectory(), componentContext.packageConfig.version);
const cwd = join(componentContext.packageConfig.getPackageDirectory(), componentContext.packageConfig.version, componentContext.manifest.basePath ? componentContext.manifest.basePath : "");

let programArgs = programSpec.args ? programSpec.args : [];
if (execSpec.args && execSpec.args.length > 0) {
Expand Down
43 changes: 25 additions & 18 deletions src/modules/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// SPDX-License-Identifier: Apache-2.0

import { Stats } from 'node:fs';
import { extname, join } from 'node:path';
import { basename, extname, join } from 'node:path';
import { cwd } from 'node:process';
import { Transform, TransformCallback, TransformOptions } from 'node:stream';
import copy from 'recursive-copy';
Expand All @@ -22,16 +22,18 @@ import { ComponentManifest } from './component';
import { PackageConfig } from './package';
import { VariableCollection } from './variables';

const SUPPORTED_TEXT_FILES_ARRAY = ['.md', '.yaml', '.yml', '.txt', '.json', '.sh', '.html', '.htm', '.xml', '.tpl'];
const SUPPORTED_TEXT_FILES_ARRAY = ['.md', '.yaml', '.yml', '.txt', '.json', '.sh', '.html', '.htm', '.xml', '.tpl', '.dockerfile'];
const SPECIAL_FILES_ARRAY = ['Dockerfile'];
const NOTICE_COMMENT = 'This file is maintained by velocitas CLI, do not modify manually. Change settings in .velocitas.json';

class ReplaceVariablesStream extends Transform {
private _fileExt: string;
class ReplaceVariablesTransform extends Transform {
private _filename: string;
private _variables: VariableCollection;
private _firstChunk: boolean;

constructor(fileExt: string, variables: VariableCollection, opts?: TransformOptions | undefined) {
constructor(filename: string, variables: VariableCollection, opts?: TransformOptions | undefined) {
super({ ...opts, readableObjectMode: true, writableObjectMode: true });
this._fileExt = fileExt;
this._filename = filename;
this._variables = variables;
this._firstChunk = true;
}
Expand All @@ -41,30 +43,30 @@ class ReplaceVariablesStream extends Transform {
_transform(chunk: any, _: string, callback: TransformCallback) {
let result = this._variables.substitute(chunk.toString());
let noticeComment: string;
const notice = 'This file is maintained by velocitas CLI, do not modify manually. Change settings in .velocitas.json';
const shebang = '#!/bin/bash';
const xmlDeclarationRegExp = new RegExp(`\\<\\?xml\\s.*?\\s\\?\\>`);
const fileExt = extname(this._filename);

if (this._firstChunk) {
if (['.txt'].includes(this._fileExt)) {
result = `${notice}\n${result}`;
} else if (['.md', '.html', '.htm', '.xml', '.tpl'].includes(this._fileExt)) {
noticeComment = `<!-- ${notice} -->`;
if (['.txt'].includes(fileExt)) {
result = `${NOTICE_COMMENT}\n${result}`;
} else if (['.md', '.html', '.htm', '.xml', '.tpl'].includes(fileExt)) {
noticeComment = `<!-- ${NOTICE_COMMENT} -->`;
const xmlDeclarationArray = xmlDeclarationRegExp.exec(result);
if (xmlDeclarationArray !== null && result.startsWith(xmlDeclarationArray[0])) {
result = this._injectNoticeAfterStartLine(result, xmlDeclarationArray[0], noticeComment);
} else {
result = `${noticeComment}\n${result}`;
}
} else if (['.yaml', '.yml', '.sh'].includes(this._fileExt)) {
noticeComment = `# ${notice}`;
} else if (['Dockerfile'].includes(this._filename) || ['.yaml', '.yml', '.sh', '.dockerfile'].includes(fileExt)) {
noticeComment = `# ${NOTICE_COMMENT}`;
if (result.startsWith(shebang)) {
result = this._injectNoticeAfterStartLine(result, shebang, noticeComment);
} else {
result = `${noticeComment}\n${result}`;
}
} else if (['.json'].includes(this._fileExt)) {
noticeComment = `// ${notice}`;
} else if (['.json'].includes(fileExt)) {
noticeComment = `// ${NOTICE_COMMENT}`;
result = `${noticeComment}\n${result}`;
}

Expand All @@ -88,19 +90,24 @@ export function installComponent(packageConfig: PackageConfig, component: Compon
let ifCondition = spec.condition ? variables.substitute(spec.condition) : 'true';

if (eval(ifCondition)) {
const sourceFileOrDir = join(packageConfig.getPackageDirectory(), packageConfig.version, src);
const sourceFileOrDir = join(
packageConfig.getPackageDirectory(),
packageConfig.version,
component.basePath ? component.basePath : '',
src,
);
const destFileOrDir = join(cwd(), dst);
try {
if (CliFileSystem.existsSync(sourceFileOrDir)) {
copy(sourceFileOrDir, destFileOrDir, {
dot: true,
overwrite: true,
transform: function (src: string, _: string, stats: Stats) {
if (!SUPPORTED_TEXT_FILES_ARRAY.includes(extname(src))) {
if (!SPECIAL_FILES_ARRAY.includes(basename(src)) && !SUPPORTED_TEXT_FILES_ARRAY.includes(extname(src))) {
return null;
}

return new ReplaceVariablesStream(extname(src), variables);
return new ReplaceVariablesTransform(extname(src), variables);
},
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Copyright (c) 2024 Contributors to the Eclipse Foundation
#
# This program and the accompanying materials are made available under the
# terms of the Apache License, Version 2.0 which is available at
# https://www.apache.org/licenses/LICENSE-2.0.
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
# SPDX-License-Identifier: Apache-2.0

FROM ${{ packageVariable }}

RUN ls -al
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@
"dst": "testFile2.sh"
}
]
},
{
"id": "test-componentFour",
"type": "setup",
"files": [
{
"src": "Dockerfile",
"dst": "Dockerfile"
}
]
}
]
}

0 comments on commit 21c1741

Please sign in to comment.