@@ -12,24 +12,11 @@ local vm = require 'vm'
1212local guide = require ' parser.guide'
1313local getDesc = require ' core.hover.description'
1414local getLabel = require ' core.hover.label'
15+ local doc2md = require ' cli.doc2md'
16+ local progress = require ' progress'
17+ local fs = require ' bee.filesystem'
1518
16- lang (LOCALE )
17-
18- if type (DOC ) ~= ' string' then
19- print (lang .script (' CLI_CHECK_ERROR_TYPE' , type (DOC )))
20- return
21- end
22-
23- local rootUri = furi .encode (DOC )
24- if not rootUri then
25- print (lang .script (' CLI_CHECK_ERROR_URI' , DOC ))
26- return
27- end
28-
29- util .enableCloseFunction ()
30-
31- local lastClock = os.clock ()
32- local results = {}
19+ local export = {}
3320
3421--- @async
3522local function packObject (source , mark )
@@ -89,7 +76,7 @@ local function packObject(source, mark)
8976 if source .bindDocs then
9077 new [' desc' ] = getDesc (source )
9178 end
92- new [' view' ] = new [' view' ] or vm .getInfer (source ):view (rootUri )
79+ new [' view' ] = new [' view' ] or vm .getInfer (source ):view (ws . rootUri )
9380 end
9481 return new
9582end
11299
113100--- @async
114101--- @param global vm.global
115- local function collect (global )
102+ --- @param results table
103+ local function collect (global , results )
116104 if guide .isBasicType (global .name ) then
117105 return
118106 end
@@ -122,7 +110,7 @@ local function collect(global)
122110 defines = {},
123111 fields = {},
124112 }
125- for _ , set in ipairs (global :getSets (rootUri )) do
113+ for _ , set in ipairs (global :getSets (ws . rootUri )) do
126114 local uri = guide .getUri (set )
127115 if files .isLibrary (uri ) then
128116 goto CONTINUE
@@ -149,7 +137,7 @@ local function collect(global)
149137 results [# results + 1 ] = result
150138 --- @async
151139 --- @diagnostic disable-next-line : not-yieldable
152- vm .getClassFields (rootUri , global , vm .ANY , function (source )
140+ vm .getClassFields (ws . rootUri , global , vm .ANY , function (source )
153141 if source .type == ' doc.field' then
154142 --- @cast source parser.object
155143 if files .isLibrary (guide .getUri (source )) then
@@ -160,7 +148,7 @@ local function collect(global)
160148 if source .field .type == ' doc.field.name' then
161149 field .name = source .field [1 ]
162150 else
163- field .name = (' [%s]' ):format (vm .getInfer (source .field ):view (rootUri ))
151+ field .name = (' [%s]' ):format (vm .getInfer (source .field ):view (ws . rootUri ))
164152 end
165153 field .type = source .type
166154 field .file = guide .getUri (source )
@@ -219,46 +207,109 @@ local function collect(global)
219207end
220208
221209--- @async
222- lclient ():start (function (client )
223- client :registerFakers ()
224-
225- client :initialize {
226- rootUri = rootUri ,
227- }
210+ --- @param outputPath string
211+ function export .makeDoc (outputPath )
212+ local results = {}
228213
229- io.write ( lang . script ( ' CLI_DOC_INITING ' ) )
214+ ws . awaitReady ( ws . rootUri )
230215
231- config .set (nil , ' Lua.diagnostics.enable' , false )
232- config .set (nil , ' Lua.hover.expandAlias' , false )
216+ local expandAlias = config .get (ws .rootUri , ' Lua.hover.expandAlias' )
217+ config .set (ws .rootUri , ' Lua.hover.expandAlias' , false )
218+ local _ <close> = function ()
219+ config .set (ws .rootUri , ' Lua.hover.expandAlias' , expandAlias )
220+ end
233221
234- ws .awaitReady (rootUri )
235222 await .sleep (0.1 )
236223
224+ local prog <close> = progress .create (ws .rootUri , ' 正在生成文档...' , 0 )
237225 local globals = vm .getGlobals ' type'
238226
239227 local max = # globals
240228 for i , global in ipairs (globals ) do
241- collect (global )
242- if os.clock () - lastClock > 0.2 then
243- lastClock = os.clock ()
244- local output = ' \x0D '
245- .. (' >' ):rep (math.ceil (i / max * 20 ))
246- .. (' =' ):rep (20 - math.ceil (i / max * 20 ))
247- .. ' '
248- .. (' 0' ):rep (# tostring (max ) - # tostring (i ))
249- .. tostring (i ) .. ' /' .. tostring (max )
250- io.write (output )
251- end
229+ collect (global , results )
230+ prog :setMessage ((' %d/%d' ):format (i , max ))
231+ prog :setPercentage (i / max * 100 )
252232 end
253- io.write (' \x0D ' )
254233
255234 table.sort (results , function (a , b )
256235 return a .name < b .name
257236 end )
258- end )
259237
260- local outpath = LOGPATH .. ' /doc.json'
261- jsonb .supportSparseArray = true
262- util .saveFile (outpath , jsonb .beautify (results ))
238+ local docPath = outputPath .. ' /doc.json'
239+ jsonb .supportSparseArray = true
240+ util .saveFile (docPath , jsonb .beautify (results ))
241+
242+ local mdPath = doc2md .buildMD (outputPath )
243+ return docPath , mdPath
244+ end
245+
246+ function export .runCLI ()
247+ lang (LOCALE )
248+
249+ if type (DOC ) ~= ' string' then
250+ print (lang .script (' CLI_CHECK_ERROR_TYPE' , type (DOC )))
251+ return
252+ end
253+
254+ local rootUri = furi .encode (fs .absolute (fs .path (DOC )):string ())
255+ if not rootUri then
256+ print (lang .script (' CLI_CHECK_ERROR_URI' , DOC ))
257+ return
258+ end
259+
260+ print (' root uri = ' .. rootUri )
261+
262+ util .enableCloseFunction ()
263+
264+ local lastClock = os.clock ()
265+ local results = {}
266+
267+ --- @async
268+ lclient ():start (function (client )
269+ client :registerFakers ()
270+
271+ client :initialize {
272+ rootUri = rootUri ,
273+ }
274+
275+ io.write (lang .script (' CLI_DOC_INITING' ))
276+
277+ config .set (nil , ' Lua.diagnostics.enable' , false )
278+ config .set (nil , ' Lua.hover.expandAlias' , false )
279+
280+ ws .awaitReady (rootUri )
281+ await .sleep (0.1 )
282+
283+ local globals = vm .getGlobals ' type'
284+
285+ local max = # globals
286+ for i , global in ipairs (globals ) do
287+ collect (global , results )
288+ if os.clock () - lastClock > 0.2 then
289+ lastClock = os.clock ()
290+ local output = ' \x0D '
291+ .. (' >' ):rep (math.ceil (i / max * 20 ))
292+ .. (' =' ):rep (20 - math.ceil (i / max * 20 ))
293+ .. ' '
294+ .. (' 0' ):rep (# tostring (max ) - # tostring (i ))
295+ .. tostring (i ) .. ' /' .. tostring (max )
296+ io.write (output )
297+ end
298+ end
299+ io.write (' \x0D ' )
300+
301+ table.sort (results , function (a , b )
302+ return a .name < b .name
303+ end )
304+ end )
305+
306+ local docPath = LOGPATH .. ' /doc.json'
307+ jsonb .supportSparseArray = true
308+ util .saveFile (docPath , jsonb .beautify (results ))
309+
310+ local mdPath = doc2md .buildMD (LOGPATH )
311+
312+ print (lang .script (' CLI_DOC_DONE' , furi .encode (docPath ), furi .encode (mdPath )))
313+ end
263314
264- require ' cli.doc2md '
315+ return export
0 commit comments