Skip to content

Commit 13f52c6

Browse files
committed
初始化流程
1 parent 7ea95e8 commit 13f52c6

File tree

7 files changed

+160
-38
lines changed

7 files changed

+160
-38
lines changed

script/luals.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ luals.inspect = require 'tools.inspect'
1010

1111
luals.json = require 'tools.json'
1212
package.loaded['json'] = luals.json
13-
require 'tools.json-beautify'
13+
package.loaded['json-beautify'] = require 'tools.json-beautify'
1414
require 'tools.jsonc'
1515
require 'tools.json-edit'
16+
require 'tools.log'
1617

1718
luals.uri = require 'tools.uri'
1819

script/master/init.lua

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
1+
local fs = require 'bee.filesystem'
22
local time = require 'bee.time'
33

44
--语言服务器自身的状态
55
---@class LuaLS.Runtime
6-
luals.runtime = require 'runtime.lua'
6+
luals.runtime = require 'runtime'
7+
8+
fs.create_directories(fs.path(luals.runtime.rootPath) / 'log')
79

810
---@class Log
9-
log = Class 'Log' {
11+
log = New 'Log' {
1012
clock = function ()
1113
return time.monotonic() / 1000.0
1214
end,
@@ -17,7 +19,7 @@ log = Class 'Log' {
1719
}
1820

1921
log.info('Lua Lsp startup!')
20-
log.info('ROOT:', luals.runtime.rootUri)
22+
log.info('LUALS:', luals.runtime.rootUri)
2123
log.info('LOGPATH:', luals.runtime.logUri)
2224
log.info('METAPATH:', luals.runtime.metaUri)
2325
log.info('VERSION:', luals.runtime.version)
@@ -29,8 +31,4 @@ xpcall(function ()
2931
require 'debugger':start "127.0.0.1:12399"
3032
end, log.warn)
3133

32-
require 'cli'
33-
34-
local _, service = xpcall(require, log.error, 'service')
35-
36-
service.start()
34+
print = log.debug

script/meta/filesystem.lua

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
---@meta bee.filesystem
2+
3+
---@class fs.path
4+
---@operator div: fs.path
5+
local fsPath = {}
6+
7+
---@return string
8+
function fsPath:string()
9+
end
10+
11+
---@return fs.path
12+
function fsPath:parent_path()
13+
end
14+
15+
---@return boolean
16+
function fsPath:is_relative()
17+
end
18+
19+
---@return fs.path
20+
function fsPath:filename()
21+
end
22+
23+
---@return fs.path
24+
function fsPath:stem()
25+
end
26+
27+
---@return fs.path
28+
function fsPath:extension()
29+
end
30+
31+
---@class fs.status
32+
local fsStatus = {}
33+
34+
---@return 'none' | 'not_found' | 'regular' | 'directory' | 'symlink' | 'block' | 'character' | 'fifo' | 'junction' | 'unknown'
35+
function fsStatus:type()
36+
end
37+
38+
---@class bee.filesystem
39+
local fs = {}
40+
41+
---@class fs.copy_options
42+
---@field overwrite_existing integer
43+
local copy_options
44+
45+
fs.copy_options = copy_options
46+
47+
---@param path string|fs.path
48+
---@return fs.path
49+
function fs.path(path)
50+
end
51+
52+
---@return fs.path
53+
function fs.exe_path()
54+
end
55+
56+
---@param path fs.path
57+
---@return boolean
58+
function fs.exists(path)
59+
end
60+
61+
---@param path fs.path
62+
---@return boolean
63+
function fs.is_directory(path)
64+
end
65+
66+
---@param path fs.path
67+
---@return fun():fs.path, fs.status
68+
function fs.pairs(path)
69+
end
70+
71+
---@param path fs.path
72+
---@return fs.path
73+
function fs.canonical(path)
74+
end
75+
76+
---@param path fs.path
77+
---@return fs.path
78+
function fs.fullpath(path)
79+
end
80+
81+
---@param path fs.path
82+
---@return fs.path
83+
function fs.absolute(path)
84+
end
85+
86+
---@param path fs.path
87+
function fs.create_directories(path)
88+
end
89+
90+
---@param path fs.path
91+
---@return fs.status
92+
function fs.symlink_status(path)
93+
end
94+
95+
---@param path fs.path
96+
---@return boolean
97+
function fs.remove(path)
98+
end
99+
100+
---@param source fs.path
101+
---@param target fs.path
102+
---@param options? integer | `fs.copy_options.overwrite_existing`
103+
function fs.copy_file(source, target, options)
104+
end
105+
106+
---@param oldPath fs.path
107+
---@param newPath fs.path
108+
function fs.rename(oldPath, newPath)
109+
end
110+
111+
---@return fs.path
112+
function fs.current_path()
113+
end
114+
115+
return fs

script/runtime/init.lua

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ local M = {}
1212
---@field DBGADDRESS? string # 调试器地址,默认为 '127.0.0.1'
1313
M.args = {
1414
--指定日志输出目录,默认为 `./log`
15-
LOGPATH = '${LUALS}/log',
15+
LOGPATH = '$LUALSPATH/log',
1616
--指定meta文件的生成目录,默认为 `./meta`
17-
METAPATH = '${LUALS}/meta',
17+
METAPATH = '$LUALSPATH/meta',
1818
--是否为开发模式
1919
DEVELOP = false,
2020
--调试器端口号,默认为 11411
@@ -40,39 +40,42 @@ M.args = {
4040
---@type 'Error' | 'Warning' | 'Information' | 'Hint'
4141
CHECKLEVEL = 'Warning',
4242
--诊断报告生成的文件路径(json),默认为 `$LOGPATH/check.json`
43-
CHECK_OUT_PATH = '${LOGPATH}/check.json',
43+
CHECK_OUT_PATH = '$LOGPATH/check.json',
4444

4545
--命令行:生成文档
4646
DOC = '',
4747
}
4848
luals.util.tableMerge(M.args, argparser.parse(arg, true))
4949

50-
--启动时的版本号
51-
M.version = version.getVersion()
52-
53-
--路径相关
54-
---@class LuaLS.Path
55-
M.path = {}
56-
5750
---@return string
5851
local function findRoot()
59-
local lastPath
60-
for i = 1, 10 do
61-
local currentPath = debug.getinfo(i, 'S').source
62-
if currentPath:sub(1, 1) ~= '@' then
52+
local currentPath
53+
for i = 1, 100 do
54+
currentPath = debug.getinfo(i, 'S').source
55+
if currentPath:match '@%a:' then
6356
break
6457
end
65-
lastPath = currentPath:sub(2)
6658
end
67-
assert(lastPath, 'Can not find root path')
68-
local rootPath = lastPath:gsub('[/\\]*[^/\\]-$', '')
59+
assert(currentPath, 'Can not find root path')
60+
local rootPath = currentPath:sub(2):gsub('[/\\]*[^/\\]-$', '')
6961
rootPath = (rootPath == '' and '.' or rootPath)
7062
return rootPath
7163
end
7264

65+
--环境变量
66+
M.env = {}
7367
--语言服务器根路径
74-
M.rootUri = luals.uri.encode(luals.util.expandPath(findRoot()))
75-
M.logUri = luals.uri.encode(luals.util.expandPath(M.args.LOGPATH))
76-
M.metaUri = luals.uri.encode(luals.util.expandPath(M.args.METAPATH))
68+
M.rootPath = luals.util.expandPath(findRoot())
69+
M.env.LUALSPATH = M.rootPath
70+
M.logPath = luals.util.expandPath(M.args.LOGPATH, M.env)
71+
M.env.LOGPATH = M.logPath
72+
M.metaPath = luals.util.expandPath(M.args.METAPATH, M.env)
73+
M.env.METAPATH = M.metaPath
74+
M.rootUri = luals.uri.encode(M.rootPath)
75+
M.logUri = luals.uri.encode(M.logPath)
76+
M.metaUri = luals.uri.encode(M.metaPath)
77+
78+
--启动时的版本号
79+
M.version = version.getVersion(M.rootPath)
7780

7881
return M

script/runtime/version.lua

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
local function loadVersion()
2-
local changelogUri = luals.uri.join(luals.runtime.rootUri, 'changelog.md')
3-
local changelog = luals.util.loadFile(luals.uri.decode(changelogUri))
1+
---@param rootPath string
2+
---@return string?
3+
local function loadVersion(rootPath)
4+
local changelog = luals.util.loadFile(rootPath .. '/changelog.md')
45
if not changelog then
56
return
67
end
@@ -19,10 +20,11 @@ end
1920
---@class Runtime.VersionParser
2021
local M = {}
2122

23+
---@param rootPath string
2224
---@return string
23-
function M.getVersion()
25+
function M.getVersion(rootPath)
2426
if not M.version then
25-
M.version = loadVersion() or '<Unknown>'
27+
M.version = loadVersion(rootPath) or '<Unknown>'
2628
end
2729

2830
return M.version

script/tools/log.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ M.time = os.time
3030
---@private
3131
M.messageFormat = '[%s][%5s][%s]: %s\n'
3232

33-
---@enum (key) Log.Level
33+
---@enum(key) Log.Level
3434
M.logLevel = {
3535
trace = 1,
3636
debug = 2,

script/tools/utility.lua

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -708,16 +708,19 @@ function m.trim(str, mode)
708708
end
709709

710710
---@param path string
711+
---@param env? table
711712
---@return string
712-
function m.expandPath(path)
713+
function m.expandPath(path, env)
713714
if path:sub(1, 1) == '~' then
714715
local home = getenv('HOME')
715716
if not home then -- has to be Windows
716717
home = getenv 'USERPROFILE' or (getenv 'HOMEDRIVE' .. getenv 'HOMEPATH')
717718
end
718719
return home .. path:sub(2)
719720
elseif path:sub(1, 1) == '$' then
720-
path = path:gsub('%$([%w_]+)', getenv)
721+
path = path:gsub('%$([%w_]+)', function (name)
722+
return env and env[name] or getenv(name)
723+
end)
721724
return path
722725
end
723726
return path

0 commit comments

Comments
 (0)