v/cmd/tools/vcreate.v

74 lines
1.2 KiB
V
Raw Normal View History

2020-02-03 05:00:36 +01:00
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
2019-11-10 22:48:56 +01:00
// 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:
2020-02-13 19:27:13 +01:00
name string
2019-11-10 22:48:56 +01:00
description string
}
fn cerror(e string){
eprintln('\nerror: $e')
}
fn (c Create)write_vmod() {
2020-02-13 19:27:13 +01:00
mut vmod := os.create('${c.name}/v.mod') or {
cerror(err)
exit(1)
}
vmod_content := [
'#V Project#\n',
'Module {',
' name: \'${c.name}\',',
' description: \'${c.description}\',',
' dependencies: []',
'}'
]
2019-11-10 22:48:56 +01:00
vmod.write(vmod_content.join('\n'))
}
fn (c Create)write_main() {
2020-02-13 19:27:13 +01:00
mut main := os.create('${c.name}/${c.name}.v') or {
cerror(err)
exit(2)
}
main_content := [
'module main\n',
'fn main() {',
' println(\'Hello World !\')',
'}'
]
2019-11-10 22:48:56 +01:00
main.write(main_content.join('\n'))
}
fn main() {
mut c := Create{}
2020-02-13 19:27:13 +01:00
print('Input your project name: ')
2019-11-10 22:48:56 +01:00
c.name = os.get_line()
2020-02-13 19:27:13 +01:00
if (os.is_dir(c.name)) {
cerror('${c.name} folder already exists')
exit(3)
}
print('Input your project description: ')
2019-11-10 22:48:56 +01:00
c.description = os.get_line()
2020-02-13 19:27:13 +01:00
2019-11-10 22:48:56 +01:00
println('Initialising ...')
2020-02-13 19:27:13 +01:00
os.mkdir(c.name) or {
panic(err)
}
2019-11-10 22:48:56 +01:00
c.write_vmod()
c.write_main()
println('Complete !')
}