tools: cleanup and simplify vcreate, for upcoming fixes and features (#19794)

This commit is contained in:
Turiiya 2023-11-07 13:10:34 +01:00 committed by GitHub
parent 50c22b5a12
commit f1127cd45c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 86 additions and 98 deletions

View file

@ -1,8 +1,8 @@
module main module main
fn (mut c Create) set_bin_project_files(new bool) { fn (mut c Create) set_bin_project_files() {
c.files << ProjectFiles{ c.files << ProjectFiles{
path: if new { '${c.name}/src/main.v' } else { 'src/main.v' } path: if c.new_dir { '${c.name}/src/main.v' } else { 'src/main.v' }
content: "module main content: "module main
fn main() { fn main() {

View file

@ -23,6 +23,7 @@ mut:
version string version string
license string license string
files []ProjectFiles files []ProjectFiles
new_dir bool
} }
struct ProjectFiles { struct ProjectFiles {
@ -51,7 +52,7 @@ fn main() {
new_project(os.args[2..]) new_project(os.args[2..])
} }
'init' { 'init' {
init_project() init_project(os.args[2..])
} }
else { else {
cerror('unknown command: ${cmd}') cerror('unknown command: ${cmd}')
@ -63,32 +64,15 @@ fn main() {
fn new_project(args []string) { fn new_project(args []string) {
mut c := Create{} mut c := Create{}
c.new_dir = true
c.name = check_name(if args.len > 0 { args[0] } else { os.input('Input your project name: ') }) c.prompt(args)
if c.name == '' {
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) {
cerror('${c.name} folder already exists')
exit(3)
}
c.prompt()
println('Initialising ...') println('Initialising ...')
if args.len == 2 { if args.len == 2 {
// E.g.: `v new my_project lib` // E.g.: `v new my_project lib`
match os.args.last() { match os.args.last() {
'bin' { 'bin' {
c.set_bin_project_files(true) c.set_bin_project_files()
} }
'lib' { 'lib' {
c.set_lib_project_files() c.set_lib_project_files()
@ -103,40 +87,55 @@ fn new_project(args []string) {
} }
} else { } else {
// E.g.: `v new my_project` // E.g.: `v new my_project`
c.set_bin_project_files(true) c.set_bin_project_files()
} }
// gen project based in the `Create.files` info // gen project based in the `Create.files` info
c.create_files_and_directories() c.create_files_and_directories()
c.write_vmod(true) c.write_vmod()
c.write_gitattributes(true) c.write_gitattributes()
c.write_editorconfig(true) c.write_editorconfig()
c.create_git_repo(c.name) c.create_git_repo(c.name)
} }
fn init_project() { fn init_project(args []string) {
mut c := Create{} mut c := Create{}
dir_name := check_name(os.file_name(os.getwd())) dir_name := check_name(os.file_name(os.getwd()))
if !os.exists('v.mod') { if !os.exists('v.mod') {
mod_dir_has_hyphens := dir_name.contains('-') mod_dir_has_hyphens := dir_name.contains('-')
c.name = if mod_dir_has_hyphens { dir_name.replace('-', '_') } else { dir_name } c.name = if mod_dir_has_hyphens { dir_name.replace('-', '_') } else { dir_name }
c.prompt() c.prompt(args)
c.write_vmod(false) c.write_vmod()
if mod_dir_has_hyphens { if mod_dir_has_hyphens {
println('The directory name `${dir_name}` is invalid as a module name. The module name in `v.mod` was set to `${c.name}`') println('The directory name `${dir_name}` is invalid as a module name. The module name in `v.mod` was set to `${c.name}`')
} }
} }
if !os.exists('src/main.v') { if !os.exists('src/main.v') {
c.set_bin_project_files(false) c.set_bin_project_files()
} }
c.create_files_and_directories() c.create_files_and_directories()
c.write_gitattributes(false) c.write_gitattributes()
c.write_editorconfig(false) c.write_editorconfig()
c.create_git_repo('.') c.create_git_repo('.')
} }
fn (mut c Create) prompt() { fn (mut c Create) prompt(args []string) {
if c.name == '' {
c.name = check_name(args[0] or { os.input('Input your project name: ') })
if c.name == '' {
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) {
cerror('${c.name} folder already exists')
exit(3)
}
}
c.description = os.input('Input your project description: ') c.description = os.input('Input your project description: ')
default_version := '0.0.0' default_version := '0.0.0'
c.version = os.input('Input your project version: (${default_version}) ') c.version = os.input('Input your project version: (${default_version}) ')
@ -175,8 +174,9 @@ fn check_name(name string) string {
return name return name
} }
fn vmod_content(c Create) string { fn (c &Create) write_vmod() {
return "Module { path := if c.new_dir { '${c.name}/v.mod' } else { 'v.mod' }
content := "Module {
name: '${c.name}' name: '${c.name}'
description: '${c.description}' description: '${c.description}'
version: '${c.version}' version: '${c.version}'
@ -184,12 +184,58 @@ fn vmod_content(c Create) string {
dependencies: [] dependencies: []
} }
" "
os.write_file(path, content) or { panic(err) }
} }
fn gen_gitignore(name string) string { fn (c &Create) write_gitattributes() {
return '# Binaries for programs and plugins path := if c.new_dir { '${c.name}/.gitattributes' } else { '.gitattributes' }
if !c.new_dir && os.exists(path) {
return
}
content := '* text=auto eol=lf
*.bat eol=crlf
**/*.v linguist-language=V
**/*.vv linguist-language=V
**/*.vsh linguist-language=V
**/v.mod linguist-language=V
'
os.write_file(path, content) or { panic(err) }
}
fn (c &Create) write_editorconfig() {
path := if c.new_dir { '${c.name}/.editorconfig' } else { '.editorconfig' }
if !c.new_dir && os.exists(path) {
return
}
content := '[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
[*.v]
indent_style = tab
'
os.write_file(path, content) or { panic(err) }
}
fn (c &Create) create_git_repo(dir string) {
// Create Git Repo and .gitignore file
if !os.is_dir('${dir}/.git') {
res := os.execute('git init ${dir}')
if res.exit_code != 0 {
cerror('Unable to create git repo')
exit(4)
}
}
ignore_path := '${dir}/.gitignore'
if os.exists(ignore_path) {
return
}
ignore_content := '# Binaries for programs and plugins
main main
${name} ${c.name}
*.exe *.exe
*.exe~ *.exe~
*.so *.so
@ -212,65 +258,7 @@ bin/
*.db *.db
*.js *.js
' '
} os.write_file(ignore_path, ignore_content) or {}
fn gitattributes_content() string {
return '* text=auto eol=lf
*.bat eol=crlf
**/*.v linguist-language=V
**/*.vv linguist-language=V
**/*.vsh linguist-language=V
**/v.mod linguist-language=V
'
}
fn editorconfig_content() string {
return '[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
[*.v]
indent_style = tab
'
}
fn (c &Create) write_vmod(new bool) {
vmod_path := if new { '${c.name}/v.mod' } else { 'v.mod' }
os.write_file(vmod_path, vmod_content(c)) or { panic(err) }
}
fn (c &Create) write_gitattributes(new bool) {
gitattributes_path := if new { '${c.name}/.gitattributes' } else { '.gitattributes' }
if !new && os.exists(gitattributes_path) {
return
}
os.write_file(gitattributes_path, gitattributes_content()) or { panic(err) }
}
fn (c &Create) write_editorconfig(new bool) {
editorconfig_path := if new { '${c.name}/.editorconfig' } else { '.editorconfig' }
if !new && os.exists(editorconfig_path) {
return
}
os.write_file(editorconfig_path, editorconfig_content()) or { panic(err) }
}
fn (c &Create) create_git_repo(dir string) {
// Create Git Repo and .gitignore file
if !os.is_dir('${dir}/.git') {
res := os.execute('git init ${dir}')
if res.exit_code != 0 {
cerror('Unable to create git repo')
exit(4)
}
}
gitignore_path := '${dir}/.gitignore'
if !os.exists(gitignore_path) {
os.write_file(gitignore_path, gen_gitignore(c.name)) or {}
}
} }
fn (mut c Create) create_files_and_directories() { fn (mut c Create) create_files_and_directories() {