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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
|
use std::{env, fs, io, path::PathBuf, process::Command};
use cargo_metadata::TargetKind;
use mingling::{
Grouped, LazyRes, RenderResult, Routable,
macros::{chain, command, metadata, pack_err, renderer, routeify},
metadata::Description,
};
use crate::{
Next, eprintln_cargo,
metadata::setup::ResMetadata,
pkg_mgr::{ErrorNoDataDirectory, ErrorRootPackageNotFound, ResPackagesDir},
println_cargo,
};
pack_err!(ErrorBuildFailed = String);
pack_err!(ErrorBinaryNotFound = String);
/// Resolved install paths, used by the build step.
#[derive(Debug, Default, Grouped)]
pub struct StateInstallBuild {
pub workspace_root: PathBuf,
pub install_dir: PathBuf,
pub release_dir: PathBuf,
pub exe_suffix: &'static str,
}
/// State after `cargo build --release`, used by the copy step.
#[derive(Debug, Default, Grouped)]
pub struct StateInstallCopy {
pub install_dir: PathBuf,
pub release_dir: PathBuf,
pub exe_suffix: &'static str,
pub installed: Vec<PathBuf>,
}
#[derive(Debug, Default, Grouped)]
pub struct ResultInstall {
pub install_dir: PathBuf,
pub installed: Vec<PathBuf>,
}
/// Parse arguments and resolve the install directory:
/// {data_dir}/.mingling/{PACKAGE_NAME}@{PACKAGE_VERSION}
#[metadata(EntryInstall)]
pub fn desc_install() -> Description {
"Install the project to the Mingling package list".into()
}
#[command(routeify)]
pub fn install(packages_dir: &ResPackagesDir, metadata: &mut LazyRes<ResMetadata>) -> Next {
let metadata = metadata.get_ref().data();
let packages_dir = &packages_dir.path;
if packages_dir.as_os_str().is_empty() {
return ErrorNoDataDirectory::default().to_chain();
}
let root_package = metadata
.root_package()
.or_else(|| metadata.workspace_packages().first().copied())
.ok_or(ErrorRootPackageNotFound::default())?;
StateInstallBuild {
workspace_root: metadata.workspace_root.clone().into_std_path_buf(),
install_dir: packages_dir.join(format!("{}@{}", root_package.name, root_package.version)),
release_dir: metadata
.target_directory
.join("release")
.into_std_path_buf(),
exe_suffix: env::consts::EXE_SUFFIX,
}
.to_chain()
}
/// Step 1: build release binaries.
#[chain(routeify)]
pub fn handle_state_install_build(state: StateInstallBuild) -> Next {
let status = Command::new("cargo")
.args(["build", "--release"])
.current_dir(&state.workspace_root)
.status()
.map_err(|e| {
ErrorBuildFailed::new(format!("failed to run `cargo build --release`: {e}"))
})?;
if !status.success() {
return ErrorBuildFailed::new(format!("`cargo build --release` failed with {status}"))
.to_chain();
}
StateInstallCopy {
install_dir: state.install_dir,
release_dir: state.release_dir,
exe_suffix: state.exe_suffix,
installed: vec![],
}
.to_chain()
}
/// Step 2: copy binaries and completion scripts.
#[chain(routeify)]
pub fn handle_state_install_copy(
mut state: StateInstallCopy,
metadata: &mut LazyRes<ResMetadata>,
) -> Next {
let metadata = metadata.get_ref().data();
fs::create_dir_all(&state.install_dir).map_err(|e| {
io::Error::new(
e.kind(),
format!("failed to create {}: {e}", state.install_dir.display()),
)
})?;
for package in metadata.workspace_packages() {
let bin_targets: Vec<_> = package
.targets
.iter()
.filter(|target| target.kind.contains(&TargetKind::Bin))
.collect();
for target in bin_targets {
let bin_file = format!("{}{}", target.name, state.exe_suffix);
let src = state.release_dir.join(&bin_file);
if !src.is_file() {
return ErrorBinaryNotFound::new(bin_file).to_chain();
}
let dst = state.install_dir.join(&bin_file);
fs::copy(&src, &dst).map_err(|e| {
io::Error::new(e.kind(), format!("failed to copy {}: {e}", src.display()))
})?;
state.installed.push(dst);
}
}
// Completion scripts are generated into the build profile directory
// (OUT_DIR/../../../), copy every one whose name contains `_comp`,
// regardless of its suffix
for entry in fs::read_dir(&state.release_dir).map_err(|e| {
io::Error::new(
e.kind(),
format!("failed to read {}: {e}", state.release_dir.display()),
)
})? {
let entry = entry.map_err(|e| {
io::Error::new(e.kind(), format!("failed to read directory entry: {e}"))
})?;
let name = entry.file_name().to_string_lossy().into_owned();
if name.contains("_comp") {
let dst = state.install_dir.join(&name);
fs::copy(entry.path(), &dst)
.map_err(|e| io::Error::new(e.kind(), format!("failed to copy {name}: {e}")))?;
state.installed.push(dst);
}
}
ResultInstall {
install_dir: state.install_dir,
installed: state.installed,
}
.to_chain()
}
#[renderer]
pub fn render_result_install(result: ResultInstall) -> RenderResult {
let mut r = RenderResult::new();
println_cargo!(r, "Installed: {}", result.install_dir.display());
for file in result.installed {
println_cargo!(r, "Copy: {}", file.display());
}
r
}
#[renderer]
pub fn render_error_build_failed(err: ErrorBuildFailed) -> RenderResult {
let mut r = RenderResult::new();
eprintln_cargo!(r, "{}", err.info);
r
}
#[renderer]
pub fn render_error_binary_not_found(err: ErrorBinaryNotFound) -> RenderResult {
let mut r = RenderResult::new();
eprintln_cargo!(r, "binary not found: {}", err.info);
r
}
|