Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit5446667

Browse files
authored
Add node resolver to pgml-components (#1037)
1 parent69caeea commit5446667

File tree

8 files changed

+136
-43
lines changed

8 files changed

+136
-43
lines changed

‎pgml-apps/cargo-pgml-components/Cargo.lock

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more aboutcustomizing how changed files appear on GitHub.

‎pgml-apps/cargo-pgml-components/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name ="cargo-pgml-components"
3-
version ="0.1.15"
3+
version ="0.1.16"
44
edition ="2021"
55
authors = ["PostgresML <team@postgresml.org>"]
66
license ="MIT"
@@ -19,6 +19,8 @@ anyhow = "1"
1919
owo-colors ="3"
2020
sailfish ="0.8"
2121
regex ="1"
22+
toml ="0.7"
23+
serde = {version ="1",features = ["derive"] }
2224

2325
[dev-dependencies]
2426
assert_cmd ="2"
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
use serde::{Deserialize,Serialize};
2+
3+
#[derive(Serialize,Deserialize,Default,Clone)]
4+
pubstructJavascript{
5+
#[serde(default ="#"diff-0ed63567528fa7cdeada2399f07e061325ae6a55f59530b50750ef873c4c6af1-empty-6-0" data-selected="false" role="gridcell" tabindex="-1" valign="top">
6+
pubadditional_paths:Vec<String>,
7+
}
8+
9+
implJavascript{
10+
fndefault_additional_paths() ->Vec<String>{
11+
vec![]
12+
}
13+
}
14+
15+
#[derive(Serialize,Deserialize,Default,Clone)]
16+
pubstructConfig{
17+
pubjavascript:Javascript,
18+
}
19+
20+
implConfig{
21+
pubfnfrom_path(path:&str) -> anyhow::Result<Config>{
22+
let config_str = std::fs::read_to_string(path)?;
23+
let config:Config = toml::from_str(&config_str)?;
24+
Ok(config)
25+
}
26+
27+
pubfnload() ->Config{
28+
matchSelf::from_path("pgml-components.toml"){
29+
Ok(config) => config,
30+
Err(_) =>Config::default(),
31+
}
32+
}
33+
}

‎pgml-apps/cargo-pgml-components/src/frontend/components.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -191,10 +191,7 @@ fn update_module(path: &Path) {
191191
}
192192

193193
ifhas_more_modules(&path){
194-
debug!("{} has more modules", path.display());
195194
update_module(&path);
196-
}else{
197-
debug!("it does not really no");
198195
}
199196

200197
let component_path = path.components().skip(2).collect::<PathBuf>();
@@ -205,8 +202,7 @@ fn update_module(path: &Path) {
205202
debug!("writing {} modules to mod.rs", modules.len());
206203

207204
let components_mod = path.join("mod.rs");
208-
let modules =
209-
unwrap_or_exit!(templates::Mod{ modules}.render_once()).replace("\n\n","\n");
205+
let modules =unwrap_or_exit!(templates::Mod{ modules}.render_once()).replace("\n\n","\n");
210206

211207
let existing_modules =if components_mod.is_file(){
212208
unwrap_or_exit!(read_to_string(&components_mod))
@@ -220,15 +216,15 @@ fn update_module(path: &Path) {
220216
info(&format!("written {}", components_mod.display().to_string()));
221217
}
222218

223-
debug!("mod.rs isthe same");
219+
debug!("{}/mod.rs isdifferent", components_mod.display());
224220
}
225221

226222
/// Check that the path has more Rust modules.
227223
fnhas_more_modules(path:&Path) ->bool{
228224
debug!("checking if {} has more modules", path.display());
229225

230226
if !path.exists(){
231-
debug!("path does not exist");
227+
debug!("path{}does not exist", path.display());
232228
returnfalse;
233229
}
234230

@@ -244,13 +240,12 @@ fn has_more_modules(path: &Path) -> bool {
244240

245241
ifletSome(file_name) = path.file_name(){
246242
if file_name !="mod.rs"{
247-
debug!("it has another file that's not mod.rs");
243+
debug!("{} has another file that's not mod.rs", path.display());
248244
returnfalse;
249245
}
250246
}
251247
}
252248

253-
debug!("it does");
254249
true
255250
}
256251

‎pgml-apps/cargo-pgml-components/src/frontend/javascript.rs

Lines changed: 56 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use std::process::{exit, Command};
99

1010
use convert_case::{Case,Casing};
1111

12+
usecrate::config::Config;
1213
usecrate::frontend::tools::execute_with_nvm;
1314
usecrate::util::{error, info, unwrap_or_exit, warn};
1415

@@ -42,16 +43,26 @@ fn cleanup_old_bundles() {
4243
}
4344
}
4445

45-
fnassemble_modules(){
46+
fnassemble_modules(config:Config){
4647
let js =unwrap_or_exit!(glob(MODULES_GLOB));
47-
let js = js.chain(unwrap_or_exit!(glob(STATIC_JS_GLOB)));
48+
letmut js = js
49+
.chain(unwrap_or_exit!(glob(STATIC_JS_GLOB)))
50+
.collect::<Vec<_>>();
51+
52+
for pathin&config.javascript.additional_paths{
53+
debug!("adding additional path to javascript bundle: {}", path);
54+
js = js
55+
.into_iter()
56+
.chain(unwrap_or_exit!(glob(path)))
57+
.collect::<Vec<_>>();
58+
}
4859

4960
// Don't bundle artifacts we produce.
50-
let js = js.filter(|path|{
61+
let js = js.iter().filter(|path|{
5162
let path = path.as_ref().unwrap();
5263
let path = path.display().to_string();
5364

54-
!path.contains("main.js") && !path.contains("bundle.js") && !path.contains("modules.js")
65+
!path.contains("main.") && !path.contains("bundle.") && !path.contains("modules.")
5566
});
5667

5768
letmut modules =unwrap_or_exit!(File::create(MODULES_FILE));
@@ -75,27 +86,37 @@ fn assemble_modules() {
7586

7687
let full_path = source.display().to_string();
7788

78-
let path = source
79-
.components()
80-
.skip(2)// skip src/components or static/js
81-
.collect::<Vec<_>>();
89+
let path = source.components().collect::<Vec<_>>();
8290

8391
assert!(!path.is_empty());
8492

8593
let path = path.iter().collect::<PathBuf>();
8694
let components = path.components();
87-
let controller_name =if components.clone().count() >1{
88-
components
95+
let file_stem = path.file_stem().unwrap().to_str().unwrap().to_string();
96+
let controller_name =if file_stem.ends_with("controller"){
97+
letmut parts =vec![];
98+
99+
let pp = components
89100
.map(|c| c.as_os_str().to_str().expect("component to be valid utf-8"))
90101
.filter(|c| !c.ends_with(".js"))
91-
.collect::<Vec<&str>>()
92-
.join("_")
102+
.collect::<Vec<&str>>();
103+
letmut saw_src =false;
104+
letmut saw_components =false;
105+
for pin pp{
106+
if p =="src"{
107+
saw_src =true;
108+
}elseif p =="components"{
109+
saw_components =true;
110+
}elseif saw_src && saw_components{
111+
parts.push(p);
112+
}
113+
}
114+
115+
assert!(!parts.is_empty());
116+
117+
parts.join("_")
93118
}else{
94-
path.file_stem()
95-
.expect("old controllers to be a single file")
96-
.to_str()
97-
.expect("stemp to be valid utf-8")
98-
.to_string()
119+
file_stem
99120
};
100121
let upper_camel = controller_name.to_case(Case::UpperCamel).to_string();
101122
let controller_name = controller_name.replace("_","-");
@@ -121,20 +142,28 @@ fn assemble_modules() {
121142
info(&format!("written {}",MODULES_FILE));
122143
}
123144

124-
pubfnbundle(){
145+
pubfnbundle(config:Config,minify:bool){
125146
cleanup_old_bundles();
126-
assemble_modules();
147+
assemble_modules(config.clone());
148+
149+
letmut command =Command::new(JS_COMPILER);
150+
151+
command
152+
.arg(MODULES_FILE)
153+
.arg("--file")
154+
.arg(JS_FILE)
155+
.arg("--format")
156+
.arg("es")
157+
.arg("-p")
158+
.arg("@rollup/plugin-node-resolve");
159+
160+
if minify{
161+
command.arg("-p").arg("@rollup/plugin-terser");
162+
}
127163

128164
// Bundle JavaScript.
129165
info("bundling javascript with rollup");
130-
unwrap_or_exit!(execute_with_nvm(
131-
Command::new(JS_COMPILER)
132-
.arg(MODULES_FILE)
133-
.arg("--file")
134-
.arg(JS_FILE)
135-
.arg("--format")
136-
.arg("es"),
137-
));
166+
unwrap_or_exit!(execute_with_nvm(&mut command));
138167

139168
info(&format!("written {}",JS_FILE));
140169

‎pgml-apps/cargo-pgml-components/src/frontend/tools.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
//! Tools required by us to build stuff.
22
3-
usecrate::util::{debug1, error, execute_command, unwrap_or_exit, warn};
3+
usecrate::util::{debug1, error, execute_command,info,unwrap_or_exit, warn};
44
use std::fs::File;
55
use std::io::Write;
6+
use std::path::Path;
67
use std::process::{exit,Command};
78

89
/// Required tools.
910
staticTOOLS:&[&str] =&["sass","rollup"];
11+
staticROLLUP_PLUGINS:&[&str] =&["@rollup/plugin-terser","@rollup/plugin-node-resolve"];
1012
staticNVM_EXEC:&'staticstr ="/tmp/pgml-components-nvm.sh";
1113
staticNVM_SOURCE:&'staticstr ="https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.5/install.sh";
1214
staticNVM_SOURCE_DOWNLOADED:&'staticstr ="/tmp/pgml-components-nvm-source.sh";
@@ -30,6 +32,27 @@ pub fn install() {
3032
}
3133
}
3234
}
35+
36+
for plugininROLLUP_PLUGINS{
37+
ifexecute_with_nvm(
38+
Command::new("rollup")
39+
.arg("-p")
40+
.arg(plugin)
41+
.arg("--version"),
42+
)
43+
.is_err()
44+
{
45+
warn(&format!("installing rollup plugin {}", plugin));
46+
unwrap_or_exit!(execute_with_nvm(
47+
Command::new("npm").arg("install").arg("-g").arg(plugin)
48+
));
49+
}
50+
}
51+
52+
ifPath::new("package.json").exists(){
53+
info("installing dependencies from package.json");
54+
unwrap_or_exit!(execute_with_nvm(Command::new("npm").arg("install")));
55+
}
3356
}
3457

3558
/// Execute a command making sure that nvm is available.

‎pgml-apps/cargo-pgml-components/src/main.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@ use std::path::Path;
99
externcrate log;
1010

1111
mod backend;
12+
mod config;
1213
mod frontend;
1314
mod util;
15+
16+
use config::Config;
1417
use util::{info, unwrap_or_exit};
1518

1619
/// These paths are exepcted to exist in the project directory.
@@ -51,7 +54,10 @@ struct PgmlCommands {
5154
#[derive(Subcommand,Debug)]
5255
enumCommands{
5356
/// Bundle SASS and JavaScript into neat bundle files.
54-
Bundle{},
57+
Bundle{
58+
#[arg(short, long, default_value ="false")]
59+
minify:bool,
60+
},
5561

5662
/// Add new elements to the project.
5763
#[command(subcommand)]
@@ -65,14 +71,15 @@ enum AddCommands {
6571
}
6672

6773
fnmain(){
74+
let config =Config::load();
6875
env_logger::init();
6976
let cli =Cli::parse();
7077

7178
match cli.subcomand{
7279
CargoSubcommands::PgmlComponents(pgml_commands) =>{
7380
validate_project(pgml_commands.project_path);
7481
match pgml_commands.command{
75-
Commands::Bundle{} =>bundle(),
82+
Commands::Bundle{ minify} =>bundle(config, minify),
7683
Commands::Add(command) =>match command{
7784
AddCommands::Component{ name} =>{
7885
crate::frontend::components::add(&Path::new(&name), pgml_commands.overwrite)
@@ -108,9 +115,9 @@ fn validate_project(project_path: Option<String>) {
108115
}
109116

110117
/// Bundle SASS and JavaScript into neat bundle files.
111-
fnbundle(){
118+
fnbundle(config:Config,minify:bool){
112119
frontend::sass::bundle();
113-
frontend::javascript::bundle();
120+
frontend::javascript::bundle(config, minify);
114121
frontend::components::update_modules();
115122

116123
info("bundle complete");

‎pgml-apps/cargo-pgml-components/src/util.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ pub fn warn(value: &str) {
3939
}
4040

4141
pubfnexecute_command(command:&mutCommand) -> std::io::Result<String>{
42+
debug!("Executing {:?}", command);
43+
4244
let output =match command.output(){
4345
Ok(output) => output,
4446
Err(err) =>{

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp