Skip to content

Commit

Permalink
fix: improve path detection for extension (#1235)
Browse files Browse the repository at this point in the history
  • Loading branch information
wesbillman authored Apr 12, 2024
1 parent 6dda791 commit c18aade
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 19 deletions.
1 change: 1 addition & 0 deletions extensions/vscode/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ dist
dist-ssr
*.local
*.vsix
**/*.map

# Editor directories and files
.vscode/*
Expand Down
41 changes: 41 additions & 0 deletions extensions/vscode/src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import * as vscode from "vscode"
import * as fs from 'fs'
import * as path from 'path'

export const getProjectOrWorkspaceRoot = async (): Promise<string> => {
const workspaceFolders = vscode.workspace.workspaceFolders
if (!workspaceFolders) {
vscode.window.showErrorMessage("FTL extension requires an open folder or workspace to work correctly.")
return ""
}

// Check each folder for the 'ftl-project.toml' file
for (const folder of workspaceFolders) {
const workspaceRootPath = folder.uri.fsPath
const ftlProjectPath = await findFileInWorkspace(workspaceRootPath, 'ftl-project.toml')
if (ftlProjectPath) {
return ftlProjectPath.replace('ftl-project.toml', '')
}
}

return workspaceFolders[0].uri.fsPath
}

export const findFileInWorkspace = async (rootPath: string, fileName: string): Promise<string | null> => {
try {
const filesAndFolders = await fs.promises.readdir(rootPath, { withFileTypes: true })

for (const dirent of filesAndFolders) {
const fullPath = path.join(rootPath, dirent.name)
if (dirent.isDirectory()) {
const result = await findFileInWorkspace(fullPath, fileName)
if (result) { return result }
} else if (dirent.isFile() && dirent.name === fileName) {
return fullPath
}
}
} catch (error) {
console.error('Failed to read directory:', rootPath)
}
return null
}
30 changes: 11 additions & 19 deletions extensions/vscode/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
} from "vscode-languageclient/node"
import * as vscode from "vscode"
import { FTLStatus } from "./status"
import { getProjectOrWorkspaceRoot } from "./config"

const clientName = "ftl languge server"
const clientId = "ftl"
Expand Down Expand Up @@ -56,7 +57,7 @@ export async function activate(context: ExtensionContext) {
statusBarItem.command = "ftl.statusItemClicked"
statusBarItem.show()

startClient(context)
await startClient(context)

context.subscriptions.push(
restartCmd,
Expand All @@ -71,43 +72,34 @@ export async function deactivate() {
await stopClient()
}

function startClient(context: ExtensionContext) {

async function startClient(context: ExtensionContext) {
console.log("Starting client")
FTLStatus.starting(statusBarItem)
outputChannel = vscode.window.createOutputChannel("FTL", 'log')

const ftlConfig = vscode.workspace.getConfiguration("ftl")
const ftlPath = ftlConfig.get("executablePath") ?? "ftl"
const ftlPath = ftlConfig.get<string>("executablePath") ?? "ftl"
const userFlags = ftlConfig.get<string[]>("devCommandFlags") ?? []

let workspaceRootPath =
vscode.workspace.workspaceFolders &&
vscode.workspace.workspaceFolders.length > 0
? vscode.workspace.workspaceFolders[0].uri.fsPath
: null

if (!workspaceRootPath) {
vscode.window.showErrorMessage(
"FTL extension requires an open folder to work correctly."
)
return
}

const root = await getProjectOrWorkspaceRoot()
const flags = ["--lsp", ...userFlags]
let serverOptions: ServerOptions = {
run: {
command: `${ftlPath}`,
args: ["dev", ".", ...flags],
options: { cwd: workspaceRootPath },
options: { cwd: root }
},
debug: {
command: `${ftlPath}`,
args: ["dev", ".", ...flags],
options: { cwd: workspaceRootPath },
options: { cwd: root }
},
}

outputChannel.appendLine(`Running ${ftlPath} with flags: ${flags.join(" ")}`)
console.log(serverOptions.debug.args)

outputChannel = vscode.window.createOutputChannel("FTL Logs")
let clientOptions: LanguageClientOptions = {
documentSelector: [
{ scheme: "file", language: "kotlin" },
Expand Down

0 comments on commit c18aade

Please sign in to comment.