-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
cot new
command for starting new projects
- Loading branch information
Showing
16 changed files
with
310 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
pub mod migration_generator; | ||
pub mod new_project; | ||
mod utils; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
use std::path::PathBuf; | ||
|
||
use convert_case::{Case, Casing}; | ||
use tracing::trace; | ||
|
||
use crate::utils::print_status_msg; | ||
|
||
macro_rules! project_file { | ||
($name:literal) => { | ||
($name, include_str!(concat!("project_template/", $name))) | ||
}; | ||
} | ||
|
||
const PROJECT_FILES: [(&'static str, &'static str); 6] = [ | ||
project_file!("Cargo.toml"), | ||
project_file!("bacon.toml"), | ||
project_file!(".gitignore"), | ||
project_file!("src/main.rs"), | ||
project_file!("static/css/main.css"), | ||
project_file!("templates/index.html"), | ||
]; | ||
|
||
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] | ||
pub enum CotSource { | ||
Git, | ||
PublishedCrate, | ||
} | ||
|
||
pub fn new_project( | ||
path: &PathBuf, | ||
project_name: &str, | ||
cot_source: CotSource, | ||
) -> anyhow::Result<()> { | ||
print_status_msg("Creating", &format!("Cot project `{project_name}`")); | ||
|
||
if path.exists() { | ||
anyhow::bail!("destination `{}` already exists", path.display()); | ||
} | ||
|
||
let app_name = format!("{}App", project_name.to_case(Case::Pascal)); | ||
let cot_source = match cot_source { | ||
CotSource::Git => { | ||
"package = \"cot\", git = \"https://github.com/cot-rs/cot.git\"".to_owned() | ||
} | ||
CotSource::PublishedCrate => format!("version = \"{}\"", env!("CARGO_PKG_VERSION")), | ||
}; | ||
|
||
for (file_name, content) in PROJECT_FILES { | ||
let file_path = path.join(file_name); | ||
trace!("Writing file: {:?}", file_path); | ||
|
||
std::fs::create_dir_all( | ||
file_path | ||
.parent() | ||
.expect("joined path should always have a parent"), | ||
)?; | ||
|
||
std::fs::write( | ||
file_path, | ||
content | ||
.replace("{{ project_name }}", project_name) | ||
.replace("{{ app_name }}", &app_name) | ||
.replace("{{ cot_source }}", &cot_source), | ||
)?; | ||
} | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# Generated by Cargo | ||
# will have compiled files and executables | ||
debug/ | ||
target/ | ||
|
||
# These are backup files generated by rustfmt | ||
**/*.rs.bk | ||
|
||
# MSVC Windows builds of rustc generate these, which store debugging information | ||
*.pdb | ||
|
||
# Test databases | ||
*.db | ||
*.sqlite3 | ||
*.sqlite3-journal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[package] | ||
name = "{{ project_name }}" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
cot = { {{ cot_source }}, features = ["full"] } | ||
rinja = "0.3" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
[jobs.serve] | ||
command = ["cargo", "run"] | ||
background = false | ||
on_change_strategy = "kill_then_restart" | ||
watch = ["templates", "static"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
use cot::bytes::Bytes; | ||
use cot::config::ProjectConfig; | ||
use cot::middleware::LiveReloadMiddleware; | ||
use cot::request::Request; | ||
use cot::response::{Response, ResponseExt}; | ||
use cot::router::{Route, Router}; | ||
use cot::static_files::StaticFilesMiddleware; | ||
use cot::{static_files, Body, CotApp, CotProject, StatusCode}; | ||
use rinja::Template; | ||
|
||
#[derive(Debug, Template)] | ||
#[template(path = "index.html")] | ||
struct IndexTemplate {} | ||
|
||
async fn index(request: Request) -> cot::Result<Response> { | ||
let index_template = IndexTemplate {}; | ||
let rendered = index_template.render()?; | ||
|
||
Ok(Response::new_html(StatusCode::OK, Body::fixed(rendered))) | ||
} | ||
|
||
struct {{ app_name }}; | ||
|
||
impl CotApp for {{ app_name }} { | ||
fn name(&self) -> &'static str { | ||
env!("CARGO_CRATE_NAME") | ||
} | ||
|
||
fn router(&self) -> Router { | ||
Router::with_urls([Route::with_handler_and_name("/", index, "index")]) | ||
} | ||
|
||
fn static_files(&self) -> Vec<(String, Bytes)> { | ||
static_files!("css/main.css") | ||
} | ||
} | ||
|
||
#[cot::main] | ||
async fn main() -> cot::Result<CotProject> { | ||
let project = CotProject::builder() | ||
.config(ProjectConfig::builder().build()) | ||
.register_app_with_views({{ app_name }}, "") | ||
.middleware_with_context(StaticFilesMiddleware::from_app_context) | ||
.middleware(LiveReloadMiddleware::new()) | ||
.build() | ||
.await?; | ||
|
||
Ok(project) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
* { | ||
box-sizing: border-box; | ||
} | ||
|
||
html, body { | ||
height: 100%; | ||
margin: 0; | ||
padding: 0; | ||
} | ||
|
||
body { | ||
text-align: center; | ||
background-image: linear-gradient(to right bottom, #0f172a, #2d3d57); | ||
font-family: sans-serif; | ||
color: #fff; | ||
} | ||
|
||
main { | ||
display: flex; | ||
flex-direction: column; | ||
justify-content: center; | ||
height: 100%; | ||
} | ||
|
||
ul { | ||
list-style: none; | ||
} | ||
|
||
h1 { | ||
margin: 0; | ||
} | ||
|
||
a { | ||
color: #f97316; | ||
} | ||
|
||
a:hover, a:focus { | ||
color: #ec9355; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>Welcome to Cot!</title> | ||
|
||
<link href="/static/css/main.css" rel="stylesheet"> | ||
</head> | ||
<body> | ||
|
||
<main> | ||
<h1>You have successfully installed Cot!</h1> | ||
|
||
<p class="lead">Now, you can start building your web application using Rust and Cot.</p> | ||
|
||
<ul> | ||
<li><a href="https://cot.rs/guide">Read the guide</a></li> | ||
<li><a href="https://docs.rs/cot">See the API reference</a></li> | ||
</ul> | ||
</main> | ||
|
||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.