1use crate::{
7 e_collect::collect_all_targets, e_processmanager::ProcessManager, e_target::CargoTarget, Cli,
8};
9#[cfg(feature = "uses_plugins")]
11use crate::plugins::plugin_api::load_plugins;
12use anyhow::{anyhow, Result};
13#[cfg(feature = "uses_plugins")]
14use once_cell::sync::OnceCell;
15use std::{collections::HashSet, path::PathBuf, sync::Arc};
16
17pub struct ExtContext {
19 pub cli: Cli,
21 pub manager: Arc<ProcessManager>,
23 pub cwd: PathBuf,
25 #[cfg(feature = "uses_plugins")]
27 plugins: OnceCell<Vec<Box<dyn crate::plugins::plugin_api::Plugin>>>,
28}
29
30impl ExtContext {
31 pub fn new(cli: Cli, manager: Arc<ProcessManager>) -> Result<Self> {
35 let cwd = std::env::current_dir()?;
36 Ok(ExtContext {
37 cli,
38 manager,
39 cwd,
40 #[cfg(feature = "uses_plugins")]
41 plugins: OnceCell::new(),
42 })
43 }
44
45 pub fn collect_targets(&self) -> Result<Vec<CargoTarget>> {
48 let threads = std::thread::available_parallelism()
50 .map(|n| n.get())
51 .unwrap_or(4);
52 let mut all = collect_all_targets(self.cli.workspace, threads)
53 .map_err(|e| anyhow!("collect_all_targets failed: {}", e))?;
54 #[cfg(feature = "uses_plugins")]
56 {
57 use crate::e_target::TargetKind;
58 use crate::e_target::TargetOrigin;
59 let plugins = self
60 .plugins
61 .get_or_try_init(|| load_plugins(&self.cli, self.manager.clone()))?;
62 for plugin in plugins.iter() {
63 if plugin.matches(&self.cwd) {
64 let plugin_path = plugin
65 .source()
66 .map(PathBuf::from)
67 .unwrap_or_else(|| self.cwd.clone());
68 for pt in plugin.collect_targets(&self.cwd)? {
69 let ct = if let Some(ct) = pt.cargo_target {
70 ct
71 } else {
72 let reported = pt
73 .metadata
74 .as_ref()
75 .map(PathBuf::from)
76 .unwrap_or_else(|| self.cwd.clone());
77 CargoTarget {
78 name: pt.name.clone(),
79 display_name: pt.name.clone(),
80 manifest_path: self.cwd.clone(),
81 kind: TargetKind::Plugin,
82 extended: false,
83 toml_specified: false,
84 origin: Some(TargetOrigin::Plugin {
85 plugin_path: plugin_path.clone(),
86 reported,
87 }),
88 }
89 };
90 all.push(ct);
91 }
92 }
93 }
94 }
95 let mut seen = HashSet::new();
97 all.retain(|t| seen.insert((t.name.clone(), t.kind, t.extended)));
98 Ok(all)
99 }
100 pub fn run_target(&self, target: &CargoTarget) -> Result<Option<std::process::ExitStatus>> {
103 #[cfg(feature = "uses_plugins")]
105 {
106 #[allow(unused_imports)]
107 use crate::e_target::{TargetKind, TargetOrigin};
108 if target.kind == TargetKind::Plugin {
109 let plugins = self
111 .plugins
112 .get_or_try_init(|| load_plugins(&self.cli, self.manager.clone()))?;
113 for plugin in plugins.iter() {
114 if let Some(crate::e_target::TargetOrigin::Plugin { plugin_path, .. }) =
115 &target.origin
116 {
117 if plugin.source().map(PathBuf::from) == Some(plugin_path.clone()) {
118 let _plugin_target =
120 crate::plugins::plugin_api::Target::from(target.clone());
121 plugin.run_with_manager(self.manager.clone(), &self.cli, target)?;
123 return Ok(None);
124 }
125 }
126 }
127 return Ok(None);
128 }
129 }
130 crate::e_runner::run_example(self.manager.clone(), &self.cli, target)
132 }
133}