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
use crate::host::{HostArch, HostOS};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::PathBuf;
pub use semver::{Version, VersionReq};
/// Represents an empty input.
#[derive(Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
pub struct EmptyInput {}
/// Information about the host environment (the current runtime).
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
pub struct Environment {
/// Current architecture.
pub arch: HostArch,
/// Current operating system.
pub os: HostOS,
/// Requested environment variables. Only non-empty values are included.
pub vars: HashMap<String, String>,
/// Current resolved version. Will be empty if not resolved.
pub version: String,
}
/// Supported types of plugins.
#[derive(Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
pub enum PluginType {
#[default]
Language,
DependencyManager,
CLI,
}
/// Input passed to the `register_tool` function.
#[derive(Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
pub struct ToolMetadataInput {
/// ID of the tool, as it was configured.
pub id: String,
/// Current environment.
pub env: Environment,
}
/// Output returned by the `register_tool` function.
#[derive(Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
pub struct ToolMetadataOutput {
/// Environment variables that should be extracted
/// and passed to other function call inputs.
pub env_vars: Vec<String>,
/// Human readable name of the tool.
pub name: String,
/// Type of the tool.
pub type_of: PluginType,
}
// Detector
/// Output returned by the `detect_version_files` function.
#[derive(Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
pub struct DetectVersionOutput {
/// List of files that should be checked for version information.
pub files: Vec<String>,
}
/// Input passed to the `parse_version_file` function.
#[derive(Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
pub struct ParseVersionFileInput {
/// File contents to parse/extract a version from.
pub content: String,
/// Current environment.
pub env: Environment,
/// Name of file that's being parsed.
pub file: String,
}
/// Output returned by the `parse_version_file` function.
#[derive(Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
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
/// Input passed to the `download_prebuilt` function.
#[derive(Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
pub struct DownloadPrebuiltInput {
/// Current environment.
pub env: Environment,
}
/// Output returned by the `download_prebuilt` function.
#[derive(Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
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>,
/// Relative path from the installation directory to the binary.
/// If not provided, will use the tool `id` as the binary name.
#[serde(skip_serializing_if = "Option::is_none")]
pub bin_path: 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,
}
/// Input passed to the `unpack_archive` function.
#[derive(Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
pub struct UnpackArchiveInput {
/// Virtual path to the downloaded file.
pub input_file: PathBuf,
/// Current environment.
pub env: Environment,
/// Virtual directory to unpack the archive into, or copy the binary to.
pub output_dir: PathBuf,
}
/// Output returned by the `verify_checksum` function.
#[derive(Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
pub struct VerifyChecksumInput {
/// The SHA-256 hash of the downloaded file.
pub checksum: String,
/// Virtual path to the checksum file.
pub checksum_file: PathBuf,
/// Virtual path to the downloaded file.
pub download_file: PathBuf,
/// Current environment.
pub env: Environment,
}
/// Output returned by the `verify_checksum` function.
#[derive(Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
pub struct VerifyChecksumOutput {
pub verified: bool,
}
// Executor
/// Input passed to the `locate_bins` function.
#[derive(Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
pub struct LocateBinsInput {
/// Current environment.
pub env: Environment,
/// Virtual path to the tool's installation directory.
pub tool_dir: PathBuf,
}
/// Output returned by the `locate_bins` function.
#[derive(Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
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<String>,
/// 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>,
}
// Resolver
/// Input passed to the `load_versions` function.
#[derive(Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
pub struct LoadVersionsInput {
/// Current environment.
pub env: Environment,
}
/// Output returned by the `load_versions` function.
#[derive(Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
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 {
/// Create the output from a list of Git tags, as semantic versions.
/// The latest version will be the highest version number.
pub fn from_tags(tags: &[String]) -> anyhow::Result<Self> {
let mut output = LoadVersionsOutput::default();
let mut latest = Version::new(0, 0, 0);
for tag in tags {
let version = Version::parse(tag)?;
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)
}
}
/// Input passed to the `resolve_version` function.
#[derive(Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
pub struct ResolveVersionInput {
/// Current resolved version candidate. Will be used if no replacement version is provided.
// pub candidate: String,
/// The alias or version currently being resolved.
pub initial: String,
/// Current environment.
pub env: Environment,
}
/// Output returned by the `resolve_version` function.
#[derive(Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
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
/// Configuration for individual shim files.
#[derive(Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
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<String>,
/// 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<str>,
{
ShimConfig {
bin_path: Some(bin_path.as_ref().to_owned()),
..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<str>,
{
ShimConfig {
bin_path: Some(bin_path.as_ref().to_owned()),
..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<str>,
P: AsRef<str>,
{
ShimConfig {
bin_path: Some(bin_path.as_ref().to_owned()),
parent_bin: Some(parent.as_ref().to_owned()),
..ShimConfig::default()
}
}
}
/// Input passed to the `create_shims` function.
#[derive(Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
pub struct CreateShimsInput {
/// Current environment.
pub env: Environment,
}
/// Output returned by the `create_shims` function.
#[derive(Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
pub struct CreateShimsOutput {
/// 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>,
}