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

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

View File

@ -43,6 +43,19 @@ fn init_and_check() ? {
'*.vv linguist-language=V text=auto eol=lf',
'',
].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() ? {
@ -84,3 +97,33 @@ fn test_v_init_no_overwrite_gitignore() ? {
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
}