Skip to content

Commit d26d0d1

Browse files
authored
feat(tolk/toolchain): support Tolk global installation (#24)
Fixes #19
1 parent ce6754b commit d26d0d1

File tree

2 files changed

+44
-12
lines changed

2 files changed

+44
-12
lines changed

server/src/languages/tolk/toolchain/toolchain.ts

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {EnvironmentInfo, ToolchainInfo} from "@shared/shared-msgtypes"
99
import {existsVFS, globalVFS} from "@server/vfs/files-adapter"
1010
import {filePathToUri} from "@server/files"
1111
import {trimPrefix} from "@server/utils/strings"
12+
import * as process from "node:process"
1213

1314
export class InvalidToolchainError extends Error {
1415
public constructor(message: string) {
@@ -41,13 +42,16 @@ export class Toolchain {
4142
}
4243

4344
public static async autoDetect(root: string): Promise<Toolchain> {
44-
const candidatesPaths = [path.join(root, "node_modules", ".bin", "tolk-js")]
45+
const candidatesPaths = [
46+
path.join(root, "node_modules", ".bin", "tolk-js"),
47+
...tolkCompilerSearchPaths(),
48+
]
4549
const foundPath = await Toolchain.findDirectory(candidatesPaths)
4650
if (!foundPath) {
4751
console.info(`cannot find toolchain in:`)
48-
candidatesPaths.forEach(it => {
49-
console.info(it)
50-
})
52+
for (const path of candidatesPaths) {
53+
console.info(path)
54+
}
5155
return fallbackToolchain
5256
}
5357

@@ -160,3 +164,26 @@ export function setProjectTolkStdlibPath(path: string | null): void {
160164
}
161165

162166
export const fallbackToolchain = new Toolchain("./node_modules/.bin/tolk-js", true, "fallback")
167+
168+
export const TOLK_KNOWN_PLATFORMS: Record<string, [string, string][] | undefined> = {
169+
linux: [["/usr/bin/tolk", "../share/ton/smartcont/tolk-stdlib"]],
170+
darwin: [
171+
["/opt/homebrew/bin/tolk", "../share/ton/ton/smartcont/tolk-stdlib"],
172+
["/usr/local/bin/tolk", "../share/ton/ton/smartcont/tolk-stdlib"],
173+
],
174+
win32: [["C:\\ProgramData\\chocolatey\\lib\\ton\\bin\\tolk.exe", "smartcont/tolk-stdlib"]],
175+
}
176+
177+
export function tolkStdlibSearchPaths(): string[] {
178+
return (TOLK_KNOWN_PLATFORMS[process.platform] ?? []).map(
179+
([compilerBinaryPath, relativeStdlibFolder]) => {
180+
return path.join(path.dirname(compilerBinaryPath), relativeStdlibFolder)
181+
},
182+
)
183+
}
184+
185+
export function tolkCompilerSearchPaths(): string[] {
186+
return (TOLK_KNOWN_PLATFORMS[process.platform] ?? []).map(([compilerBinaryPath]) => {
187+
return compilerBinaryPath
188+
})
189+
}

server/src/server.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ import {existsVFS} from "@server/vfs/files-adapter"
1313
import type {ClientOptions} from "@shared/config-scheme"
1414
import {
1515
DocumentationAtPositionRequest,
16+
SetToolchainVersionNotification,
17+
SetToolchainVersionParams,
1618
TypeAtPositionParams,
1719
TypeAtPositionRequest,
1820
TypeAtPositionResponse,
19-
SetToolchainVersionNotification,
20-
SetToolchainVersionParams,
2121
} from "@shared/shared-msgtypes"
2222
import {Logger} from "@server/utils/logger"
2323
import {clearDocumentSettings, getDocumentSettings, ServerSettings} from "@server/settings/settings"
@@ -78,8 +78,9 @@ import {
7878
provideTolkCompletionResolve,
7979
} from "@server/languages/tolk/completion"
8080
import {
81-
setProjectTolkStdlibPath,
8281
InvalidToolchainError,
82+
setProjectTolkStdlibPath,
83+
tolkStdlibSearchPaths,
8384
} from "@server/languages/tolk/toolchain/toolchain"
8485
import {
8586
provideTolkDocumentSymbols,
@@ -185,7 +186,12 @@ async function findTolkStdlib(settings: ServerSettings, rootDir: string): Promis
185186
return settings.tolk.stdlib.path
186187
}
187188

188-
const searchDirs = ["node_modules/@ton/tolk-js/dist/tolk-stdlib", "stdlib"]
189+
const searchDirs = [
190+
`${rootDir}/node_modules/@ton/tolk-js/dist/tolk-stdlib`,
191+
`${rootDir}/stdlib`,
192+
`${rootDir}/tolk-stdlib`,
193+
...tolkStdlibSearchPaths(),
194+
]
189195

190196
const testStdlibOath = process.env["TEST_TOLK_STDLIB_PATH"]
191197
if (testStdlibOath) {
@@ -194,17 +200,17 @@ async function findTolkStdlib(settings: ServerSettings, rootDir: string): Promis
194200

195201
async function findDirectory(): Promise<string | null> {
196202
for (const searchDir of searchDirs) {
197-
if (await existsVFS(globalVFS, filePathToUri(path.join(rootDir, searchDir)))) {
203+
if (await existsVFS(globalVFS, filePathToUri(searchDir))) {
198204
return searchDir
199205
}
200206
}
201207

202208
return null
203209
}
204210

205-
const localFolder = await findDirectory()
211+
const stdlibPath = await findDirectory()
206212

207-
if (localFolder === null) {
213+
if (stdlibPath === null) {
208214
console.error(
209215
"Tolk standard library not found! Searched in:\n",
210216
searchDirs.map(dir => path.join(rootDir, dir)).join("\n"),
@@ -216,7 +222,6 @@ async function findTolkStdlib(settings: ServerSettings, rootDir: string): Promis
216222
return null
217223
}
218224

219-
const stdlibPath = path.join(rootDir, localFolder)
220225
console.info(`Using Tolk Standard library from ${stdlibPath}`)
221226
return stdlibPath
222227
}

0 commit comments

Comments
 (0)