cli: create project helper

pull/2728/head
Henrixounez 2019-11-10 22:48:56 +01:00 committed by Alexander Medvednikov
parent 703202cc08
commit 76c27c0b03
4 changed files with 62 additions and 0 deletions

2
.gitignore vendored
View File

@ -16,6 +16,8 @@
/tools/vup.exe /tools/vup.exe
/tools/vpm /tools/vpm
/tools/vpm.exe /tools/vpm.exe
/tools/vcreate
/tools/vcreate.exe
*.exe *.exe
*.o *.o
.*.c .*.c

55
tools/vcreate.v 100644
View File

@ -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 !')
}

4
v.v
View File

@ -55,6 +55,10 @@ fn main() {
compiler.create_symlink() compiler.create_symlink()
return return
} }
else if 'create' in commands {
compiler.launch_tool('vcreate')
return
}
// TODO quit if the v compiler is too old // TODO quit if the v compiler is too old
// u := os.file_last_mod_unix('v') // 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 // If there's no tmp path with current version yet, the user must be using a pre-built package

View File

@ -71,6 +71,7 @@ Commands:
fmt Run vfmt to format the source code. [wip] fmt Run vfmt to format the source code. [wip]
doc Run vdoc over the source code and produce documentation. doc Run vdoc over the source code and produce documentation.
translate Translates C to V. [wip, will be available in V 0.3] 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: V package management commands:
search keywords Search the https://vpm.vlang.io/ module repository for matching modules and shows their details. search keywords Search the https://vpm.vlang.io/ module repository for matching modules and shows their details.