1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
use anyhow::{format_err, Context, Result};
use super::common;
#[derive(serde::Serialize, serde::Deserialize)]
pub struct StaticData {
pub name: String,
pub registry_host_names: Vec<String>,
}
#[derive(Debug, Clone)]
pub struct ProcessExtension {
process_path_: std::path::PathBuf,
name_: String,
registry_host_names_: Vec<String>,
}
impl common::FromProcess for ProcessExtension {
fn from_process(
process_path: &std::path::PathBuf,
extension_config_path: &std::path::PathBuf,
) -> Result<Self>
where
Self: Sized,
{
let static_data: StaticData = if extension_config_path.is_file() {
let file = std::fs::File::open(&extension_config_path)?;
let reader = std::io::BufReader::new(file);
serde_yaml::from_reader(reader)?
} else {
let static_data: Box<StaticData> = run_process(&process_path, &vec!["static-data"])?;
let static_data = *static_data;
let file = std::fs::OpenOptions::new()
.write(true)
.create(true)
.open(&extension_config_path)
.context(format!(
"Can't open/create file for writing: {}",
extension_config_path.display()
))?;
let writer = std::io::BufWriter::new(file);
serde_yaml::to_writer(writer, &static_data)?;
static_data
};
Ok(ProcessExtension {
process_path_: process_path.clone(),
name_: static_data.name,
registry_host_names_: static_data.registry_host_names,
})
}
}
impl common::Extension for ProcessExtension {
fn name(&self) -> String {
self.name_.clone()
}
fn registries(&self) -> Vec<String> {
self.registry_host_names_.clone()
}
fn identify_local_dependencies(
&self,
working_directory: &std::path::PathBuf,
) -> Result<Vec<common::DependenciesSpec>> {
let working_directory = working_directory.to_str().ok_or(format_err!(
"Failed to parse path into string: {}",
working_directory.display()
))?;
let args = vec!["identify-local-dependencies", working_directory];
let output: Box<Vec<common::DependenciesSpec>> = run_process(&self.process_path_, &args)?;
Ok(*output)
}
fn remote_package_metadata(
&self,
package_name: &str,
package_version: &str,
) -> Result<common::RemotePackageMetadata> {
let args = vec!["remote-package-metadata", package_name, package_version];
let output: Box<common::RemotePackageMetadata> = run_process(&self.process_path_, &args)?;
Ok(*output)
}
}
fn run_process<'a, T: ?Sized>(process_path: &std::path::PathBuf, args: &Vec<&str>) -> Result<Box<T>>
where
for<'de> T: serde::Deserialize<'de> + 'a,
{
let process = process_path.to_str().ok_or(format_err!(
"Failed to parse string from process path: {}",
process_path.display()
))?;
let handle = std::process::Command::new(process)
.args(args)
.stdin(std::process::Stdio::null())
.stderr(std::process::Stdio::piped())
.stdout(std::process::Stdio::piped())
.output()?;
let stdout = String::from_utf8_lossy(&handle.stdout);
let output = serde_json::from_str(&stdout)?;
Ok(Box::new(output))
}