Skip to content

Commit

Permalink
feat: Support custom file Path
Browse files Browse the repository at this point in the history
  • Loading branch information
max-muoto committed Feb 3, 2025
1 parent 0552980 commit 7e36d73
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 0 deletions.
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@
"type": "string"
},
"type": "array"
},
"tach.configuration": {
"default": "",
"description": "Path to a `tach.toml` file to use for configuration. By default, the extension will mirror the behavior that the `tach` CLI would have.",
"scope": "resource",
"type": "string"
}
}
},
Expand Down
40 changes: 40 additions & 0 deletions src/common/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,37 @@ import { traceError, traceInfo, traceVerbose } from './log/logging';
import { getExtensionSettings, getGlobalSettings, getWorkspaceSettings, ISettings } from './settings';
import { getLSClientTraceLevel, getProjectRoot } from './utilities';
import { isVirtualWorkspace } from './vscodeapi';
import { execFile } from 'child_process';
import { supportsCustomConfig, VersionInfo } from './version';

export type IInitOptions = { settings: ISettings[]; globalSettings: ISettings };


/**
* Executes a command for the given executable and returns the stdout output.
*/
function executeTachBinary(file: string, args: string[] = []): Promise<string> {
return new Promise((resolve, reject) => {
execFile(file, args, (error, stdout, stderr) => {
if (error) {
reject(new Error(stderr || error.message));
} else {
resolve(stdout);
}
});
});
}

/**
* Gets the version information from the Tach executable.
*/
async function getTachVersion(pythonExecutable: string): Promise<VersionInfo> {
const stdout = await executeTachBinary(pythonExecutable, ["-m", "tach", "--version"]);
const version = stdout.trim().split(" ")[1];
const [major, minor, patch] = version.split(".").map((x) => parseInt(x, 10));
return new VersionInfo(major, minor, patch);
}

async function createServer(
settings: ISettings,
serverId: string,
Expand All @@ -34,7 +62,19 @@ async function createServer(
newEnv.PYTHONPATH = BUNDLED_PYTHON_LIBS_DIR;
}


const args = settings.interpreter.slice(1).concat(["-m", "tach", "server"]);

if (settings.configuration) {
const version = await getTachVersion(command);
if (!supportsCustomConfig(version)) {
traceError(`Server: Tach version ${version.toString()} does not support custom configuration files.`);
} else {
traceInfo(`Server: Using custom configuration file: ${settings.configuration}`);
args.push("-c", settings.configuration);
}
}

traceInfo(`Server run command: ${[command, ...args].join(' ')}`);

const serverOptions: ServerOptions = {
Expand Down
3 changes: 3 additions & 0 deletions src/common/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export interface ISettings {
workspace: string;
interpreter: string[];
importStrategy: ImportStrategy;
configuration: string | null;
}

export function getExtensionSettings(namespace: string, includeInterpreter?: boolean): Promise<ISettings[]> {
Expand Down Expand Up @@ -65,6 +66,7 @@ export async function getWorkspaceSettings(
workspace: workspace.uri.toString(),
interpreter: resolveVariables(interpreter, workspace),
importStrategy: config.get<ImportStrategy>(`importStrategy`) ?? 'useBundled',
configuration: config.get<string>(`configuration`) ?? null,
};
return workspaceSetting;
}
Expand All @@ -90,6 +92,7 @@ export async function getGlobalSettings(namespace: string, includeInterpreter?:
workspace: process.cwd(),
interpreter: interpreter,
importStrategy: getGlobalValue<ImportStrategy>(config, 'importStrategy', 'useBundled'),
configuration: getGlobalValue<string | null>(config, 'configuration', null),
};
return setting;
}
Expand Down
1 change: 1 addition & 0 deletions src/common/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import * as path from 'path';
import { LogLevel, Uri, WorkspaceFolder } from 'vscode';
import { Trace } from 'vscode-jsonrpc/node';
import { getWorkspaceFolders } from './vscodeapi';
import { LanguageClient } from 'vscode-languageclient/node';

function logLevelToTrace(logLevel: LogLevel): Trace {
switch (logLevel) {
Expand Down
33 changes: 33 additions & 0 deletions src/common/version.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
export class VersionInfo {
constructor(
public major: number,
public minor: number,
public patch: number
) {}

toString(): string {
return `${this.major}.${this.minor}.${this.patch}`;
}
}

function versionGte(a: VersionInfo, b: VersionInfo): boolean {
if (a.major !== b.major) {
return a.major > b.major;
}
if (a.minor !== b.minor) {
return a.minor > b.minor;
}
return a.patch >= b.patch;
}

const MIN_VERSION_WITH_CONFIG = {
major: 0,
minor: 24,
patch: 0,
};



export function supportsCustomConfig(version: VersionInfo): boolean {
return versionGte(version, MIN_VERSION_WITH_CONFIG);
}

0 comments on commit 7e36d73

Please sign in to comment.