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
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
use crate::host_funcs::ExecCommandOutput;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::ffi::OsStr;
use std::path::PathBuf;
use warpgate_api::VirtualPath;

pub use semver::{Version, VersionReq};

#[macro_export]
macro_rules! json_struct {
    ($struct:item) => {
        #[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
        #[serde(default)]
        $struct
    };
}

#[macro_export]
macro_rules! json_enum {
    ($struct:item) => {
        #[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
        $struct
    };
}

json_struct!(
    /// Represents an empty input.
    pub struct EmptyInput {}
);

json_struct!(
    /// Information about the current state of the tool.
    pub struct ToolContext {
        /// Requested environment variables. Only non-empty values are included.
        pub env_vars: HashMap<String, String>,

        /// Virtual path to the tool's installation directory.
        pub tool_dir: VirtualPath,

        /// Current version. Will be empty if not resolved.
        pub version: String,
    }
);

json_enum!(
    /// Supported types of plugins.
    #[derive(Default)]
    pub enum PluginType {
        #[default]
        Language,
        DependencyManager,
        CLI,
    }
);

json_struct!(
    /// Input passed to the `register_tool` function.
    pub struct ToolMetadataInput {
        /// ID of the tool, as it was configured.
        pub id: String,
    }
);

json_struct!(
    /// Controls aspects of the tool inventory.
    pub struct ToolInventoryMetadata {
        /// Disable progress bars when installing or uninstalling tools.
        pub disable_progress_bars: bool,

        /// Override the tool inventory directory (where all versions are installed).
        /// This is an advanced feature and should only be used when absolutely necessary.
        #[serde(skip_serializing_if = "Option::is_none")]
        pub override_dir: Option<PathBuf>,

        /// Suffix to append to all versions when labeling directories.
        #[serde(skip_serializing_if = "Option::is_none")]
        pub version_suffix: Option<String>,
    }
);

json_struct!(
    /// Output returned by the `register_tool` function.
    pub struct ToolMetadataOutput {
        /// Default alias or version to use as a fallback.
        #[serde(skip_serializing_if = "Option::is_none")]
        pub default_version: Option<String>,

        /// Environment variables that should be extracted
        /// and passed to other function call inputs.
        pub env_vars: Vec<String>,

        /// Controls aspects of the tool inventory.
        pub inventory: ToolInventoryMetadata,

        /// Human readable name of the tool.
        pub name: String,

        /// Version of the plugin.
        #[serde(skip_serializing_if = "Option::is_none")]
        pub plugin_version: Option<String>,

        /// Type of the tool.
        pub type_of: PluginType,
    }
);

// Detector

json_struct!(
    /// Output returned by the `detect_version_files` function.
    pub struct DetectVersionOutput {
        /// List of files that should be checked for version information.
        pub files: Vec<String>,
    }
);

json_struct!(
    /// Input passed to the `parse_version_file` function.
    pub struct ParseVersionFileInput {
        /// File contents to parse/extract a version from.
        pub content: String,

        /// Name of file that's being parsed.
        pub file: String,
    }
);

json_struct!(
    /// Output returned by the `parse_version_file` function.
    pub struct ParseVersionFileOutput {
        /// The version that was extracted from the file.
        /// Can be a semantic version or a version requirement/range.
        #[serde(skip_serializing_if = "Option::is_none")]
        pub version: Option<String>,
    }
);

// Downloader, Installer, Verifier

json_struct!(
    /// Input passed to the `native_install` function.
    pub struct NativeInstallInput {
        /// Current tool context.
        pub context: ToolContext,
    }
);

json_struct!(
    /// Output returned by the `native_install` function.
    pub struct NativeInstallOutput {
        /// Whether the install was successful.
        pub installed: bool,
    }
);

json_struct!(
    /// Input passed to the `native_uninstall` function.
    pub struct NativeUninstallInput {
        /// Current tool context.
        pub context: ToolContext,
    }
);

json_struct!(
    /// Output returned by the `native_uninstall` function.
    pub struct NativeUninstallOutput {
        /// Whether the install was successful.
        pub uninstalled: bool,
    }
);

json_struct!(
    /// Input passed to the `download_prebuilt` function.
    pub struct DownloadPrebuiltInput {
        /// Current tool context.
        pub context: ToolContext,
    }
);

json_struct!(
    /// Output returned by the `download_prebuilt` function.
    pub struct DownloadPrebuiltOutput {
        /// Name of the direct folder within the archive that contains the tool,
        /// and will be removed when unpacking the archive.
        #[serde(skip_serializing_if = "Option::is_none")]
        pub archive_prefix: Option<String>,

        /// File name of the checksum to download. If not provided,
        /// will attempt to extract it from the URL.
        #[serde(skip_serializing_if = "Option::is_none")]
        pub checksum_name: Option<String>,

        /// A secure URL to download the checksum file for verification.
        /// If the tool does not support checksum verification, this setting can be omitted.
        #[serde(skip_serializing_if = "Option::is_none")]
        pub checksum_url: Option<String>,

        /// File name of the archive to download. If not provided,
        /// will attempt to extract it from the URL.
        #[serde(skip_serializing_if = "Option::is_none")]
        pub download_name: Option<String>,

        /// A secure URL to download the tool/archive.
        pub download_url: String,
    }
);

json_struct!(
    /// Input passed to the `unpack_archive` function.
    pub struct UnpackArchiveInput {
        /// Current tool context.
        pub context: ToolContext,

        /// Virtual path to the downloaded file.
        pub input_file: VirtualPath,

        /// Virtual directory to unpack the archive into, or copy the binary to.
        pub output_dir: VirtualPath,
    }
);

json_struct!(
    /// Output returned by the `verify_checksum` function.
    pub struct VerifyChecksumInput {
        /// Current tool context.
        pub context: ToolContext,

        /// The SHA-256 hash of the downloaded file.
        pub checksum: String,

        /// Virtual path to the checksum file.
        pub checksum_file: VirtualPath,

        /// Virtual path to the downloaded file.
        pub download_file: VirtualPath,
    }
);

json_struct!(
    /// Output returned by the `verify_checksum` function.
    pub struct VerifyChecksumOutput {
        pub verified: bool,
    }
);

// Executor

json_struct!(
    /// Input passed to the `locate_bins` function.
    pub struct LocateBinsInput {
        /// Current tool context.
        pub context: ToolContext,
    }
);

json_struct!(
    /// Output returned by the `locate_bins` function.
    pub struct LocateBinsOutput {
        /// Relative path from the tool directory to the binary to execute.
        #[serde(skip_serializing_if = "Option::is_none")]
        pub bin_path: Option<PathBuf>,

        /// When true, the last item in `globals_lookup_dirs` will be used,
        /// regardless if it exists on the file system or not.
        pub fallback_last_globals_dir: bool,

        /// List of directory paths to find the globals installation directory.
        /// Each path supports environment variable expansion.
        pub globals_lookup_dirs: Vec<String>,

        /// A string that all global binaries are prefixed with, and will be removed
        /// when listing and filtering available globals.
        #[serde(skip_serializing_if = "Option::is_none")]
        pub globals_prefix: Option<String>,
    }
);

json_struct!(
    /// Input passed to the `install_global` function.
    pub struct InstallGlobalInput {
        /// Current tool context.
        pub context: ToolContext,

        /// Name (and optional version) of the global dependency to install.
        pub dependency: String,

        /// Virtual path to the global's installation directory.
        pub globals_dir: VirtualPath,
    }
);

json_struct!(
    /// Output returned by the `install_global` function.
    pub struct InstallGlobalOutput {
        /// Whether the install was successful.
        pub installed: bool,

        /// Error message if the install failed.
        #[serde(skip_serializing_if = "Option::is_none")]
        pub error: Option<String>,
    }
);

impl InstallGlobalOutput {
    pub fn from_exec_command(result: ExecCommandOutput) -> Self {
        if result.exit_code == 0 {
            return Self {
                installed: true,
                error: None,
            };
        }

        Self {
            installed: false,
            error: Some(result.stderr),
        }
    }
}

json_struct!(
    /// Input passed to the `uninstall_global` function.
    pub struct UninstallGlobalInput {
        /// Current tool context.
        pub context: ToolContext,

        /// Name (and optional version) of the global dependency to uninstall.
        pub dependency: String,

        /// Virtual path to the global's installation directory.
        pub globals_dir: VirtualPath,
    }
);

json_struct!(
    /// Output returned by the `uninstall_global` function.
    pub struct UninstallGlobalOutput {
        /// Whether the uninstall was successful.
        pub uninstalled: bool,

        /// Error message if the uninstall failed.
        #[serde(skip_serializing_if = "Option::is_none")]
        pub error: Option<String>,
    }
);

impl UninstallGlobalOutput {
    pub fn from_exec_command(result: ExecCommandOutput) -> Self {
        if result.exit_code == 0 {
            return Self {
                uninstalled: true,
                error: None,
            };
        }

        Self {
            uninstalled: false,
            error: Some(result.stderr),
        }
    }
}

// Resolver

json_struct!(
    /// Input passed to the `load_versions` function.
    pub struct LoadVersionsInput {
        /// Current tool context.
        pub context: ToolContext,

        /// The alias or version currently being resolved.
        pub initial: String,
    }
);

json_struct!(
    /// Output returned by the `load_versions` function.
    pub struct LoadVersionsOutput {
        /// Latest stable version.
        #[serde(skip_serializing_if = "Option::is_none")]
        pub latest: Option<Version>,

        /// Mapping of aliases (channels, etc) to a version.
        pub aliases: HashMap<String, Version>,

        /// List of available production versions to install.
        pub versions: Vec<Version>,

        /// List of available canary versions to install.
        pub canary_versions: Vec<Version>,
    }
);

impl LoadVersionsOutput {
    #[deprecated = "Use from() instead."]
    pub fn from_tags(tags: &[String]) -> anyhow::Result<Self> {
        Self::from(tags.to_vec())
    }

    /// Create the output from a list of strings that'll be parsed as versions.
    /// The latest version will be the highest version number.
    pub fn from(values: Vec<String>) -> anyhow::Result<Self> {
        let mut versions = vec![];

        for value in values {
            versions.push(Version::parse(&value)?);
        }

        Self::from_versions(versions)
    }

    /// Create the output from a list of versions.
    /// The latest version will be the highest version number.
    pub fn from_versions(versions: Vec<Version>) -> anyhow::Result<Self> {
        let mut output = LoadVersionsOutput::default();
        let mut latest = Version::new(0, 0, 0);

        for version in versions {
            if version.pre.is_empty() && version.build.is_empty() && version > latest {
                latest = version.clone();
            }

            output.versions.push(version);
        }

        output.latest = Some(latest.clone());
        output.aliases.insert("latest".into(), latest);

        Ok(output)
    }
}

json_struct!(
    /// Input passed to the `resolve_version` function.
    pub struct ResolveVersionInput {
        /// Current tool context.
        pub context: ToolContext,

        /// The alias or version currently being resolved.
        pub initial: String,
    }
);

json_struct!(
    /// Output returned by the `resolve_version` function.
    pub struct ResolveVersionOutput {
        /// New alias or version candidate to resolve.
        #[serde(skip_serializing_if = "Option::is_none")]
        pub candidate: Option<String>,

        /// An explicitly resolved version to be used as-is.
        /// Note: Only use this field if you know what you're doing!
        #[serde(skip_serializing_if = "Option::is_none")]
        pub version: Option<String>,
    }
);

// Shimmer

json_struct!(
    /// Configuration for individual shim files.
    pub struct ShimConfig {
        /// Relative path from the tool directory to the binary to execute.
        #[serde(skip_serializing_if = "Option::is_none")]
        pub bin_path: Option<PathBuf>,

        /// Name of a parent binary that's required for this shim to work.
        /// For example, `npm` requires `node`.
        #[serde(skip_serializing_if = "Option::is_none")]
        pub parent_bin: Option<String>,

        /// Custom args to prepend to user-provided args.
        #[serde(skip_serializing_if = "Option::is_none")]
        pub before_args: Option<String>,

        /// Custom args to append to user-provided args.
        #[serde(skip_serializing_if = "Option::is_none")]
        pub after_args: Option<String>,
    }
);

impl ShimConfig {
    /// Create a global shim that executes the parent tool,
    /// but uses the provided binary as the entry point.
    pub fn global_with_alt_bin<B>(bin_path: B) -> ShimConfig
    where
        B: AsRef<OsStr>,
    {
        ShimConfig {
            bin_path: Some(bin_path.as_ref().into()),
            ..ShimConfig::default()
        }
    }

    /// Create a global shim that executes the parent tool,
    /// but prefixes the user-provided arguments with the
    /// provided arguments (typically a sub-command).
    pub fn global_with_sub_command<A>(args: A) -> ShimConfig
    where
        A: AsRef<str>,
    {
        ShimConfig {
            before_args: Some(args.as_ref().to_owned()),
            ..ShimConfig::default()
        }
    }

    /// Create a local shim that executes the provided binary.
    pub fn local<B>(bin_path: B) -> ShimConfig
    where
        B: AsRef<OsStr>,
    {
        ShimConfig {
            bin_path: Some(bin_path.as_ref().into()),
            ..ShimConfig::default()
        }
    }

    /// Create a local shim that executes the provided binary
    /// through the context of the configured parent.
    pub fn local_with_parent<B, P>(bin_path: B, parent: P) -> ShimConfig
    where
        B: AsRef<OsStr>,
        P: AsRef<str>,
    {
        ShimConfig {
            bin_path: Some(bin_path.as_ref().into()),
            parent_bin: Some(parent.as_ref().to_owned()),
            ..ShimConfig::default()
        }
    }
}

json_struct!(
    /// Input passed to the `create_shims` function.
    pub struct CreateShimsInput {
        /// Current tool context.
        pub context: ToolContext,
    }
);

json_struct!(
    /// Output returned by the `create_shims` function.
    pub struct CreateShimsOutput {
        /// Avoid creating the global shim.
        pub no_primary_global: bool,

        /// Configures the default/primary global shim.
        #[serde(skip_serializing_if = "Option::is_none")]
        pub primary: Option<ShimConfig>,

        /// Additional global shims to create in the `~/.proto/bin` directory.
        /// Maps a shim name to a relative binary path.
        pub global_shims: HashMap<String, ShimConfig>,

        /// Local shims to create in the `~/.proto/tools/<id>/<version>/shims` directory.
        /// Maps a shim name to its configuration.
        pub local_shims: HashMap<String, ShimConfig>,
    }
);

// Misc

json_struct!(
    /// Input passed to the `sync_manifest` function.
    pub struct SyncManifestInput {
        /// Current tool context.
        pub context: ToolContext,
    }
);

json_struct!(
    /// Output returned by the `sync_manifest` function.
    pub struct SyncManifestOutput {
        /// Override the default version with a new alias or version.
        #[serde(skip_serializing_if = "Option::is_none")]
        pub default_version: Option<String>,

        /// List of versions that are currently installed. Will replace
        /// what is currently in the manifest.
        #[serde(skip_serializing_if = "Option::is_none")]
        pub versions: Option<Vec<Version>>,

        /// Whether to skip the syncing process or not.
        pub skip_sync: bool,
    }
);

json_struct!(
    /// Input passed to the `sync_shell_profile` function.
    pub struct SyncShellProfileInput {
        /// Current tool context.
        pub context: ToolContext,

        /// Arguments passed after `--` that was directly passed to the tool's binary.
        pub passthrough_args: Vec<String>,
    }
);

json_struct!(
    /// Output returned by the `sync_shell_profile` function.
    pub struct SyncShellProfileOutput {
        /// An environment variable to check for in the shell profile.
        /// If the variable exists, injecting path and exports will be avoided.
        pub check_var: String,

        /// A mapping of environment variables that will be injected as exports.
        #[serde(skip_serializing_if = "Option::is_none")]
        pub export_vars: Option<HashMap<String, String>>,

        /// A list of paths to prepend to the `PATH` environment variable.
        #[serde(skip_serializing_if = "Option::is_none")]
        pub extend_path: Option<Vec<String>>,

        /// Whether to skip the syncing process or not.
        pub skip_sync: bool,
    }
);