2022-01-04 12:21:08 +03:00
|
|
|
// Copyright (c) 2019-2022 Alexander Medvednikov. All rights reserved.
|
2020-07-10 19:09:08 +03:00
|
|
|
// Use of this source code is governed by an MIT license that can be found in the LICENSE file.
|
2021-03-13 09:43:12 +03:00
|
|
|
module main
|
|
|
|
|
2022-12-21 22:13:06 +03:00
|
|
|
import os
|
|
|
|
|
|
|
|
// Note: this program follows a similar convention to Rust: `init` makes the
|
2020-04-07 20:37:15 +03:00
|
|
|
// structure of the program in the _current_ directory, while `new`
|
2020-03-15 13:20:12 +03:00
|
|
|
// makes the program structure in a _sub_ directory. Besides that, the
|
|
|
|
// functionality is essentially the same.
|
2022-12-21 22:13:06 +03:00
|
|
|
|
|
|
|
// Note: here are the currently supported invokations so far:
|
|
|
|
// 1) `v init` -> create a new project in the current folder
|
|
|
|
// 2) `v new abc` -> create a new project in the new folder `abc`, by default a "hello world" project.
|
|
|
|
// 3) `v new abcd web` -> create a new project in the new folder `abcd`, using the vweb template.
|
|
|
|
// 4) `v new abcde gg` -> create a new project in the new folder `abcde`, using the gg template.
|
|
|
|
|
|
|
|
// Note: run `v cmd/tools/vcreate_test.v` after changes to this program, to avoid regressions.
|
2019-11-11 00:48:56 +03:00
|
|
|
|
|
|
|
struct Create {
|
|
|
|
mut:
|
2020-02-13 21:27:13 +03:00
|
|
|
name string
|
2019-11-11 00:48:56 +03:00
|
|
|
description string
|
2021-03-07 12:38:56 +03:00
|
|
|
version string
|
|
|
|
license string
|
2019-11-11 00:48:56 +03:00
|
|
|
}
|
|
|
|
|
2020-07-10 19:09:08 +03:00
|
|
|
fn cerror(e string) {
|
2022-11-15 16:53:13 +03:00
|
|
|
eprintln('\nerror: ${e}')
|
2019-11-11 00:48:56 +03:00
|
|
|
}
|
|
|
|
|
2021-03-12 18:09:43 +03:00
|
|
|
fn check_name(name string) string {
|
2021-05-24 15:17:57 +03:00
|
|
|
if name.trim_space().len == 0 {
|
|
|
|
cerror('project name cannot be empty')
|
|
|
|
exit(1)
|
|
|
|
}
|
2021-03-13 09:43:12 +03:00
|
|
|
if name.is_title() {
|
|
|
|
mut cname := name.to_lower()
|
|
|
|
if cname.contains(' ') {
|
|
|
|
cname = cname.replace(' ', '_')
|
|
|
|
}
|
2022-11-15 16:53:13 +03:00
|
|
|
eprintln('warning: the project name cannot be capitalized, the name will be changed to `${cname}`')
|
2021-03-13 09:43:12 +03:00
|
|
|
return cname
|
|
|
|
}
|
2021-03-12 18:09:43 +03:00
|
|
|
if name.contains(' ') {
|
|
|
|
cname := name.replace(' ', '_')
|
2022-11-15 16:53:13 +03:00
|
|
|
eprintln('warning: the project name cannot contain spaces, the name will be changed to `${cname}`')
|
2021-03-12 18:09:43 +03:00
|
|
|
return cname
|
|
|
|
}
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
|
2021-03-07 12:38:56 +03:00
|
|
|
fn vmod_content(c Create) string {
|
2022-01-20 14:01:30 +03:00
|
|
|
return "Module {
|
2022-11-15 16:53:13 +03:00
|
|
|
name: '${c.name}'
|
|
|
|
description: '${c.description}'
|
|
|
|
version: '${c.version}'
|
|
|
|
license: '${c.license}'
|
2022-01-20 14:01:30 +03:00
|
|
|
dependencies: []
|
|
|
|
}
|
|
|
|
"
|
2019-11-11 00:48:56 +03:00
|
|
|
}
|
|
|
|
|
2022-12-21 19:33:19 +03:00
|
|
|
fn new_project_content() string {
|
2022-12-21 22:13:06 +03:00
|
|
|
if os.args.len == 2 && os.args[1] == 'init' {
|
|
|
|
return main_content()
|
|
|
|
}
|
2022-12-21 19:33:19 +03:00
|
|
|
if os.args.len == 3 {
|
|
|
|
return main_content()
|
2022-12-21 22:13:06 +03:00
|
|
|
}
|
|
|
|
if os.args.len == 4 {
|
2022-12-21 19:33:19 +03:00
|
|
|
kind := os.args.last()
|
|
|
|
return match kind {
|
|
|
|
'web' {
|
|
|
|
simple_web_app()
|
|
|
|
}
|
|
|
|
'gg' {
|
|
|
|
main_content() // TODO
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
''
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
|
2020-03-15 13:20:12 +03:00
|
|
|
fn main_content() string {
|
2022-01-20 14:01:30 +03:00
|
|
|
return "module main
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
println('Hello World!')
|
|
|
|
}
|
|
|
|
"
|
2019-11-11 00:48:56 +03:00
|
|
|
}
|
|
|
|
|
2020-03-15 13:20:12 +03:00
|
|
|
fn gen_gitignore(name string) string {
|
2022-01-20 14:01:30 +03:00
|
|
|
return '# Binaries for programs and plugins
|
|
|
|
main
|
2022-11-15 16:53:13 +03:00
|
|
|
${name}
|
2022-01-20 14:01:30 +03:00
|
|
|
*.exe
|
|
|
|
*.exe~
|
|
|
|
*.so
|
|
|
|
*.dylib
|
|
|
|
*.dll
|
2022-11-02 21:41:12 +03:00
|
|
|
|
2022-11-20 15:43:33 +03:00
|
|
|
# Ignore binary output folders
|
|
|
|
bin/
|
|
|
|
|
2022-11-02 21:41:12 +03:00
|
|
|
# Ignore common editor/system specific metadata
|
|
|
|
.DS_Store
|
|
|
|
.idea/
|
|
|
|
.vscode/
|
|
|
|
*.iml
|
2022-01-20 14:01:30 +03:00
|
|
|
'
|
2021-12-17 17:11:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn gitattributes_content() string {
|
2022-11-02 21:41:12 +03:00
|
|
|
return '* text=auto eol=lf
|
|
|
|
*.bat eol=crlf
|
|
|
|
|
|
|
|
**/*.v linguist-language=V
|
|
|
|
**/*.vv linguist-language=V
|
|
|
|
**/*.vsh linguist-language=V
|
|
|
|
**/v.mod linguist-language=V
|
2022-01-20 14:01:30 +03:00
|
|
|
'
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
'
|
2020-03-15 13:20:12 +03:00
|
|
|
}
|
|
|
|
|
2020-07-10 19:09:08 +03:00
|
|
|
fn (c &Create) write_vmod(new bool) {
|
2022-11-15 16:53:13 +03:00
|
|
|
vmod_path := if new { '${c.name}/v.mod' } else { 'v.mod' }
|
2021-12-17 17:11:19 +03:00
|
|
|
os.write_file(vmod_path, vmod_content(c)) or { panic(err) }
|
2020-03-15 13:20:12 +03:00
|
|
|
}
|
|
|
|
|
2020-07-10 19:09:08 +03:00
|
|
|
fn (c &Create) write_main(new bool) {
|
|
|
|
if !new && (os.exists('${c.name}.v') || os.exists('src/${c.name}.v')) {
|
|
|
|
return
|
|
|
|
}
|
2022-11-15 16:53:13 +03:00
|
|
|
main_path := if new { '${c.name}/${c.name}.v' } else { '${c.name}.v' }
|
2022-12-21 19:33:19 +03:00
|
|
|
os.write_file(main_path, new_project_content()) or { panic(err) }
|
2021-12-17 17:11:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn (c &Create) write_gitattributes(new bool) {
|
2022-11-15 16:53:13 +03:00
|
|
|
gitattributes_path := if new { '${c.name}/.gitattributes' } else { '.gitattributes' }
|
2022-01-20 14:01:30 +03:00
|
|
|
if !new && os.exists(gitattributes_path) {
|
|
|
|
return
|
|
|
|
}
|
2021-12-17 17:11:19 +03:00
|
|
|
os.write_file(gitattributes_path, gitattributes_content()) or { panic(err) }
|
2020-03-15 13:20:12 +03:00
|
|
|
}
|
|
|
|
|
2022-01-20 14:01:30 +03:00
|
|
|
fn (c &Create) write_editorconfig(new bool) {
|
2022-11-15 16:53:13 +03:00
|
|
|
editorconfig_path := if new { '${c.name}/.editorconfig' } else { '.editorconfig' }
|
2022-01-20 14:01:30 +03:00
|
|
|
if !new && os.exists(editorconfig_path) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
os.write_file(editorconfig_path, editorconfig_content()) or { panic(err) }
|
|
|
|
}
|
|
|
|
|
2020-07-10 19:09:08 +03:00
|
|
|
fn (c &Create) create_git_repo(dir string) {
|
2020-03-15 13:20:12 +03:00
|
|
|
// Create Git Repo and .gitignore file
|
2022-11-15 16:53:13 +03:00
|
|
|
if !os.is_dir('${dir}/.git') {
|
|
|
|
res := os.execute('git init ${dir}')
|
2021-03-08 21:52:13 +03:00
|
|
|
if res.exit_code != 0 {
|
2020-03-15 13:20:12 +03:00
|
|
|
cerror('Unable to create git repo')
|
2020-04-07 20:37:15 +03:00
|
|
|
exit(4)
|
2020-03-15 13:20:12 +03:00
|
|
|
}
|
2021-06-19 21:36:12 +03:00
|
|
|
}
|
2022-11-15 16:53:13 +03:00
|
|
|
gitignore_path := '${dir}/.gitignore'
|
2021-12-17 17:11:19 +03:00
|
|
|
if !os.exists(gitignore_path) {
|
|
|
|
os.write_file(gitignore_path, gen_gitignore(c.name)) or {}
|
2020-03-15 13:20:12 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-06 22:03:20 +03:00
|
|
|
fn create(args []string) {
|
2022-12-21 19:33:19 +03:00
|
|
|
if os.args.len == 4 {
|
|
|
|
template := os.args.last()
|
|
|
|
if template !in ['web', 'gg'] {
|
|
|
|
eprintln('uknown template "${template}", possible templates: web, gg')
|
|
|
|
exit(1)
|
|
|
|
}
|
|
|
|
}
|
2019-11-11 00:48:56 +03:00
|
|
|
mut c := Create{}
|
2021-03-12 18:09:43 +03:00
|
|
|
c.name = check_name(if args.len > 0 { args[0] } else { os.input('Input your project name: ') })
|
2020-07-10 19:09:08 +03:00
|
|
|
if c.name == '' {
|
|
|
|
cerror('project name cannot be empty')
|
|
|
|
exit(1)
|
|
|
|
}
|
|
|
|
if c.name.contains('-') {
|
2022-11-15 16:53:13 +03:00
|
|
|
cerror('"${c.name}" should not contain hyphens')
|
2020-07-10 19:09:08 +03:00
|
|
|
exit(1)
|
|
|
|
}
|
2020-03-15 13:20:12 +03:00
|
|
|
if os.is_dir(c.name) {
|
2022-11-15 16:53:13 +03:00
|
|
|
cerror('${c.name} folder already exists')
|
2020-02-13 21:27:13 +03:00
|
|
|
exit(3)
|
|
|
|
}
|
2021-03-06 22:03:20 +03:00
|
|
|
c.description = if args.len > 1 { args[1] } else { os.input('Input your project description: ') }
|
2021-03-07 12:38:56 +03:00
|
|
|
default_version := '0.0.0'
|
2022-11-15 16:53:13 +03:00
|
|
|
c.version = os.input('Input your project version: (${default_version}) ')
|
2021-03-07 12:38:56 +03:00
|
|
|
if c.version == '' {
|
|
|
|
c.version = default_version
|
|
|
|
}
|
2022-04-16 19:09:08 +03:00
|
|
|
default_license := os.getenv_opt('VLICENSE') or { 'MIT' }
|
2022-11-15 16:53:13 +03:00
|
|
|
c.license = os.input('Input your project license: (${default_license}) ')
|
2021-03-07 12:38:56 +03:00
|
|
|
if c.license == '' {
|
|
|
|
c.license = default_license
|
|
|
|
}
|
2019-11-11 00:48:56 +03:00
|
|
|
println('Initialising ...')
|
2021-03-01 02:18:14 +03:00
|
|
|
os.mkdir(c.name) or { panic(err) }
|
2020-07-10 19:09:08 +03:00
|
|
|
c.write_vmod(true)
|
|
|
|
c.write_main(true)
|
2021-12-17 17:11:19 +03:00
|
|
|
c.write_gitattributes(true)
|
2022-01-20 14:01:30 +03:00
|
|
|
c.write_editorconfig(true)
|
2020-03-15 13:20:12 +03:00
|
|
|
c.create_git_repo(c.name)
|
|
|
|
}
|
|
|
|
|
2020-07-10 19:09:08 +03:00
|
|
|
fn init_project() {
|
2020-03-15 13:20:12 +03:00
|
|
|
mut c := Create{}
|
2021-03-12 18:09:43 +03:00
|
|
|
c.name = check_name(os.file_name(os.getwd()))
|
2022-01-20 14:01:30 +03:00
|
|
|
if !os.exists('v.mod') {
|
|
|
|
c.description = ''
|
|
|
|
c.write_vmod(false)
|
|
|
|
println('Change the description of your project in `v.mod`')
|
|
|
|
}
|
2020-07-10 19:09:08 +03:00
|
|
|
c.write_main(false)
|
2021-12-17 17:11:19 +03:00
|
|
|
c.write_gitattributes(false)
|
2022-01-20 14:01:30 +03:00
|
|
|
c.write_editorconfig(false)
|
2021-06-16 19:57:51 +03:00
|
|
|
c.create_git_repo('.')
|
2020-03-15 13:20:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2021-03-13 09:43:12 +03:00
|
|
|
cmd := os.args[1]
|
|
|
|
match cmd {
|
|
|
|
'new' {
|
|
|
|
create(os.args[2..])
|
|
|
|
}
|
|
|
|
'init' {
|
|
|
|
init_project()
|
|
|
|
}
|
|
|
|
else {
|
2022-11-15 16:53:13 +03:00
|
|
|
cerror('unknown command: ${cmd}')
|
2021-03-13 09:43:12 +03:00
|
|
|
exit(1)
|
|
|
|
}
|
2020-03-15 13:20:12 +03:00
|
|
|
}
|
|
|
|
println('Complete!')
|
2019-11-11 00:48:56 +03:00
|
|
|
}
|
2022-12-21 19:33:19 +03:00
|
|
|
|
|
|
|
fn simple_web_app() string {
|
|
|
|
return "import vweb
|
|
|
|
import sqlite // can change to 'mysql', 'pg'
|
|
|
|
|
|
|
|
const (
|
|
|
|
port = 8082
|
|
|
|
)
|
|
|
|
|
|
|
|
struct App {
|
|
|
|
vweb.Context
|
|
|
|
mut:
|
|
|
|
db shared sqlite.DB
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (app App) before_request() {
|
|
|
|
println('[web] before_request: \${app.req.method} \${app.req.url}')
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
vweb.run(&App{
|
|
|
|
db: sqlite.connect('vweb.sql')!
|
|
|
|
}, port)
|
|
|
|
}
|
|
|
|
|
|
|
|
['/users/:name']
|
|
|
|
pub fn (mut app App) user(name string) vweb.Result {
|
|
|
|
user := sql app.db {
|
|
|
|
select from User where name == name
|
|
|
|
}
|
|
|
|
return \$vweb.html()
|
|
|
|
}
|
|
|
|
|
|
|
|
['/api/users/:name']
|
|
|
|
pub fn (mut app App) user(name string) vweb.Result {
|
|
|
|
user := sql app.db {
|
|
|
|
select from User where name == name
|
|
|
|
}
|
|
|
|
return app.json({
|
|
|
|
user: id
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (mut app App) index() vweb.Result {
|
|
|
|
return \$vweb.html()
|
|
|
|
}
|
|
|
|
|
|
|
|
[post]
|
|
|
|
['/register']
|
|
|
|
pub fn (mut app App) register_user(name string, password string) vweb.Result {
|
|
|
|
user := User{name:name, password, password}
|
|
|
|
sql app.db {
|
|
|
|
insert user into User
|
|
|
|
}
|
|
|
|
return app.redirect('/')
|
|
|
|
}
|
|
|
|
"
|
|
|
|
}
|