2020-07-08 13:29:55 +02:00
|
|
|
module main
|
|
|
|
|
2020-02-03 05:00:36 +01:00
|
|
|
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
|
2019-11-10 22:48:56 +01:00
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
|
2020-03-15 11:20:12 +01:00
|
|
|
// This module follows a similar convention to Rust: `init` makes the
|
2020-04-07 19:37:15 +02:00
|
|
|
// structure of the program in the _current_ directory, while `new`
|
2020-03-15 11:20:12 +01:00
|
|
|
// makes the program structure in a _sub_ directory. Besides that, the
|
|
|
|
// functionality is essentially the same.
|
2019-11-10 22:48:56 +01:00
|
|
|
|
2020-04-26 08:32:05 +02:00
|
|
|
import os
|
2019-11-10 22:48:56 +01:00
|
|
|
|
|
|
|
struct Create {
|
|
|
|
mut:
|
2020-02-13 19:27:13 +01:00
|
|
|
name string
|
2019-11-10 22:48:56 +01:00
|
|
|
description string
|
|
|
|
}
|
|
|
|
|
|
|
|
fn cerror(e string){
|
|
|
|
eprintln('\nerror: $e')
|
|
|
|
}
|
|
|
|
|
2020-03-15 11:20:12 +01:00
|
|
|
fn vmod_content(name, desc string) string {
|
|
|
|
return [
|
2020-02-13 19:27:13 +01:00
|
|
|
'Module {',
|
2020-03-15 11:20:12 +01:00
|
|
|
' name: \'${name}\',',
|
|
|
|
' description: \'${desc}\',',
|
2020-02-13 19:27:13 +01:00
|
|
|
' dependencies: []',
|
|
|
|
'}'
|
2020-03-15 11:20:12 +01:00
|
|
|
].join('\n')
|
2019-11-10 22:48:56 +01:00
|
|
|
}
|
|
|
|
|
2020-03-15 11:20:12 +01:00
|
|
|
fn main_content() string {
|
|
|
|
return [
|
2020-02-13 19:27:13 +01:00
|
|
|
'module main\n',
|
|
|
|
'fn main() {',
|
|
|
|
' println(\'Hello World !\')',
|
|
|
|
'}'
|
2020-03-15 11:20:12 +01:00
|
|
|
].join('\n')
|
2019-11-10 22:48:56 +01:00
|
|
|
}
|
|
|
|
|
2020-03-15 11:20:12 +01:00
|
|
|
fn gen_gitignore(name string) string {
|
|
|
|
return [
|
|
|
|
'main',
|
|
|
|
'$name',
|
|
|
|
'*.so',
|
2020-05-29 04:52:19 +02:00
|
|
|
'*.dylib',
|
2020-03-15 11:20:12 +01:00
|
|
|
'*.dll'
|
|
|
|
].join('\n')
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (c &Create)create_vmod() {
|
|
|
|
mut vmod := os.create('${c.name}/v.mod') or {
|
|
|
|
cerror(err)
|
|
|
|
exit(1)
|
|
|
|
}
|
|
|
|
vmod.write(vmod_content(c.name, c.description))
|
|
|
|
vmod.close()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (c &Create)create_main() {
|
|
|
|
mut main := os.create('${c.name}/${c.name}.v') or {
|
|
|
|
cerror(err)
|
|
|
|
exit(2)
|
|
|
|
}
|
|
|
|
main.write(main_content())
|
|
|
|
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) {
|
|
|
|
// Create Git Repo and .gitignore file
|
|
|
|
if !os.is_dir('${dir}/.git') {
|
|
|
|
os.exec('git init ${dir}') or {
|
|
|
|
cerror('Unable to create git repo')
|
2020-04-07 19:37:15 +02:00
|
|
|
exit(4)
|
2020-03-15 11:20:12 +01:00
|
|
|
}
|
|
|
|
if !os.exists('${dir}/.gitignore') {
|
|
|
|
mut fl := os.create('${dir}/.gitignore') or {
|
|
|
|
// We don't really need a .gitignore, it's just a nice-to-have
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fl.write(gen_gitignore(c.name))
|
|
|
|
fl.close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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() {
|
2019-11-10 22:48:56 +01:00
|
|
|
mut c := Create{}
|
2020-02-13 19:27:13 +01:00
|
|
|
|
|
|
|
print('Input your project name: ')
|
2019-11-10 22:48:56 +01:00
|
|
|
c.name = os.get_line()
|
2020-03-15 11:20:12 +01:00
|
|
|
|
|
|
|
if os.is_dir(c.name) {
|
2020-02-13 19:27:13 +01:00
|
|
|
cerror('${c.name} folder already exists')
|
|
|
|
exit(3)
|
|
|
|
}
|
|
|
|
|
|
|
|
print('Input your project description: ')
|
2019-11-10 22:48:56 +01:00
|
|
|
c.description = os.get_line()
|
2020-02-13 19:27:13 +01:00
|
|
|
|
2019-11-10 22:48:56 +01:00
|
|
|
println('Initialising ...')
|
2020-02-13 19:27:13 +01:00
|
|
|
|
|
|
|
os.mkdir(c.name) or {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-03-15 11:20:12 +01:00
|
|
|
c.create_vmod()
|
|
|
|
c.create_main()
|
|
|
|
c.create_git_repo(c.name)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn init() {
|
|
|
|
if os.exists('v.mod') {
|
|
|
|
cerror('`v init` cannot be run on existing v modules')
|
|
|
|
exit(3)
|
|
|
|
}
|
|
|
|
mut c := Create{}
|
2020-03-19 15:49:07 +01:00
|
|
|
c.name = os.file_name(os.getwd())
|
2020-03-15 11:20:12 +01:00
|
|
|
c.description = ''
|
|
|
|
c.init_vmod()
|
|
|
|
c.init_main()
|
|
|
|
c.create_git_repo('')
|
|
|
|
println("Change your module's description in `v.mod`")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2020-04-07 19:37:15 +02:00
|
|
|
if 'new' == os.args[1] {
|
2020-03-15 11:20:12 +01:00
|
|
|
create()
|
|
|
|
} else if 'init' == os.args[1] {
|
|
|
|
init()
|
|
|
|
} else {
|
|
|
|
cerror('Unknown command: ${os.args[1]}')
|
|
|
|
exit(1)
|
|
|
|
}
|
|
|
|
println('Complete!')
|
2019-11-10 22:48:56 +01:00
|
|
|
}
|