2020-07-18 09:41:06 +02:00
|
|
|
module main
|
|
|
|
|
2020-02-03 05:00:36 +01:00
|
|
|
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
|
2020-07-10 18:09:08 +02: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.
|
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
|
|
|
|
}
|
|
|
|
|
2020-07-10 18:09:08 +02:00
|
|
|
fn cerror(e string) {
|
2019-11-10 22:48:56 +01:00
|
|
|
eprintln('\nerror: $e')
|
|
|
|
}
|
|
|
|
|
2020-10-15 15:17:52 +02:00
|
|
|
fn vmod_content(name string, desc string) string {
|
2020-07-10 18:09:08 +02:00
|
|
|
return [
|
2020-02-13 19:27:13 +01:00
|
|
|
'Module {',
|
2020-08-19 16:07:10 +02:00
|
|
|
" name: '$name'",
|
|
|
|
" description: '$desc'",
|
|
|
|
" version: '0.0.0'",
|
2020-02-13 19:27:13 +01:00
|
|
|
' dependencies: []',
|
2020-08-19 16:07:10 +02:00
|
|
|
'}',
|
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() {',
|
2020-07-10 18:09:08 +02:00
|
|
|
" println('Hello World!')",
|
2020-08-19 16:07:10 +02:00
|
|
|
'}',
|
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 [
|
2020-10-09 15:56:43 +02:00
|
|
|
'# Binaries for programs and plugins',
|
2020-03-15 11:20:12 +01:00
|
|
|
'main',
|
|
|
|
'$name',
|
2020-10-09 15:56:43 +02:00
|
|
|
'*.exe',
|
|
|
|
'*.exe~',
|
2020-03-15 11:20:12 +01:00
|
|
|
'*.so',
|
2020-05-29 04:52:19 +02:00
|
|
|
'*.dylib',
|
2020-08-19 16:07:10 +02:00
|
|
|
'*.dll',
|
2020-03-15 11:20:12 +01:00
|
|
|
].join('\n')
|
|
|
|
}
|
|
|
|
|
2020-07-10 18:09:08 +02:00
|
|
|
fn (c &Create) write_vmod(new bool) {
|
|
|
|
vmod_path := if new { '$c.name/v.mod' } else { 'v.mod' }
|
|
|
|
mut vmod := os.create(vmod_path) or {
|
2020-03-15 11:20:12 +01:00
|
|
|
cerror(err)
|
|
|
|
exit(1)
|
|
|
|
}
|
2020-11-15 21:54:47 +01:00
|
|
|
vmod.write_str(vmod_content(c.name, c.description))
|
2020-03-15 11:20:12 +01:00
|
|
|
vmod.close()
|
|
|
|
}
|
|
|
|
|
2020-07-10 18:09:08 +02:00
|
|
|
fn (c &Create) write_main(new bool) {
|
|
|
|
if !new && (os.exists('${c.name}.v') || os.exists('src/${c.name}.v')) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
main_path := if new { '$c.name/${c.name}.v' } else { '${c.name}.v' }
|
|
|
|
mut main := os.create(main_path) or {
|
2020-03-15 11:20:12 +01:00
|
|
|
cerror(err)
|
|
|
|
exit(2)
|
|
|
|
}
|
2020-11-15 21:54:47 +01:00
|
|
|
main.write_str(main_content())
|
2020-03-15 11:20:12 +01:00
|
|
|
main.close()
|
|
|
|
}
|
|
|
|
|
2020-07-10 18:09:08 +02:00
|
|
|
fn (c &Create) create_git_repo(dir string) {
|
2020-03-15 11:20:12 +01:00
|
|
|
// Create Git Repo and .gitignore file
|
2020-07-10 18:09:08 +02:00
|
|
|
if !os.is_dir('$dir/.git') {
|
|
|
|
os.exec('git init $dir') or {
|
2020-03-15 11:20:12 +01:00
|
|
|
cerror('Unable to create git repo')
|
2020-04-07 19:37:15 +02:00
|
|
|
exit(4)
|
2020-03-15 11:20:12 +01:00
|
|
|
}
|
2020-07-10 18:09:08 +02:00
|
|
|
if !os.exists('$dir/.gitignore') {
|
|
|
|
mut fl := os.create('$dir/.gitignore') or {
|
2020-03-15 11:20:12 +01:00
|
|
|
// We don't really need a .gitignore, it's just a nice-to-have
|
|
|
|
return
|
|
|
|
}
|
2020-11-15 21:54:47 +01:00
|
|
|
fl.write_str(gen_gitignore(c.name))
|
2020-03-15 11:20:12 +01:00
|
|
|
fl.close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn create() {
|
2019-11-10 22:48:56 +01:00
|
|
|
mut c := Create{}
|
2020-07-10 18:09:08 +02:00
|
|
|
c.name = 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)
|
|
|
|
}
|
2020-03-15 11:20:12 +01:00
|
|
|
if os.is_dir(c.name) {
|
2020-07-10 18:09:08 +02:00
|
|
|
cerror('$c.name folder already exists')
|
2020-02-13 19:27:13 +01:00
|
|
|
exit(3)
|
|
|
|
}
|
2020-07-10 18:09:08 +02:00
|
|
|
c.description = os.input('Input your project description: ')
|
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-07-10 18:09:08 +02:00
|
|
|
c.write_vmod(true)
|
|
|
|
c.write_main(true)
|
2020-03-15 11:20:12 +01:00
|
|
|
c.create_git_repo(c.name)
|
|
|
|
}
|
|
|
|
|
2020-07-10 18:09:08 +02:00
|
|
|
fn init_project() {
|
2020-03-15 11:20:12 +01:00
|
|
|
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 = ''
|
2020-07-10 18:09:08 +02:00
|
|
|
c.write_vmod(false)
|
|
|
|
c.write_main(false)
|
2020-03-15 11:20:12 +01:00
|
|
|
c.create_git_repo('')
|
|
|
|
println("Change your module's description in `v.mod`")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2020-07-10 18:09:08 +02:00
|
|
|
if os.args[1] == 'new' {
|
2020-03-15 11:20:12 +01:00
|
|
|
create()
|
2020-07-10 18:09:08 +02:00
|
|
|
} else if os.args[1] == 'init' {
|
|
|
|
init_project()
|
2020-03-15 11:20:12 +01:00
|
|
|
} else {
|
|
|
|
cerror('Unknown command: ${os.args[1]}')
|
|
|
|
exit(1)
|
|
|
|
}
|
|
|
|
println('Complete!')
|
2019-11-10 22:48:56 +01:00
|
|
|
}
|