vcreate: various fixes and improvements (#5786)
parent
bf14e666ea
commit
10e0c39115
|
@ -1,13 +1,11 @@
|
||||||
module main
|
|
||||||
|
|
||||||
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
|
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
|
||||||
// Use of this source code is governed by an MIT license
|
// Use of this source code is governed by an MIT license that can be found in the LICENSE file.
|
||||||
// that can be found in the LICENSE file.
|
//
|
||||||
|
|
||||||
// This module follows a similar convention to Rust: `init` makes the
|
// This module follows a similar convention to Rust: `init` makes the
|
||||||
// structure of the program in the _current_ directory, while `new`
|
// structure of the program in the _current_ directory, while `new`
|
||||||
// makes the program structure in a _sub_ directory. Besides that, the
|
// makes the program structure in a _sub_ directory. Besides that, the
|
||||||
// functionality is essentially the same.
|
// functionality is essentially the same.
|
||||||
|
module main
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
|
||||||
|
@ -24,8 +22,9 @@ fn cerror(e string){
|
||||||
fn vmod_content(name, desc string) string {
|
fn vmod_content(name, desc string) string {
|
||||||
return [
|
return [
|
||||||
'Module {',
|
'Module {',
|
||||||
' name: \'${name}\',',
|
" name: '$name',",
|
||||||
' description: \'${desc}\',',
|
" description: '$desc',",
|
||||||
|
" version: '0.0.0',",
|
||||||
' dependencies: []',
|
' dependencies: []',
|
||||||
'}'
|
'}'
|
||||||
].join('\n')
|
].join('\n')
|
||||||
|
@ -35,7 +34,7 @@ fn main_content() string {
|
||||||
return [
|
return [
|
||||||
'module main\n',
|
'module main\n',
|
||||||
'fn main() {',
|
'fn main() {',
|
||||||
' println(\'Hello World !\')',
|
" println('Hello World!')",
|
||||||
'}'
|
'}'
|
||||||
].join('\n')
|
].join('\n')
|
||||||
}
|
}
|
||||||
|
@ -50,8 +49,9 @@ fn gen_gitignore(name string) string {
|
||||||
].join('\n')
|
].join('\n')
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (c &Create)create_vmod() {
|
fn (c &Create) write_vmod(new bool) {
|
||||||
mut vmod := os.create('${c.name}/v.mod') or {
|
vmod_path := if new { '$c.name/v.mod' } else { 'v.mod' }
|
||||||
|
mut vmod := os.create(vmod_path) or {
|
||||||
cerror(err)
|
cerror(err)
|
||||||
exit(1)
|
exit(1)
|
||||||
}
|
}
|
||||||
|
@ -59,8 +59,12 @@ fn (c &Create)create_vmod() {
|
||||||
vmod.close()
|
vmod.close()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (c &Create)create_main() {
|
fn (c &Create) write_main(new bool) {
|
||||||
mut main := os.create('${c.name}/${c.name}.v') or {
|
if !new && (os.exists('${c.name}.v') || os.exists('src/${c.name}.v')) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
main_path := if new { '$c.name/${c.name}.v' } else { '${c.name}.v' }
|
||||||
|
mut main := os.create(main_path) or {
|
||||||
cerror(err)
|
cerror(err)
|
||||||
exit(2)
|
exit(2)
|
||||||
}
|
}
|
||||||
|
@ -68,24 +72,15 @@ fn (c &Create)create_main() {
|
||||||
main.close()
|
main.close()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (c &Create)init_vmod() {
|
|
||||||
mut vmod := os.create('v.mod') or {
|
|
||||||
cerror(err)
|
|
||||||
exit(1)
|
|
||||||
}
|
|
||||||
vmod.write(vmod_content(c.name, c.description))
|
|
||||||
vmod.close()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn (c &Create) create_git_repo(dir string) {
|
fn (c &Create) create_git_repo(dir string) {
|
||||||
// Create Git Repo and .gitignore file
|
// Create Git Repo and .gitignore file
|
||||||
if !os.is_dir('${dir}/.git') {
|
if !os.is_dir('$dir/.git') {
|
||||||
os.exec('git init ${dir}') or {
|
os.exec('git init $dir') or {
|
||||||
cerror('Unable to create git repo')
|
cerror('Unable to create git repo')
|
||||||
exit(4)
|
exit(4)
|
||||||
}
|
}
|
||||||
if !os.exists('${dir}/.gitignore') {
|
if !os.exists('$dir/.gitignore') {
|
||||||
mut fl := os.create('${dir}/.gitignore') or {
|
mut fl := os.create('$dir/.gitignore') or {
|
||||||
// We don't really need a .gitignore, it's just a nice-to-have
|
// We don't really need a .gitignore, it's just a nice-to-have
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -95,45 +90,32 @@ fn (c &Create)create_git_repo(dir string) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (c &Create)init_main() {
|
|
||||||
// The file already exists, don't over-write anything.
|
|
||||||
// Searching in the 'src' directory allows flexibility user module structure
|
|
||||||
if os.exists('${c.name}.v') || os.exists('src/${c.name}.v') {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
mut main := os.create('${c.name}.v') or {
|
|
||||||
cerror(err)
|
|
||||||
exit(2)
|
|
||||||
}
|
|
||||||
main.write(main_content())
|
|
||||||
main.close()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn create() {
|
fn create() {
|
||||||
mut c := Create{}
|
mut c := Create{}
|
||||||
|
c.name = os.input('Input your project name: ')
|
||||||
print('Input your project name: ')
|
if c.name == '' {
|
||||||
c.name = os.get_line()
|
cerror('project name cannot be empty')
|
||||||
|
exit(1)
|
||||||
|
}
|
||||||
|
if c.name.contains('-') {
|
||||||
|
cerror('"$c.name" should not contain hyphens')
|
||||||
|
exit(1)
|
||||||
|
}
|
||||||
if os.is_dir(c.name) {
|
if os.is_dir(c.name) {
|
||||||
cerror('${c.name} folder already exists')
|
cerror('$c.name folder already exists')
|
||||||
exit(3)
|
exit(3)
|
||||||
}
|
}
|
||||||
|
c.description = os.input('Input your project description: ')
|
||||||
print('Input your project description: ')
|
|
||||||
c.description = os.get_line()
|
|
||||||
|
|
||||||
println('Initialising ...')
|
println('Initialising ...')
|
||||||
|
|
||||||
os.mkdir(c.name) or {
|
os.mkdir(c.name) or {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
c.create_vmod()
|
c.write_vmod(true)
|
||||||
c.create_main()
|
c.write_main(true)
|
||||||
c.create_git_repo(c.name)
|
c.create_git_repo(c.name)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn init() {
|
fn init_project() {
|
||||||
if os.exists('v.mod') {
|
if os.exists('v.mod') {
|
||||||
cerror('`v init` cannot be run on existing v modules')
|
cerror('`v init` cannot be run on existing v modules')
|
||||||
exit(3)
|
exit(3)
|
||||||
|
@ -141,17 +123,17 @@ fn init() {
|
||||||
mut c := Create{}
|
mut c := Create{}
|
||||||
c.name = os.file_name(os.getwd())
|
c.name = os.file_name(os.getwd())
|
||||||
c.description = ''
|
c.description = ''
|
||||||
c.init_vmod()
|
c.write_vmod(false)
|
||||||
c.init_main()
|
c.write_main(false)
|
||||||
c.create_git_repo('')
|
c.create_git_repo('')
|
||||||
println("Change your module's description in `v.mod`")
|
println("Change your module's description in `v.mod`")
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
if 'new' == os.args[1] {
|
if os.args[1] == 'new' {
|
||||||
create()
|
create()
|
||||||
} else if 'init' == os.args[1] {
|
} else if os.args[1] == 'init' {
|
||||||
init()
|
init_project()
|
||||||
} else {
|
} else {
|
||||||
cerror('Unknown command: ${os.args[1]}')
|
cerror('Unknown command: ${os.args[1]}')
|
||||||
exit(1)
|
exit(1)
|
||||||
|
|
Loading…
Reference in New Issue