🌐 AI搜索 & 代理 主页
Skip to content

Commit 7225d9f

Browse files
committed
Add lock ability to pgml-components
1 parent 7627d4d commit 7225d9f

File tree

5 files changed

+74
-7
lines changed

5 files changed

+74
-7
lines changed

packages/cargo-pgml-components/Cargo.lock

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

packages/cargo-pgml-components/Cargo.toml

Lines changed: 2 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.18-alpha.2"
3+
version = "0.1.19"
44
edition = "2021"
55
authors = ["PostgresML <team@postgresml.org>"]
66
license = "MIT"
@@ -21,6 +21,7 @@ sailfish = "0.8"
2121
regex = "1"
2222
toml = "0.7"
2323
serde = { version = "1", features = ["derive"] }
24+
file-lock = "2"
2425

2526
[dev-dependencies]
2627
assert_cmd = "2"

packages/cargo-pgml-components/src/frontend/sass.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,6 @@ fn cleanup_old_bundles() {
7272

7373
/// Entrypoint.
7474
pub fn bundle() {
75-
crate::frontend::tools::install();
76-
7775
assemble_modules();
7876
cleanup_old_bundles();
7977

packages/cargo-pgml-components/src/frontend/tools.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,13 @@ fn install_node() {
116116
warn("installed node")
117117
}
118118
}
119+
120+
pub fn debug() {
121+
let node = unwrap_or_exit!(execute_with_nvm(Command::new("which").arg("node")));
122+
println!("node: {}", node.trim());
123+
124+
for tool in TOOLS {
125+
let output = unwrap_or_exit!(execute_with_nvm(Command::new("which").arg(tool)));
126+
println!("{}: {}", tool, output.trim());
127+
}
128+
}

packages/cargo-pgml-components/src/main.rs

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
//! A tool to assemble and bundle our frontend components.
22
33
use clap::{Args, Parser, Subcommand};
4+
use file_lock::{FileLock, FileOptions};
45
use std::env::{current_dir, set_current_dir};
5-
use std::fs::create_dir_all;
6+
use std::fs::{create_dir_all, File};
7+
use std::io::Write;
68
use std::path::Path;
79

810
#[macro_use]
@@ -58,6 +60,12 @@ enum Commands {
5860
Bundle {
5961
#[arg(short, long, default_value = "false")]
6062
minify: bool,
63+
64+
#[arg(short, long, default_value = "false")]
65+
debug: bool,
66+
67+
#[arg(short, long, default_value = "false")]
68+
lock: bool,
6169
},
6270

6371
/// Add new elements to the project.
@@ -91,7 +99,11 @@ fn main() {
9199
CargoSubcommands::PgmlComponents(pgml_commands) => {
92100
validate_project(pgml_commands.project_path);
93101
match pgml_commands.command {
94-
Commands::Bundle { minify } => bundle(config, minify),
102+
Commands::Bundle {
103+
minify,
104+
debug,
105+
lock,
106+
} => bundle(config, minify, debug, lock),
95107
Commands::Add(command) => match command {
96108
AddCommands::Component { name } => {
97109
crate::frontend::components::add(&Path::new(&name), pgml_commands.overwrite)
@@ -131,10 +143,45 @@ fn validate_project(project_path: Option<String>) {
131143
}
132144

133145
/// Bundle SASS and JavaScript into neat bundle files.
134-
fn bundle(config: Config, minify: bool) {
146+
fn bundle(config: Config, minify: bool, debug: bool, lock: bool) {
147+
let lock = if lock { Some(acquire_lock()) } else { None };
148+
149+
if debug {
150+
frontend::tools::debug();
151+
}
152+
153+
frontend::tools::install();
154+
155+
if debug {
156+
frontend::tools::debug();
157+
}
158+
135159
frontend::sass::bundle();
136160
frontend::javascript::bundle(config, minify);
137161
frontend::components::update_modules();
138162

139163
info("bundle complete");
164+
165+
if let Some(lock) = lock {
166+
unwrap_or_exit!(lock.unlock());
167+
}
168+
}
169+
170+
fn acquire_lock() -> FileLock {
171+
print!("acquiring lock...");
172+
unwrap_or_exit!(std::io::stdout().flush());
173+
174+
let file = "/tmp/pgml-components-lock";
175+
176+
// Create file if not exists
177+
if !Path::new(file).exists() {
178+
unwrap_or_exit!(File::create(file));
179+
}
180+
181+
let options = FileOptions::new().write(true).create(true).append(true);
182+
183+
let lock = unwrap_or_exit!(FileLock::lock(file, true, options));
184+
185+
info("ok");
186+
lock
140187
}

0 commit comments

Comments
 (0)