Skip to content

Commit

Permalink
feat(cli): ✨ added --release mode to cli (#35)
Browse files Browse the repository at this point in the history
* feat(cli): ✨ added `--release` mode to cli

Perseus apps can now be built for production!

* refactor: 🚨 refactored to fix clippy warnings
  • Loading branch information
arctic-hen7 authored Sep 29, 2021
1 parent 83e365c commit f66bbb9
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 25 deletions.
2 changes: 1 addition & 1 deletion packages/perseus-actix-web/src/initial_load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ pub async fn initial_load<C: ConfigManager, T: TranslationsManager>(
let mut http_res = HttpResponse::Ok();
http_res.content_type("text/html");
// Generate and add HTTP headers
for (key, val) in template.get_headers(page_data.state.clone()) {
for (key, val) in template.get_headers(page_data.state) {
http_res.set_header(key.unwrap(), val);
}

Expand Down
2 changes: 1 addition & 1 deletion packages/perseus-cli/src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ fn core(dir: PathBuf) -> Result<i32, Error> {
// We don't delete `render_conf.json` because it's literally impossible for that to be the source of a problem right now
delete_artifacts(dir.clone(), "static")?;
delete_artifacts(dir.clone(), "pkg")?;
delete_artifacts(dir.clone(), "exported")?;
delete_artifacts(dir, "exported")?;
} else {
// This command deletes the `.perseus/` directory completely, which musn't happen if the user has ejected
if has_ejected(dir.clone()) && !clean_opts.force {
Expand Down
15 changes: 9 additions & 6 deletions packages/perseus-cli/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ pub fn build_internal(
dir: PathBuf,
spinners: &MultiProgress,
num_steps: u8,
is_release: bool,
) -> Result<
(
ThreadHandle<impl FnOnce() -> Result<i32, ExecutionError>, Result<i32, ExecutionError>>,
Expand Down Expand Up @@ -80,8 +81,9 @@ pub fn build_internal(
let sg_thread = spawn_thread(move || {
handle_exit_code!(run_stage(
vec![&format!(
"{} run",
env::var("PERSEUS_CARGO_PATH").unwrap_or_else(|_| "cargo".to_string())
"{} run {}",
env::var("PERSEUS_CARGO_PATH").unwrap_or_else(|_| "cargo".to_string()),
if is_release { "--release" } else { "" }
)],
&sg_target,
&sg_spinner,
Expand All @@ -93,8 +95,9 @@ pub fn build_internal(
let wb_thread = spawn_thread(move || {
handle_exit_code!(run_stage(
vec![&format!(
"{} build --target web",
env::var("PERSEUS_WASM_PACK_PATH").unwrap_or_else(|_| "wasm-pack".to_string())
"{} build --target web {}",
env::var("PERSEUS_WASM_PACK_PATH").unwrap_or_else(|_| "wasm-pack".to_string()),
if is_release { "--release" } else { "" }
)],
&wb_target,
&wb_spinner,
Expand All @@ -108,10 +111,10 @@ pub fn build_internal(
}

/// Builds the subcrates to get a directory that we can serve. Returns an exit code.
pub fn build(dir: PathBuf, _opts: BuildOpts) -> Result<i32, ExecutionError> {
pub fn build(dir: PathBuf, opts: BuildOpts) -> Result<i32, ExecutionError> {
let spinners = MultiProgress::new();

let (sg_thread, wb_thread) = build_internal(dir.clone(), &spinners, 2)?;
let (sg_thread, wb_thread) = build_internal(dir.clone(), &spinners, 2, opts.release)?;
let sg_res = sg_thread
.join()
.map_err(|_| ExecutionError::ThreadWaitFailed)??;
Expand Down
15 changes: 9 additions & 6 deletions packages/perseus-cli/src/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ pub fn export_internal(
dir: PathBuf,
spinners: &MultiProgress,
num_steps: u8,
is_release: bool,
) -> Result<
(
ThreadHandle<impl FnOnce() -> Result<i32, ExportError>, Result<i32, ExportError>>,
Expand Down Expand Up @@ -164,8 +165,9 @@ pub fn export_internal(
let ep_thread = spawn_thread(move || {
handle_exit_code!(run_stage(
vec![&format!(
"{} run --bin perseus-exporter",
env::var("PERSEUS_CARGO_PATH").unwrap_or_else(|_| "cargo".to_string())
"{} run --bin perseus-exporter {}",
env::var("PERSEUS_CARGO_PATH").unwrap_or_else(|_| "cargo".to_string()),
if is_release { "--release" } else { "" }
)],
&ep_target,
&ep_spinner,
Expand All @@ -177,8 +179,9 @@ pub fn export_internal(
let wb_thread = spawn_thread(move || {
handle_exit_code!(run_stage(
vec![&format!(
"{} build --target web",
env::var("PERSEUS_WASM_PACK_PATH").unwrap_or_else(|_| "wasm-pack".to_string())
"{} build --target web {}",
env::var("PERSEUS_WASM_PACK_PATH").unwrap_or_else(|_| "wasm-pack".to_string()),
if is_release { "--release" } else { "" }
)],
&wb_target,
&wb_spinner,
Expand All @@ -192,10 +195,10 @@ pub fn export_internal(
}

/// Builds the subcrates to get a directory that we can serve. Returns an exit code.
pub fn export(dir: PathBuf, _opts: ExportOpts) -> Result<i32, ExportError> {
pub fn export(dir: PathBuf, opts: ExportOpts) -> Result<i32, ExportError> {
let spinners = MultiProgress::new();

let (ep_thread, wb_thread) = export_internal(dir.clone(), &spinners, 2)?;
let (ep_thread, wb_thread) = export_internal(dir.clone(), &spinners, 2, opts.release)?;
let ep_res = ep_thread
.join()
.map_err(|_| ExecutionError::ThreadWaitFailed)??;
Expand Down
6 changes: 3 additions & 3 deletions packages/perseus-cli/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ pub enum Subcommand {
pub struct BuildOpts {
/// Build for production
#[clap(long)]
release: bool,
pub release: bool,
}
/// Exports your app to purely static files
#[derive(Clap)]
pub struct ExportOpts {
/// Export for production
#[clap(long)]
release: bool,
pub release: bool,
}
/// Serves your app (set the `$HOST` and `$PORT` environment variables to change the location it's served at)
#[derive(Clap)]
Expand All @@ -52,7 +52,7 @@ pub struct ServeOpts {
pub no_build: bool,
/// Build and serve for production
#[clap(long)]
release: bool,
pub release: bool,
}
/// Removes `.perseus/` entirely for updates or to fix corruptions
#[derive(Clap)]
Expand Down
16 changes: 12 additions & 4 deletions packages/perseus-cli/src/serve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ fn build_server(
spinners: &MultiProgress,
did_build: bool,
exec: Arc<Mutex<String>>,
is_release: bool,
) -> Result<
ThreadHandle<impl FnOnce() -> Result<i32, ExecutionError>, Result<i32, ExecutionError>>,
ExecutionError,
Expand Down Expand Up @@ -64,8 +65,9 @@ fn build_server(
let (stdout, _stderr) = handle_exit_code!(run_stage(
vec![&format!(
// This sets Cargo to tell us everything, including the executable path to the server
"{} build --message-format json",
env::var("PERSEUS_CARGO_PATH").unwrap_or_else(|_| "cargo".to_string())
"{} build --message-format json {}",
env::var("PERSEUS_CARGO_PATH").unwrap_or_else(|_| "cargo".to_string()),
if is_release { "--release" } else { "" }
)],
&sb_target,
&sb_spinner,
Expand Down Expand Up @@ -182,10 +184,16 @@ pub fn serve(dir: PathBuf, opts: ServeOpts) -> Result<i32, ExecutionError> {
// We need to have a way of knowing what the executable path to the server is
let exec = Arc::new(Mutex::new(String::new()));
// We can begin building the server in a thread without having to deal with the rest of the build stage yet
let sb_thread = build_server(dir.clone(), &spinners, did_build, Arc::clone(&exec))?;
let sb_thread = build_server(
dir.clone(),
&spinners,
did_build,
Arc::clone(&exec),
opts.release,
)?;
// Only build if the user hasn't set `--no-build`, handling non-zero exit codes
if did_build {
let (sg_thread, wb_thread) = build_internal(dir.clone(), &spinners, 4)?;
let (sg_thread, wb_thread) = build_internal(dir.clone(), &spinners, 4, opts.release)?;
let sg_res = sg_thread
.join()
.map_err(|_| ExecutionError::ThreadWaitFailed)??;
Expand Down
6 changes: 2 additions & 4 deletions packages/perseus/src/translations_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,11 @@ impl TranslationsManager for FsTranslationsManager {
}
})?,
Err(err) if err.kind() == std::io::ErrorKind::NotFound => {
return Err(TranslationsManagerError::NotFound {
locale: locale.clone(),
})
return Err(TranslationsManagerError::NotFound { locale })
}
Err(err) => {
return Err(TranslationsManagerError::ReadFailed {
locale: locale.clone(),
locale,
source: err.into(),
})
}
Expand Down

0 comments on commit f66bbb9

Please sign in to comment.