aboutsummaryrefslogtreecommitdiff
path: root/mingling_cli/src/pkg_mgr/cmd_install.rs
blob: 731ba988ebe8a3f600174bf2ccf5d0a612c07229 (plain) (blame)
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
use std::{env, fs, io, path::PathBuf, process::Command};

use cargo_metadata::TargetKind;
use mingling::{
    Grouped, LazyRes, RenderResult, Routable, ShellContext, Suggest,
    macros::{arg, chain, command, completion, metadata, pack_err, renderer, routeify, suggest},
    metadata::Description,
    picker::{EntryPicker, PickerArg, value::Flag},
};

use crate::{
    Next, eprintln_cargo,
    metadata::setup::ResMetadata,
    pkg_mgr::{ErrorNoDataDirectory, ErrorRootPackageNotFound, ResPackagesDir},
    println_cargo,
};

pack_err!(ErrorBuildFailed = String);
pack_err!(ErrorBinaryNotFound = String);
pack_err!(ErrorPkgEnableFailed = String);

/// Flag: `--enable` — run `mling pkg-enable` after a successful install
/// to enable the package being installed.
pub static ARG_ENABLE: PickerArg<Flag> = arg![enable: Flag];

/// 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,
    pub enable: bool,
}

/// 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>,
    pub enable: bool,
}

/// State after the copy step when `--enable` was given: run `mling pkg-enable`.
#[derive(Debug, Default, Grouped)]
pub struct StateInstallEnable {
    pub install_dir: PathBuf,
    pub installed: Vec<PathBuf>,
    pub name: String,
    pub version: String,
}

#[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/packages/{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(
    args: EntryInstall,
    packages_dir: &ResPackagesDir,
    metadata: &mut LazyRes<ResMetadata>,
) -> Next {
    let enable = args.pick(&ARG_ENABLE).to_result()?;
    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,
        enable: enable.bool(),
    }
    .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![],
        enable: state.enable,
    }
    .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);
        }
    }

    if state.enable {
        let root_package = metadata
            .root_package()
            .or_else(|| metadata.workspace_packages().first().copied())
            .ok_or(ErrorRootPackageNotFound::default())?;
        return StateInstallEnable {
            install_dir: state.install_dir,
            installed: state.installed,
            name: root_package.name.to_string(),
            version: root_package.version.to_string(),
        }
        .to_chain();
    }

    ResultInstall {
        install_dir: state.install_dir,
        installed: state.installed,
    }
    .to_chain()
}

/// Step 3 (optional): enable the installed package via `mling pkg-enable`
/// when `--enable` was given.
#[chain(routeify)]
pub fn handle_state_install_enable(state: StateInstallEnable) -> Next {
    let spec = format!("{}@{}", state.name, state.version);
    let status = Command::new("mling")
        .args(["pkg-enable", &spec])
        .status()
        .map_err(|e| {
            ErrorPkgEnableFailed::new(format!("failed to run `mling pkg-enable {spec}`: {e}"))
        })?;
    if !status.success() {
        return ErrorPkgEnableFailed::new(format!(
            "`mling pkg-enable {spec}` failed with {status}"
        ))
        .to_chain();
    }

    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
}

#[renderer]
pub fn render_error_pkg_enable_failed(err: ErrorPkgEnableFailed) -> RenderResult {
    let mut r = RenderResult::new();
    eprintln_cargo!(r, "{}", err.info);
    r
}

#[completion(EntryInstall)]
pub fn complete_install(ctx: &ShellContext) -> Suggest {
    if ctx.previous_word != "install" {
        return Suggest::FileCompletion;
    }
    suggest! {
        ARG_ENABLE: "Enable the package after installing (runs `mling pkg-enable`)"
    }
}