feat: add source and public protocol inputs
This commit is contained in:
1 parent
68b14db095
commit
e67697be06
30 files changed
+3021
No files matched your search
@@ -0,0 +1,124 @@
|
||||
use std::{
|
||||
fs,
|
||||
path::{Path, PathBuf},
|
||||
process::{Command, Output},
|
||||
};
|
||||
|
||||
use serde_json::Value;
|
||||
use tempfile::NamedTempFile;
|
||||
|
||||
use crate::error::{io, LintError, Result};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct MarqdoRunner {
|
||||
binary: PathBuf,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct CheckOutcome {
|
||||
pub success: bool,
|
||||
pub analysis: Option<Value>,
|
||||
pub output: String,
|
||||
}
|
||||
|
||||
impl MarqdoRunner {
|
||||
pub fn discover(explicit: Option<&Path>) -> Result<Self> {
|
||||
let binary = explicit
|
||||
.map(Path::to_path_buf)
|
||||
.or_else(|| std::env::var_os("MARQDO_BIN").map(PathBuf::from))
|
||||
.unwrap_or_else(|| PathBuf::from("marqdo"));
|
||||
let runner = Self { binary };
|
||||
runner.ensure_compatible()?;
|
||||
Ok(runner)
|
||||
}
|
||||
|
||||
pub fn binary(&self) -> &Path {
|
||||
&self.binary
|
||||
}
|
||||
|
||||
pub fn ensure_compatible(&self) -> Result<()> {
|
||||
let output = self.run(&[std::ffi::OsStr::new("--version")])?;
|
||||
if !output.status.success() {
|
||||
return Err(LintError::Core(format_output(&output)));
|
||||
}
|
||||
let version = String::from_utf8_lossy(&output.stdout);
|
||||
let version = version.trim().strip_prefix("marqdo ").unwrap_or(version.trim());
|
||||
let major = version
|
||||
.split('.')
|
||||
.next()
|
||||
.and_then(|part| part.parse::<u64>().ok());
|
||||
if major != Some(3) {
|
||||
return Err(LintError::Protocol(format!(
|
||||
"需要 Marqdo >=3.0.0-beta.1,<4.0.0,实际为 {version}"
|
||||
)));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn inspect(&self, file: &Path) -> Result<Value> {
|
||||
let output = Command::new(&self.binary)
|
||||
.arg("inspect")
|
||||
.arg(file)
|
||||
.arg("--ast")
|
||||
.arg("--json")
|
||||
.output()
|
||||
.map_err(|error| io(&self.binary, error))?;
|
||||
if !output.status.success() {
|
||||
return Err(LintError::Core(format_output(&output)));
|
||||
}
|
||||
let value: Value = serde_json::from_slice(&output.stdout)
|
||||
.map_err(|error| LintError::Protocol(format!("inspect AST JSON 无效:{error}")))?;
|
||||
if value.get("command").and_then(Value::as_str) != Some("inspect") {
|
||||
return Err(LintError::Protocol("inspect 输出缺少 command=inspect".into()));
|
||||
}
|
||||
value
|
||||
.get("ast")
|
||||
.cloned()
|
||||
.ok_or_else(|| LintError::Protocol("inspect 输出缺少 ast 字段".into()))
|
||||
}
|
||||
|
||||
pub fn check(&self, file: &Path) -> Result<CheckOutcome> {
|
||||
let temporary = NamedTempFile::new().map_err(|error| io("Analysis 临时文件", error))?;
|
||||
let output = Command::new(&self.binary)
|
||||
.arg("check")
|
||||
.arg(file)
|
||||
.arg("--emit")
|
||||
.arg(temporary.path())
|
||||
.output()
|
||||
.map_err(|error| io(&self.binary, error))?;
|
||||
let text = format_output(&output);
|
||||
if !output.status.success() {
|
||||
return Ok(CheckOutcome {
|
||||
success: false,
|
||||
analysis: None,
|
||||
output: text,
|
||||
});
|
||||
}
|
||||
let bytes = fs::read(temporary.path()).map_err(|error| io(temporary.path(), error))?;
|
||||
let analysis = serde_json::from_slice(&bytes)
|
||||
.map_err(|error| LintError::Protocol(format!("Analysis JSON 无效:{error}")))?;
|
||||
Ok(CheckOutcome {
|
||||
success: true,
|
||||
analysis: Some(analysis),
|
||||
output: text,
|
||||
})
|
||||
}
|
||||
|
||||
fn run(&self, args: &[&std::ffi::OsStr]) -> Result<Output> {
|
||||
Command::new(&self.binary)
|
||||
.args(args)
|
||||
.output()
|
||||
.map_err(|error| io(&self.binary, error))
|
||||
}
|
||||
}
|
||||
|
||||
fn format_output(output: &Output) -> String {
|
||||
let stdout = String::from_utf8_lossy(&output.stdout);
|
||||
let stderr = String::from_utf8_lossy(&output.stderr);
|
||||
match (stdout.trim().is_empty(), stderr.trim().is_empty()) {
|
||||
(false, false) => format!("{}\n{}", stdout.trim(), stderr.trim()),
|
||||
(false, true) => stdout.trim().to_string(),
|
||||
(true, false) => stderr.trim().to_string(),
|
||||
(true, true) => format!("核心退出状态 {}", output.status),
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user