Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions karavan-vscode/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,53 @@ Build-in catalogues:
The **Camel version** extension setting controls the version passed to Camel JBang.
Leave it empty to omit `-Dcamel.jbang.version` and let Camel JBang select its default version.

## Custom Kamelets

Set **Karavan: Custom Kamelets Path** (`Karavan.kameletsPath`) to a local
directory, a single YAML file, or an HTTP(S) URL serving raw YAML:

```json
{
"Karavan.kameletsPath": "C:\\camel\\kamelets.yaml"
}
```

Alternatively, use a URL such as `https://example.com/kamelets.yaml`.
URLs and local paths can reference extension-host environment variables using
`${env:NAME}` or `${NAME}`, including a variable containing the entire URL:

```json
{
"Karavan.kameletsPath": "https://${env:KAMELET_HOST}/catalog.yaml?token=${env:KAMELET_TOKEN}"
}
```

Values are substituted literally, once, without URL encoding; use URL-encoded
values where required. Undefined variables report an error and leave the built-in
catalog available. Empty values are allowed. Variables come from the VS Code
extension host's environment (the remote host for remote workspaces), not from
`.env` files or terminal-only exports. Restart VS Code after changing its
environment. Browser-hosted extensions do not have access to OS environment
variables.

Each non-empty YAML document must have `kind: Kamelet`. A file can contain
multiple Kamelets separated by `---`; LF and CRLF line endings are supported.
Custom Kamelets are loaded alongside the built-in catalog when the designer
loads. Reopen the designer after changing the setting.
Both built-in and custom catalogs support LF and CRLF line endings.

Directory loading remains non-recursive and reads filenames ending in `yaml`.
Relative local paths retain their existing resolution against the extension
host's working directory; absolute paths are recommended.

`Karavan.kameletsPath` is **editor-only**, including directory sources: it is
never passed to Camel JBang run/export or downloaded into the integration project.
Remote loading has a 30-second timeout and reports HTTP, network, and YAML errors.
Browser-hosted extensions also require the server to allow cross-origin requests.

If you previously relied on `Karavan.kameletsPath` for runtime Kamelets, supply
`--local-kamelet-dir` explicitly when running or exporting with the CLI.

## Run integration locally
* Run in UI: click `Run` button

Expand Down
3 changes: 2 additions & 1 deletion karavan-vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@
"Karavan.kameletsPath": {
"type": "string",
"default": "",
"description": "Custom Kamelets Path",
"description": "Custom Kamelets Path for the editor only: local directory, YAML file (including multiple documents), or HTTP(S) YAML URL. Supports ${env:NAME} and ${NAME} environment variables from the extension host. Does not affect Camel JBang run/export.",
"scope": "machine",
"order": 65
},
Expand Down Expand Up @@ -677,6 +677,7 @@
"lint": "eslint src webview",
"lint:fix": "eslint --fix src webview",
"test": "node ./out/test/runTest.js",
"test:custom-kamelets": "node --test test/custom-kamelets.test.cjs",
"test:dsl-selector": "node --test test/dsl-selector.test.cjs",
"open-in-browser": "vscode-test-web --extensionDevelopmentPath=. ../karavan-demo/hello-world/"
},
Expand Down
9 changes: 2 additions & 7 deletions karavan-vscode/src/jbang.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,16 @@ function prepareCommand(command: string): string {

export function camelJbangRun() {
const maxMessages: number = workspace.getConfiguration().get("camel.maxMessages") || -1;
const kameletsPath: string | undefined = workspace.getConfiguration().get("Karavan.kameletsPath");
const dev: boolean = workspace.getConfiguration().get("camel.dev") || false;
const cmd = "run * "
+ (maxMessages > -1 ? " --max-messages=" + maxMessages : "")
+ (kameletsPath && kameletsPath.trim().length > 0 ? " --local-kamelet-dir=" + kameletsPath : "");
+ (maxMessages > -1 ? " --max-messages=" + maxMessages : "");
const command = prepareCommand(cmd) + (dev === true ? " --dev" : "");
exec.execTerminalCommand("jbang-run", command);
}

export function createExportCommand(fullPath: string, runtime: string) {
const kameletsPath: string | undefined = workspace.getConfiguration().get("Karavan.kameletsPath");
const cmd = "export --fresh "
+ (fullPath ? " --directory=" + fullPath : '')
+ (kameletsPath && kameletsPath.trim().length > 0 ? " --local-kamelet-dir=" + kameletsPath : "");
+ (fullPath ? " --directory=" + fullPath : '');
return prepareCommand(cmd);
}

Expand All @@ -84,4 +80,3 @@ function executeJbangCommand(rootPath: string, command: string, callback: (code:
window.showErrorMessage("JBang not found!");
}
}

59 changes: 52 additions & 7 deletions karavan-vscode/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* limitations under the License.
*/
import * as path from "path";
import * as yaml from "js-yaml";
import {ExtensionContext, FileType, Uri, window, workspace} from "vscode";
import {CamelDefinitionYaml} from "@karavan-core/api/CamelDefinitionYaml";
import {Integration, KameletTypes} from "@karavan-core/model/IntegrationDefinition";
Expand Down Expand Up @@ -64,26 +65,70 @@ export function getRalativePath(fullPath: string): string {
}

export async function readKamelets(context: ExtensionContext) {
const yamls: string[] = await readBuildInKamelets(context);
const yamls: string[] = await readBuildInKamelets(context);
const kameletsPath: string | undefined = workspace.getConfiguration().get("Karavan.kameletsPath");
if (kameletsPath && kameletsPath.trim().length > 0) {
const kameletsDir = path.isAbsolute(kameletsPath) ? kameletsPath : path.resolve(kameletsPath);
const files = await readFilesInDirByExtension(kameletsDir, "yaml");
const customKamelets: string[] = Array.from(files.values());
if (customKamelets && customKamelets.length > 0) yamls.push(...customKamelets);
try {
const source = kameletsPath.trim().replace(/\$\{(?:env:)?([A-Za-z_][A-Za-z0-9_]*)\}/g, (_match, name: string) => {
const value = typeof process !== "undefined" ? process.env[name] : undefined;
if (value === undefined) {
throw new Error(`Environment variable "${name}" is not defined in the extension host`);
}
return value;
});
let sources: string[];
if (/^https?:\/\//i.test(source)) {
const controller = new AbortController();
const timeout = setTimeout(() => controller.abort(), 30000);
try {
const response = await fetch(source, {signal: controller.signal});
if (!response.ok) {
throw new Error(`HTTP ${response.status} ${response.statusText}`);
}
sources = [await response.text()];
} finally {
clearTimeout(timeout);
}
} else {
const fullPath = path.resolve(source);
const fileStat = await stat(fullPath);
if (fileStat.type & FileType.Directory) {
const files = await readFilesInDirByExtension(fullPath, "yaml");
sources = Array.from(files.values());
} else {
sources = [Buffer.from(await readFile(fullPath)).toString('utf8')];
}
}
const customKamelets = sources.flatMap(parseKamelets);
yamls.push(...customKamelets);
} catch (err) {
console.error("Karavan: failed to load custom Kamelets", err);
window.showErrorMessage(`Karavan: failed to load custom Kamelets: ${err instanceof Error ? err.message : String(err)}`);
}
}
return yamls;
}

function parseKamelets(code: string): string[] {
return yaml.loadAll(code)
.filter(document => document !== null && document !== undefined)
.map(document => {
if (typeof document !== "object" || !("kind" in document) || document.kind !== "Kamelet") {
throw new Error("Each YAML document must be a Kamelet (kind: Kamelet)");
}
return yaml.dump(document);
});
}

async function readBuildInKamelets(context: ExtensionContext) {
const kameletsPath = path.join(context.extensionPath, 'metadata', "kamelets.yaml");
try {
const file = await readFile(kameletsPath);
const code = Buffer.from(file).toString('utf8');
return code.split("\n---\n").map(c => c.trim()).filter(c => c.length > 0);
return parseKamelets(code);
} catch (err) {
// Built-in Kamelets are optional: a missing catalog must not block the designer from loading.
console.error(`Karavan: built-in Kamelets not found at ${kameletsPath}`, err);
console.error(`Karavan: failed to load built-in Kamelets at ${kameletsPath}`, err);
return [];
}
}
Expand Down
Loading