tools: add `.gitattributes` with `v init` and `v new`(#12879)

pull/12885/head
Subhomoy Haldar 2021-12-17 19:41:19 +05:30 committed by GitHub
parent 130d189fce
commit 4ecdb28f5a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 25 deletions

View File

@ -51,7 +51,7 @@ fn vmod_content(c Create) string {
' dependencies: []', ' dependencies: []',
'}', '}',
'', '',
].join('\n') ].join_lines()
} }
fn main_content() string { fn main_content() string {
@ -61,7 +61,7 @@ fn main_content() string {
" println('Hello World!')", " println('Hello World!')",
'}', '}',
'', '',
].join('\n') ].join_lines()
} }
fn gen_gitignore(name string) string { fn gen_gitignore(name string) string {
@ -76,17 +76,20 @@ fn gen_gitignore(name string) string {
'*.dll', '*.dll',
'vls.log', 'vls.log',
'', '',
].join('\n') ].join_lines()
}
fn gitattributes_content() string {
return [
'*.v linguist-language=V text=auto eol=lf',
'*.vv linguist-language=V text=auto eol=lf',
'',
].join_lines()
} }
fn (c &Create) write_vmod(new bool) { fn (c &Create) write_vmod(new bool) {
vmod_path := if new { '$c.name/v.mod' } else { 'v.mod' } vmod_path := if new { '$c.name/v.mod' } else { 'v.mod' }
mut vmod := os.create(vmod_path) or { os.write_file(vmod_path, vmod_content(c)) or { panic(err) }
cerror(err.msg)
exit(1)
}
vmod.write_string(vmod_content(c)) or { panic(err) }
vmod.close()
} }
fn (c &Create) write_main(new bool) { fn (c &Create) write_main(new bool) {
@ -94,12 +97,12 @@ fn (c &Create) write_main(new bool) {
return return
} }
main_path := if new { '$c.name/${c.name}.v' } else { '${c.name}.v' } main_path := if new { '$c.name/${c.name}.v' } else { '${c.name}.v' }
mut mainfile := os.create(main_path) or { os.write_file(main_path, main_content()) or { panic(err) }
cerror(err.msg) }
exit(2)
} fn (c &Create) write_gitattributes(new bool) {
mainfile.write_string(main_content()) or { panic(err) } gitattributes_path := if new { '$c.name/.gitattributes' } else { '.gitattributes' }
mainfile.close() os.write_file(gitattributes_path, gitattributes_content()) or { panic(err) }
} }
fn (c &Create) create_git_repo(dir string) { fn (c &Create) create_git_repo(dir string) {
@ -111,13 +114,9 @@ fn (c &Create) create_git_repo(dir string) {
exit(4) exit(4)
} }
} }
if !os.exists('$dir/.gitignore') { gitignore_path := '$dir/.gitignore'
mut fl := os.create('$dir/.gitignore') or { if !os.exists(gitignore_path) {
// We don't really need a .gitignore, it's just a nice-to-have os.write_file(gitignore_path, gen_gitignore(c.name)) or {}
return
}
fl.write_string(gen_gitignore(c.name)) or { panic(err) }
fl.close()
} }
} }
@ -151,6 +150,7 @@ fn create(args []string) {
os.mkdir(c.name) or { panic(err) } os.mkdir(c.name) or { panic(err) }
c.write_vmod(true) c.write_vmod(true)
c.write_main(true) c.write_main(true)
c.write_gitattributes(true)
c.create_git_repo(c.name) c.create_git_repo(c.name)
} }
@ -164,6 +164,7 @@ fn init_project() {
c.description = '' c.description = ''
c.write_vmod(false) c.write_vmod(false)
c.write_main(false) c.write_main(false)
c.write_gitattributes(false)
c.create_git_repo('.') c.create_git_repo('.')
println('Change the description of your project in `v.mod`') println('Change the description of your project in `v.mod`')

View File

@ -12,7 +12,7 @@ fn init_and_check() ? {
" println('Hello World!')", " println('Hello World!')",
'}', '}',
'', '',
].join('\n') ].join_lines()
assert os.read_file('v.mod') ? == [ assert os.read_file('v.mod') ? == [
'Module {', 'Module {',
@ -23,7 +23,7 @@ fn init_and_check() ? {
' dependencies: []', ' dependencies: []',
'}', '}',
'', '',
].join('\n') ].join_lines()
assert os.read_file('.gitignore') ? == [ assert os.read_file('.gitignore') ? == [
'# Binaries for programs and plugins', '# Binaries for programs and plugins',
@ -36,7 +36,13 @@ fn init_and_check() ? {
'*.dll', '*.dll',
'vls.log', 'vls.log',
'', '',
].join('\n') ].join_lines()
assert os.read_file('.gitattributes') ? == [
'*.v linguist-language=V text=auto eol=lf',
'*.vv linguist-language=V text=auto eol=lf',
'',
].join_lines()
} }
fn test_v_init() ? { fn test_v_init() ? {