#![doc = include_str!("../README.md")]
#![doc(
html_logo_url = "https://raw.githubusercontent.com/alloy-rs/core/main/assets/alloy.jpg",
html_favicon_url = "https://raw.githubusercontent.com/alloy-rs/core/main/assets/favicon.ico"
)]
#![warn(rustdoc::all)]
#![cfg_attr(
not(any(test, feature = "cli", feature = "solc")),
warn(unused_crate_dependencies)
)]
#![deny(unused_must_use, rust_2018_idioms)]
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
use semver::Version;
use std::fs;
mod error;
pub use error::SvmError;
mod install;
#[cfg(feature = "blocking")]
pub use install::blocking_install;
pub use install::install;
mod paths;
pub use paths::{data_dir, global_version_path, setup_data_dir, version_binary, version_path};
mod platform;
pub use platform::{platform, Platform};
mod releases;
pub use releases::{all_releases, BuildInfo, Releases};
#[cfg(feature = "blocking")]
pub use releases::blocking_all_releases;
#[cfg(feature = "cli")]
#[doc(hidden)]
pub const VERSION_MESSAGE: &str = concat!(
env!("CARGO_PKG_VERSION"),
" (",
env!("VERGEN_GIT_SHA"),
" ",
env!("VERGEN_BUILD_DATE"),
")"
);
pub fn get_global_version() -> Result<Option<Version>, SvmError> {
let v = fs::read_to_string(global_version_path())?;
Ok(Version::parse(v.trim_end_matches('\n')).ok())
}
pub fn set_global_version(version: &Version) -> Result<(), SvmError> {
fs::write(global_version_path(), version.to_string()).map_err(Into::into)
}
pub fn unset_global_version() -> Result<(), SvmError> {
fs::write(global_version_path(), "").map_err(Into::into)
}
pub fn installed_versions() -> Result<Vec<Version>, SvmError> {
let mut versions = vec![];
for v in fs::read_dir(data_dir())? {
let v = v?;
let path = v.path();
let Some(file_name) = path.file_name() else {
continue;
};
let Some(file_name) = file_name.to_str() else {
continue;
};
if file_name == ".global-version" {
continue;
}
versions.push(Version::parse(file_name)?);
}
versions.sort();
Ok(versions)
}
#[cfg(feature = "blocking")]
pub fn blocking_all_versions() -> Result<Vec<Version>, SvmError> {
Ok(releases::blocking_all_releases(platform::platform())?.into_versions())
}
pub async fn all_versions() -> Result<Vec<Version>, SvmError> {
Ok(releases::all_releases(platform::platform())
.await?
.into_versions())
}
pub fn remove_version(version: &Version) -> Result<(), SvmError> {
fs::remove_dir_all(version_path(version.to_string().as_str())).map_err(Into::into)
}
fn setup_version(version: &str) -> Result<(), SvmError> {
let v = version_path(version);
if !v.exists() {
fs::create_dir_all(v)?;
}
Ok(())
}