|
| 1 | +use crate::cmd::run_cmd_directly; |
| 2 | +use crate::errors::*; |
| 3 | +use crate::parse::{InitOpts, NewOpts}; |
| 4 | +use std::fs; |
| 5 | +use std::path::{Path, PathBuf}; |
| 6 | + |
| 7 | +/// Creates the named file with the given contents if it doesn't already exist, |
| 8 | +/// printing a warning if it does. |
| 9 | +fn create_file_if_not_present( |
| 10 | + filename: &Path, |
| 11 | + contents: &str, |
| 12 | + name: &str, |
| 13 | +) -> Result<(), InitError> { |
| 14 | + let filename_str = filename.to_str().unwrap(); |
| 15 | + if fs::metadata(filename).is_ok() { |
| 16 | + eprintln!("[WARNING]: Didn't create '{}', since it already exists. If you didn't mean for this to happen, you should remove this file and try again.", filename_str); |
| 17 | + } else { |
| 18 | + let contents = contents.replace("%name", name); |
| 19 | + fs::write(filename, contents).map_err(|err| InitError::CreateInitFileFailed { |
| 20 | + source: err, |
| 21 | + filename: filename_str.to_string(), |
| 22 | + })?; |
| 23 | + } |
| 24 | + Ok(()) |
| 25 | +} |
| 26 | + |
| 27 | +/// Initializes a new Perseus project in the given directory, based on either |
| 28 | +/// the default template or one from a given URL. |
| 29 | +pub fn init(dir: PathBuf, opts: InitOpts) -> Result<i32, InitError> { |
| 30 | + // Create the basic directory structure (this will create both `src/` and |
| 31 | + // `src/templates/`) |
| 32 | + fs::create_dir_all(dir.join("src/templates")) |
| 33 | + .map_err(|err| InitError::CreateDirStructureFailed { source: err })?; |
| 34 | + // Now create each file |
| 35 | + create_file_if_not_present(&dir.join("Cargo.toml"), DFLT_INIT_CARGO_TOML, &opts.name)?; |
| 36 | + create_file_if_not_present(&dir.join(".gitignore"), DFLT_INIT_GITIGNORE, &opts.name)?; |
| 37 | + create_file_if_not_present(&dir.join("src/lib.rs"), DFLT_INIT_LIB_RS, &opts.name)?; |
| 38 | + create_file_if_not_present( |
| 39 | + &dir.join("src/templates/mod.rs"), |
| 40 | + DFLT_INIT_MOD_RS, |
| 41 | + &opts.name, |
| 42 | + )?; |
| 43 | + create_file_if_not_present( |
| 44 | + &dir.join("src/templates/index.rs"), |
| 45 | + DFLT_INIT_INDEX_RS, |
| 46 | + &opts.name, |
| 47 | + )?; |
| 48 | + |
| 49 | + // And now tell the user about some stuff |
| 50 | + println!("Your new app has been created! Run `perseus serve -w` to get to work! You can find more details, including about improving compilation speeds in the Perseus docs (https://arctic-hen7.github.io/perseus/en-US/docs/)."); |
| 51 | + |
| 52 | + Ok(0) |
| 53 | +} |
| 54 | +/// Initializes a new Perseus project in a new directory that's a child of the |
| 55 | +/// current one. |
| 56 | +// The `dir` here is the current dir, the name of the one to create is in `opts` |
| 57 | +pub fn new(dir: PathBuf, opts: NewOpts) -> Result<i32, NewError> { |
| 58 | + // Create the directory (if the user provided a name explicitly, use that, |
| 59 | + // otherwise use the project name) |
| 60 | + let target = dir.join(opts.dir.unwrap_or(opts.name.clone())); |
| 61 | + |
| 62 | + // Check if we're using the default template or one from a URL |
| 63 | + if let Some(url) = opts.template { |
| 64 | + let url_parts = url.split('@').collect::<Vec<&str>>(); |
| 65 | + let engine_url = url_parts[0]; |
| 66 | + // A custom branch can be specified after a `@`, or we'll use `stable` |
| 67 | + let cmd = format!( |
| 68 | + // We'll only clone the production branch, and only the top level, we don't need the |
| 69 | + // whole shebang |
| 70 | + "{} clone --single-branch {branch} --depth 1 {repo} {output}", |
| 71 | + std::env::var("PERSEUS_GIT_PATH").unwrap_or_else(|_| "git".to_string()), |
| 72 | + branch = if let Some(branch) = url_parts.get(1) { |
| 73 | + format!("--branch {}", branch) |
| 74 | + } else { |
| 75 | + String::new() |
| 76 | + }, |
| 77 | + repo = engine_url, |
| 78 | + output = target.to_string_lossy() |
| 79 | + ); |
| 80 | + println!( |
| 81 | + "Fetching custom initialization template with command: '{}'.", |
| 82 | + &cmd |
| 83 | + ); |
| 84 | + // Tell the user what command we're running so that they can debug it |
| 85 | + let exit_code = run_cmd_directly( |
| 86 | + cmd, |
| 87 | + &dir, // We'll run this in the current directory and output into `.perseus/` |
| 88 | + vec![], |
| 89 | + ) |
| 90 | + .map_err(|err| NewError::GetCustomInitFailed { source: err })?; |
| 91 | + if exit_code != 0 { |
| 92 | + return Err(NewError::GetCustomInitNonZeroExitCode { exit_code }); |
| 93 | + } |
| 94 | + // Now delete the Git internals |
| 95 | + let git_target = target.join(".git"); |
| 96 | + if let Err(err) = fs::remove_dir_all(&git_target) { |
| 97 | + return Err(NewError::RemoveCustomInitGitFailed { |
| 98 | + target_dir: git_target.to_str().map(|s| s.to_string()), |
| 99 | + source: err, |
| 100 | + }); |
| 101 | + } |
| 102 | + Ok(0) |
| 103 | + } else { |
| 104 | + fs::create_dir(&target).map_err(|err| NewError::CreateProjectDirFailed { source: err })?; |
| 105 | + // Now initialize in there |
| 106 | + let exit_code = init(target, InitOpts { name: opts.name })?; |
| 107 | + Ok(exit_code) |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +// --- BELOW ARE THE RAW FILES FOR DEFAULT INTIALIZATION --- |
| 112 | +// The token `%name` in all of these will be replaced with the given project |
| 113 | +// name NOTE: These must be updated for breaking changes |
| 114 | + |
| 115 | +static DFLT_INIT_CARGO_TOML: &str = r#"[package] |
| 116 | +name = "%name" |
| 117 | +version = "0.1.0" |
| 118 | +edition = "2021" |
| 119 | +
|
| 120 | +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html |
| 121 | +
|
| 122 | +# Dependencies for the engine and the browser go here |
| 123 | +[dependencies] |
| 124 | +perseus = { version = "=0.4.0-beta.3", features = [ "hydrate" ] } |
| 125 | +sycamore = "=0.8.0-beta.7" |
| 126 | +serde = { version = "1", features = [ "derive" ] } |
| 127 | +serde_json = "1" |
| 128 | +
|
| 129 | +# Engine-only dependencies go here |
| 130 | +[target.'cfg(not(target_arch = "wasm32"))'.dependencies] |
| 131 | +tokio = { version = "1", features = [ "macros", "rt", "rt-multi-thread" ] } |
| 132 | +perseus-warp = { version = "=0.4.0-beta.3", features = [ "dflt-server" ] } |
| 133 | +
|
| 134 | +# Browser-only dependencies go here |
| 135 | +[target.'cfg(target_arch = "wasm32")'.dependencies] |
| 136 | +wasm-bindgen = "0.2" |
| 137 | +
|
| 138 | +# We'll use `src/lib.rs` as both a binary *and* a library at the same time (which we need to tell Cargo explicitly) |
| 139 | +[lib] |
| 140 | +name = "lib" |
| 141 | +path = "src/lib.rs" |
| 142 | +crate-type = [ "cdylib", "rlib" ] |
| 143 | +
|
| 144 | +[[bin]] |
| 145 | +name = "%name" |
| 146 | +path = "src/lib.rs" |
| 147 | +
|
| 148 | +# This section adds some optimizations to make your app nice and speedy in production |
| 149 | +[package.metadata.wasm-pack.profile.release] |
| 150 | +wasm-opt = [ "-Oz" ]"#; |
| 151 | +static DFLT_INIT_GITIGNORE: &str = r#"dist/ |
| 152 | +target_wasm/ |
| 153 | +target_engine/"#; |
| 154 | +static DFLT_INIT_LIB_RS: &str = r#"mod templates; |
| 155 | +
|
| 156 | +use perseus::{Html, PerseusApp}; |
| 157 | +
|
| 158 | +#[perseus::main(perseus_warp::dflt_server)] |
| 159 | +pub fn main<G: Html>() -> PerseusApp<G> { |
| 160 | + PerseusApp::new() |
| 161 | + .template(crate::templates::index::get_template) |
| 162 | +}"#; |
| 163 | +static DFLT_INIT_MOD_RS: &str = r#"pub mod index;"#; |
| 164 | +static DFLT_INIT_INDEX_RS: &str = r#"use perseus::Template; |
| 165 | +use sycamore::prelude::{view, Html, Scope, SsrNode, View}; |
| 166 | +
|
| 167 | +#[perseus::template_rx] |
| 168 | +pub fn index_page<G: Html>(cx: Scope) -> View<G> { |
| 169 | + view! { cx, |
| 170 | + // Don't worry, there are much better ways of styling in Perseus! |
| 171 | + div(style = "display: flex; flex-direction: column; justify-content: center; align-items: center; height: 95vh;") { |
| 172 | + h1 { "Welome to Perseus!" } |
| 173 | + p { |
| 174 | + "This is just an example app. Try changing some code inside " |
| 175 | + code { "src/templates/index.rs" } |
| 176 | + " and you'll be able to see the results here!" |
| 177 | + } |
| 178 | + } |
| 179 | + } |
| 180 | +} |
| 181 | +
|
| 182 | +#[perseus::head] |
| 183 | +pub fn head(cx: Scope) -> View<SsrNode> { |
| 184 | + view! { cx, |
| 185 | + title { "Welcome to Perseus!" } |
| 186 | + } |
| 187 | +} |
| 188 | +
|
| 189 | +pub fn get_template<G: Html>() -> Template<G> { |
| 190 | + Template::new("index").template(index_page).head(head) |
| 191 | +}"#; |
0 commit comments