@@ -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
7879function io .open (filename , mode ) end
@@ -275,3 +276,214 @@ io.stdin = nil
275276--- * `io.stdout`: Standard out.
276277--- @type file
277278io.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+
0 commit comments