diff --git a/.gitignore b/.gitignore index 4bc9df70c3..cf118032f5 100644 --- a/.gitignore +++ b/.gitignore @@ -16,6 +16,8 @@ /tools/vup.exe /tools/vpm /tools/vpm.exe +/tools/vcreate +/tools/vcreate.exe *.exe *.o .*.c diff --git a/tools/vcreate.v b/tools/vcreate.v new file mode 100644 index 0000000000..7c5c345d20 --- /dev/null +++ b/tools/vcreate.v @@ -0,0 +1,55 @@ +// Copyright (c) 2019 Alexander Medvednikov. All rights reserved. +// Use of this source code is governed by an MIT license +// that can be found in the LICENSE file. + +module main + +import ( + os +) + +struct Create { +mut: + name string + description string +} + +fn cerror(e string){ + eprintln('\nerror: $e') +} + +fn (c Create)write_vmod() { + vmod := os.create('${c.name}/v.mod') or { cerror(err) exit(1) } + mut vmod_content := []string + vmod_content << '#V Project#\n' + vmod_content << 'Module {' + vmod_content << ' name: \'${c.name}\',' + vmod_content << ' description: \'${c.description}\',' + vmod_content << ' dependencies: []' + vmod_content << '}' + vmod.write(vmod_content.join('\n')) +} + +fn (c Create)write_main() { + main := os.create('${c.name}/${c.name}.v') or { cerror(err) exit(2) } + mut main_content := []string + main_content << 'module main\n' + main_content << 'fn main() {' + main_content << ' println(\'Hello World !\')' + main_content << '}' + main.write(main_content.join('\n')) +} + +fn main() { + mut c := Create{} + print('Choose your project name: ') + c.name = os.get_line() + print('Choose your project description: ') + c.description = os.get_line() + println('Initialising ...') + if (os.is_dir(c.name)) { cerror('folder already exists') exit(3) } + os.mkdir(c.name) + c.write_vmod() + c.write_main() + println('Complete !') +} diff --git a/v.v b/v.v index 5bc1985ece..bdf975d695 100755 --- a/v.v +++ b/v.v @@ -55,6 +55,10 @@ fn main() { compiler.create_symlink() return } + else if 'create' in commands { + compiler.launch_tool('vcreate') + return + } // TODO quit if the v compiler is too old // u := os.file_last_mod_unix('v') // If there's no tmp path with current version yet, the user must be using a pre-built package diff --git a/vlib/compiler/vhelp.v b/vlib/compiler/vhelp.v index 71fa2315e8..8da96f3b71 100644 --- a/vlib/compiler/vhelp.v +++ b/vlib/compiler/vhelp.v @@ -71,6 +71,7 @@ Commands: fmt Run vfmt to format the source code. [wip] doc Run vdoc over the source code and produce documentation. translate Translates C to V. [wip, will be available in V 0.3] + create Create a new v project interactively. Answer the questions, and run it with `v run projectname` V package management commands: search keywords Search the https://vpm.vlang.io/ module repository for matching modules and shows their details.