Skip to content

Commit

Permalink
clean up code
Browse files Browse the repository at this point in the history
  • Loading branch information
rcannood committed Jul 9, 2024
1 parent 713b01d commit 6cbd013
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 64 deletions.
78 changes: 14 additions & 64 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,70 +1,14 @@
import * as vscode from 'vscode';
import * as cp from 'child_process';
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';

const versionCachedDir = path.join(os.homedir(), '.viash', 'releases');

function getViashSchemaFile(version: string | undefined = undefined): string | undefined {
if (!version) {
version = getViashVersion();
}
const schemaPath = path.join(versionCachedDir, version, 'schema.json');

if (!fs.existsSync(schemaPath)) {
try {
fs.mkdirSync(path.dirname(schemaPath), { recursive: true }); // Ensure directory exists

const { status, stderr } = cp.spawnSync(
'viash',
['export', 'json_schema', '--format', 'json', '--output', schemaPath],
{ cwd: vscode.workspace.workspaceFolders?.[0].uri.fsPath }
);
if (status) {
vscode.window.showErrorMessage(`Error getting Viash schema: ${stderr}`);
return undefined;
}
console.log(`Schema file created at: ${schemaPath}`);
} catch (error) {
vscode.window.showErrorMessage(`Error getting Viash schema: ${error}`);
return undefined;
}
}

return schemaPath
}

function getViashVersion(): string {
try {
const { stdout } = cp.spawnSync(
"viash",
["--version"],
{ cwd: vscode.workspace.workspaceFolders?.[0].uri.fsPath }
);
// stdout format: 'viash <version> (c) <year> <author>'
return stdout.toString().trim().split(' ')[1];
} catch (error) {
vscode.window.showErrorMessage(`Error getting Viash version: ${error}`);
return "unknown";
}
}
import * as vscode from "vscode";
import { getViashVersion } from "./getViashVersion";
import { getViashSchemaFile } from "./getViashSchemaFile";

export async function activate(context: vscode.ExtensionContext) {
// set schema
activateViashSchema(context);
}

// This method is called when your extension is deactivated
export function deactivate() {}

function isViashComponent(document: vscode.TextDocument) {
return document.fileName.endsWith(".vsh.yaml");
}

export async function activateViashSchema(
context: vscode.ExtensionContext
) {
export async function activateViashSchema(context: vscode.ExtensionContext) {
const version = getViashVersion();
if (!version) {
return;
Expand All @@ -77,12 +21,18 @@ export async function activateViashSchema(

const config = vscode.workspace.getConfiguration("yaml");
const schemas = config.get<any>("schemas") || {};
if (schemas[schemaPath] && schemas[schemaPath].length > 0 && schemas[schemaPath][0] === "*.vsh.yaml") {
if (
schemas[schemaPath] &&
schemas[schemaPath].length == 1 &&
schemas[schemaPath][0] === "*.vsh.yaml"
) {
// nothing needs to be done
return;
}

schemas[schemaPath] = ["*.vsh.yaml"];
config.update("schemas", schemas, vscode.ConfigurationTarget.Workspace);
vscode.window.showInformationMessage(`Viash ${version} detected. Set schema to '${schemaPath}'.`);
}
vscode.window.showInformationMessage(
`Viash ${version} detected. Set schema to '${schemaPath}'.`
);
}
39 changes: 39 additions & 0 deletions src/getViashSchemaFile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import * as vscode from "vscode";
import * as cp from "child_process";
import * as fs from "fs";
import * as os from "os";
import * as path from "path";
import { getViashVersion } from "./getViashVersion";

const versionCachedDir = path.join(os.homedir(), ".viash", "releases");

export function getViashSchemaFile(
version: string | undefined = undefined
): string | undefined {
if (!version) {
version = getViashVersion();
}
const schemaPath = path.join(versionCachedDir, version, "schema.json");

if (!fs.existsSync(schemaPath)) {
try {
fs.mkdirSync(path.dirname(schemaPath), { recursive: true }); // Ensure directory exists

const { status, stderr } = cp.spawnSync(
"viash",
["export", "json_schema", "--format", "json", "--output", schemaPath],
{ cwd: vscode.workspace.workspaceFolders?.[0].uri.fsPath }
);
if (status) {
vscode.window.showErrorMessage(`Error getting Viash schema: ${stderr}`);
return undefined;
}
console.log(`Schema file created at: ${schemaPath}`);
} catch (error) {
vscode.window.showErrorMessage(`Error getting Viash schema: ${error}`);
return undefined;
}
}

return schemaPath;
}
15 changes: 15 additions & 0 deletions src/getViashVersion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import * as vscode from "vscode";
import * as cp from "child_process";

export function getViashVersion(): string {
try {
const { stdout } = cp.spawnSync("viash", ["--version"], {
cwd: vscode.workspace.workspaceFolders?.[0].uri.fsPath,
});
// stdout format: 'viash <version> (c) <year> <author>'
return stdout.toString().trim().split(" ")[1];
} catch (error) {
vscode.window.showErrorMessage(`Error getting Viash version: ${error}`);
return "unknown";
}
}

0 comments on commit 6cbd013

Please sign in to comment.