Skip to content

Commit

Permalink
tools.vcreate: rework cli arg handling to extend scaffolding and fix …
Browse files Browse the repository at this point in the history
…issues (#19889)
  • Loading branch information
ttytm committed Nov 16, 2023
1 parent 9308bcd commit e9258c2
Show file tree
Hide file tree
Showing 16 changed files with 234 additions and 136 deletions.
4 changes: 2 additions & 2 deletions cmd/tools/vcreate/project_model_bin.v
Expand Up @@ -3,9 +3,9 @@ module main
import os

fn (mut c Create) set_bin_project_files() {
main_path := os.join_path('src', 'main.v')
base := if c.new_dir { c.name } else { '' }
c.files << ProjectFiles{
path: if c.new_dir { os.join_path(c.name, main_path) } else { main_path }
path: os.join_path(base, 'src', 'main.v')
content: "module main
fn main() {
Expand Down
5 changes: 3 additions & 2 deletions cmd/tools/vcreate/project_model_lib.v
Expand Up @@ -3,8 +3,9 @@ module main
import os

fn (mut c Create) set_lib_project_files() {
base := if c.new_dir { c.name } else { '' }
c.files << ProjectFiles{
path: os.join_path(c.name, 'src', c.name + '.v')
path: os.join_path(base, 'src', c.name + '.v')
content: 'module ${c.name}
// square calculates the second power of `x`
Expand All @@ -14,7 +15,7 @@ pub fn square(x int) int {
'
}
c.files << ProjectFiles{
path: os.join_path(c.name, 'tests', 'square_test.v')
path: os.join_path(base, 'tests', 'square_test.v')
content: 'import ${c.name}
fn test_square() {
Expand Down
37 changes: 19 additions & 18 deletions cmd/tools/vcreate/project_model_web.v
Expand Up @@ -3,8 +3,9 @@ module main
import os { join_path }

fn (mut c Create) set_web_project_files() {
base := if c.new_dir { c.name } else { '' }
c.files << ProjectFiles{
path: join_path(c.name, 'src', 'databases', 'config_databases_sqlite.v')
path: join_path(base, 'src', 'databases', 'config_databases_sqlite.v')
content: "module databases
import db.sqlite // can change to 'db.mysql', 'db.pg'
Expand All @@ -16,7 +17,7 @@ pub fn create_db_connection() !sqlite.DB {
"
}
c.files << ProjectFiles{
path: join_path(c.name, 'src', 'templates', 'header_component.html')
path: join_path(base, 'src', 'templates', 'header_component.html')
content: "<nav>
<div class='nav-wrapper'>
<a href='javascript:window.history.back();' class='left'>
Expand All @@ -35,7 +36,7 @@ pub fn create_db_connection() !sqlite.DB {
"
}
c.files << ProjectFiles{
path: join_path(c.name, 'src', 'templates', 'products.css')
path: join_path(base, 'src', 'templates', 'products.css')
content: 'h1.title {
font-family: Arial, Helvetica, sans-serif;
color: #3b7bbf;
Expand All @@ -49,7 +50,7 @@ div.products-table {
}'
}
c.files << ProjectFiles{
path: join_path(c.name, 'src', 'templates', 'products.html')
path: join_path(base, 'src', 'templates', 'products.html')
content: "<!DOCTYPE html>
<html>
<head>
Expand Down Expand Up @@ -146,7 +147,7 @@ div.products-table {
</html>"
}
c.files << ProjectFiles{
path: join_path(c.name, 'src', 'auth_controllers.v')
path: join_path(base, 'src', 'auth_controllers.v')
content: "module main
import vweb
Expand All @@ -163,7 +164,7 @@ pub fn (mut app App) controller_auth(username string, password string) vweb.Resu
"
}
c.files << ProjectFiles{
path: join_path(c.name, 'src', 'auth_dto.v')
path: join_path(base, 'src', 'auth_dto.v')
content: 'module main
struct AuthRequestDto {
Expand All @@ -173,7 +174,7 @@ struct AuthRequestDto {
'
}
c.files << ProjectFiles{
path: join_path(c.name, 'src', 'auth_services.v')
path: join_path(base, 'src', 'auth_services.v')
content: "module main
import crypto.hmac
Expand Down Expand Up @@ -268,7 +269,7 @@ fn auth_verify(token string) bool {
"
}
c.files << ProjectFiles{
path: join_path(c.name, 'src', 'index.html')
path: join_path(base, 'src', 'index.html')
content: "<!DOCTYPE html>
<html>
<head>
Expand Down Expand Up @@ -347,7 +348,7 @@ fn auth_verify(token string) bool {
"
}
c.files << ProjectFiles{
path: join_path(c.name, 'src', 'main.v')
path: join_path(base, 'src', 'main.v')
content: "module main
import vweb
Expand Down Expand Up @@ -392,7 +393,7 @@ pub fn (mut app App) index() vweb.Result {
"
}
c.files << ProjectFiles{
path: join_path(c.name, 'src', 'product_controller.v')
path: join_path(base, 'src', 'product_controller.v')
content: "module main
import vweb
Expand Down Expand Up @@ -458,7 +459,7 @@ pub fn (mut app App) controller_create_product(product_name string) vweb.Result
"
}
c.files << ProjectFiles{
path: join_path(c.name, 'src', 'product_entities.v')
path: join_path(base, 'src', 'product_entities.v')
content: "module main
@[table: 'products']
Expand All @@ -471,7 +472,7 @@ struct Product {
"
}
c.files << ProjectFiles{
path: join_path(c.name, 'src', 'product_service.v')
path: join_path(base, 'src', 'product_service.v')
content: "module main
import databases
Expand Down Expand Up @@ -518,7 +519,7 @@ fn (mut app App) service_get_all_products_from(user_id int) ![]Product {
"
}
c.files << ProjectFiles{
path: join_path(c.name, 'src', 'product_view_api.v')
path: join_path(base, 'src', 'product_view_api.v')
content: "module main
import json
Expand Down Expand Up @@ -557,7 +558,7 @@ pub fn get_product(token string) ![]User {
"
}
c.files << ProjectFiles{
path: join_path(c.name, 'src', 'product_view.v')
path: join_path(base, 'src', 'product_view.v')
content: "module main
import vweb
Expand All @@ -579,7 +580,7 @@ pub fn (mut app App) products() !vweb.Result {
"
}
c.files << ProjectFiles{
path: join_path(c.name, 'src', 'user_controllers.v')
path: join_path(base, 'src', 'user_controllers.v')
content: "module main
import vweb
Expand Down Expand Up @@ -649,7 +650,7 @@ pub fn (mut app App) controller_create_user(username string, password string) vw
"
}
c.files << ProjectFiles{
path: join_path(c.name, 'src', 'user_entities.v')
path: join_path(base, 'src', 'user_entities.v')
content: "module main
@[table: 'users']
Expand All @@ -664,7 +665,7 @@ mut:
"
}
c.files << ProjectFiles{
path: join_path(c.name, 'src', 'user_services.v')
path: join_path(base, 'src', 'user_services.v')
content: "module main
import crypto.bcrypt
Expand Down Expand Up @@ -733,7 +734,7 @@ fn (mut app App) service_get_user(id int) !User {
"
}
c.files << ProjectFiles{
path: join_path(c.name, 'src', 'user_view_api.v')
path: join_path(base, 'src', 'user_view_api.v')
content: "module main
import json
Expand Down
2 changes: 1 addition & 1 deletion cmd/tools/vcreate/tests/init.expect
Expand Up @@ -10,6 +10,6 @@ spawn $v_root/v init
expect "Input your project description: " { send "\r" } timeout { exit 1 }
expect "Input your project version: (0.0.0) " { send "\r" } timeout { exit 1 }
expect "Input your project license: (MIT) " { send "\r" } timeout { exit 1 }
expect "Complete!" {} timeout {} timeout { exit 1 }
# The completion message is verified in `vcreate_init_test.v`.

expect eof
Expand Up @@ -13,6 +13,6 @@ expect "Input your project description: " { send "\r" } timeout { exit 1 }
expect "Input your project version: (0.0.0) " { send "\r" } timeout { exit 1 }
expect "Input your project license: (MIT) " { send "\r" } timeout { exit 1 }
expect "The directory name `$project_dir_name` is invalid as a module name. The module name in `v.mod` was set to `$corrected_mod_name`" {} timeout { exit 1 }
expect "Complete!" {} timeout {} timeout { exit 1 }
expect "Created binary (application) project `$corrected_mod_name`" {} timeout {} timeout { exit 1 }

expect eof
17 changes: 17 additions & 0 deletions cmd/tools/vcreate/tests/init_with_model_arg.expect
@@ -0,0 +1,17 @@
#!/usr/bin/env expect

set timeout 3

# Pass v_root as arg, since we chdir into a temp directory during testing and create a project there.
set v_root [lindex $argv 0]
set model [lindex $argv 1]

spawn $v_root/v init $model

expect "Input your project description: " { send "My Awesome V Application.\r" } timeout { exit 1 }
expect "Input your project version: (0.0.0) " { send "0.0.1\r" } timeout { exit 1 }
expect "Input your project license: (MIT) " { send "\r" } timeout { exit 1 }
expect "Initialising ..." {} timeout { exit 1 }
# The completion message is verified in `vcreate_init_test.v`.

expect eof
8 changes: 4 additions & 4 deletions cmd/tools/vcreate/tests/new_with_model_arg.expect
Expand Up @@ -4,15 +4,15 @@ set timeout 3

# Pass v_root as arg, since we chdir into a temp directory during testing and create a project there.
set v_root [lindex $argv 0]
set project_name [lindex $argv 1]
set model [lindex $argv 2]
set model [lindex $argv 1]
set project_name [lindex $argv 2]

spawn $v_root/v new $project_name $model
spawn $v_root/v new $model $project_name

expect "Input your project description: " { send "My Awesome V Project.\r" } timeout { exit 1 }
expect "Input your project version: (0.0.0) " { send "0.0.1\r" } timeout { exit 1 }
expect "Input your project license: (MIT) " { send "\r" } timeout { exit 1 }
expect "Initialising ..." {} timeout { exit 1 }
expect "Complete!" {} timeout { exit 1 }
expect "Created library project `$project_name`" {} timeout { exit 1 }

expect eof
2 changes: 1 addition & 1 deletion cmd/tools/vcreate/tests/new_with_name_arg.expect
Expand Up @@ -12,6 +12,6 @@ expect "Input your project description: " { send "\r" } timeout { exit 1 }
expect "Input your project version: (0.0.0) " { send "\r" } timeout { exit 1 }
expect "Input your project license: (MIT) " { send "\r" } timeout { exit 1 }
expect "Initialising ..." {} timeout { exit 1 }
expect "Complete!" {} timeout { exit 1 }
expect "Created binary (application) project `$project_name`" {} timeout { exit 1 }

expect eof
2 changes: 1 addition & 1 deletion cmd/tools/vcreate/tests/new_with_no_arg.expect
Expand Up @@ -13,6 +13,6 @@ expect "Input your project description: " { send "My Awesome V Project.\r" } tim
expect "Input your project version: (0.0.0) " { send "0.1.0\r" } timeout { exit 1 }
expect "Input your project license: (MIT) " { send "GPL\r" } timeout { exit 1 }
expect "Initialising ..." {} timeout { exit 1 }
expect "Complete!" {} timeout { exit 1 }
expect "Created binary (application) project `$project_name`" {} timeout { exit 1 }

expect eof

0 comments on commit e9258c2

Please sign in to comment.