midenup 0.2.0

The Miden toolchain manager
Documentation
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
use std::{ffi::OsString, fmt::Display, string::ToString};

use anyhow::{Context, anyhow, bail};
use colored::Colorize;

pub use crate::config::Config;
use crate::{
    channel::{Channel, CliCommands, Component, InstalledFile, resolve_command},
    manifest::Manifest,
    toolchain::Toolchain,
};

/// These are the know help messages variants that midenup is aware of.
enum HelpMessage {
    /// Show the default help message, similar to the one you would get with clap's "--help" flag.
    Default,
    /// Show a help message specific to the current active [Toolchain].
    ///
    /// NOTE: This help message *could* trigger an install if the active [Toolchain] is not
    /// installed.
    Toolchain,
    /// This variant represents a "fallback" option where we save the user's input so that we later
    /// on try to map it to a [Component].
    ///
    /// This mapping is dependent on the currently active [Toolchain]. These will try to be resolved
    /// into a [MidenArgument].
    ///
    /// NOTE: This help message *could* trigger an install if the active [Toolchain] is not
    /// installed.
    Resolve(String),
}

/// The possible non-help commands that a user's input can be resolved into.
#[derive(Debug)]
enum MidenArgument {
    /// The passed argument was an alias stored in the local [Manifest].
    ///
    /// [AliasResolution] represents the list of commands that need to be executed.
    ///
    /// NOTE: Some of these might need to get resolved.
    Alias(Component, CliCommands),
    /// The argument was the name of a component stored in the [Manifest].
    Component(Component),
}

/// Struct containing the command to execute and the channel to execute it against.
struct ExecutionEnvironment<'a> {
    argument: MidenArgument,
    active_channel: &'a Channel,
}

enum EnvironmentError {
    UnkownArgument(String),
    LibraryAsExecutable(String),
    AliasOnly(String),
}

impl Display for EnvironmentError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match &self {
            EnvironmentError::UnkownArgument(err) => write!(f, "{err}"),
            EnvironmentError::LibraryAsExecutable(err) => write!(f, "{err}"),
            EnvironmentError::AliasOnly(err) => write!(f, "{err}"),
        }
    }
}

#[derive(Debug)]
struct ToolchainEnvironment<'a> {
    /// We use the original channel as a fallback to [`ToolchainEnvironment::active_channel`].
    ///
    /// If the active channel does not contain a requested component, for convenience's sake, we
    /// check if it exists in the original_channel. If it does, we execute it, after displaying a
    /// warning message.
    installed_channel: &'a Channel,
    /// This is the channel that is currently active.
    ///
    /// This *might* differ slightly from the original upstream channel equivalent in some
    /// scenarios, e.g. the user only selected a subset of components for downloads.
    active_channel: Option<Channel>,
}

#[derive(Debug, Clone, Copy)]
enum ChannelType {
    Installed,
    Active,
}
impl<'a> ToolchainEnvironment<'a> {
    fn new(installed_channel: &'a Channel, active_channel: Option<Channel>) -> Self {
        ToolchainEnvironment { installed_channel, active_channel }
    }

    /// This is the channel that is currently active.
    ///
    /// This *might* differ slightly from the original upstream channel equivalent in some
    /// scenarios, e.g. the user only selected a subset of components for downloads.
    fn get_active_channel(&self) -> (&Channel, ChannelType) {
        if let Some(active_channel) = self.active_channel.as_ref() {
            (active_channel, ChannelType::Active)
        } else {
            (self.installed_channel, ChannelType::Installed)
        }
    }

    /// Parses the user's input and returns the required [ExecutionEnvironment] to execute the
    /// requested command.
    fn resolve(&self, argument: String) -> Result<ExecutionEnvironment<'_>, EnvironmentError> {
        [
            (self.active_channel.as_ref() , ChannelType::Active),
            (Some(self.installed_channel), ChannelType::Installed),
        ]
        .into_iter()
        // We only consider the channel as available if it's not None. We can always fallback on the
        // installed channel.
        .filter_map(|(ch, ch_type)| ch.map(|ch| (ch, ch_type)))
        .flat_map(|(ch, ch_type)| ch.components.iter().map(move |comp| (comp, ch_type, ch)))
        .find_map(|(comp, ch_type, ch)| {
            if let Some(associated_command) = comp.aliases.get(&argument) {
                Some(Ok((ch, (MidenArgument::Alias(comp.clone(), associated_command.to_owned()), ch_type))))
            } else if comp.name == argument {
                match comp.get_installed_file() {
                    InstalledFile::Executable { alias_only: false, binary_name: _ } => {
                        Some(Ok((ch, (MidenArgument::Component(comp.clone()), ch_type))))
                    },
                    InstalledFile::Executable { alias_only: true, binary_name: _ } => {
                        let aliases = comp.aliases.keys().map(|alias| format!("'{}'", alias)).collect::<Vec<_>>().join(", ");
                        Some(Err(EnvironmentError::AliasOnly(format!("'{}' is not intended to be called via 'miden', but rather by its aliases: {aliases}", comp.name))))
                    },
                    InstalledFile::Library { library_name, library_struct: _ } => {
                        Some(Err(EnvironmentError::LibraryAsExecutable(
                            format!("'{}' installs the {} library. It is not intended to be executed as a binary.", comp.name, library_name))))
                    },
                }
            } else {
                None
            }
        })
        .inspect(|resolution| {
            if let Some(warning_message) = match resolution {
                // We only display an eror message if a user tried to access a component that was
                // available via the installed channel while having an active channel that was
                // missing said component.
                Ok((_, (MidenArgument::Alias(comp, _ ), ChannelType::Installed))) => Some(format!(
                    "{}: {} is an alias from component {}, which is installed but is not part of the current active toolchain.",
                    "WARNING".yellow().bold(),
                    argument,
                    comp.name,

                )),
                Ok((_, (MidenArgument::Component(comp), ChannelType::Installed))) => Some(format!(
                    "{}: {} is installed, but it is not part of the current active toolchain.",
                    "WARNING".yellow().bold(),
                    comp.name,

                )),
                _ => None,
            } {
                println!("{warning_message}")
            }
        }
        )
        .unwrap_or(Err(EnvironmentError::UnkownArgument(format!("Failed to resolve '{}': Neither known alias or component.", argument))))
        .map(|(channel, (argument, _))| Ok(ExecutionEnvironment { argument, active_channel: channel }))?
    }

    fn get_executables_display(&self) -> String {
        self.get_active_channel()
            .0
            .components
            .iter()
            .filter(|c| {
                matches!(
                    c.get_installed_file(),
                    InstalledFile::Executable { binary_name: _, alias_only: false }
                )
            })
            .map(|c| format!("  {}\n", c.name.bold()))
            .collect::<String>()
    }

    fn get_libraries_display(&self) -> String {
        self.get_active_channel()
            .0
            .components
            .iter()
            .filter_map(|comp| match comp.get_installed_file() {
                InstalledFile::Library { library_name, .. } => {
                    let display_name = format!("  {}\n", library_name);
                    Some(display_name)
                },
                _ => None,
            })
            .collect::<String>()
    }

    fn get_aliases_display(&self) -> String {
        let aliases = self.get_active_channel().0.get_aliases();
        let mut keys: Vec<_> = aliases.keys().collect();
        keys.sort();
        keys.iter().map(|alias| format!("  {}\n", alias.bold())).collect::<String>()
    }
}

/// These are the possible types of subcommands that `miden` is aware of.
enum MidenSubcommand {
    /// Aliases that correspond to a tuple of a known component + a set of prefixed arguments.
    ///
    /// For more information, see [MidenAliases].
    ///
    /// NOTE: With the exception of [`HelpMessage::Default`], this command *could* trigger an
    /// install if the active [Toolchain] is not installed.
    Help(HelpMessage),
    /// Displays midenup cargo version ang git revision hash.
    Version,
    /// The user passed in a subcommand that needs to be resolved using the currently active
    /// [Toolchain].
    ///
    /// Resolution can result in one of the following elements:
    ///
    /// - An alias
    /// - A [Component]
    ///
    /// If it's none of those, then we error out.
    ///
    /// NOTE: This command *could* trigger an install if the active [Toolchain] is not installed.
    Resolve(String),
}

fn parse_subcommand(subcommand: &str, argv: &[OsString]) -> MidenSubcommand {
    if subcommand == "help" {
        match argv.get(2).and_then(|c| c.to_str()) {
            None => MidenSubcommand::Help(HelpMessage::Default),
            Some("toolchain") => MidenSubcommand::Help(HelpMessage::Toolchain),
            Some(other) => MidenSubcommand::Help(HelpMessage::Resolve(other.to_string())),
        }
    } else if subcommand == "--version" {
        MidenSubcommand::Version
    } else {
        MidenSubcommand::Resolve(subcommand.to_string())
    }
}

pub fn miden_wrapper(
    argv: Vec<OsString>,
    config: &Config,
    local_manifest: &mut Manifest,
) -> anyhow::Result<()> {
    // Extract the target binary to execute from argv[1]
    let subcommand = {
        let subcommand = argv.get(1).with_context(|| {
            format!(
                "
{}: '{}' requires a subcommand but one was not provided

{} {} <ALIAS|COMMAND>

For more information, try 'miden help'.
",
                "error:".red().bold(),
                "miden".yellow().bold(),
                "Usage".bold().underline(),
                "miden".bold(),
            )
        })?;
        subcommand.to_str().expect("Invalid command name: {subcommand}")
    };

    let parsed_subcommand = parse_subcommand(subcommand, &argv);

    // NOTE: We handle these case first to avoid triggering an install when help related commands
    // are run.
    match parsed_subcommand {
        MidenSubcommand::Help(HelpMessage::Default) => {
            println!("{}", default_help());
            return Ok(());
        },
        MidenSubcommand::Version => {
            println!("{}", display_version(config));
            return Ok(());
        },
        _ => (),
    }

    // Make sure we know the current toolchain so we can modify the PATH appropriately
    let (toolchain, _, partial_channel) =
        Toolchain::ensure_current_is_installed(config, local_manifest)?;

    let toolchain_environment = {
        let installed_channel = local_manifest
            .get_channel(&toolchain.channel)
            .context("Couldn't find active toolchain in the manifest.")?;

        ToolchainEnvironment::new(installed_channel, partial_channel)
    };

    let help_flag = match parsed_subcommand {
        MidenSubcommand::Help(HelpMessage::Default) => unreachable!(),
        MidenSubcommand::Help(HelpMessage::Toolchain) => {
            let help = toolchain_help(&toolchain_environment);

            println!("{help}");

            return Ok(());
        },
        MidenSubcommand::Help(HelpMessage::Resolve(_)) => {
            // NOTE: We rely on the different component's CLI interfaces to recognize the "--help"
            // flag. Currently, this relies on the fact that clap recognizes said flag by default.
            // Source: https://github.com/clap-rs/clap/blob/583ba4ad9a4aea71e5b852b142715acaeaaaa050/src/_features.rs#L10
            Some(String::from("--help"))
        },
        _ => None,
    };

    // We obtain the target executable and prefixes that are associated with the passed subcommand.
    let (target_exe, mut prefix_args, active_channel) = match parsed_subcommand {
        MidenSubcommand::Version => unreachable!(),
        MidenSubcommand::Help(HelpMessage::Default) => unreachable!(),
        MidenSubcommand::Help(HelpMessage::Toolchain) => unreachable!(),
        // Resolution, either for help or for actual execution is the same. The only difference is
        // wheter we append "--help" at the end and if we process additional arguments.
        MidenSubcommand::Help(HelpMessage::Resolve(resolve))
        | MidenSubcommand::Resolve(resolve) => {
            match toolchain_environment.resolve(resolve.clone()) {
                Ok(ExecutionEnvironment {
                    argument: MidenArgument::Alias(component, alias_resolutions),
                    active_channel,
                }) => {
                    let commands =
                        resolve_command(&alias_resolutions, active_channel, &component, config)?;

                    // SAFETY: Safe under the assumption that every alias has an associated command.
                    let command = commands.first().unwrap().clone();
                    let aliased_arguments: Vec<String> = commands.into_iter().skip(1).collect();

                    (command, aliased_arguments, active_channel)
                },
                Ok(ExecutionEnvironment {
                    argument: MidenArgument::Component(component),
                    active_channel,
                }) => {
                    let call_convention = resolve_command(
                        &component.get_call_format(),
                        active_channel,
                        &component,
                        config,
                    )?;

                    // SAFETY: Safe under the assumption that every call_format has at least one
                    // argument
                    let command = call_convention.first().unwrap().clone();
                    let args: Vec<String> = call_convention.into_iter().skip(1).collect();

                    (command, args, active_channel)
                },
                Err(err) => {
                    let help_message = toolchain_help(&toolchain_environment);
                    let err_msg = format!(
                        "{}

{}",
                        err, help_message
                    );
                    bail!(err_msg);
                },
            }
        },
    };

    let rest_of_args = if let Some(help_flag) = help_flag {
        prefix_args.extend([help_flag]);
        // If the user requested for help for a specific component, we skip any additional passed in
        // arguments.
        argv.iter().skip(argv.len())
    } else {
        // argv is of the form:
        //
        // miden <alias|component> ...
        //
        // So we skip the first two and pass the rest to the underlying executable.
        argv.iter().skip(2)
    };

    let prefix_args = prefix_args.iter().map(OsString::from).chain(rest_of_args.cloned()).collect();

    let mut command = config
        .execute_command(active_channel, &target_exe, &prefix_args)
        .with_context(|| format!("failed to run 'miden {subcommand}'"))?;

    let status = command.wait().with_context(|| {
        format!("error occurred while waiting for 'miden {subcommand}' to finish executing")
    })?;

    if status.success() {
        Ok(())
    } else {
        bail!("'miden {}' failed with status {}", subcommand, status.code().unwrap_or(1))
    }
}

pub fn display_version(config: &Config) -> String {
    // NOTE: These files are generated in the project's build.rs.

    let compiled_cargo_version = include_str!(concat!(env!("OUT_DIR"), "/cargo_version.in"));

    let git_revision = include_str!(concat!(env!("OUT_DIR"), "/git_revision.in"));

    let midenup_version = env!(
        "CARGO_PKG_VERSION",
        "CARGO_PKG_VERSION environment variable not set.This should be set by cargo by default; \
         however, if not, it can be manually set using the `version` field in the Cargo.toml file"
    );
    let cargo_version = {
        std::process::Command::new("cargo")
            .arg("--version")
            .output()
            .map_err(|err| anyhow::anyhow!("failed to run 'cargo --version' because of {err}"))
            .and_then(|output| {
                String::from_utf8(output.stdout).map_err(|err| {
                    anyhow::anyhow!("failed to parse cargo version because of: {err}")
                })
            })
            .inspect_err(|e| {
                println!("Failed to obtain cargo version:");
                println!("{}", e);
                println!("Leaving as unknown")
            })
            .unwrap_or("unknown".to_string())
    };
    let cargo_version = cargo_version.trim();

    let toolchain_version = Toolchain::current(config)
        .and_then(|(toolchain, _)| {
            config
                .manifest
                .get_channel(&toolchain.channel)
                .map(|channel| channel.name.to_string())
                .ok_or(anyhow!("channel: {} doesn't exist or isn't available ", toolchain.channel))
        })
        .inspect_err(|err| {
            println!(
                "failed to obtain current toolchain error because of: {err}, leaving as unknown"
            )
        })
        .unwrap_or("unknown".to_string());

    let github_issue = {
        let short_body = format!(
            "<!--- (leave this at the bottom) --> midenup:{midenup_version}, toolchain: \
             {toolchain_version}, cargo:{cargo_version}, rev:{git_revision}"
        );
        format!(
            "https://github.com/0xMiden/midenup/issues/new?title=bug:<YOUR_ISSUE>&body={short_body}"
        )
    };

    format!(
        "
The Miden toolchain porcelain:

Environment:
- cargo version: {cargo_version}.

Midenup:
- midenup + miden version: {midenup_version}.
- active toolchain version: {toolchain_version}.
- midenup revision: {git_revision}.
- midenup was compiled with {compiled_cargo_version}.


Found a bug? Create an issue by copying this into your browser:

{github_issue}
"
    )
}

fn toolchain_help(toolchain_environment: &ToolchainEnvironment) -> String {
    let usage = "Usage:".bold().underline();
    let miden = "miden".bold();
    let asterisk = "*".bold();

    let available_aliases_text = "Available aliases:".bold().underline();
    let available_aliases: String = toolchain_environment.get_aliases_display();

    let available_components_text = "Available components:".bold().underline();
    let available_components: String = toolchain_environment.get_executables_display();

    let available_libraries_text = "Available libraries:".bold().underline();
    let available_libraries: String = toolchain_environment.get_libraries_display();

    let help = "Help:".bold().underline();

    format!(
        "The Miden toolchain porcelain

{usage} {miden} <ALIAS|COMPONENT>

{available_aliases_text}
{available_aliases}
{available_components_text}
{available_components}
{available_libraries_text}
{available_libraries}

{help}
  help                   Print this help message
  help toolchain         Print this help message {asterisk}
  help <COMPONENT>       Print <COMPONENTS>'s help message {asterisk}

{asterisk}: These commands will install the currently present toolchain if not installed.
",
    )
}

fn default_help() -> String {
    let asterisk = "*".bold();
    let help = "Help:".bold().underline();
    format!(
        "The Miden toolchain porcelain

{help}
  help                   Print this help message
  help toolchain         Print help about the currently available aliases and components {asterisk}
  help <COMPONENT>       Print a specific <COMPONENTS>'s help message {asterisk}

{asterisk}: These commands will install the currently present toolchain if not installed.
",
    )
}