From f4f9979d47acbc6dfcb9237a9be5482901346f2d Mon Sep 17 00:00:00 2001 From: Lev Date: Fri, 29 Sep 2023 15:32:51 -0700 Subject: [PATCH 1/3] Unite apps --- .../src/frontend/components.rs | 3 +- .../src/frontend/javascript.rs | 50 +++++++++++++------ .../src/frontend/tools.rs | 17 +++++++ pgml-apps/cargo-pgml-components/src/util.rs | 2 + 4 files changed, 55 insertions(+), 17 deletions(-) diff --git a/pgml-apps/cargo-pgml-components/src/frontend/components.rs b/pgml-apps/cargo-pgml-components/src/frontend/components.rs index 5a8a479df..3ce05c0b3 100644 --- a/pgml-apps/cargo-pgml-components/src/frontend/components.rs +++ b/pgml-apps/cargo-pgml-components/src/frontend/components.rs @@ -205,8 +205,7 @@ fn update_module(path: &Path) { debug!("writing {} modules to mod.rs", modules.len()); let components_mod = path.join("mod.rs"); - let modules = - unwrap_or_exit!(templates::Mod { modules }.render_once()).replace("\n\n", "\n"); + let modules = unwrap_or_exit!(templates::Mod { modules }.render_once()).replace("\n\n", "\n"); let existing_modules = if components_mod.is_file() { unwrap_or_exit!(read_to_string(&components_mod)) diff --git a/pgml-apps/cargo-pgml-components/src/frontend/javascript.rs b/pgml-apps/cargo-pgml-components/src/frontend/javascript.rs index 9f1c80fc5..29c1e5193 100644 --- a/pgml-apps/cargo-pgml-components/src/frontend/javascript.rs +++ b/pgml-apps/cargo-pgml-components/src/frontend/javascript.rs @@ -25,6 +25,11 @@ static JS_HASH_FILE: &'static str = "static/js/.pgml-bundle"; static MODULES_GLOB: &'static str = "src/components/**/*.js"; static STATIC_JS_GLOB: &'static str = "static/js/*.js"; +// Dashboard glob +static DASHBOARD_JS_GLOB: &'static str = "../deps/postgresml/pgml-dashboard/static/js/*.js"; +static DASHBOARD_COMPONENTS_JS_GLOB: &'static str = + "../deps/postgresml/pgml-dashboard/src/components/**/*.js"; + /// Finds old JS bundles we created. static OLD_BUNLDES_GLOB: &'static str = "static/js/*.*.js"; @@ -45,13 +50,15 @@ fn cleanup_old_bundles() { fn assemble_modules() { let js = unwrap_or_exit!(glob(MODULES_GLOB)); let js = js.chain(unwrap_or_exit!(glob(STATIC_JS_GLOB))); + let js = js.chain(unwrap_or_exit!(glob(DASHBOARD_JS_GLOB))); + let js = js.chain(unwrap_or_exit!(glob(DASHBOARD_COMPONENTS_JS_GLOB))); // Don't bundle artifacts we produce. let js = js.filter(|path| { let path = path.as_ref().unwrap(); let path = path.display().to_string(); - !path.contains("main.js") && !path.contains("bundle.js") && !path.contains("modules.js") + !path.contains("main.") && !path.contains("bundle.") && !path.contains("modules.") }); let mut modules = unwrap_or_exit!(File::create(MODULES_FILE)); @@ -75,27 +82,37 @@ fn assemble_modules() { let full_path = source.display().to_string(); - let path = source - .components() - .skip(2) // skip src/components or static/js - .collect::>(); + let path = source.components().collect::>(); assert!(!path.is_empty()); let path = path.iter().collect::(); let components = path.components(); - let controller_name = if components.clone().count() > 1 { - components + let file_stem = path.file_stem().unwrap().to_str().unwrap().to_string(); + let controller_name = if file_stem.ends_with("controller") { + let mut parts = vec![]; + + let pp = components .map(|c| c.as_os_str().to_str().expect("component to be valid utf-8")) .filter(|c| !c.ends_with(".js")) - .collect::>() - .join("_") + .collect::>(); + let mut saw_src = false; + let mut saw_components = false; + for p in pp { + if p == "src" { + saw_src = true; + } else if p == "components" { + saw_components = true; + } else if saw_src && saw_components { + parts.push(p); + } + } + + assert!(!parts.is_empty()); + + parts.join("_") } else { - path.file_stem() - .expect("old controllers to be a single file") - .to_str() - .expect("stemp to be valid utf-8") - .to_string() + file_stem }; let upper_camel = controller_name.to_case(Case::UpperCamel).to_string(); let controller_name = controller_name.replace("_", "-"); @@ -133,7 +150,10 @@ pub fn bundle() { .arg("--file") .arg(JS_FILE) .arg("--format") - .arg("es"), + .arg("es") + .arg("-p") + .arg("@rollup/plugin-node-resolve") // .arg("-p") + // .arg("@rollup/plugin-terser"), )); info(&format!("written {}", JS_FILE)); diff --git a/pgml-apps/cargo-pgml-components/src/frontend/tools.rs b/pgml-apps/cargo-pgml-components/src/frontend/tools.rs index 5c7809fd9..2f27310a2 100644 --- a/pgml-apps/cargo-pgml-components/src/frontend/tools.rs +++ b/pgml-apps/cargo-pgml-components/src/frontend/tools.rs @@ -7,6 +7,7 @@ use std::process::{exit, Command}; /// Required tools. static TOOLS: &[&str] = &["sass", "rollup"]; +static ROLLUP_PLUGINS: &[&str] = &["@rollup/plugin-terser", "@rollup/plugin-node-resolve"]; static NVM_EXEC: &'static str = "/tmp/pgml-components-nvm.sh"; static NVM_SOURCE: &'static str = "https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.5/install.sh"; static NVM_SOURCE_DOWNLOADED: &'static str = "/tmp/pgml-components-nvm-source.sh"; @@ -30,6 +31,22 @@ pub fn install() { } } } + + for plugin in ROLLUP_PLUGINS { + if execute_with_nvm( + Command::new("rollup") + .arg("-p") + .arg(plugin) + .arg("--version"), + ) + .is_err() + { + warn(&format!("installing rollup plugin {}", plugin)); + unwrap_or_exit!(execute_with_nvm( + Command::new("npm").arg("install").arg("-g").arg(plugin) + )); + } + } } /// Execute a command making sure that nvm is available. diff --git a/pgml-apps/cargo-pgml-components/src/util.rs b/pgml-apps/cargo-pgml-components/src/util.rs index df906d557..2290cb378 100644 --- a/pgml-apps/cargo-pgml-components/src/util.rs +++ b/pgml-apps/cargo-pgml-components/src/util.rs @@ -39,6 +39,8 @@ pub fn warn(value: &str) { } pub fn execute_command(command: &mut Command) -> std::io::Result { + debug!("Executing {:?}", command); + let output = match command.output() { Ok(output) => output, Err(err) => { From ca95cd41d637837a8a5e32aa89ed3b1d2774d1b0 Mon Sep 17 00:00:00 2001 From: Lev Date: Fri, 29 Sep 2023 15:33:13 -0700 Subject: [PATCH 2/3] v --- pgml-apps/cargo-pgml-components/Cargo.lock | 2 +- pgml-apps/cargo-pgml-components/Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pgml-apps/cargo-pgml-components/Cargo.lock b/pgml-apps/cargo-pgml-components/Cargo.lock index 37c6a0e41..5a12d976b 100644 --- a/pgml-apps/cargo-pgml-components/Cargo.lock +++ b/pgml-apps/cargo-pgml-components/Cargo.lock @@ -126,7 +126,7 @@ dependencies = [ [[package]] name = "cargo-pgml-components" -version = "0.1.15" +version = "0.1.16" dependencies = [ "anyhow", "assert_cmd", diff --git a/pgml-apps/cargo-pgml-components/Cargo.toml b/pgml-apps/cargo-pgml-components/Cargo.toml index a12c8bd27..fd8fbf7ad 100644 --- a/pgml-apps/cargo-pgml-components/Cargo.toml +++ b/pgml-apps/cargo-pgml-components/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "cargo-pgml-components" -version = "0.1.15" +version = "0.1.16" edition = "2021" authors = ["PostgresML "] license = "MIT" From 37616758c593fe5dfb775e497537e4781d2483bf Mon Sep 17 00:00:00 2001 From: Lev Date: Sat, 30 Sep 2023 09:09:08 -0700 Subject: [PATCH 3/3] Options --- pgml-apps/cargo-pgml-components/Cargo.lock | 2 + pgml-apps/cargo-pgml-components/Cargo.toml | 2 + pgml-apps/cargo-pgml-components/src/config.rs | 33 +++++++++++ .../src/frontend/components.rs | 10 +--- .../src/frontend/javascript.rs | 55 +++++++++++-------- .../src/frontend/tools.rs | 8 ++- pgml-apps/cargo-pgml-components/src/main.rs | 15 +++-- 7 files changed, 90 insertions(+), 35 deletions(-) create mode 100644 pgml-apps/cargo-pgml-components/src/config.rs diff --git a/pgml-apps/cargo-pgml-components/Cargo.lock b/pgml-apps/cargo-pgml-components/Cargo.lock index 5a12d976b..997ac2cf8 100644 --- a/pgml-apps/cargo-pgml-components/Cargo.lock +++ b/pgml-apps/cargo-pgml-components/Cargo.lock @@ -141,6 +141,8 @@ dependencies = [ "predicates", "regex", "sailfish", + "serde", + "toml", ] [[package]] diff --git a/pgml-apps/cargo-pgml-components/Cargo.toml b/pgml-apps/cargo-pgml-components/Cargo.toml index fd8fbf7ad..75f992f75 100644 --- a/pgml-apps/cargo-pgml-components/Cargo.toml +++ b/pgml-apps/cargo-pgml-components/Cargo.toml @@ -19,6 +19,8 @@ anyhow = "1" owo-colors = "3" sailfish = "0.8" regex = "1" +toml = "0.7" +serde = { version = "1", features = ["derive"] } [dev-dependencies] assert_cmd = "2" diff --git a/pgml-apps/cargo-pgml-components/src/config.rs b/pgml-apps/cargo-pgml-components/src/config.rs new file mode 100644 index 000000000..7d0a5e06d --- /dev/null +++ b/pgml-apps/cargo-pgml-components/src/config.rs @@ -0,0 +1,33 @@ +use serde::{Deserialize, Serialize}; + +#[derive(Serialize, Deserialize, Default, Clone)] +pub struct Javascript { + #[serde(default = "Javascript::default_additional_paths")] + pub additional_paths: Vec, +} + +impl Javascript { + fn default_additional_paths() -> Vec { + vec![] + } +} + +#[derive(Serialize, Deserialize, Default, Clone)] +pub struct Config { + pub javascript: Javascript, +} + +impl Config { + pub fn from_path(path: &str) -> anyhow::Result { + let config_str = std::fs::read_to_string(path)?; + let config: Config = toml::from_str(&config_str)?; + Ok(config) + } + + pub fn load() -> Config { + match Self::from_path("pgml-components.toml") { + Ok(config) => config, + Err(_) => Config::default(), + } + } +} diff --git a/pgml-apps/cargo-pgml-components/src/frontend/components.rs b/pgml-apps/cargo-pgml-components/src/frontend/components.rs index 3ce05c0b3..06b73d6d8 100644 --- a/pgml-apps/cargo-pgml-components/src/frontend/components.rs +++ b/pgml-apps/cargo-pgml-components/src/frontend/components.rs @@ -191,10 +191,7 @@ fn update_module(path: &Path) { } if has_more_modules(&path) { - debug!("{} has more modules", path.display()); update_module(&path); - } else { - debug!("it does not really no"); } let component_path = path.components().skip(2).collect::(); @@ -219,7 +216,7 @@ fn update_module(path: &Path) { info(&format!("written {}", components_mod.display().to_string())); } - debug!("mod.rs is the same"); + debug!("{}/mod.rs is different", components_mod.display()); } /// Check that the path has more Rust modules. @@ -227,7 +224,7 @@ fn has_more_modules(path: &Path) -> bool { debug!("checking if {} has more modules", path.display()); if !path.exists() { - debug!("path does not exist"); + debug!("path {} does not exist", path.display()); return false; } @@ -243,13 +240,12 @@ fn has_more_modules(path: &Path) -> bool { if let Some(file_name) = path.file_name() { if file_name != "mod.rs" { - debug!("it has another file that's not mod.rs"); + debug!("{} has another file that's not mod.rs", path.display()); return false; } } } - debug!("it does"); true } diff --git a/pgml-apps/cargo-pgml-components/src/frontend/javascript.rs b/pgml-apps/cargo-pgml-components/src/frontend/javascript.rs index 29c1e5193..8784d2a98 100644 --- a/pgml-apps/cargo-pgml-components/src/frontend/javascript.rs +++ b/pgml-apps/cargo-pgml-components/src/frontend/javascript.rs @@ -9,6 +9,7 @@ use std::process::{exit, Command}; use convert_case::{Case, Casing}; +use crate::config::Config; use crate::frontend::tools::execute_with_nvm; use crate::util::{error, info, unwrap_or_exit, warn}; @@ -25,11 +26,6 @@ static JS_HASH_FILE: &'static str = "static/js/.pgml-bundle"; static MODULES_GLOB: &'static str = "src/components/**/*.js"; static STATIC_JS_GLOB: &'static str = "static/js/*.js"; -// Dashboard glob -static DASHBOARD_JS_GLOB: &'static str = "../deps/postgresml/pgml-dashboard/static/js/*.js"; -static DASHBOARD_COMPONENTS_JS_GLOB: &'static str = - "../deps/postgresml/pgml-dashboard/src/components/**/*.js"; - /// Finds old JS bundles we created. static OLD_BUNLDES_GLOB: &'static str = "static/js/*.*.js"; @@ -47,14 +43,22 @@ fn cleanup_old_bundles() { } } -fn assemble_modules() { +fn assemble_modules(config: Config) { let js = unwrap_or_exit!(glob(MODULES_GLOB)); - let js = js.chain(unwrap_or_exit!(glob(STATIC_JS_GLOB))); - let js = js.chain(unwrap_or_exit!(glob(DASHBOARD_JS_GLOB))); - let js = js.chain(unwrap_or_exit!(glob(DASHBOARD_COMPONENTS_JS_GLOB))); + let mut js = js + .chain(unwrap_or_exit!(glob(STATIC_JS_GLOB))) + .collect::>(); + + for path in &config.javascript.additional_paths { + debug!("adding additional path to javascript bundle: {}", path); + js = js + .into_iter() + .chain(unwrap_or_exit!(glob(path))) + .collect::>(); + } // Don't bundle artifacts we produce. - let js = js.filter(|path| { + let js = js.iter().filter(|path| { let path = path.as_ref().unwrap(); let path = path.display().to_string(); @@ -138,23 +142,28 @@ fn assemble_modules() { info(&format!("written {}", MODULES_FILE)); } -pub fn bundle() { +pub fn bundle(config: Config, minify: bool) { cleanup_old_bundles(); - assemble_modules(); + assemble_modules(config.clone()); + + let mut command = Command::new(JS_COMPILER); + + command + .arg(MODULES_FILE) + .arg("--file") + .arg(JS_FILE) + .arg("--format") + .arg("es") + .arg("-p") + .arg("@rollup/plugin-node-resolve"); + + if minify { + command.arg("-p").arg("@rollup/plugin-terser"); + } // Bundle JavaScript. info("bundling javascript with rollup"); - unwrap_or_exit!(execute_with_nvm( - Command::new(JS_COMPILER) - .arg(MODULES_FILE) - .arg("--file") - .arg(JS_FILE) - .arg("--format") - .arg("es") - .arg("-p") - .arg("@rollup/plugin-node-resolve") // .arg("-p") - // .arg("@rollup/plugin-terser"), - )); + unwrap_or_exit!(execute_with_nvm(&mut command)); info(&format!("written {}", JS_FILE)); diff --git a/pgml-apps/cargo-pgml-components/src/frontend/tools.rs b/pgml-apps/cargo-pgml-components/src/frontend/tools.rs index 2f27310a2..1f37ec5e9 100644 --- a/pgml-apps/cargo-pgml-components/src/frontend/tools.rs +++ b/pgml-apps/cargo-pgml-components/src/frontend/tools.rs @@ -1,8 +1,9 @@ //! Tools required by us to build stuff. -use crate::util::{debug1, error, execute_command, unwrap_or_exit, warn}; +use crate::util::{debug1, error, execute_command, info, unwrap_or_exit, warn}; use std::fs::File; use std::io::Write; +use std::path::Path; use std::process::{exit, Command}; /// Required tools. @@ -47,6 +48,11 @@ pub fn install() { )); } } + + if Path::new("package.json").exists() { + info("installing dependencies from package.json"); + unwrap_or_exit!(execute_with_nvm(Command::new("npm").arg("install"))); + } } /// Execute a command making sure that nvm is available. diff --git a/pgml-apps/cargo-pgml-components/src/main.rs b/pgml-apps/cargo-pgml-components/src/main.rs index a03d7069f..139720815 100644 --- a/pgml-apps/cargo-pgml-components/src/main.rs +++ b/pgml-apps/cargo-pgml-components/src/main.rs @@ -9,8 +9,11 @@ use std::path::Path; extern crate log; mod backend; +mod config; mod frontend; mod util; + +use config::Config; use util::{info, unwrap_or_exit}; /// These paths are exepcted to exist in the project directory. @@ -51,7 +54,10 @@ struct PgmlCommands { #[derive(Subcommand, Debug)] enum Commands { /// Bundle SASS and JavaScript into neat bundle files. - Bundle {}, + Bundle { + #[arg(short, long, default_value = "false")] + minify: bool, + }, /// Add new elements to the project. #[command(subcommand)] @@ -65,6 +71,7 @@ enum AddCommands { } fn main() { + let config = Config::load(); env_logger::init(); let cli = Cli::parse(); @@ -72,7 +79,7 @@ fn main() { CargoSubcommands::PgmlComponents(pgml_commands) => { validate_project(pgml_commands.project_path); match pgml_commands.command { - Commands::Bundle {} => bundle(), + Commands::Bundle { minify } => bundle(config, minify), Commands::Add(command) => match command { AddCommands::Component { name } => { crate::frontend::components::add(&Path::new(&name), pgml_commands.overwrite) @@ -108,9 +115,9 @@ fn validate_project(project_path: Option) { } /// Bundle SASS and JavaScript into neat bundle files. -fn bundle() { +fn bundle(config: Config, minify: bool) { frontend::sass::bundle(); - frontend::javascript::bundle(); + frontend::javascript::bundle(config, minify); frontend::components::update_modules(); info("bundle complete"); pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy