Skip to content

Commit

Permalink
tools: handle paths in vcreate more gracefully (#19870)
Browse files Browse the repository at this point in the history
  • Loading branch information
ttytm committed Nov 14, 2023
1 parent e4bae91 commit bf120cc
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 25 deletions.
5 changes: 4 additions & 1 deletion cmd/tools/vcreate/project_model_bin.v
@@ -1,8 +1,11 @@
module main

import os

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

import os

fn (mut c Create) set_lib_project_files() {
c.files << ProjectFiles{
path: '${c.name}/src/${c.name}.v'
path: os.join_path(c.name, 'src', c.name + '.v')
content: 'module ${c.name}
// square calculates the second power of `x`
Expand All @@ -12,7 +14,7 @@ pub fn square(x int) int {
'
}
c.files << ProjectFiles{
path: '${c.name}/tests/square_test.v'
path: os.join_path(c.name, 'tests', 'square_test.v')
content: 'import ${c.name}
fn test_square() {
Expand Down
38 changes: 20 additions & 18 deletions cmd/tools/vcreate/project_model_web.v
@@ -1,8 +1,10 @@
module main

import os { join_path }

fn (mut c Create) set_web_project_files() {
c.files << ProjectFiles{
path: '${c.name}/src/databases/config_databases_sqlite.v'
path: join_path(c.name, 'src', 'databases', 'config_databases_sqlite.v')
content: "module databases
import db.sqlite // can change to 'db.mysql', 'db.pg'
Expand All @@ -14,7 +16,7 @@ pub fn create_db_connection() !sqlite.DB {
"
}
c.files << ProjectFiles{
path: '${c.name}/src/templates/header_component.html'
path: join_path(c.name, 'src', 'templates', 'header_component.html')
content: "<nav>
<div class='nav-wrapper'>
<a href='javascript:window.history.back();' class='left'>
Expand All @@ -33,7 +35,7 @@ pub fn create_db_connection() !sqlite.DB {
"
}
c.files << ProjectFiles{
path: '${c.name}/src/templates/products.css'
path: join_path(c.name, 'src', 'templates', 'products.css')
content: 'h1.title {
font-family: Arial, Helvetica, sans-serif;
color: #3b7bbf;
Expand All @@ -47,7 +49,7 @@ div.products-table {
}'
}
c.files << ProjectFiles{
path: '${c.name}/src/templates/products.html'
path: join_path(c.name, 'src', 'templates', 'products.html')
content: "<!DOCTYPE html>
<html>
<head>
Expand Down Expand Up @@ -144,7 +146,7 @@ div.products-table {
</html>"
}
c.files << ProjectFiles{
path: '${c.name}/src/auth_controllers.v'
path: join_path(c.name, 'src', 'auth_controllers.v')
content: "module main
import vweb
Expand All @@ -161,7 +163,7 @@ pub fn (mut app App) controller_auth(username string, password string) vweb.Resu
"
}
c.files << ProjectFiles{
path: '${c.name}/src/auth_dto.v'
path: join_path(c.name, 'src', 'auth_dto.v')
content: 'module main
struct AuthRequestDto {
Expand All @@ -171,7 +173,7 @@ struct AuthRequestDto {
'
}
c.files << ProjectFiles{
path: '${c.name}/src/auth_services.v'
path: join_path(c.name, 'src', 'auth_services.v')
content: "module main
import crypto.hmac
Expand Down Expand Up @@ -266,7 +268,7 @@ fn auth_verify(token string) bool {
"
}
c.files << ProjectFiles{
path: '${c.name}/src/index.html'
path: join_path(c.name, 'src', 'index.html')
content: "<!DOCTYPE html>
<html>
<head>
Expand Down Expand Up @@ -345,7 +347,7 @@ fn auth_verify(token string) bool {
"
}
c.files << ProjectFiles{
path: '${c.name}/src/main.v'
path: join_path(c.name, 'src', 'main.v')
content: "module main
import vweb
Expand Down Expand Up @@ -390,7 +392,7 @@ pub fn (mut app App) index() vweb.Result {
"
}
c.files << ProjectFiles{
path: '${c.name}/src/product_controller.v'
path: join_path(c.name, 'src', 'product_controller.v')
content: "module main
import vweb
Expand Down Expand Up @@ -456,7 +458,7 @@ pub fn (mut app App) controller_create_product(product_name string) vweb.Result
"
}
c.files << ProjectFiles{
path: '${c.name}/src/product_entities.v'
path: join_path(c.name, 'src', 'product_entities.v')
content: "module main
[table: 'products']
Expand All @@ -469,7 +471,7 @@ struct Product {
"
}
c.files << ProjectFiles{
path: '${c.name}/src/product_service.v'
path: join_path(c.name, 'src', 'product_service.v')
content: "module main
import databases
Expand Down Expand Up @@ -516,7 +518,7 @@ fn (mut app App) service_get_all_products_from(user_id int) ![]Product {
"
}
c.files << ProjectFiles{
path: '${c.name}/src/product_view_api.v'
path: join_path(c.name, 'src', 'product_view_api.v')
content: "module main
import json
Expand Down Expand Up @@ -555,7 +557,7 @@ pub fn get_product(token string) ![]User {
"
}
c.files << ProjectFiles{
path: '${c.name}/src/product_view.v'
path: join_path(c.name, 'src', 'product_view.v')
content: "module main
import vweb
Expand All @@ -577,7 +579,7 @@ pub fn (mut app App) products() !vweb.Result {
"
}
c.files << ProjectFiles{
path: '${c.name}/src/user_controllers.v'
path: join_path(c.name, 'src', 'user_controllers.v')
content: "module main
import vweb
Expand Down Expand Up @@ -647,7 +649,7 @@ pub fn (mut app App) controller_create_user(username string, password string) vw
"
}
c.files << ProjectFiles{
path: '${c.name}/src/user_entities.v'
path: join_path(c.name, 'src', 'user_entities.v')
content: "module main
[table: 'users']
Expand All @@ -662,7 +664,7 @@ mut:
"
}
c.files << ProjectFiles{
path: '${c.name}/src/user_services.v'
path: join_path(c.name, 'src', 'user_services.v')
content: "module main
import crypto.bcrypt
Expand Down Expand Up @@ -731,7 +733,7 @@ fn (mut app App) service_get_user(id int) !User {
"
}
c.files << ProjectFiles{
path: '${c.name}/src/user_view_api.v'
path: join_path(c.name, 'src', 'user_view_api.v')
content: "module main
import json
Expand Down
5 changes: 1 addition & 4 deletions cmd/tools/vcreate/vcreate.v
Expand Up @@ -263,10 +263,7 @@ bin/

fn (mut c Create) create_files_and_directories() {
for file in c.files {
// get dir and convert path separator
dir := file.path.split('/')#[..-1].join(os.path_separator)
// create all directories, if not exist
os.mkdir_all(dir) or { panic(err) }
os.mkdir_all(os.dir(file.path)) or { panic(err) }
os.write_file(file.path, file.content) or { panic(err) }
}
}

0 comments on commit bf120cc

Please sign in to comment.