Skip to content

Commit 10e9dc1

Browse files
committed
add document
1 parent f6445f8 commit 10e9dc1

File tree

15 files changed

+971
-147
lines changed

15 files changed

+971
-147
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---@meta
2+
-- Minimal base64 helpers for typing/completion.
3+
4+
---@class base64lib
5+
base64 = {}
6+
7+
--- Encode bytes or string to base64 text.
8+
---@param s string
9+
---@return string
10+
function base64.encode(s) end
11+
12+
--- Decode base64 text to raw string bytes.
13+
---@param s string
14+
---@return string
15+
function base64.decode(s) end
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
---@meta
2+
-- Minimal CSV helpers for xmake scripts.
3+
4+
---@class csvlib
5+
csv = {}
6+
7+
---@class csv_parse_opt
8+
---@field header? boolean # treat first row as header
9+
---@field sep? string # separator, default ","
10+
---@field quote? string # quote char, default '"'
11+
12+
---@class csv_table: table
13+
14+
--- Parse CSV text into Lua table (array of rows or array of dicts if header=true).
15+
---@param s string
16+
---@param opt? csv_parse_opt
17+
---@return csv_table
18+
function csv.parse(s, opt) end
19+
20+
--- Encode Lua table into CSV text.
21+
---@param rows csv_table
22+
---@param opt? csv_parse_opt
23+
---@return string
24+
function csv.encode(rows, opt) end
25+
26+
--- Load CSV file.
27+
---@param path string
28+
---@param opt? csv_parse_opt
29+
---@return csv_table|nil, string? err
30+
function csv.load(path, opt) end
31+
32+
--- Save CSV file.
33+
---@param path string
34+
---@param rows csv_table
35+
---@param opt? csv_parse_opt
36+
---@return boolean ok, string? err
37+
function csv.save(path, rows, opt) end
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---@meta
2+
--
3+
-- Minimal xmake hash helpers for static typing/completion.
4+
-- Reference: xmake/core/base/hash.lua
5+
6+
---@class hashlib
7+
hash = {}
8+
9+
--- Calculate MD5 of a string and return hex digest.
10+
---@param s string
11+
---@return string
12+
function hash.md5(s) end
13+
14+
--- Calculate SHA256 of a string and return hex digest.
15+
---@param s string
16+
---@return string
17+
function hash.sha256(s) end
18+
19+
--- Calculate SHA1 of a string and return hex digest.
20+
---@param s string
21+
---@return string
22+
function hash.sha1(s) end
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
---@meta
2+
--
3+
-- Minimal xmake http helpers for static typing/completion.
4+
-- Reference: xmake/core/base/http.lua
5+
6+
---@class httplib
7+
http = {}
8+
9+
---@class http_request_opt
10+
---@field headers? table<string,string>
11+
---@field timeout? integer
12+
---@field insecure? boolean @allow insecure https
13+
---@field proxy? string
14+
15+
---@class http_response
16+
---@field status integer
17+
---@field headers table<string,string>
18+
---@field body string
19+
20+
--- Perform a HTTP GET request and return response.
21+
---@param url string
22+
---@param opt? http_request_opt
23+
---@return http_response
24+
function http.get(url, opt) end
25+
26+
---@class http_download_opt: http_request_opt
27+
---@field continue? boolean @resume download if possible
28+
29+
--- Download a file to destination path.
30+
---@param url string
31+
---@param dst string
32+
---@param opt? http_download_opt
33+
---@return boolean ok
34+
function http.download(url, dst, opt) end

crates/xmake_code_analysis/resources/std/io.lua

Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ function io.lines(filename, ...) end
7373
--- some systems to open the file in binary mode.
7474
---@param filename string
7575
---@param mode? iolib.OpenMode
76+
---@overload fun(filename: string, mode?: iolib.OpenMode, opt?: table): file?, string?
7677
---@return file?
7778
---@return string? err
7879
function io.open(filename, mode) end
@@ -275,3 +276,214 @@ io.stdin = nil
275276
--- * `io.stdout`: Standard out.
276277
---@type file
277278
io.stdout = nil
279+
280+
-------------------------------------------------------------------------------
281+
-- Xmake extensions (io)
282+
-- These APIs extend the standard Lua io library and are provided by Xmake.
283+
-------------------------------------------------------------------------------
284+
285+
--- Get a std stream as a file by pseudo path.
286+
--- Paths: `/dev/stdin`, `/dev/stdout`, `/dev/stderr`.
287+
---@param path string
288+
---@return file? f
289+
---@return string? err
290+
function io.stdfile(path) end
291+
292+
--- Open a file lock object for the given path.
293+
--- The lock file will be created if not exists.
294+
---@param path string
295+
---@return filelock? lock
296+
---@return string? err
297+
function io.openlock(path) end
298+
299+
--- Read all data from a file with optional options (e.g. encoding).
300+
---@param filepath string
301+
---@param opt? table
302+
---@return string|nil data
303+
---@return string? err
304+
function io.readfile(filepath, opt) end
305+
306+
--- Write all data to a file with optional options (e.g. encoding).
307+
---@param filepath string
308+
---@param data string
309+
---@param opt? table
310+
---@return boolean ok
311+
---@return string? err
312+
function io.writefile(filepath, data, opt) end
313+
314+
--- Save a Lua object to file (serialized text).
315+
--- Only writes when changed if `opt.only_changed` is true.
316+
---@param filepath string
317+
---@param object any
318+
---@param opt? table
319+
---@return boolean ok
320+
---@return string? err
321+
function io.save(filepath, object, opt) end
322+
323+
--- Load a Lua object from file (deserialize text content).
324+
---@param filepath string
325+
---@param opt? table
326+
---@return any result
327+
---@return string? err
328+
function io.load(filepath, opt) end
329+
330+
--- Iterate file lines, with options. When `opt.close_on_finished` is true,
331+
--- the file will be closed when iteration ends.
332+
---@overload fun(filename?: string): fun():any
333+
---@param filename string
334+
---@param opt? table
335+
---@return fun():string|nil
336+
function io.lines(filename, opt) end
337+
338+
--- Replace text in file with Lua string.gsub semantics.
339+
--- Returns the new content and replacement count.
340+
---@param filepath string
341+
---@param pattern string|table
342+
---@param replace string|function
343+
---@param opt? table
344+
---@return string|nil data
345+
---@return integer count
346+
---@return string? err
347+
function io.gsub(filepath, pattern, replace, opt) end
348+
349+
--- Replace text in file using pattern/replace with extra options.
350+
--- Similar to `io.gsub` but supports additional flags (e.g. plain, etc.).
351+
---@param filepath string
352+
---@param pattern string
353+
---@param replace string|function
354+
---@param opt? table
355+
---@return string|nil data
356+
---@return integer count
357+
---@return string? err
358+
function io.replace(filepath, pattern, replace, opt) end
359+
360+
--- Insert text before a specific line number and return new content.
361+
--- Line index starts from 1.
362+
---@param filepath string
363+
---@param lineidx integer
364+
---@param text string
365+
---@param opt? table
366+
---@return string|nil data
367+
---@return string? err
368+
function io.insert(filepath, lineidx, text, opt) end
369+
370+
--- Print file content to stdout. If linecount is set, stop after N lines.
371+
---@param filepath string
372+
---@param linecount? integer
373+
---@param opt? table
374+
function io.cat(filepath, linecount, opt) end
375+
376+
--- Tail file content to stdout. If linecount < 0, print whole file.
377+
---@param filepath string
378+
---@param linecount? integer
379+
---@param opt? table
380+
function io.tail(filepath, linecount, opt) end
381+
382+
--- Check whether the given file (default stdout) is a TTY.
383+
---@param file? file
384+
---@return boolean|nil ok
385+
function io.isatty(file) end
386+
387+
--- Read from stdin with options.
388+
---@param fmt std.readmode|string
389+
---@param opt? table
390+
---@return any
391+
---@return any ...
392+
function io.read(fmt, opt) end
393+
394+
--- True if stdin has readable data.
395+
---@return boolean
396+
function io.readable() end
397+
398+
--- Print formatted text to stdout and append newline.
399+
---@param ... any
400+
function io.print(...) end
401+
402+
--- Print formatted text to stdout without newline.
403+
---@param ... any
404+
function io.printf(...) end
405+
406+
-------------------------------------------------------------------------------
407+
-- Xmake extensions (file methods)
408+
-------------------------------------------------------------------------------
409+
410+
--- Get file size in bytes.
411+
---@return integer|nil size
412+
---@return string? err
413+
function file:size() end
414+
415+
--- Return whether file is readable (nonblocking check on pipes/streams).
416+
---@return boolean ok
417+
---@return string? err
418+
function file:readable() end
419+
420+
--- Check if file is attached to a TTY.
421+
---@return boolean|nil ok
422+
---@return string? err
423+
function file:isatty() end
424+
425+
--- Get underlying raw file descriptor/handle.
426+
--- Windows returns HANDLE casted to integer.
427+
---@return integer|nil fd
428+
---@return string? err
429+
function file:rawfd() end
430+
431+
--- Write a formatted line (adds trailing newline).
432+
---@param ... any
433+
function file:print(...) end
434+
435+
--- Write a formatted string (no trailing newline).
436+
---@param ... any
437+
function file:printf(...) end
438+
439+
--- Serialize and save a Lua object to this file.
440+
---@param object any
441+
---@param opt? table
442+
---@return boolean ok
443+
---@return string? err
444+
function file:save(object, opt) end
445+
446+
--- Read and deserialize a Lua object from this file.
447+
---@return any result
448+
---@return string? err
449+
function file:load() end
450+
451+
-------------------------------------------------------------------------------
452+
-- Xmake filelock
453+
-------------------------------------------------------------------------------
454+
455+
--- File lock object used to synchronize access across processes.
456+
---@class filelock
457+
local filelock = {}
458+
459+
--- Try to acquire a lock. When `opt.shared` is true, take a shared lock.
460+
--- Re-entrant: multiple lock() calls must be matched by unlock().
461+
---@param opt? table
462+
---@return boolean ok
463+
---@return string? err
464+
function filelock:lock(opt) end
465+
466+
--- Try to acquire a lock without blocking.
467+
---@param opt? table
468+
---@return boolean ok
469+
---@return string? err
470+
function filelock:trylock(opt) end
471+
472+
--- Release one level of lock. Fully unlocks when the counter reaches zero.
473+
---@return boolean ok
474+
---@return string? err
475+
function filelock:unlock() end
476+
477+
--- Close the lock handle.
478+
---@return boolean ok
479+
---@return string? err
480+
function filelock:close() end
481+
482+
--- True if currently locked by this process.
483+
---@return boolean
484+
function filelock:islocked() end
485+
486+
--- Absolute path of the lock file.
487+
---@return string
488+
function filelock:path() end
489+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---@meta
2+
--
3+
-- Minimal xmake json helpers for static typing/completion.
4+
-- Reference: xmake/core/base/json.lua
5+
6+
---@class jsonlib
7+
json = {}
8+
9+
---@class json_encode_opt
10+
---@field indent? string|integer
11+
---@field sort_keys? boolean
12+
13+
--- Encode any lua value to json string.
14+
---@param v any
15+
---@param opt? json_encode_opt
16+
---@return string
17+
function json.encode(v, opt) end
18+
19+
---@class json_decode_opt
20+
---@field null? any @value to map JSON null, default: nil
21+
22+
--- Decode json string to lua value.
23+
---@param s string
24+
---@param opt? json_decode_opt
25+
---@return any
26+
function json.decode(s, opt) end
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
---@meta
2+
-- Minimal net helpers for xmake scripts (TCP sockets).
3+
4+
---@class netlib
5+
net = {}
6+
7+
---@class net_tcp_opt
8+
---@field timeout? integer # milliseconds
9+
10+
---@class net_tcp
11+
local net_tcp = {}
12+
13+
--- Send raw bytes/string.
14+
---@param data string
15+
---@return integer|nil sent, string? err
16+
function net_tcp:send(data) end
17+
18+
--- Receive bytes up to `n` or until closed.
19+
---@param n? integer
20+
---@return string|nil data, string? err
21+
function net_tcp:recv(n) end
22+
23+
--- Close socket.
24+
function net_tcp:close() end
25+
26+
--- Connect to `host:port` and return tcp socket.
27+
---@param host string
28+
---@param port integer
29+
---@param opt? net_tcp_opt
30+
---@return net_tcp|nil sock, string? err
31+
function net.tcp_connect(host, port, opt) end

0 commit comments

Comments
 (0)