1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

cmd/tools: add support for .editorconfig and improve v init to initialize missing files (#13230)

This commit is contained in:
Subhomoy Haldar 2022-01-20 16:31:30 +05:30 committed by GitHub
parent 09797e493e
commit 5143837d66
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 101 additions and 42 deletions

View File

@ -42,49 +42,55 @@ fn check_name(name string) string {
} }
fn vmod_content(c Create) string { fn vmod_content(c Create) string {
return [ return "Module {
'Module {', name: '$c.name'
" name: '$c.name'", description: '$c.description'
" description: '$c.description'", version: '$c.version'
" version: '$c.version'", license: '$c.license'
" license: '$c.license'", dependencies: []
' dependencies: []', }
'}', "
'',
].join_lines()
} }
fn main_content() string { fn main_content() string {
return [ return "module main
'module main\n',
'fn main() {', fn main() {
" println('Hello World!')", println('Hello World!')
'}', }
'', "
].join_lines()
} }
fn gen_gitignore(name string) string { fn gen_gitignore(name string) string {
return [ return '# Binaries for programs and plugins
'# Binaries for programs and plugins', main
'main', $name
'$name', *.exe
'*.exe', *.exe~
'*.exe~', *.so
'*.so', *.dylib
'*.dylib', *.dll
'*.dll', vls.log
'vls.log', '
'',
].join_lines()
} }
fn gitattributes_content() string { fn gitattributes_content() string {
return [ return '*.v linguist-language=V text=auto eol=lf
'*.v linguist-language=V text=auto eol=lf', *.vv linguist-language=V text=auto eol=lf
'*.vv linguist-language=V text=auto eol=lf', '
'', }
].join_lines()
fn editorconfig_content() string {
return '[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
[*.v]
indent_style = tab
indent_size = 4
'
} }
fn (c &Create) write_vmod(new bool) { fn (c &Create) write_vmod(new bool) {
@ -102,9 +108,20 @@ fn (c &Create) write_main(new bool) {
fn (c &Create) write_gitattributes(new bool) { fn (c &Create) write_gitattributes(new bool) {
gitattributes_path := if new { '$c.name/.gitattributes' } else { '.gitattributes' } 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) } 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) { 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') {
@ -151,23 +168,22 @@ fn create(args []string) {
c.write_vmod(true) c.write_vmod(true)
c.write_main(true) c.write_main(true)
c.write_gitattributes(true) c.write_gitattributes(true)
c.write_editorconfig(true)
c.create_git_repo(c.name) c.create_git_repo(c.name)
} }
fn init_project() { fn init_project() {
if os.exists('v.mod') {
cerror('`v init` cannot be run on existing v modules')
exit(3)
}
mut c := Create{} mut c := Create{}
c.name = check_name(os.file_name(os.getwd())) c.name = check_name(os.file_name(os.getwd()))
if !os.exists('v.mod') {
c.description = '' c.description = ''
c.write_vmod(false) c.write_vmod(false)
println('Change the description of your project in `v.mod`')
}
c.write_main(false) c.write_main(false)
c.write_gitattributes(false) c.write_gitattributes(false)
c.write_editorconfig(false)
c.create_git_repo('.') c.create_git_repo('.')
println('Change the description of your project in `v.mod`')
} }
fn main() { fn main() {

View File

@ -43,6 +43,19 @@ fn init_and_check() ? {
'*.vv linguist-language=V text=auto eol=lf', '*.vv linguist-language=V text=auto eol=lf',
'', '',
].join_lines() ].join_lines()
assert os.read_file('.editorconfig') ? == [
'[*]',
'charset = utf-8',
'end_of_line = lf',
'insert_final_newline = true',
'trim_trailing_whitespace = true',
'',
'[*.v]',
'indent_style = tab',
'indent_size = 4',
'',
].join_lines()
} }
fn test_v_init() ? { fn test_v_init() ? {
@ -84,3 +97,33 @@ fn test_v_init_no_overwrite_gitignore() ? {
assert os.read_file('.gitignore') ? == 'blah' assert os.read_file('.gitignore') ? == 'blah'
} }
fn test_v_init_no_overwrite_gitattributes_and_editorconfig() ? {
git_attributes_content := '*.v linguist-language=V text=auto eol=lf'
editor_config_content := '[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
[*.v]
indent_style = tab
indent_size = 4
'
dir := os.join_path(os.temp_dir(), test_path)
os.rmdir_all(dir) or {}
os.mkdir(dir) or {}
os.write_file('$dir/.gitattributes', git_attributes_content) ?
os.write_file('$dir/.editorconfig', editor_config_content) ?
defer {
os.rmdir_all(dir) or {}
}
os.chdir(dir) ?
vexe := @VEXE
os.execute_or_exit('$vexe init')
assert os.read_file('.gitattributes') ? == git_attributes_content
assert os.read_file('.editorconfig') ? == editor_config_content
}