Skip to main content

waveless_compiler/
new.rs

1// Waveless
2// Copyright (C) 2026 Oscar Alvarez Gonzalez
3
4//!
5//!  Handles the new project creation.
6//!
7use crate::*;
8
9use waveless_sql::http_executor::{mysql::*, *};
10
11/// Create a new project in the current dir with the specified name
12#[instrument(skip_all)]
13pub fn new_project(name: CompactString) -> Result<ResultContext> {
14    // Create the default `project.toml` file.
15    let default_project = project::Project::default();
16
17    // Create the project's folder.
18    let project_path = current_dir()?.join(&name);
19
20    {
21        if let Err(err) = create_dir(&project_path) {
22            Err(err)
23                .wrap_err(format!("Cannot create project's folder {}.", name))
24                .suggestion("Are you sure that there is no project with the same name and that you have write permissions?")?;
25        }
26
27        debug!("Created project's folder at {}.", project_path.display());
28    }
29
30    // Serialize default `project.toml` file.
31    {
32        let mut config_file = File::create_new(project_path.join("project.toml"))
33            .context("Unexpected error, cannot create `project.toml` file.")?;
34
35        let _ = config_file.write(toml::to_string_pretty(&default_project)?.as_bytes())?;
36
37        debug!("Loaded default `project.toml` file.");
38    }
39
40    // Generate all subfolders.
41    {
42        create_dir(project_path.join(default_project.compiler().endpoints_dir()))?;
43
44        create_dir(project_path.join(".generated_endpoints"))?;
45
46        create_dir(project_path.join("target"))?;
47
48        debug!("Created project directories.");
49    }
50
51    // Serialize a sample endpoint.
52    {
53        let endpoints = Endpoints::new_unchecked(CheapVec::from_vec(vec![
54            EndpointBuilder::default()
55                .id("ListProducts".into())
56                .execution_target(ExecutionTarget::Http(
57                    HttpTargetBuilder::default()
58                        .route("/products/{size}".into())
59                        .version("v1".into())
60                        .method(HttpMethod::Get)
61                        .execution_pipeline(
62                            Arc::<MySQLExecutor>::new(
63                                SQLQueryWrapper::new(
64                                    "SELECT * FROM products WHERE size = {size}".into(),
65                                )
66                                .into(),
67                            )
68                            .into(),
69                        )
70                        .build()
71                        .unwrap(),
72                ))
73                .description("Get all the products by the given size.".into())
74                .build()
75                .unwrap(),
76            EndpointBuilder::default()
77                .id("ListPosts".into())
78                .execution_target(ExecutionTarget::Http(
79                    HttpTargetBuilder::default()
80                        .route("posts".into())
81                        .version("v1".into())
82                        .method(HttpMethod::Get)
83                        .execution_pipeline(
84                            Arc::<MySQLExecutor>::new(
85                                SQLQueryWrapper::new("SELECT * FROM posts".into()).into(),
86                            )
87                            .into(),
88                        )
89                        .build()
90                        .unwrap(),
91                ))
92                .description("Get all posts.".into())
93                .build()
94                .unwrap(),
95        ]));
96
97        let mut sample_endpoint_file = File::create_new(
98            project_path
99                .join(default_project.compiler().endpoints_dir())
100                .join("sample_endpoint.toml"),
101        )
102        .context("Unexpected error, cannot create `sample_endpoint.toml` file.")?;
103
104        let _ = sample_endpoint_file.write(toml::to_string_pretty(&endpoints)?.as_bytes())?;
105    }
106
107    Ok(format!(
108        "New project '{}' was created at '{}' with a default '{}' and a sample endpoint at '{}'.",
109        name,
110        project_path.display(),
111        "project.toml",
112        Path::new(default_project.compiler().endpoints_dir())
113            .join("sample_endpoint.toml")
114            .display()
115    )
116    .into())
117}