2022-01-04 10:21:08 +01:00
|
|
|
// Copyright (c) 2019-2022 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.
|
2021-03-13 07:43:12 +01:00
|
|
|
module main
|
|
|
|
|
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
|
2021-03-07 10:38:56 +01:00
|
|
|
version string
|
|
|
|
license string
|
2019-11-10 22:48:56 +01:00
|
|
|
}
|
|
|
|
|
2020-07-10 18:09:08 +02:00
|
|
|
fn cerror(e string) {
|
2019-11-10 22:48:56 +01:00
|
|
|
eprintln('\nerror: $e')
|
|
|
|
}
|
|
|
|
|
2021-03-12 16:09:43 +01:00
|
|
|
fn check_name(name string) string {
|
2021-05-24 14:17:57 +02:00
|
|
|
if name.trim_space().len == 0 {
|
|
|
|
cerror('project name cannot be empty')
|
|
|
|
exit(1)
|
|
|
|
}
|
2021-03-13 07:43:12 +01:00
|
|
|
if name.is_title() {
|
|
|
|
mut cname := name.to_lower()
|
|
|
|
if cname.contains(' ') {
|
|
|
|
cname = cname.replace(' ', '_')
|
|
|
|
}
|
|
|
|
eprintln('warning: the project name cannot be capitalized, the name will be changed to `$cname`')
|
|
|
|
return cname
|
|
|
|
}
|
2021-03-12 16:09:43 +01:00
|
|
|
if name.contains(' ') {
|
|
|
|
cname := name.replace(' ', '_')
|
|
|
|
eprintln('warning: the project name cannot contain spaces, the name will be changed to `$cname`')
|
|
|
|
return cname
|
|
|
|
}
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
|
2021-03-07 10:38:56 +01:00
|
|
|
fn vmod_content(c Create) string {
|
2020-07-10 18:09:08 +02:00
|
|
|
return [
|
2020-02-13 19:27:13 +01:00
|
|
|
'Module {',
|
2021-03-07 10:38:56 +01:00
|
|
|
" name: '$c.name'",
|
|
|
|
" description: '$c.description'",
|
|
|
|
" version: '$c.version'",
|
|
|
|
" license: '$c.license'",
|
2020-02-13 19:27:13 +01:00
|
|
|
' dependencies: []',
|
2020-08-19 16:07:10 +02:00
|
|
|
'}',
|
2021-06-16 18:57:51 +02:00
|
|
|
'',
|
2021-12-17 15:11:19 +01:00
|
|
|
].join_lines()
|
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
|
|
|
'}',
|
2021-06-16 18:57:51 +02:00
|
|
|
'',
|
2021-12-17 15:11:19 +01:00
|
|
|
].join_lines()
|
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',
|
2021-11-19 18:23:35 +01:00
|
|
|
'vls.log',
|
2021-06-16 18:57:51 +02:00
|
|
|
'',
|
2021-12-17 15:11:19 +01:00
|
|
|
].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()
|
2020-03-15 11:20:12 +01:00
|
|
|
}
|
|
|
|
|
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' }
|
2021-12-17 15:11:19 +01:00
|
|
|
os.write_file(vmod_path, vmod_content(c)) or { panic(err) }
|
2020-03-15 11:20:12 +01:00
|
|
|
}
|
|
|
|
|
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' }
|
2021-12-17 15:11:19 +01:00
|
|
|
os.write_file(main_path, main_content()) or { panic(err) }
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (c &Create) write_gitattributes(new bool) {
|
|
|
|
gitattributes_path := if new { '$c.name/.gitattributes' } else { '.gitattributes' }
|
|
|
|
os.write_file(gitattributes_path, gitattributes_content()) or { panic(err) }
|
2020-03-15 11:20:12 +01:00
|
|
|
}
|
|
|
|
|
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') {
|
2021-03-08 19:52:13 +01:00
|
|
|
res := os.execute('git init $dir')
|
|
|
|
if res.exit_code != 0 {
|
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
|
|
|
}
|
2021-06-19 20:36:12 +02:00
|
|
|
}
|
2021-12-17 15:11:19 +01:00
|
|
|
gitignore_path := '$dir/.gitignore'
|
|
|
|
if !os.exists(gitignore_path) {
|
|
|
|
os.write_file(gitignore_path, gen_gitignore(c.name)) or {}
|
2020-03-15 11:20:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-06 20:03:20 +01:00
|
|
|
fn create(args []string) {
|
2019-11-10 22:48:56 +01:00
|
|
|
mut c := Create{}
|
2021-03-12 16:09:43 +01:00
|
|
|
c.name = check_name(if args.len > 0 { args[0] } else { os.input('Input your project name: ') })
|
2020-07-10 18:09:08 +02:00
|
|
|
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)
|
|
|
|
}
|
2021-03-06 20:03:20 +01:00
|
|
|
c.description = if args.len > 1 { args[1] } else { os.input('Input your project description: ') }
|
2021-03-07 10:38:56 +01:00
|
|
|
default_version := '0.0.0'
|
|
|
|
c.version = os.input('Input your project version: ($default_version) ')
|
|
|
|
if c.version == '' {
|
|
|
|
c.version = default_version
|
|
|
|
}
|
|
|
|
default_license := 'MIT'
|
|
|
|
c.license = os.input('Input your project license: ($default_license) ')
|
|
|
|
if c.license == '' {
|
|
|
|
c.license = default_license
|
|
|
|
}
|
2019-11-10 22:48:56 +01:00
|
|
|
println('Initialising ...')
|
2021-03-01 00:18:14 +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)
|
2021-12-17 15:11:19 +01:00
|
|
|
c.write_gitattributes(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{}
|
2021-03-12 16:09:43 +01:00
|
|
|
c.name = check_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)
|
2021-12-17 15:11:19 +01:00
|
|
|
c.write_gitattributes(false)
|
2021-06-16 18:57:51 +02:00
|
|
|
c.create_git_repo('.')
|
2021-05-24 14:17:57 +02:00
|
|
|
|
2021-06-24 17:31:16 +02:00
|
|
|
println('Change the description of your project in `v.mod`')
|
2020-03-15 11:20:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2021-03-13 07:43:12 +01:00
|
|
|
cmd := os.args[1]
|
|
|
|
match cmd {
|
|
|
|
'new' {
|
|
|
|
create(os.args[2..])
|
|
|
|
}
|
|
|
|
'init' {
|
|
|
|
init_project()
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
cerror('unknown command: $cmd')
|
|
|
|
exit(1)
|
|
|
|
}
|
2020-03-15 11:20:12 +01:00
|
|
|
}
|
|
|
|
println('Complete!')
|
2019-11-10 22:48:56 +01:00
|
|
|
}
|