diff --git a/README.md b/README.md new file mode 100644 index 0000000..cffed5f --- /dev/null +++ b/README.md @@ -0,0 +1,13 @@ +# V Documentation Server + +Synopsis: + +``` +generate and serve HTML documentation for V module + +usage: vdocs [-l addr:port] [] + +options: + -help print this help message and exit + -l, -listen listen address, 127.0.0.1:4000 by default +``` diff --git a/UNLICENSE b/UNLICENSE new file mode 100644 index 0000000..c91541e --- /dev/null +++ b/UNLICENSE @@ -0,0 +1,22 @@ +This is free and unencumbered software released into the public domain. + +Anyone is free to copy, modify, publish, use, compile, sell, or distribute this +software, either in source code form or as a compiled binary, for any purpose, +commercial or non-commercial, and by any means. + +In jurisdictions that recognize copyright laws, the author or authors of this +software dedicate any and all copyright interest in the software to the public +domain. We make this dedication for the benefit of the public at large and to +the detriment of our heirs and +successors. We intend this dedication to be an overt act of relinquishment in +perpetuity of all present and future rights to this software under copyright +law. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF +CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +For more information, please refer to diff --git a/v.mod b/v.mod index 0bc8749..adc1472 100644 --- a/v.mod +++ b/v.mod @@ -1,7 +1,7 @@ Module { - name: 'vdocserve' - description: 'Generate and serve module documentation' + name: 'vdocs' + description: 'Generate and serve V module documentation' version: '0.1.0' - license: 'MIT' + license: 'Unlicense' dependencies: [] } diff --git a/vdocs.v b/vdocs.v new file mode 100644 index 0000000..89ab636 --- /dev/null +++ b/vdocs.v @@ -0,0 +1,60 @@ +module main + +import flag +import log +import net.http.file +import os +import rand +import v.vmod + +struct FlagConfig { +mut: + help bool + listen string = '127.0.0.1:4000' @[short: l] + dir string @[ignore] +} + +fn main() { + mut flags, no_matches := flag.to_struct[FlagConfig](os.args, skip: 1, style: .go_flag) or { + eprintln('cmdline parsing error, see -help for info') + exit(2) + } + if no_matches.len == 0 { + flags.dir = '.' + } else if no_matches.len == 1 { + flags.dir = no_matches[0] + } else { + eprintln('unrecognized arguments: ${no_matches[1..]}') + exit(2) + } + if flags.help { + println('generate and serve HTML documentation for V module') + println('') + println('usage: vdocs [-l addr:port] []') + println('') + println('options:') + println(' -help print this help message and exit') + println(' -l, -listen listen address, 127.0.0.1:4000 by default') + exit(0) + } + mut cache := vmod.get_cache() + modfile := cache.get_by_folder(os.getwd()) + modname := vmod.from_file(modfile.vmod_file)!.name + tmp_dir := os.join_path(os.temp_dir(), os.geteuid().str(), modname + '_docs_' + rand.u32().hex()) + log.info('generate HTML docs...') + os.mkdir_all(tmp_dir, mode: 0o755)! + os.execute_or_exit('v doc -f html -o ${tmp_dir} -m ${flags.dir} ') + signal_callback := fn [tmp_dir] (_ os.Signal) { + eprintln('\rCleanup...') + os.rmdir_all(tmp_dir) or { + log.error('unable to delete temporary dir ${tmp_dir}') + exit(1) + } + exit(0) + } + os.signal_opt(.int, signal_callback)! + os.signal_opt(.term, signal_callback)! + log.info('docs stored in temporary dir ${tmp_dir}') + log.info('use ^C to quit server') + file.serve(folder: tmp_dir, index_file: modname + '.html', on: flags.listen) +} diff --git a/vdocserve.v b/vdocserve.v deleted file mode 100644 index a4e0578..0000000 --- a/vdocserve.v +++ /dev/null @@ -1,42 +0,0 @@ -module main - -import flag -import log -import net.http.file -import os -import rand -import v.vmod - -fn main() { - mut fp := flag.new_flag_parser(os.args) - fp.application('vdocserve') - fp.description('generate and serve V module HTML documentation') - fp.version('0.1.0') - fp.skip_executable() - fp.limit_free_args(0, 2)! - listen := fp.string('listen', u8(`l`), '127.0.0.1:4000', 'HTTP server listen address:port') - fp.finalize() or { - eprintln(err) - println(fp.usage()) - exit(1) - } - mut cache := vmod.get_cache() - modfile := cache.get_by_folder(os.getwd()) - modname := vmod.from_file(modfile.vmod_file)!.name - docs_dir := os.join_path(os.temp_dir(), os.geteuid().str(), modname + '_docs_' + - rand.u16().hex()) - log.info('generating HTML docs in temporary dir ${docs_dir}') - os.mkdir_all(docs_dir, mode: 0o755)! - os.execute_or_exit('v doc -f html -o ${docs_dir} -m . ') - sigint_callback := fn [docs_dir] (_ os.Signal) { - os.rmdir_all(docs_dir) or { - log.error('unable to delete temporary dir ${docs_dir}') - exit(1) - } - eprintln('cleanup temporary data') - exit(0) - } - os.signal_opt(.int, sigint_callback)! - log.info('starting docs server, use ^C to quit') - file.serve(folder: docs_dir, index_file: modname + '.html', on: listen) -}