Skip to content

[pgml-components] Add template-only option & remove boilerplate #1403

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/cargo-pgml-components/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/cargo-pgml-components/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cargo-pgml-components"
version = "0.1.24"
version = "0.1.25"
edition = "2021"
authors = ["PostgresML <team@postgresml.org>"]
license = "MIT"
Expand Down
18 changes: 11 additions & 7 deletions packages/cargo-pgml-components/src/frontend/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ impl From<&Path> for Component {
}

/// Add a new component.
pub fn add(path: &Path, overwrite: bool) {
pub fn add(path: &Path, overwrite: bool, template_only: bool) {
if let Some(_extension) = path.extension() {
error("component name should not contain an extension");
exit(1);
Expand Down Expand Up @@ -154,17 +154,21 @@ pub fn add(path: &Path, overwrite: bool) {
unwrap_or_exit!(write_to_file(&html_path, &html));
info(&format!("written {}", html_path.display()));

let stimulus_path = path.join(&component.controller_path());
unwrap_or_exit!(write_to_file(&stimulus_path, &stimulus));
info(&format!("written {}", stimulus_path.display()));
if !template_only {
let stimulus_path = path.join(&component.controller_path());
unwrap_or_exit!(write_to_file(&stimulus_path, &stimulus));
info(&format!("written {}", stimulus_path.display()));
}

let rust_path = path.join("mod.rs");
unwrap_or_exit!(write_to_file(&rust_path, &rust));
info(&format!("written {}", rust_path.display()));

let scss_path = path.join(&format!("{}.scss", component.name()));
unwrap_or_exit!(write_to_file(&scss_path, &scss));
info(&format!("written {}", scss_path.display()));
if !template_only {
let scss_path = path.join(&format!("{}.scss", component.name()));
unwrap_or_exit!(write_to_file(&scss_path, &scss));
info(&format!("written {}", scss_path.display()));
}

update_modules();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,11 @@ use pgml_components::component;

#[derive(TemplateOnce, Default)]
#[template(path = "<%= component.path() %>/template.html")]
pub struct <%= component.rust_name() %> {
value: String,
}
pub struct <%= component.rust_name() %> {}

impl <%= component.rust_name() %> {
pub fn new() -> <%= component.rust_name() %> {
<%= component.rust_name() %> {
value: String::from("<%= component.full_path() %>"),
}
<%= component.rust_name() %> {}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
div[data-controller="<%= component.controller_name() %>"] {
// Used to identify the component in the DOM.
// Delete these styles if you don't need them.
min-width: 100px;
width: 100%;
height: 100px;

background: red;

display: flex;
justify-content: center;
align-items: center;

h3 {
color: white;
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Controller } from '@hotwired/stimulus'

export default class extends Controller {
static targets = []
static outlets = []
static targets = [];
static outlets = [];

initialize() {
console.log('Initialized <%= controller_name %>')
console.log("Initialized <%= controller_name %>");
}

connect() {}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
<div data-controller="<%= component.controller_name() %>">
<h3 class="text-center h3">
<%%= value %>
</h3>

</div>
20 changes: 16 additions & 4 deletions packages/cargo-pgml-components/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,14 @@ enum Commands {
#[derive(Subcommand, Debug)]
enum AddCommands {
/// Add a new component.
Component { name: String },
Component {
/// Name of the new component.
name: String,

/// Generate only the HTML template. Don't generate SCSS and JavaScript.
#[arg(short, long, default_value = "false")]
template_only: bool,
},
}

#[derive(Subcommand, Debug)]
Expand All @@ -114,9 +121,14 @@ fn main() {
lock,
} => bundle(config, minify, debug, lock),
Commands::Add(command) => match command {
AddCommands::Component { name } => {
crate::frontend::components::add(&Path::new(&name), pgml_commands.overwrite)
}
AddCommands::Component {
name,
template_only,
} => crate::frontend::components::add(
&Path::new(&name),
pgml_commands.overwrite,
template_only,
),
},
Commands::LocalDev(command) => match command {
LocalDevCommands::Check {} => local_dev::setup(),
Expand Down
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