dart-decimate 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (188) hide show
  1. package/Cargo.lock +572 -0
  2. package/Cargo.toml +41 -0
  3. package/LICENSE +21 -0
  4. package/README.md +617 -0
  5. package/assets/dart-decimate-banner.png +0 -0
  6. package/npm/bin/dart-decimate-mcp.js +48 -0
  7. package/npm/bin/dart-decimate.js +48 -0
  8. package/npm/scripts/postinstall.js +46 -0
  9. package/npm/scripts/test-npx-local.js +54 -0
  10. package/npm/scripts/test-npx-mcp-local.js +56 -0
  11. package/package.json +45 -0
  12. package/src/baseline.rs +643 -0
  13. package/src/bin/dart-decimate-mcp.rs +11 -0
  14. package/src/boundaries/tests.rs +60 -0
  15. package/src/boundaries.rs +317 -0
  16. package/src/changed_scope.rs +117 -0
  17. package/src/ci_template.rs +399 -0
  18. package/src/cli/analyze.rs +462 -0
  19. package/src/cli/analyzer_options.rs +82 -0
  20. package/src/cli/audit_run.rs +327 -0
  21. package/src/cli/boundary_args.rs +44 -0
  22. package/src/cli/ci_template_run.rs +196 -0
  23. package/src/cli/command_name.rs +20 -0
  24. package/src/cli/common_args.rs +167 -0
  25. package/src/cli/config_run.rs +78 -0
  26. package/src/cli/coverage_run.rs +393 -0
  27. package/src/cli/decision_surface_run.rs +99 -0
  28. package/src/cli/default_command.rs +69 -0
  29. package/src/cli/dupes_args.rs +150 -0
  30. package/src/cli/entry_points.rs +121 -0
  31. package/src/cli/error.rs +149 -0
  32. package/src/cli/error_output.rs +68 -0
  33. package/src/cli/explain_run.rs +72 -0
  34. package/src/cli/fix_run.rs +162 -0
  35. package/src/cli/flags_args.rs +23 -0
  36. package/src/cli/health_args.rs +200 -0
  37. package/src/cli/hooks_run.rs +191 -0
  38. package/src/cli/impact_run.rs +95 -0
  39. package/src/cli/init_run.rs +49 -0
  40. package/src/cli/inspect_args.rs +20 -0
  41. package/src/cli/inspect_run.rs +66 -0
  42. package/src/cli/issue_filter_args.rs +240 -0
  43. package/src/cli/list_run.rs +311 -0
  44. package/src/cli/mode_args.rs +72 -0
  45. package/src/cli/output_format.rs +62 -0
  46. package/src/cli/regression_args.rs +156 -0
  47. package/src/cli/request_args.rs +175 -0
  48. package/src/cli/schema_commands.rs +72 -0
  49. package/src/cli/scope_args.rs +174 -0
  50. package/src/cli/security_args.rs +175 -0
  51. package/src/cli/security_gate_run.rs +66 -0
  52. package/src/cli/security_summary_run.rs +43 -0
  53. package/src/cli/summary_args.rs +12 -0
  54. package/src/cli/tests.rs +516 -0
  55. package/src/cli/trace_args.rs +67 -0
  56. package/src/cli/trace_run.rs +131 -0
  57. package/src/cli/trace_tests.rs +235 -0
  58. package/src/cli/unsupported_run.rs +116 -0
  59. package/src/cli/watch_run.rs +153 -0
  60. package/src/cli.rs +603 -0
  61. package/src/config/boundary_config.rs +235 -0
  62. package/src/config/cache.rs +25 -0
  63. package/src/config/dependencies.rs +114 -0
  64. package/src/config/health.rs +237 -0
  65. package/src/config/jsonc.rs +66 -0
  66. package/src/config/policy.rs +91 -0
  67. package/src/config/rule_aliases.rs +487 -0
  68. package/src/config/rules.rs +339 -0
  69. package/src/config.rs +635 -0
  70. package/src/coverage.rs +469 -0
  71. package/src/dart_parser.rs +673 -0
  72. package/src/decision_surface.rs +405 -0
  73. package/src/dependencies/analyze.rs +218 -0
  74. package/src/dependencies/codegen.rs +118 -0
  75. package/src/dependencies/discovery.rs +60 -0
  76. package/src/dependencies/overrides.rs +76 -0
  77. package/src/dependencies/private_src_imports.rs +32 -0
  78. package/src/dependencies/pubspec_document.rs +86 -0
  79. package/src/dependencies/pubspec_entry.rs +96 -0
  80. package/src/dependencies/tests.rs +544 -0
  81. package/src/dependencies/usage.rs +33 -0
  82. package/src/dependencies.rs +553 -0
  83. package/src/dependency_scripts.rs +91 -0
  84. package/src/dupes/lex.rs +236 -0
  85. package/src/dupes/tests.rs +119 -0
  86. package/src/dupes.rs +625 -0
  87. package/src/explain.rs +692 -0
  88. package/src/extract/directives.rs +252 -0
  89. package/src/extract/error_tests.rs +25 -0
  90. package/src/extract/members.rs +210 -0
  91. package/src/extract/primary_constructors.rs +421 -0
  92. package/src/extract/references.rs +151 -0
  93. package/src/extract/routes.rs +691 -0
  94. package/src/extract/signatures.rs +266 -0
  95. package/src/extract/string_tests.rs +18 -0
  96. package/src/extract/strings.rs +107 -0
  97. package/src/extract/tests.rs +697 -0
  98. package/src/extract.rs +680 -0
  99. package/src/feature_flags/tests.rs +76 -0
  100. package/src/feature_flags.rs +453 -0
  101. package/src/finding_identity.rs +60 -0
  102. package/src/fix.rs +551 -0
  103. package/src/generated.rs +53 -0
  104. package/src/graph/augment_tests.rs +212 -0
  105. package/src/graph/conditionals.rs +74 -0
  106. package/src/graph/package_config_tests.rs +100 -0
  107. package/src/graph/parts.rs +248 -0
  108. package/src/graph/pubspec_tests.rs +119 -0
  109. package/src/graph/tests.rs +837 -0
  110. package/src/graph/uri_tests.rs +102 -0
  111. package/src/graph.rs +644 -0
  112. package/src/health/coverage.rs +309 -0
  113. package/src/health/ownership.rs +175 -0
  114. package/src/health/runtime_coverage.rs +616 -0
  115. package/src/health/runtime_intelligence.rs +511 -0
  116. package/src/health/scores.rs +296 -0
  117. package/src/health/tests.rs +240 -0
  118. package/src/health/threshold_types.rs +163 -0
  119. package/src/health/thresholds.rs +229 -0
  120. package/src/health/types.rs +636 -0
  121. package/src/health.rs +597 -0
  122. package/src/hooks/agent_settings.rs +175 -0
  123. package/src/hooks/templates.rs +51 -0
  124. package/src/hooks.rs +569 -0
  125. package/src/impact.rs +287 -0
  126. package/src/init.rs +237 -0
  127. package/src/inspect.rs +142 -0
  128. package/src/intelligence/tests.rs +232 -0
  129. package/src/intelligence.rs +305 -0
  130. package/src/lib.rs +198 -0
  131. package/src/main.rs +5 -0
  132. package/src/manifest/mcp.rs +565 -0
  133. package/src/manifest.rs +552 -0
  134. package/src/mcp/cli_args/value_args.rs +175 -0
  135. package/src/mcp/cli_args.rs +658 -0
  136. package/src/mcp/cli_args_tests.rs +522 -0
  137. package/src/mcp/code_execute.rs +671 -0
  138. package/src/mcp/tools.rs +660 -0
  139. package/src/mcp.rs +325 -0
  140. package/src/output/audit_risk.rs +135 -0
  141. package/src/output/dependency_findings.rs +327 -0
  142. package/src/output/duplication_findings.rs +114 -0
  143. package/src/output/feature_flag_findings.rs +122 -0
  144. package/src/output/format.rs +31 -0
  145. package/src/output/graph_findings.rs +461 -0
  146. package/src/output/health_findings.rs +612 -0
  147. package/src/output/human.rs +27 -0
  148. package/src/output/next_steps.rs +92 -0
  149. package/src/output/route_findings.rs +76 -0
  150. package/src/output/runtime_coverage.rs +515 -0
  151. package/src/output/scope.rs +183 -0
  152. package/src/output/security_findings.rs +390 -0
  153. package/src/output/security_sarif.rs +150 -0
  154. package/src/output/suppressions.rs +323 -0
  155. package/src/output/symbol_findings.rs +257 -0
  156. package/src/output/tests.rs +114 -0
  157. package/src/output/types.rs +604 -0
  158. package/src/output/widget_findings.rs +213 -0
  159. package/src/output.rs +682 -0
  160. package/src/package_map.rs +495 -0
  161. package/src/policy/packs.rs +232 -0
  162. package/src/policy/tests.rs +134 -0
  163. package/src/policy.rs +640 -0
  164. package/src/project_list.rs +376 -0
  165. package/src/report_schema/inventory.rs +418 -0
  166. package/src/report_schema.rs +488 -0
  167. package/src/routes.rs +354 -0
  168. package/src/scan/tests.rs +250 -0
  169. package/src/scan.rs +275 -0
  170. package/src/security/detect.rs +639 -0
  171. package/src/security/tests.rs +178 -0
  172. package/src/security.rs +373 -0
  173. package/src/security_gate.rs +555 -0
  174. package/src/symbols/path_filters.rs +27 -0
  175. package/src/symbols/private_type_leaks.rs +125 -0
  176. package/src/symbols/riverpod.rs +113 -0
  177. package/src/symbols/tests.rs +692 -0
  178. package/src/symbols.rs +693 -0
  179. package/src/trace/tests.rs +225 -0
  180. package/src/trace.rs +690 -0
  181. package/src/unsupported.rs +47 -0
  182. package/src/widgets/lifecycle.rs +484 -0
  183. package/src/widgets/params.rs +236 -0
  184. package/src/widgets/tests.rs +430 -0
  185. package/src/widgets/top_level.rs +110 -0
  186. package/src/widgets/unrendered.rs +374 -0
  187. package/src/widgets.rs +590 -0
  188. package/src/workspace_scope.rs +358 -0
@@ -0,0 +1,327 @@
1
+ use std::collections::BTreeSet;
2
+ use std::fs;
3
+ use std::path::{Component, Path, PathBuf};
4
+ use std::process::Command;
5
+ use std::time::{SystemTime, UNIX_EPOCH};
6
+
7
+ use clap::{Arg, ArgMatches};
8
+
9
+ use crate::baseline::baseline_from_report;
10
+ use crate::changed_scope::{changed_file_scope_from_changed, changed_files};
11
+ use crate::config::apply_rules_to_report;
12
+ use crate::graph::normalize_path;
13
+ use crate::output::{
14
+ AnalysisResults, JsonReport, ReportCommand, apply_audit_risk, build_json_report,
15
+ filter_report_findings,
16
+ };
17
+ use crate::scan::{ScannedProject, scan_project_with_options};
18
+ use crate::{BoundaryCallRule, BoundaryRule, HealthOptions};
19
+
20
+ use super::{CliError, CommandRequest, analyze::analyze_project};
21
+
22
+ #[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
23
+ pub(super) enum AuditGate {
24
+ NewOnly,
25
+ #[default]
26
+ All,
27
+ }
28
+
29
+ pub(super) fn gate_arg() -> Arg {
30
+ Arg::new("gate")
31
+ .long("gate")
32
+ .value_name("MODE")
33
+ .value_parser(["new-only", "all"])
34
+ .default_value("all")
35
+ .help("Audit gate mode: all visible findings or only findings introduced by changed files")
36
+ }
37
+
38
+ pub(super) fn gate(command: ReportCommand, subcommand: &ArgMatches) -> AuditGate {
39
+ if command != ReportCommand::Audit {
40
+ return AuditGate::All;
41
+ }
42
+ match subcommand.get_one::<String>("gate").map(String::as_str) {
43
+ Some("new-only") => AuditGate::NewOnly,
44
+ _ => AuditGate::All,
45
+ }
46
+ }
47
+
48
+ #[derive(Debug, Default)]
49
+ pub(super) struct AuditContext {
50
+ pub(super) changed_files: Vec<PathBuf>,
51
+ pub(super) base_finding_identities: BTreeSet<String>,
52
+ }
53
+
54
+ pub(super) fn prepare_context(
55
+ project: &ScannedProject,
56
+ request: &CommandRequest,
57
+ results: &mut AnalysisResults,
58
+ ) -> Result<AuditContext, CliError> {
59
+ if request.command != ReportCommand::Audit {
60
+ return Ok(AuditContext::default());
61
+ }
62
+
63
+ let Some(base) = request.audit_base.as_deref() else {
64
+ unreachable!("clap requires --base for audit");
65
+ };
66
+ let changed = changed_files(&project.root, base)?;
67
+ results.file_scope = Some(changed_file_scope_from_changed(project, &changed));
68
+ let base_finding_identities = base_finding_identities(request, base)?;
69
+
70
+ Ok(AuditContext {
71
+ changed_files: changed,
72
+ base_finding_identities,
73
+ })
74
+ }
75
+
76
+ pub(super) fn apply_risk(root: &Path, context: &AuditContext, report: &mut JsonReport) {
77
+ apply_audit_risk(
78
+ root,
79
+ &context.changed_files,
80
+ &context.base_finding_identities,
81
+ report,
82
+ );
83
+ }
84
+
85
+ fn base_finding_identities(
86
+ request: &CommandRequest,
87
+ base: &str,
88
+ ) -> Result<BTreeSet<String>, CliError> {
89
+ let snapshot = GitSnapshot::create(&request.root, base)?;
90
+ let project = scan_project_with_options(snapshot.root(), &request.scan_options)?;
91
+ let base_request = request_for_snapshot(request, snapshot.root());
92
+ let results = analyze_project(&project, &base_request)?;
93
+ let mut report = build_json_report(&project, &results);
94
+ apply_rules_to_report(&mut report, &request.rules)?;
95
+ filter_report_findings(&mut report, &request.issue_filters.kinds);
96
+ Ok(baseline_from_report(&report)
97
+ .findings
98
+ .into_iter()
99
+ .map(|finding| finding.identity)
100
+ .collect())
101
+ }
102
+
103
+ fn request_for_snapshot(request: &CommandRequest, snapshot_root: &Path) -> CommandRequest {
104
+ let original_root = normalize_path(&request.root);
105
+ let mut snapshot_request = request.clone();
106
+ snapshot_request.root = snapshot_root.to_path_buf();
107
+ snapshot_request.entry_points =
108
+ rebase_project_paths(&original_root, snapshot_root, &request.entry_points);
109
+ snapshot_request.boundaries =
110
+ rebase_boundary_rules(&original_root, snapshot_root, &request.boundaries);
111
+ snapshot_request.boundary_calls =
112
+ rebase_boundary_calls(&original_root, snapshot_root, &request.boundary_calls);
113
+ snapshot_request.policy_packs = absolutize_project_paths(&original_root, &request.policy_packs);
114
+ snapshot_request.health_options =
115
+ rebase_health_options(&original_root, &request.health_options);
116
+ snapshot_request
117
+ }
118
+
119
+ fn rebase_project_paths(
120
+ original_root: &Path,
121
+ snapshot_root: &Path,
122
+ paths: &[PathBuf],
123
+ ) -> Vec<PathBuf> {
124
+ paths
125
+ .iter()
126
+ .map(|path| rebase_project_path(original_root, snapshot_root, path))
127
+ .collect()
128
+ }
129
+
130
+ fn absolutize_project_paths(original_root: &Path, paths: &[PathBuf]) -> Vec<PathBuf> {
131
+ paths
132
+ .iter()
133
+ .map(|path| {
134
+ if path.is_absolute() {
135
+ path.clone()
136
+ } else {
137
+ original_root.join(path)
138
+ }
139
+ })
140
+ .collect()
141
+ }
142
+
143
+ fn rebase_project_path(original_root: &Path, snapshot_root: &Path, path: &Path) -> PathBuf {
144
+ if path.is_absolute() {
145
+ return path.strip_prefix(original_root).map_or_else(
146
+ |_| path.to_path_buf(),
147
+ |relative| snapshot_root.join(relative),
148
+ );
149
+ }
150
+ path.to_path_buf()
151
+ }
152
+
153
+ fn rebase_boundary_rules(
154
+ original_root: &Path,
155
+ snapshot_root: &Path,
156
+ rules: &[BoundaryRule],
157
+ ) -> Vec<BoundaryRule> {
158
+ rules
159
+ .iter()
160
+ .map(|rule| BoundaryRule {
161
+ from: rebase_project_path(original_root, snapshot_root, &rule.from),
162
+ disallow: rebase_project_path(original_root, snapshot_root, &rule.disallow),
163
+ })
164
+ .collect()
165
+ }
166
+
167
+ fn rebase_boundary_calls(
168
+ original_root: &Path,
169
+ snapshot_root: &Path,
170
+ rules: &[BoundaryCallRule],
171
+ ) -> Vec<BoundaryCallRule> {
172
+ rules
173
+ .iter()
174
+ .map(|rule| BoundaryCallRule {
175
+ from: rebase_project_path(original_root, snapshot_root, &rule.from),
176
+ forbidden: rule.forbidden.clone(),
177
+ })
178
+ .collect()
179
+ }
180
+
181
+ fn rebase_health_options(original_root: &Path, options: &HealthOptions) -> HealthOptions {
182
+ let mut options = options.clone();
183
+ options.coverage_path = options
184
+ .coverage_path
185
+ .map(|path| absolutize_project_path(original_root, path));
186
+ options.runtime_coverage_path = options
187
+ .runtime_coverage_path
188
+ .map(|path| absolutize_project_path(original_root, path));
189
+ options
190
+ }
191
+
192
+ fn absolutize_project_path(original_root: &Path, path: PathBuf) -> PathBuf {
193
+ if path.is_absolute() {
194
+ path
195
+ } else {
196
+ original_root.join(path)
197
+ }
198
+ }
199
+
200
+ struct GitSnapshot {
201
+ root: PathBuf,
202
+ }
203
+
204
+ impl GitSnapshot {
205
+ fn create(project_root: &Path, base: &str) -> Result<Self, CliError> {
206
+ let snapshot = Self {
207
+ root: create_temp_dir()?,
208
+ };
209
+ snapshot.materialize(project_root, base)?;
210
+ Ok(snapshot)
211
+ }
212
+
213
+ fn root(&self) -> &Path {
214
+ &self.root
215
+ }
216
+
217
+ fn materialize(&self, project_root: &Path, base: &str) -> Result<(), CliError> {
218
+ for path in git_tree_paths(project_root, base)? {
219
+ if should_materialize(&path) {
220
+ self.write_git_file(project_root, base, &path)?;
221
+ }
222
+ }
223
+ Ok(())
224
+ }
225
+
226
+ fn write_git_file(&self, project_root: &Path, base: &str, path: &str) -> Result<(), CliError> {
227
+ let Some(target) = snapshot_target(&self.root, path) else {
228
+ return Ok(());
229
+ };
230
+ if let Some(parent) = target.parent() {
231
+ fs::create_dir_all(parent)?;
232
+ }
233
+ let spec = format!("{base}:{path}");
234
+ let bytes = git_output(project_root, base, ["show", spec.as_str()])?;
235
+ fs::write(target, bytes)?;
236
+ Ok(())
237
+ }
238
+ }
239
+
240
+ impl Drop for GitSnapshot {
241
+ fn drop(&mut self) {
242
+ let _ = fs::remove_dir_all(&self.root);
243
+ }
244
+ }
245
+
246
+ fn snapshot_target(root: &Path, path: &str) -> Option<PathBuf> {
247
+ let relative = Path::new(path);
248
+ if relative.is_absolute()
249
+ || relative
250
+ .components()
251
+ .any(|component| !matches!(component, Component::Normal(_)))
252
+ {
253
+ return None;
254
+ }
255
+ Some(root.join(relative))
256
+ }
257
+
258
+ fn git_tree_paths(project_root: &Path, base: &str) -> Result<Vec<String>, CliError> {
259
+ let output = git_output(
260
+ project_root,
261
+ base,
262
+ ["ls-tree", "-r", "-z", "--name-only", base, "--"],
263
+ )?;
264
+ Ok(output
265
+ .split(|byte| *byte == b'\0')
266
+ .filter_map(|path| std::str::from_utf8(path).ok())
267
+ .filter(|path| !path.is_empty())
268
+ .map(str::to_owned)
269
+ .collect())
270
+ }
271
+
272
+ fn git_output<'a, I>(project_root: &Path, base: &str, args: I) -> Result<Vec<u8>, CliError>
273
+ where
274
+ I: IntoIterator<Item = &'a str>,
275
+ {
276
+ let output = Command::new("git")
277
+ .args(args)
278
+ .current_dir(project_root)
279
+ .output()
280
+ .map_err(|source| crate::changed_scope::ChangedScopeError::Git { source })?;
281
+ if output.status.success() {
282
+ return Ok(output.stdout);
283
+ }
284
+ Err(crate::changed_scope::ChangedScopeError::GitDiff {
285
+ base: base.to_owned(),
286
+ stderr: String::from_utf8_lossy(&output.stderr).trim().to_owned(),
287
+ }
288
+ .into())
289
+ }
290
+
291
+ fn should_materialize(path: &str) -> bool {
292
+ let path = Path::new(path);
293
+ path.extension()
294
+ .is_some_and(|extension| extension.eq_ignore_ascii_case("dart"))
295
+ || path
296
+ .file_name()
297
+ .and_then(|name| name.to_str())
298
+ .is_some_and(|name| {
299
+ matches!(
300
+ name,
301
+ "pubspec.yaml" | "pubspec_overrides.yaml" | "pubspec.lock"
302
+ )
303
+ })
304
+ || path.ends_with(Path::new(".dart_tool/package_config.json"))
305
+ }
306
+
307
+ fn create_temp_dir() -> Result<PathBuf, std::io::Error> {
308
+ let base = std::env::temp_dir();
309
+ let nanos = SystemTime::now()
310
+ .duration_since(UNIX_EPOCH)
311
+ .map_or(0, |duration| duration.as_nanos());
312
+ for attempt in 0..100u8 {
313
+ let path = base.join(format!(
314
+ "dart-decimate-audit-base-{}-{nanos}-{attempt}",
315
+ std::process::id()
316
+ ));
317
+ match fs::create_dir(&path) {
318
+ Ok(()) => return Ok(path),
319
+ Err(error) if error.kind() == std::io::ErrorKind::AlreadyExists => {}
320
+ Err(error) => return Err(error),
321
+ }
322
+ }
323
+ Err(std::io::Error::new(
324
+ std::io::ErrorKind::AlreadyExists,
325
+ "could not create unique Dart Decimate audit temp directory",
326
+ ))
327
+ }
@@ -0,0 +1,44 @@
1
+ use std::path::PathBuf;
2
+
3
+ use clap::{Arg, ArgAction, Command, value_parser};
4
+
5
+ pub(super) fn boundary_command(command: Command) -> Command {
6
+ command
7
+ .arg(
8
+ Arg::new("boundary")
9
+ .long("boundary")
10
+ .value_name("FROM:DISALLOW")
11
+ .help("Flag imports from FROM into DISALLOW")
12
+ .num_args(1)
13
+ .action(ArgAction::Append),
14
+ )
15
+ .arg(
16
+ Arg::new("boundary-coverage")
17
+ .long("boundary-coverage")
18
+ .help("Report Dart library files outside every configured architecture boundary")
19
+ .action(ArgAction::SetTrue),
20
+ )
21
+ .arg(
22
+ Arg::new("boundary-call")
23
+ .long("boundary-call")
24
+ .value_name("FROM:PATTERN")
25
+ .help("Flag direct calls from FROM that match PATTERN")
26
+ .num_args(1)
27
+ .action(ArgAction::Append),
28
+ )
29
+ .arg(
30
+ Arg::new("policy-pack")
31
+ .long("policy-pack")
32
+ .value_name("PATH")
33
+ .help("Load a declarative policy rule pack")
34
+ .value_parser(value_parser!(PathBuf))
35
+ .num_args(1)
36
+ .action(ArgAction::Append),
37
+ )
38
+ .arg(
39
+ Arg::new("policy-violations")
40
+ .long("policy-violations")
41
+ .help("Run declarative policy rule-pack checks")
42
+ .action(ArgAction::SetTrue),
43
+ )
44
+ }
@@ -0,0 +1,196 @@
1
+ use std::io::Write;
2
+ use std::path::PathBuf;
3
+
4
+ use clap::{Arg, ArgAction, ArgMatches, Command, value_parser};
5
+
6
+ use crate::ci_template::{
7
+ CiReconcileReviewOptions, CiReviewProvider, CiTemplatePlatform, ci_reconcile_review_report,
8
+ ci_template_report, render_ci_template, vendor_ci_template,
9
+ };
10
+
11
+ use super::CliError;
12
+
13
+ pub(super) fn ci_template_command() -> Command {
14
+ Command::new("ci-template")
15
+ .about("Print or vendor CI integration templates")
16
+ .arg(
17
+ Arg::new("platform")
18
+ .value_name("PLATFORM")
19
+ .help("CI platform")
20
+ .default_value("github")
21
+ .value_parser(["github", "gitlab"]),
22
+ )
23
+ .arg(
24
+ Arg::new("format")
25
+ .long("format")
26
+ .value_name("FORMAT")
27
+ .help("Output format")
28
+ .default_value("yaml")
29
+ .value_parser(["yaml", "json"]),
30
+ )
31
+ .arg(
32
+ Arg::new("vendor")
33
+ .long("vendor")
34
+ .help("Write vendored CI files under --root")
35
+ .action(ArgAction::SetTrue),
36
+ )
37
+ .arg(
38
+ Arg::new("root")
39
+ .long("root")
40
+ .value_name("PATH")
41
+ .help("Project root for --vendor")
42
+ .default_value(".")
43
+ .value_parser(value_parser!(PathBuf)),
44
+ )
45
+ .arg(
46
+ Arg::new("force")
47
+ .long("force")
48
+ .help("Overwrite existing vendored files")
49
+ .action(ArgAction::SetTrue),
50
+ )
51
+ }
52
+
53
+ pub(super) fn ci_command() -> Command {
54
+ Command::new("ci")
55
+ .about("Run CI integration utilities")
56
+ .subcommand_required(true)
57
+ .arg_required_else_help(true)
58
+ .subcommand(
59
+ Command::new("reconcile-review")
60
+ .about("Dry-run stale review reconciliation from a typed review envelope")
61
+ .arg(
62
+ Arg::new("provider")
63
+ .long("provider")
64
+ .value_name("PROVIDER")
65
+ .help("Review provider")
66
+ .default_value("github")
67
+ .value_parser(["github", "gitlab"]),
68
+ )
69
+ .arg(
70
+ Arg::new("repo")
71
+ .long("repo")
72
+ .value_name("OWNER/REPO")
73
+ .help("GitHub repository slug"),
74
+ )
75
+ .arg(
76
+ Arg::new("project-id")
77
+ .long("project-id")
78
+ .value_name("ID")
79
+ .help("GitLab project id"),
80
+ )
81
+ .arg(Arg::new("pr").long("pr").value_name("NUMBER"))
82
+ .arg(Arg::new("mr").long("mr").value_name("IID"))
83
+ .arg(Arg::new("api-url").long("api-url").value_name("URL"))
84
+ .arg(
85
+ Arg::new("envelope")
86
+ .long("envelope")
87
+ .value_name("PATH")
88
+ .help("Path to review-github or review-gitlab envelope JSON")
89
+ .required(true)
90
+ .value_parser(value_parser!(PathBuf)),
91
+ )
92
+ .arg(
93
+ Arg::new("dry-run")
94
+ .long("dry-run")
95
+ .help("Validate reconciliation without provider mutation")
96
+ .action(ArgAction::SetTrue),
97
+ )
98
+ .arg(
99
+ Arg::new("format")
100
+ .long("format")
101
+ .value_name("FORMAT")
102
+ .help("Output format")
103
+ .default_value("json")
104
+ .value_parser(["json"]),
105
+ ),
106
+ )
107
+ }
108
+
109
+ pub(super) fn run_ci<W: Write>(subcommand: &ArgMatches, writer: W) -> Result<i32, CliError> {
110
+ match subcommand.subcommand() {
111
+ Some(("reconcile-review", command)) => run_ci_reconcile_review(command, writer),
112
+ _ => unreachable!("clap requires a ci subcommand"),
113
+ }
114
+ }
115
+
116
+ pub(super) fn run_ci_template<W: Write>(
117
+ subcommand: &ArgMatches,
118
+ mut writer: W,
119
+ ) -> Result<i32, CliError> {
120
+ let platform = platform(subcommand);
121
+ let vendor = subcommand.get_flag("vendor");
122
+ let report = if vendor {
123
+ let root = subcommand
124
+ .get_one::<PathBuf>("root")
125
+ .cloned()
126
+ .unwrap_or_else(|| PathBuf::from("."));
127
+ vendor_ci_template(root, platform, subcommand.get_flag("force"))?
128
+ } else {
129
+ ci_template_report(platform, false)
130
+ };
131
+
132
+ match output_format(subcommand) {
133
+ CiTemplateOutputFormat::Json => serde_json::to_writer_pretty(&mut writer, &report)?,
134
+ CiTemplateOutputFormat::Yaml => writer.write_all(render_ci_template(&report).as_bytes())?,
135
+ }
136
+ writeln!(writer)?;
137
+ Ok(0)
138
+ }
139
+
140
+ fn run_ci_reconcile_review<W: Write>(
141
+ subcommand: &ArgMatches,
142
+ mut writer: W,
143
+ ) -> Result<i32, CliError> {
144
+ let Some(envelope) = subcommand.get_one::<PathBuf>("envelope").cloned() else {
145
+ return Err(CliError::MissingCiReviewEnvelope);
146
+ };
147
+ let report = ci_reconcile_review_report(CiReconcileReviewOptions {
148
+ provider: review_provider(subcommand),
149
+ envelope,
150
+ dry_run: subcommand.get_flag("dry-run"),
151
+ repo: subcommand.get_one::<String>("repo").cloned(),
152
+ project_id: subcommand.get_one::<String>("project-id").cloned(),
153
+ pr: subcommand.get_one::<String>("pr").cloned(),
154
+ mr: subcommand.get_one::<String>("mr").cloned(),
155
+ api_url: subcommand.get_one::<String>("api-url").cloned(),
156
+ })?;
157
+ serde_json::to_writer_pretty(&mut writer, &report)?;
158
+ writeln!(writer)?;
159
+ Ok(0)
160
+ }
161
+
162
+ #[derive(Debug, Clone, Copy, PartialEq, Eq)]
163
+ enum CiTemplateOutputFormat {
164
+ Yaml,
165
+ Json,
166
+ }
167
+
168
+ fn output_format(subcommand: &ArgMatches) -> CiTemplateOutputFormat {
169
+ match subcommand
170
+ .get_one::<String>("format")
171
+ .map_or("yaml", String::as_str)
172
+ {
173
+ "json" => CiTemplateOutputFormat::Json,
174
+ _ => CiTemplateOutputFormat::Yaml,
175
+ }
176
+ }
177
+
178
+ fn platform(subcommand: &ArgMatches) -> CiTemplatePlatform {
179
+ match subcommand
180
+ .get_one::<String>("platform")
181
+ .map_or("github", String::as_str)
182
+ {
183
+ "gitlab" => CiTemplatePlatform::Gitlab,
184
+ _ => CiTemplatePlatform::Github,
185
+ }
186
+ }
187
+
188
+ fn review_provider(subcommand: &ArgMatches) -> CiReviewProvider {
189
+ match subcommand
190
+ .get_one::<String>("provider")
191
+ .map_or("github", String::as_str)
192
+ {
193
+ "gitlab" => CiReviewProvider::Gitlab,
194
+ _ => CiReviewProvider::Github,
195
+ }
196
+ }
@@ -0,0 +1,20 @@
1
+ use crate::output::ReportCommand;
2
+
3
+ pub(super) fn report_command_from_name(name: &str) -> ReportCommand {
4
+ match name {
5
+ "check" => ReportCommand::Check,
6
+ "audit" => ReportCommand::Audit,
7
+ "dead-code" => ReportCommand::DeadCode,
8
+ "cycles" => ReportCommand::Cycles,
9
+ "dupes" => ReportCommand::Dupes,
10
+ "health" => ReportCommand::Health,
11
+ "flags" => ReportCommand::Flags,
12
+ "security" => ReportCommand::Security,
13
+ "trace" | "trace-symbol" => ReportCommand::TraceSymbol,
14
+ "trace-file" => ReportCommand::TraceFile,
15
+ "trace-dependency" => ReportCommand::TraceDependency,
16
+ "trace-clone" => ReportCommand::TraceClone,
17
+ "inspect" => ReportCommand::Inspect,
18
+ _ => unreachable!("clap rejects unknown subcommands"),
19
+ }
20
+ }