```text
chore: 新增项目规则文档、构建别名及忽略规则 添加 AGENTS.md 项目规则文件,涵盖语言与工具链、模块设计、 Git 提交规范及验证流程等内容。 变更要点: - .cargo/config.toml 新增 xtask 命令别名 - .gitignore 忽略运行时地图缓存、接收器配置文件、 构建锁文件及本地 docs/ 目录 - 新增 AGENTS.md 定义项目编码、提交与审查规则 ```
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
[package]
|
||||
name = "xtask"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
serde_json = "1"
|
||||
@@ -0,0 +1,177 @@
|
||||
use std::{
|
||||
fs,
|
||||
path::{Path, PathBuf},
|
||||
process::{Command, ExitCode},
|
||||
};
|
||||
fn run(command: &mut Command) -> Result<(), String> {
|
||||
let status = command.status().map_err(|e| e.to_string())?;
|
||||
if status.success() {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(format!("command failed: {command:?}"))
|
||||
}
|
||||
}
|
||||
fn tool(env: &str, binary: &str, local: &str) -> PathBuf {
|
||||
std::env::var_os(env).map(PathBuf::from).unwrap_or_else(|| {
|
||||
let fallback = PathBuf::from(local);
|
||||
if fallback.is_file() {
|
||||
fallback
|
||||
} else {
|
||||
PathBuf::from(binary)
|
||||
}
|
||||
})
|
||||
}
|
||||
fn web(root: &Path) -> Result<(), String> {
|
||||
let _build_guard = if std::env::var_os("RID_PACKAGE_BUILD").is_none() {
|
||||
let file = fs::OpenOptions::new()
|
||||
.create(true)
|
||||
.truncate(false)
|
||||
.read(true)
|
||||
.write(true)
|
||||
.open(root.join(".build.lock"))
|
||||
.map_err(|e| format!("build lock unavailable: {e}"))?;
|
||||
file.try_lock()
|
||||
.map_err(|e| format!("another build is running: {e}"))?;
|
||||
Some(file)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let trunk = tool(
|
||||
"RID_TRUNK",
|
||||
"trunk",
|
||||
"D:/Rust_Contact/util/tools/trunk-0.22.0/trunk.exe",
|
||||
);
|
||||
let bindgen = tool(
|
||||
"RID_WASM_BINDGEN",
|
||||
"wasm-bindgen",
|
||||
"D:/Rust_Contact/util/tools/wasm-bindgen-0.2.129/wasm-bindgen-0.2.129-x86_64-pc-windows-msvc/wasm-bindgen.exe",
|
||||
);
|
||||
for (tool, expected) in [(&trunk, "0.22.0"), (&bindgen, "0.2.129")] {
|
||||
let output = Command::new(tool)
|
||||
.arg("--version")
|
||||
.output()
|
||||
.map_err(|e| e.to_string())?;
|
||||
if !output.status.success() || !String::from_utf8_lossy(&output.stdout).contains(expected) {
|
||||
return Err(format!(
|
||||
"expected tool version {expected}: {}",
|
||||
tool.display()
|
||||
));
|
||||
}
|
||||
}
|
||||
let mut path = vec![bindgen.parent().unwrap().to_owned()];
|
||||
path.extend(std::env::split_paths(
|
||||
&std::env::var_os("PATH").unwrap_or_default(),
|
||||
));
|
||||
run(Command::new(trunk)
|
||||
.args([
|
||||
"build",
|
||||
"--release",
|
||||
"--locked",
|
||||
"--dist",
|
||||
"../../dist/web-ui",
|
||||
])
|
||||
.env(
|
||||
"PATH",
|
||||
std::env::join_paths(path).map_err(|e| e.to_string())?,
|
||||
)
|
||||
.current_dir(root.join("crates/web-ui")))?;
|
||||
// Externalize Trunk's inline module initializer to preserve a strict CSP.
|
||||
let index = root.join("dist/web-ui/index.html");
|
||||
let mut html = fs::read_to_string(&index).map_err(|e| e.to_string())?;
|
||||
let mut n = 0;
|
||||
let mut cursor = 0;
|
||||
while let Some(offset) = html[cursor..].find("<script") {
|
||||
let start = cursor + offset;
|
||||
let tag_end = start + html[start..].find('>').ok_or("invalid script tag")?;
|
||||
let end = tag_end
|
||||
+ 1
|
||||
+ html[tag_end + 1..]
|
||||
.find("</script>")
|
||||
.ok_or("invalid script close")?;
|
||||
if !html[start..tag_end].contains("src=") {
|
||||
let filename = format!("bootstrap-{n}.js");
|
||||
fs::write(
|
||||
root.join("dist/web-ui").join(&filename),
|
||||
&html[tag_end + 1..end],
|
||||
)
|
||||
.map_err(|e| e.to_string())?;
|
||||
let replacement = format!("<script type=\"module\" src=\"/{filename}\"></script>");
|
||||
html.replace_range(start..end + 9, &replacement);
|
||||
cursor = start + replacement.len();
|
||||
n += 1;
|
||||
} else {
|
||||
cursor = end + 9;
|
||||
}
|
||||
}
|
||||
fs::write(index, html).map_err(|e| e.to_string())?;
|
||||
run(Command::new("cargo")
|
||||
.args([
|
||||
"build",
|
||||
"--release",
|
||||
"--locked",
|
||||
"--target",
|
||||
"wasm32-unknown-unknown",
|
||||
"-p",
|
||||
"rid-web-worker",
|
||||
])
|
||||
.current_dir(root))?;
|
||||
let metadata = Command::new("cargo")
|
||||
.args(["metadata", "--format-version", "1", "--no-deps"])
|
||||
.current_dir(root)
|
||||
.output()
|
||||
.map_err(|e| e.to_string())?;
|
||||
if !metadata.status.success() {
|
||||
return Err("cargo metadata failed".into());
|
||||
}
|
||||
let metadata: serde_json::Value =
|
||||
serde_json::from_slice(&metadata.stdout).map_err(|e| e.to_string())?;
|
||||
let wasm = PathBuf::from(
|
||||
metadata["target_directory"]
|
||||
.as_str()
|
||||
.ok_or("target_directory missing")?,
|
||||
)
|
||||
.join("wasm32-unknown-unknown/release/rid_web_worker.wasm");
|
||||
let workers = root.join("dist/web-ui/workers");
|
||||
fs::create_dir_all(&workers).map_err(|e| e.to_string())?;
|
||||
run(Command::new(bindgen)
|
||||
.arg(wasm)
|
||||
.args(["--target", "web", "--out-dir"])
|
||||
.arg(&workers))?;
|
||||
fs::copy(
|
||||
root.join("crates/web-worker/entry.js"),
|
||||
workers.join("entry.js"),
|
||||
)
|
||||
.map_err(|e| e.to_string())?;
|
||||
Ok(())
|
||||
}
|
||||
fn main() -> ExitCode {
|
||||
let root = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
|
||||
.parent()
|
||||
.unwrap()
|
||||
.to_owned();
|
||||
let command = std::env::args().nth(1).unwrap_or("check".into());
|
||||
let result = match command.as_str() {
|
||||
"web" => web(&root),
|
||||
"check" => run(Command::new("cargo")
|
||||
.args(["test", "--workspace", "--exclude", "rid-web-ui", "--locked"])
|
||||
.current_dir(&root)),
|
||||
"package" => run(Command::new("powershell")
|
||||
.env_remove("PSModulePath")
|
||||
.args([
|
||||
"-NoProfile",
|
||||
"-ExecutionPolicy",
|
||||
"Bypass",
|
||||
"-File",
|
||||
"scripts/package.ps1",
|
||||
])
|
||||
.current_dir(&root)),
|
||||
_ => Err("usage: cargo xtask [web|check|package]".into()),
|
||||
};
|
||||
match result {
|
||||
Ok(()) => ExitCode::SUCCESS,
|
||||
Err(e) => {
|
||||
eprintln!("{e}");
|
||||
ExitCode::FAILURE
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user