feat: add version management flag

This commit is contained in:
daimond113 2024-09-29 00:37:38 +02:00
parent 821cc989ef
commit 2b6a731ea9
No known key found for this signature in database
GPG key ID: 3A8ECE51328B513C
7 changed files with 49 additions and 42 deletions

View file

@ -30,6 +30,7 @@ bin = [
] ]
wally-compat = ["zip", "serde_json"] wally-compat = ["zip", "serde_json"]
patches = ["git2"] patches = ["git2"]
version-management = ["bin"]
[[bin]] [[bin]]
name = "pesde" name = "pesde"

View file

@ -14,7 +14,9 @@ mod patch;
mod patch_commit; mod patch_commit;
mod publish; mod publish;
mod run; mod run;
#[cfg(feature = "version-management")]
mod self_install; mod self_install;
#[cfg(feature = "version-management")]
mod self_upgrade; mod self_upgrade;
mod update; mod update;
@ -41,6 +43,7 @@ pub enum Subcommand {
Publish(publish::PublishCommand), Publish(publish::PublishCommand),
/// Installs the pesde binary and scripts /// Installs the pesde binary and scripts
#[cfg(feature = "version-management")]
SelfInstall(self_install::SelfInstallCommand), SelfInstall(self_install::SelfInstallCommand),
/// Sets up a patching environment for a package /// Sets up a patching environment for a package
@ -52,6 +55,7 @@ pub enum Subcommand {
PatchCommit(patch_commit::PatchCommitCommand), PatchCommit(patch_commit::PatchCommitCommand),
/// Installs the latest version of pesde /// Installs the latest version of pesde
#[cfg(feature = "version-management")]
SelfUpgrade(self_upgrade::SelfUpgradeCommand), SelfUpgrade(self_upgrade::SelfUpgradeCommand),
/// Adds a dependency to the project /// Adds a dependency to the project
@ -82,11 +86,13 @@ impl Subcommand {
Subcommand::Run(run) => run.run(project), Subcommand::Run(run) => run.run(project),
Subcommand::Install(install) => install.run(project, multi, reqwest), Subcommand::Install(install) => install.run(project, multi, reqwest),
Subcommand::Publish(publish) => publish.run(project, reqwest), Subcommand::Publish(publish) => publish.run(project, reqwest),
#[cfg(feature = "version-management")]
Subcommand::SelfInstall(self_install) => self_install.run(), Subcommand::SelfInstall(self_install) => self_install.run(),
#[cfg(feature = "patches")] #[cfg(feature = "patches")]
Subcommand::Patch(patch) => patch.run(project, reqwest), Subcommand::Patch(patch) => patch.run(project, reqwest),
#[cfg(feature = "patches")] #[cfg(feature = "patches")]
Subcommand::PatchCommit(patch_commit) => patch_commit.run(project), Subcommand::PatchCommit(patch_commit) => patch_commit.run(project),
#[cfg(feature = "version-management")]
Subcommand::SelfUpgrade(self_upgrade) => self_upgrade.run(reqwest), Subcommand::SelfUpgrade(self_upgrade) => self_upgrade.run(reqwest),
Subcommand::Add(add) => add.run(project), Subcommand::Add(add) => add.run(project),
Subcommand::Update(update) => update.run(project, multi, reqwest), Subcommand::Update(update) => update.run(project, multi, reqwest),

View file

@ -24,6 +24,7 @@ pub mod commands;
pub mod config; pub mod config;
pub mod files; pub mod files;
pub mod repos; pub mod repos;
#[cfg(feature = "version-management")]
pub mod version; pub mod version;
pub const HOME_DIR: &str = concat!(".", env!("CARGO_PKG_NAME")); pub const HOME_DIR: &str = concat!(".", env!("CARGO_PKG_NAME"));
@ -119,6 +120,7 @@ impl<V: FromStr<Err = E>, E: Into<anyhow::Error>, N: FromStr<Err = F>, F: Into<a
} }
impl VersionedPackageName { impl VersionedPackageName {
#[cfg(feature = "patches")]
fn get(self, graph: &DownloadedGraph) -> anyhow::Result<(PackageNames, VersionId)> { fn get(self, graph: &DownloadedGraph) -> anyhow::Result<(PackageNames, VersionId)> {
let version_id = match self.1 { let version_id = match self.1 {
Some(version) => version, Some(version) => version,

View file

@ -157,10 +157,6 @@ pub fn get_or_download_version(
reqwest: &reqwest::blocking::Client, reqwest: &reqwest::blocking::Client,
version: &Version, version: &Version,
) -> anyhow::Result<Option<PathBuf>> { ) -> anyhow::Result<Option<PathBuf>> {
#[cfg(debug_assertions)]
// possible hard to debug issues with the versioning system overtaking the debug build
return Ok(None);
let path = home_dir()?.join("versions"); let path = home_dir()?.join("versions");
create_dir_all(&path).context("failed to create versions directory")?; create_dir_all(&path).context("failed to create versions directory")?;
@ -196,9 +192,6 @@ pub fn get_or_download_version(
} }
pub fn max_installed_version() -> anyhow::Result<Version> { pub fn max_installed_version() -> anyhow::Result<Version> {
#[cfg(debug_assertions)]
return Ok(current_version());
let versions_dir = home_dir()?.join("versions"); let versions_dir = home_dir()?.join("versions");
create_dir_all(&versions_dir).context("failed to create versions directory")?; create_dir_all(&versions_dir).context("failed to create versions directory")?;

View file

@ -193,8 +193,9 @@ impl Project {
let dir = dir.as_ref().to_path_buf(); let dir = dir.as_ref().to_path_buf();
let manifest = std::fs::read_to_string(dir.join(MANIFEST_FILE_NAME)) let manifest = std::fs::read_to_string(dir.join(MANIFEST_FILE_NAME))
.map_err(|e| errors::WorkspaceMembersError::ManifestMissing(dir.to_path_buf(), e))?; .map_err(|e| errors::WorkspaceMembersError::ManifestMissing(dir.to_path_buf(), e))?;
let manifest = toml::from_str::<Manifest>(&manifest) let manifest = toml::from_str::<Manifest>(&manifest).map_err(|e| {
.map_err(|e| errors::WorkspaceMembersError::ManifestDeser(dir.to_path_buf(), e))?; errors::WorkspaceMembersError::ManifestDeser(dir.to_path_buf(), Box::new(e))
})?;
let members = manifest let members = manifest
.workspace_members .workspace_members
@ -211,8 +212,9 @@ impl Project {
.map(|path| { .map(|path| {
let manifest = std::fs::read_to_string(path.join(MANIFEST_FILE_NAME)) let manifest = std::fs::read_to_string(path.join(MANIFEST_FILE_NAME))
.map_err(|e| errors::WorkspaceMembersError::ManifestMissing(path.clone(), e))?; .map_err(|e| errors::WorkspaceMembersError::ManifestMissing(path.clone(), e))?;
let manifest = toml::from_str::<Manifest>(&manifest) let manifest = toml::from_str::<Manifest>(&manifest).map_err(|e| {
.map_err(|e| errors::WorkspaceMembersError::ManifestDeser(path.clone(), e))?; errors::WorkspaceMembersError::ManifestDeser(path.clone(), Box::new(e))
})?;
Ok((path, manifest)) Ok((path, manifest))
}) })
.collect::<Result<_, _>>() .collect::<Result<_, _>>()
@ -273,7 +275,7 @@ pub mod errors {
/// An error occurred deserializing the manifest file /// An error occurred deserializing the manifest file
#[error("error deserializing manifest file at {0}")] #[error("error deserializing manifest file at {0}")]
ManifestDeser(PathBuf, #[source] toml::de::Error), ManifestDeser(PathBuf, #[source] Box<toml::de::Error>),
/// An error occurred interacting with the filesystem /// An error occurred interacting with the filesystem
#[error("error interacting with the filesystem")] #[error("error interacting with the filesystem")]

View file

@ -10,13 +10,12 @@ use std::{
path::{Path, PathBuf}, path::{Path, PathBuf},
}; };
#[cfg(feature = "version-management")]
use crate::cli::version::{
check_for_updates, current_version, get_or_download_version, max_installed_version,
};
use crate::cli::{ use crate::cli::{
auth::get_token, auth::get_token, config::read_config, home_dir, repos::update_repo_dependencies, HOME_DIR,
config::read_config,
home_dir,
repos::update_repo_dependencies,
version::{check_for_updates, current_version, get_or_download_version, max_installed_version},
HOME_DIR,
}; };
mod cli; mod cli;
@ -233,35 +232,39 @@ fn run() -> anyhow::Result<()> {
.build()? .build()?
}; };
let target_version = project #[cfg(feature = "version-management")]
.deser_manifest() {
.ok() let target_version = project
.and_then(|manifest| manifest.pesde_version); .deser_manifest()
.ok()
.and_then(|manifest| manifest.pesde_version);
// store the current version in case it needs to be used later // store the current version in case it needs to be used later
get_or_download_version(&reqwest, &current_version())?; get_or_download_version(&reqwest, &current_version())?;
let exe_path = if let Some(version) = target_version { let exe_path = if let Some(version) = target_version {
Some(get_or_download_version(&reqwest, &version)?) Some(get_or_download_version(&reqwest, &version)?)
} else { } else {
None None
}; };
let exe_path = if let Some(exe_path) = exe_path { let exe_path = if let Some(exe_path) = exe_path {
exe_path exe_path
} else { } else {
get_or_download_version(&reqwest, &max_installed_version()?)? get_or_download_version(&reqwest, &max_installed_version()?)?
}; };
if let Some(exe_path) = exe_path { if let Some(exe_path) = exe_path {
let status = std::process::Command::new(exe_path) let status = std::process::Command::new(exe_path)
.args(std::env::args_os().skip(1)) .args(std::env::args_os().skip(1))
.status() .status()
.expect("failed to run new version"); .expect("failed to run new version");
std::process::exit(status.code().unwrap()); std::process::exit(status.code().unwrap());
}
display_err(check_for_updates(&reqwest), " while checking for updates");
} }
display_err(check_for_updates(&reqwest), " while checking for updates");
display_err( display_err(
update_repo_dependencies(&project), update_repo_dependencies(&project),
" while updating repository dependencies", " while updating repository dependencies",

View file

@ -11,7 +11,7 @@ pub trait GitBasedSource {
fn repo_url(&self) -> &gix::Url; fn repo_url(&self) -> &gix::Url;
/// Gets the tree of the repository /// Gets the tree of the repository
fn tree<'a>(&'a self, repo: &'a gix::Repository) -> Result<gix::Tree, errors::TreeError> { fn tree<'a>(&'a self, repo: &'a gix::Repository) -> Result<gix::Tree<'a>, errors::TreeError> {
// this is a bare repo, so this is the actual path // this is a bare repo, so this is the actual path
let path = repo.path().to_path_buf(); let path = repo.path().to_path_buf();