|
| 1 | +//! Ruby 固定解析器的有界项目版本约束;声明不适用时不制造源码诊断。 |
| 2 | +use codeguard_runtime::read_bounded_regular_file; |
| 3 | +use serde_json::Value; |
| 4 | +use std::{ |
| 5 | + io::ErrorKind, |
| 6 | + path::{Component, Path, PathBuf}, |
| 7 | + sync::atomic::{AtomicBool, Ordering}, |
| 8 | + time::Instant, |
| 9 | +}; |
| 10 | + |
| 11 | +/// 获取单文件入口的受限项目根;按工作台、Git、Gemfile优先级寻找最近标记,不执行配置。 |
| 12 | +pub(crate) fn source_root(source: &Path) -> Option<PathBuf> { |
| 13 | + let absolute = if source.is_absolute() { |
| 14 | + source.to_path_buf() |
| 15 | + } else { |
| 16 | + std::env::current_dir().ok()?.join(source) |
| 17 | + }; |
| 18 | + let parent = absolute.parent()?; |
| 19 | + Some( |
| 20 | + [".codeguard", ".git", "Gemfile"] |
| 21 | + .iter() |
| 22 | + .find_map(|name| { |
| 23 | + parent |
| 24 | + .ancestors() |
| 25 | + .take(64) |
| 26 | + .find(|p| std::fs::symlink_metadata(p.join(name)).is_ok()) |
| 27 | + }) |
| 28 | + .unwrap_or(parent) |
| 29 | + .to_path_buf(), |
| 30 | + ) |
| 31 | +} |
| 32 | + |
| 33 | +/// 版本观察输入,保留已有声明及更近目录中尚不存在的声明路径。 |
| 34 | +type VersionInputs = Vec<(PathBuf, Option<Vec<u8>>)>; |
| 35 | + |
| 36 | +/// 读取源码祖先内最近版本声明及所有更近缺项;返回原始字节快照或环境原因。 |
| 37 | +fn capture(root: &Path, source: &Path) -> Result<VersionInputs, &'static str> { |
| 38 | + if !root.is_absolute() |
| 39 | + || source |
| 40 | + .components() |
| 41 | + .any(|c| matches!(c, Component::ParentDir)) |
| 42 | + || !source.starts_with(root) |
| 43 | + { |
| 44 | + return Err("ruby_project_version_unresolved"); |
| 45 | + } |
| 46 | + let mut inputs = Vec::new(); |
| 47 | + for parent in source |
| 48 | + .parent() |
| 49 | + .ok_or("ruby_project_version_unresolved")? |
| 50 | + .ancestors() |
| 51 | + .take(64) |
| 52 | + { |
| 53 | + if !parent.starts_with(root) { |
| 54 | + break; |
| 55 | + } |
| 56 | + let mut prefix = PathBuf::new(); |
| 57 | + for component in parent.components() { |
| 58 | + prefix.push(component); |
| 59 | + if std::fs::symlink_metadata(&prefix) |
| 60 | + .map_err(|_| "ruby_project_version_unreadable")? |
| 61 | + .file_type() |
| 62 | + .is_symlink() |
| 63 | + { |
| 64 | + return Err("ruby_project_version_unreadable"); |
| 65 | + } |
| 66 | + } |
| 67 | + let path = parent.join(".ruby-version"); |
| 68 | + match std::fs::symlink_metadata(&path) { |
| 69 | + Err(e) if e.kind() == ErrorKind::NotFound => inputs.push((path, None)), |
| 70 | + Err(_) => return Err("ruby_project_version_unreadable"), |
| 71 | + Ok(_) => { |
| 72 | + let bytes = read_bounded_regular_file(&path, 4096) |
| 73 | + .map_err(|_| "ruby_project_version_unreadable")?; |
| 74 | + let text = std::str::from_utf8(&bytes) |
| 75 | + .map_err(|_| "ruby_project_version_unresolved")? |
| 76 | + .trim(); |
| 77 | + let version = text.strip_prefix("ruby-").unwrap_or(text); |
| 78 | + if !matches!(version, "2.6.10" | "2.6.10p210") { |
| 79 | + let numeric = version.split('.').collect::<Vec<_>>(); |
| 80 | + return Err( |
| 81 | + if numeric.len() == 3 |
| 82 | + && numeric |
| 83 | + .iter() |
| 84 | + .all(|p| !p.is_empty() && p.bytes().all(|b| b.is_ascii_digit())) |
| 85 | + { |
| 86 | + "ruby_project_version_mismatch" |
| 87 | + } else { |
| 88 | + "ruby_project_version_unresolved" |
| 89 | + }, |
| 90 | + ); |
| 91 | + } |
| 92 | + inputs.push((path, Some(bytes))); |
| 93 | + return Ok(inputs); |
| 94 | + } |
| 95 | + } |
| 96 | + if parent == root { |
| 97 | + return Ok(inputs); |
| 98 | + } |
| 99 | + } |
| 100 | + Err("ruby_project_version_unresolved") |
| 101 | +} |
| 102 | + |
| 103 | +/// 仅核对当前明确声明对固定解析器是否适用;无声明仍是未批准的初步观察。 |
| 104 | +pub(crate) fn compatible(root: &Path, source: &Path) -> bool { |
| 105 | + capture(root, source).is_ok() |
| 106 | +} |
| 107 | + |
| 108 | +/// 调用固定Ruby解析器并复核版本输入;参数绑定项目根、源码路径、字节和共同预算。 |
| 109 | +pub(crate) fn observe( |
| 110 | + root: &Path, |
| 111 | + source: &Path, |
| 112 | + tool: &Path, |
| 113 | + bytes: &[u8], |
| 114 | + deadline: Instant, |
| 115 | + cancelled: &AtomicBool, |
| 116 | +) -> Value { |
| 117 | + if cancelled.load(Ordering::Relaxed) || codeguard_runtime::sigint_cancellation_requested() { |
| 118 | + return crate::ruby_lint_command::unavailable("request_cancelled"); |
| 119 | + } |
| 120 | + if Instant::now() >= deadline { |
| 121 | + return crate::ruby_lint_command::unavailable("request_deadline_exceeded"); |
| 122 | + } |
| 123 | + let original = match capture(root, source) { |
| 124 | + Ok(inputs) => inputs, |
| 125 | + Err(reason) => return crate::ruby_lint_command::unavailable(reason), |
| 126 | + }; |
| 127 | + let report = crate::ruby_syntax_probe::observe(tool, bytes, deadline, cancelled); |
| 128 | + if capture(root, source).ok().as_ref() != Some(&original) { |
| 129 | + crate::ruby_lint_command::unavailable("ruby_project_version_changed_during_check") |
| 130 | + } else { |
| 131 | + report |
| 132 | + } |
| 133 | +} |
0 commit comments