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,167 @@
1
+ use std::collections::BTreeMap;
2
+ use std::path::PathBuf;
3
+
4
+ use clap::{Arg, ArgAction, ArgMatches, Command, value_parser};
5
+
6
+ use super::regression_args::regression_command;
7
+
8
+ pub(super) fn scan_command(command: Command) -> Command {
9
+ scan_command_with_format(command, format_arg())
10
+ }
11
+
12
+ pub(super) fn report_command(command: Command) -> Command {
13
+ super::scope_args::report_command(super::summary_args::summary_command(
14
+ scan_command_with_format(command, report_format_arg()),
15
+ ))
16
+ }
17
+
18
+ fn scan_command_with_format(command: Command, format: Arg) -> Command {
19
+ command
20
+ .arg(root_arg())
21
+ .arg(root_flag_arg())
22
+ .arg(format)
23
+ .arg(quiet_arg())
24
+ .arg(config_arg())
25
+ .arg(entry_arg())
26
+ .arg(dart_platform_arg())
27
+ .arg(super::mode_args::production_arg())
28
+ .arg(super::mode_args::no_production_arg())
29
+ }
30
+
31
+ pub(super) fn symbol_options_command(command: Command) -> Command {
32
+ include_entry_exports_command(command).arg(super::mode_args::private_type_leaks_arg())
33
+ }
34
+
35
+ pub(super) fn baseline_command(command: Command) -> Command {
36
+ regression_command(report_command(command))
37
+ .arg(baseline_arg())
38
+ .arg(save_baseline_arg())
39
+ }
40
+
41
+ pub(super) fn root_arg() -> Arg {
42
+ Arg::new("root")
43
+ .value_name("ROOT")
44
+ .help("Project root")
45
+ .default_value(".")
46
+ .value_parser(value_parser!(PathBuf))
47
+ }
48
+
49
+ pub(super) fn root_flag_arg() -> Arg {
50
+ Arg::new("root-flag")
51
+ .long("root")
52
+ .value_name("ROOT")
53
+ .help("Project root")
54
+ .value_parser(value_parser!(PathBuf))
55
+ }
56
+
57
+ pub(super) fn root_path(matches: &ArgMatches) -> PathBuf {
58
+ matches
59
+ .try_get_one::<PathBuf>("root-flag")
60
+ .ok()
61
+ .flatten()
62
+ .or_else(|| matches.try_get_one::<PathBuf>("root").ok().flatten())
63
+ .cloned()
64
+ .unwrap_or_else(|| PathBuf::from("."))
65
+ }
66
+
67
+ pub(super) fn format_arg() -> Arg {
68
+ Arg::new("format")
69
+ .long("format")
70
+ .value_name("FORMAT")
71
+ .help("Output format")
72
+ .default_value("human")
73
+ .value_parser(["human", "json"])
74
+ }
75
+
76
+ fn report_format_arg() -> Arg {
77
+ format_arg().value_parser(["human", "json", "sarif"])
78
+ }
79
+
80
+ pub(super) fn config_arg() -> Arg {
81
+ Arg::new("config")
82
+ .long("config")
83
+ .value_name("PATH")
84
+ .help("Dart Decimate config file")
85
+ .value_parser(value_parser!(PathBuf))
86
+ }
87
+
88
+ fn quiet_arg() -> Arg {
89
+ Arg::new("quiet")
90
+ .long("quiet")
91
+ .help("Suppress non-JSON progress output")
92
+ .action(ArgAction::SetTrue)
93
+ }
94
+
95
+ pub(super) fn entry_arg() -> Arg {
96
+ Arg::new("entry")
97
+ .long("entry")
98
+ .value_name("PATH")
99
+ .help("Dart entry point for reachability")
100
+ .num_args(1)
101
+ .action(ArgAction::Append)
102
+ .value_parser(value_parser!(PathBuf))
103
+ }
104
+
105
+ pub(super) fn conditional_environment(matches: &ArgMatches) -> BTreeMap<String, String> {
106
+ match matches
107
+ .try_get_one::<String>("dart-platform")
108
+ .ok()
109
+ .flatten()
110
+ .map(String::as_str)
111
+ {
112
+ Some("web") => dart_platform_environment(true),
113
+ Some("vm") => dart_platform_environment(false),
114
+ _ => BTreeMap::new(),
115
+ }
116
+ }
117
+
118
+ pub(super) fn dart_platform_arg() -> Arg {
119
+ Arg::new("dart-platform")
120
+ .long("dart-platform")
121
+ .value_name("PLATFORM")
122
+ .help("Resolve Dart conditional imports/exports for a target platform")
123
+ .value_parser(["vm", "web"])
124
+ }
125
+
126
+ fn dart_platform_environment(web: bool) -> BTreeMap<String, String> {
127
+ [
128
+ ("dart.library.html", web),
129
+ ("dart.library.js", web),
130
+ ("dart.library.js_interop", web),
131
+ ("dart.library.io", !web),
132
+ ("dart.library.ffi", !web),
133
+ ("dart.library.isolate", !web),
134
+ ("dart.library.mirrors", !web),
135
+ ]
136
+ .into_iter()
137
+ .map(|(name, enabled)| (name.to_owned(), enabled.to_string()))
138
+ .collect()
139
+ }
140
+
141
+ pub(super) fn audit_baseline_arg(id: &'static str, help: &'static str) -> Arg {
142
+ Arg::new(id)
143
+ .long(id)
144
+ .value_name("PATH")
145
+ .help(help)
146
+ .value_parser(value_parser!(PathBuf))
147
+ }
148
+
149
+ fn include_entry_exports_command(command: Command) -> Command {
150
+ command.arg(super::mode_args::include_entry_exports_arg())
151
+ }
152
+
153
+ fn baseline_arg() -> Arg {
154
+ Arg::new("baseline")
155
+ .long("baseline")
156
+ .value_name("PATH")
157
+ .help("Suppress findings already captured in a Dart Decimate baseline")
158
+ .value_parser(value_parser!(PathBuf))
159
+ }
160
+
161
+ fn save_baseline_arg() -> Arg {
162
+ Arg::new("save-baseline")
163
+ .long("save-baseline")
164
+ .value_name("PATH")
165
+ .help("Write current visible findings to a Dart Decimate baseline")
166
+ .value_parser(value_parser!(PathBuf))
167
+ }
@@ -0,0 +1,78 @@
1
+ use std::io::Write;
2
+ use std::path::Path;
3
+
4
+ use clap::{Arg, Command};
5
+ use serde::Serialize;
6
+
7
+ use super::common_args::{config_arg, format_arg, root_arg, root_flag_arg, root_path};
8
+ use super::{CliError, OutputFormat, load_config, output_format};
9
+ use crate::config::{CONFIG_SCHEMA_VERSION, DartDecimateConfig};
10
+
11
+ pub(super) fn config_command() -> Command {
12
+ Command::new("config")
13
+ .about("Print the resolved Dart Decimate configuration")
14
+ .arg(root_arg())
15
+ .arg(root_flag_arg())
16
+ .arg(format_arg())
17
+ .arg(config_arg())
18
+ .arg(
19
+ Arg::new("path")
20
+ .long("path")
21
+ .help("Print only the discovered config path")
22
+ .action(clap::ArgAction::SetTrue),
23
+ )
24
+ }
25
+
26
+ pub(super) fn run_config<W: Write>(
27
+ subcommand: &clap::ArgMatches,
28
+ mut writer: W,
29
+ ) -> Result<i32, CliError> {
30
+ let root = root_path(subcommand);
31
+ let config = load_config(&root, subcommand)?;
32
+
33
+ if subcommand.get_flag("path") {
34
+ if let Some(path) = &config.path {
35
+ writeln!(writer, "{}", path.display())?;
36
+ return Ok(0);
37
+ }
38
+ return Ok(3);
39
+ }
40
+
41
+ match output_format(subcommand, &config) {
42
+ OutputFormat::Json => {
43
+ serde_json::to_writer_pretty(
44
+ &mut writer,
45
+ &ConfigEnvelope {
46
+ schema_version: CONFIG_SCHEMA_VERSION,
47
+ path: config.path.as_deref(),
48
+ config: &config,
49
+ },
50
+ )?;
51
+ writeln!(writer)?;
52
+ }
53
+ OutputFormat::Human => writer.write_all(render_config(&config).as_bytes())?,
54
+ }
55
+
56
+ Ok(0)
57
+ }
58
+
59
+ fn render_config(config: &DartDecimateConfig) -> String {
60
+ let Some(path) = &config.path else {
61
+ return "No Dart Decimate config found\n".to_owned();
62
+ };
63
+
64
+ format!(
65
+ "Config: {}\nEntries: {}\nBoundaries: {}\nIgnore patterns: {}\n",
66
+ path.display(),
67
+ config.entry_points.len(),
68
+ config.boundaries.len(),
69
+ config.ignore_patterns.len()
70
+ )
71
+ }
72
+
73
+ #[derive(Debug, Serialize)]
74
+ struct ConfigEnvelope<'a> {
75
+ schema_version: &'static str,
76
+ path: Option<&'a Path>,
77
+ config: &'a DartDecimateConfig,
78
+ }
@@ -0,0 +1,393 @@
1
+ use std::io::Write;
2
+ use std::path::{Path, PathBuf};
3
+
4
+ use clap::{Arg, ArgAction, ArgMatches, Command, parser::ValueSource, value_parser};
5
+
6
+ use crate::coverage::{
7
+ coverage_analysis_report, coverage_inventory_upload_report, coverage_setup_report,
8
+ coverage_source_maps_upload_report, render_coverage_analysis_report,
9
+ render_coverage_setup_report, render_coverage_upload_report,
10
+ };
11
+ use crate::output::json_runtime_coverage;
12
+ use crate::scan::{ScanOptions, scan_project_with_options};
13
+ use crate::{HealthOptions, LowTrafficThreshold};
14
+
15
+ use super::common_args::{config_arg, format_arg, root_arg, root_flag_arg, root_path};
16
+ use super::health_args::runtime_coverage_args;
17
+ use super::{CliError, OutputFormat, load_config, output_format};
18
+
19
+ pub(super) fn coverage_command() -> Command {
20
+ Command::new("coverage")
21
+ .about("Runtime coverage setup and analysis")
22
+ .subcommand_required(true)
23
+ .arg_required_else_help(true)
24
+ .subcommand(coverage_setup_command())
25
+ .subcommand(coverage_analyze_command())
26
+ .subcommand(coverage_upload_inventory_command())
27
+ .subcommand(coverage_upload_source_maps_command())
28
+ }
29
+
30
+ pub(super) fn run_coverage<W: Write>(subcommand: &ArgMatches, writer: W) -> Result<i32, CliError> {
31
+ match subcommand.subcommand() {
32
+ Some(("setup", setup)) => run_coverage_setup(setup, writer),
33
+ Some(("analyze", analyze)) => run_coverage_analyze(analyze, writer),
34
+ Some(("upload-inventory", upload)) => run_coverage_upload_inventory(upload, writer),
35
+ Some(("upload-source-maps", upload)) => run_coverage_upload_source_maps(upload, writer),
36
+ _ => unreachable!("clap requires a coverage subcommand"),
37
+ }
38
+ }
39
+
40
+ fn coverage_setup_command() -> Command {
41
+ Command::new("setup")
42
+ .about("Plan or write local runtime coverage defaults")
43
+ .arg(root_arg())
44
+ .arg(root_flag_arg())
45
+ .arg(format_arg())
46
+ .arg(config_arg())
47
+ .arg(
48
+ Arg::new("yes")
49
+ .long("yes")
50
+ .help("Write safe local coverage defaults without prompting")
51
+ .action(ArgAction::SetTrue),
52
+ )
53
+ .arg(
54
+ Arg::new("non-interactive")
55
+ .long("non-interactive")
56
+ .help("Do not prompt; emit a deterministic setup plan")
57
+ .action(ArgAction::SetTrue),
58
+ )
59
+ }
60
+
61
+ fn coverage_analyze_command() -> Command {
62
+ runtime_coverage_args(
63
+ Command::new("analyze")
64
+ .about("Analyze local V8 or Istanbul runtime coverage")
65
+ .arg(root_arg())
66
+ .arg(root_flag_arg())
67
+ .arg(format_arg())
68
+ .arg(config_arg())
69
+ .arg(
70
+ Arg::new("cloud")
71
+ .long("cloud")
72
+ .help("Cloud runtime coverage is not supported yet")
73
+ .action(ArgAction::SetTrue),
74
+ ),
75
+ false,
76
+ )
77
+ .arg(repo_arg(false))
78
+ .arg(
79
+ Arg::new("top")
80
+ .long("top")
81
+ .value_name("N")
82
+ .help("Show only the N highest runtime hot paths and findings")
83
+ .value_parser(value_parser!(usize)),
84
+ )
85
+ }
86
+
87
+ fn coverage_upload_inventory_command() -> Command {
88
+ Command::new("upload-inventory")
89
+ .about("Build a local source inventory upload dry-run packet")
90
+ .arg(root_arg())
91
+ .arg(root_flag_arg())
92
+ .arg(format_arg())
93
+ .arg(config_arg())
94
+ .arg(repo_arg(false))
95
+ .arg(dry_run_arg())
96
+ }
97
+
98
+ fn coverage_upload_source_maps_command() -> Command {
99
+ Command::new("upload-source-maps")
100
+ .about("Build a source-map upload dry-run packet")
101
+ .arg(root_arg())
102
+ .arg(root_flag_arg())
103
+ .arg(format_arg())
104
+ .arg(config_arg())
105
+ .arg(
106
+ Arg::new("dir")
107
+ .long("dir")
108
+ .value_name("PATH")
109
+ .help("Directory containing source-map files")
110
+ .required(true)
111
+ .value_parser(value_parser!(PathBuf)),
112
+ )
113
+ .arg(
114
+ Arg::new("git-sha")
115
+ .long("git-sha")
116
+ .value_name("SHA")
117
+ .help("Git commit SHA for the uploaded source maps")
118
+ .required(true)
119
+ .value_parser(value_parser!(String)),
120
+ )
121
+ .arg(repo_arg(true))
122
+ .arg(
123
+ Arg::new("strip-path")
124
+ .long("strip-path")
125
+ .value_name("BOOL")
126
+ .help("Strip project-root prefixes from uploaded source-map paths")
127
+ .default_value("true")
128
+ .default_missing_value("true")
129
+ .num_args(0..=1)
130
+ .value_parser(value_parser!(bool)),
131
+ )
132
+ .arg(dry_run_arg())
133
+ }
134
+
135
+ fn run_coverage_setup<W: Write>(subcommand: &ArgMatches, mut writer: W) -> Result<i32, CliError> {
136
+ let root = root_path(subcommand);
137
+ let config = load_config(&root, subcommand)?;
138
+ let format = output_format(subcommand, &config);
139
+ let project = scan_project_with_options(
140
+ &root,
141
+ &ScanOptions {
142
+ ignore_patterns: config.ignore_patterns.clone(),
143
+ ..ScanOptions::default()
144
+ },
145
+ )?;
146
+ let report = coverage_setup_report(
147
+ &project,
148
+ subcommand.get_flag("yes"),
149
+ subcommand.get_flag("non-interactive"),
150
+ config.health.coverage_path.is_some() || config.health.runtime_coverage.is_some(),
151
+ )?;
152
+
153
+ match format {
154
+ OutputFormat::Human => {
155
+ writer.write_all(render_coverage_setup_report(&report).as_bytes())?;
156
+ }
157
+ OutputFormat::Json => {
158
+ serde_json::to_writer_pretty(&mut writer, &report)?;
159
+ writeln!(writer)?;
160
+ }
161
+ }
162
+
163
+ Ok(0)
164
+ }
165
+
166
+ fn run_coverage_analyze<W: Write>(subcommand: &ArgMatches, mut writer: W) -> Result<i32, CliError> {
167
+ if subcommand.get_flag("cloud") {
168
+ return Err(CliError::UnsupportedCoverageCloud);
169
+ }
170
+ if let Some(repo) = subcommand.get_one::<String>("repo") {
171
+ validate_repo(repo)?;
172
+ }
173
+ let root = root_path(subcommand);
174
+ require_runtime_coverage(subcommand)?;
175
+ let config = load_config(&root, subcommand)?;
176
+ let format = output_format(subcommand, &config);
177
+ let health_options = coverage_health_options(subcommand, config.health_options());
178
+
179
+ let project = scan_project_with_options(
180
+ &root,
181
+ &ScanOptions {
182
+ ignore_patterns: config.ignore_patterns.clone(),
183
+ ..ScanOptions::default()
184
+ },
185
+ )?;
186
+ let mut health = crate::analyze_health(&project, &health_options)?;
187
+ let Some(runtime) = health.runtime_coverage.as_mut() else {
188
+ return Err(CliError::MissingRuntimeCoverage);
189
+ };
190
+ if let Some(top) = health_options.top {
191
+ runtime.hot_paths.truncate(top);
192
+ runtime.findings.truncate(top);
193
+ runtime.coverage_intelligence.truncate(top);
194
+ runtime.blast_radius.truncate(top);
195
+ runtime.importance.truncate(top);
196
+ }
197
+ let report = coverage_analysis_report(json_runtime_coverage(&project.root, runtime));
198
+
199
+ match format {
200
+ OutputFormat::Human => {
201
+ writer.write_all(render_coverage_analysis_report(&report).as_bytes())?;
202
+ }
203
+ OutputFormat::Json => {
204
+ serde_json::to_writer_pretty(&mut writer, &report)?;
205
+ writeln!(writer)?;
206
+ }
207
+ }
208
+
209
+ Ok(0)
210
+ }
211
+
212
+ fn run_coverage_upload_inventory<W: Write>(
213
+ subcommand: &ArgMatches,
214
+ mut writer: W,
215
+ ) -> Result<i32, CliError> {
216
+ require_dry_run(subcommand, "coverage upload-inventory")?;
217
+ if let Some(repo) = subcommand.get_one::<String>("repo") {
218
+ validate_repo(repo)?;
219
+ }
220
+ let root = root_path(subcommand);
221
+ let config = load_config(&root, subcommand)?;
222
+ let format = output_format(subcommand, &config);
223
+ let project = scan_project_with_options(
224
+ &root,
225
+ &ScanOptions {
226
+ ignore_patterns: config.ignore_patterns.clone(),
227
+ ..ScanOptions::default()
228
+ },
229
+ )?;
230
+ let report = coverage_inventory_upload_report(
231
+ &project,
232
+ subcommand.get_one::<String>("repo").cloned(),
233
+ subcommand.get_flag("dry-run"),
234
+ );
235
+ match format {
236
+ OutputFormat::Human => {
237
+ writer.write_all(render_coverage_upload_report(&report).as_bytes())?;
238
+ }
239
+ OutputFormat::Json => {
240
+ serde_json::to_writer_pretty(&mut writer, &report)?;
241
+ writeln!(writer)?;
242
+ }
243
+ }
244
+ Ok(0)
245
+ }
246
+
247
+ fn run_coverage_upload_source_maps<W: Write>(
248
+ subcommand: &ArgMatches,
249
+ mut writer: W,
250
+ ) -> Result<i32, CliError> {
251
+ require_dry_run(subcommand, "coverage upload-source-maps")?;
252
+ let root = root_path(subcommand);
253
+ let config = load_config(&root, subcommand)?;
254
+ let format = output_format(subcommand, &config);
255
+ let repo = subcommand
256
+ .get_one::<String>("repo")
257
+ .cloned()
258
+ .unwrap_or_default();
259
+ validate_repo(&repo)?;
260
+ let git_sha = subcommand
261
+ .get_one::<String>("git-sha")
262
+ .cloned()
263
+ .unwrap_or_default();
264
+ validate_git_sha(&git_sha)?;
265
+ let Some(dir_arg) = subcommand.get_one::<PathBuf>("dir") else {
266
+ return Err(CliError::CoverageUploadDir {
267
+ path: PathBuf::from("<missing>"),
268
+ });
269
+ };
270
+ let dir = resolve_path(&root, dir_arg);
271
+ if !dir.is_dir() {
272
+ return Err(CliError::CoverageUploadDir { path: dir });
273
+ }
274
+ let strip_path = subcommand
275
+ .get_one::<bool>("strip-path")
276
+ .copied()
277
+ .unwrap_or(true);
278
+ let report = coverage_source_maps_upload_report(
279
+ &root,
280
+ &dir,
281
+ repo,
282
+ git_sha,
283
+ strip_path,
284
+ subcommand.get_flag("dry-run"),
285
+ )?;
286
+ match format {
287
+ OutputFormat::Human => {
288
+ writer.write_all(render_coverage_upload_report(&report).as_bytes())?;
289
+ }
290
+ OutputFormat::Json => {
291
+ serde_json::to_writer_pretty(&mut writer, &report)?;
292
+ writeln!(writer)?;
293
+ }
294
+ }
295
+ Ok(0)
296
+ }
297
+
298
+ fn require_runtime_coverage(subcommand: &ArgMatches) -> Result<(), CliError> {
299
+ if subcommand.get_one::<PathBuf>("runtime-coverage").is_some() {
300
+ Ok(())
301
+ } else {
302
+ Err(CliError::MissingRuntimeCoverage)
303
+ }
304
+ }
305
+
306
+ fn coverage_health_options(subcommand: &ArgMatches, mut options: HealthOptions) -> HealthOptions {
307
+ options.coverage_path = None;
308
+ options.coverage_gaps = false.into();
309
+ options.max_crap = None;
310
+ options.threshold_overrides.clear();
311
+ options.file_scores = false.into();
312
+ options.hotspots = false.into();
313
+ options.targets = false.into();
314
+ options.ownership = false.into();
315
+
316
+ options.runtime_coverage_path = subcommand.get_one::<PathBuf>("runtime-coverage").cloned();
317
+ if subcommand.value_source("min-invocations-hot") == Some(ValueSource::CommandLine)
318
+ && let Some(value) = subcommand.get_one::<usize>("min-invocations-hot")
319
+ {
320
+ options.min_invocations_hot = (*value).max(1);
321
+ }
322
+ if subcommand.value_source("min-observation-volume") == Some(ValueSource::CommandLine)
323
+ && let Some(value) = subcommand.get_one::<usize>("min-observation-volume")
324
+ {
325
+ options.min_observation_volume = (*value).max(1);
326
+ }
327
+ if subcommand.value_source("low-traffic-threshold") == Some(ValueSource::CommandLine)
328
+ && let Some(value) = subcommand.get_one::<f64>("low-traffic-threshold")
329
+ {
330
+ options.low_traffic_threshold = LowTrafficThreshold::from_ratio(*value);
331
+ }
332
+ if let Some(top) = subcommand.get_one::<usize>("top") {
333
+ options.top = Some(*top);
334
+ }
335
+ options
336
+ }
337
+
338
+ fn resolve_path(root: &Path, path: &Path) -> PathBuf {
339
+ if path.is_absolute() {
340
+ path.to_path_buf()
341
+ } else {
342
+ root.join(path)
343
+ }
344
+ }
345
+
346
+ fn dry_run_arg() -> Arg {
347
+ Arg::new("dry-run")
348
+ .long("dry-run")
349
+ .help("Build the upload packet without network writes")
350
+ .action(ArgAction::SetTrue)
351
+ }
352
+
353
+ fn repo_arg(required: bool) -> Arg {
354
+ Arg::new("repo")
355
+ .long("repo")
356
+ .value_name("OWNER/REPO")
357
+ .help("Repository slug for cloud runtime coverage metadata")
358
+ .required(required)
359
+ .value_parser(value_parser!(String))
360
+ }
361
+
362
+ fn require_dry_run(subcommand: &ArgMatches, command: &'static str) -> Result<(), CliError> {
363
+ if subcommand.get_flag("dry-run") {
364
+ Ok(())
365
+ } else {
366
+ Err(CliError::CoverageUploadDryRunRequired { command })
367
+ }
368
+ }
369
+
370
+ fn validate_repo(repo: &str) -> Result<(), CliError> {
371
+ let mut parts = repo.split('/');
372
+ let valid = matches!(
373
+ (parts.next(), parts.next(), parts.next()),
374
+ (Some(owner), Some(name), None) if !owner.is_empty() && !name.is_empty()
375
+ );
376
+ if valid {
377
+ Ok(())
378
+ } else {
379
+ Err(CliError::CoverageUploadRepo {
380
+ value: repo.to_owned(),
381
+ })
382
+ }
383
+ }
384
+
385
+ fn validate_git_sha(value: &str) -> Result<(), CliError> {
386
+ if (7..=40).contains(&value.len()) && value.chars().all(|char| char.is_ascii_hexdigit()) {
387
+ Ok(())
388
+ } else {
389
+ Err(CliError::CoverageUploadGitSha {
390
+ value: value.to_owned(),
391
+ })
392
+ }
393
+ }