Skip to content

Commit

Permalink
buildable, add vers-plugin crate, fix names
Browse files Browse the repository at this point in the history
  • Loading branch information
reynn committed Sep 18, 2023
1 parent c3a8168 commit 1233817
Show file tree
Hide file tree
Showing 24 changed files with 259 additions and 78 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ Cargo.lock
/target

.DS_Store
.idea/
61 changes: 52 additions & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "vers"
version = "0.5.0"
version = "0.6.0"
edition = "2021"
description = "Install and manage dev tools with multiple environments"
repository = "https://github.com/reynn/vers"
Expand Down Expand Up @@ -29,7 +29,7 @@ clap_complete = "4.3"
directories-next = "2.0"
indicatif = "0.17"
is_executable = "1.0"
octocrab = "0.25"
octocrab = "0.28"
once_cell = "1.18"
regex = "1.8"
reqwest = "0.11"
Expand All @@ -47,8 +47,8 @@ walkdir = "2.3"
tar = "0.4"
zip = "0.6"
flate2 = "1.0"
vers-types = { path = "crates/types" }
vers-plugin = { path = "crates/plugins" }
vers-plugin = { path = "crates/vers-plugin" }
vers-types = { path = "crates/vers-types" }

[workspace]
members = ["crates/*"]
26 changes: 0 additions & 26 deletions crates/github/src/main.rs

This file was deleted.

7 changes: 0 additions & 7 deletions crates/types/src/plugin.rs

This file was deleted.

9 changes: 6 additions & 3 deletions crates/github/Cargo.toml → crates/vers-github/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
[package]
name = "github"
name = "vers-github"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
octocrab = "0.25"
vers-types = { path = "../types" }
octocrab = "0.28"
vers-plugin = { path = "../vers-plugin" }
vers-types = { path = "../vers-types" }
tracing = "0.1"
regex = "1.0"
139 changes: 139 additions & 0 deletions crates/vers-github/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
use octocrab::models::repos::{Asset, Release};
use regex::Regex;
use std::path::PathBuf;
use vers_types::*;
use vers_plugin::*;

type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;

fn main() {}

#[derive(Debug, Default)]
struct VersGithub;

impl VersPlugin for VersGithub {
fn load(&self) {
todo!()
}

fn list_versions(&self, name: Option<&str>) -> PluginResult<Vec<Version>> {
todo!()
}

fn get_version(&self, name: Option<&str>, version: &str) -> PluginResult<String> {
todo!()
}

fn get_version_assets(&self, name: Option<&str>, version: &Version) -> PluginResult<Vec<vers_types::Asset>> {
todo!()
}

fn get_download_path(&self, base_path: &PathBuf, name: Option<&str>, version: &Version) -> &PathBuf {
todo!()
}
}

pub async fn get_repo_releases(
owner: &'_ str,
repo: &'_ str,
pre_release: bool,
) -> Result<Vec<String>> {
Ok(octocrab::instance()
.repos(owner, repo)
.releases()
.list()
.per_page(100)
.send()
.await?
.items
.iter()
.filter_map(|release| match pre_release {
true => Some(release.tag_name.to_string()),
false => match release.prerelease {
true => None,
false => Some(release.tag_name.to_string()),
},
})
.collect())
}

pub async fn get_specific_release_for_repo(
owner: &'_ str,
repo: &'_ str,
version: &'_ Version,
) -> Result<Release> {
tracing::info!(
"Getting release({}) for {}/{}",
version.as_tag(),
owner,
repo
);
let octo = octocrab::instance();
if version == &Version::Latest {
match octo.repos(owner, repo).releases().get_latest().await {
Ok(latest_release) => Ok(latest_release),
Err(e) => Err(e.into()),
}
} else {
match octo
.repos(owner, repo)
.releases()
.get_by_tag(&version.as_tag())
.await
{
Ok(tagged_release) => Ok(tagged_release),
Err(_) => {
match octo
.repos(owner, repo)
.releases()
.get_by_tag(&format!("v{}", version.as_tag()))
.await
{
Ok(tagged_release) => Ok(tagged_release),
Err(e) => Err(e.into()),
}
}
}
}
}

pub async fn get_latest_release_tag(owner: &'_ str, repo: &'_ str) -> Option<Version> {
match octocrab::instance()
.repos(owner, repo)
.releases()
.get_latest()
.await
{
Ok(tag) => Some(parse_version(&tag.tag_name)),
Err(_) => None,
}
}

pub fn get_platform_specific_asset(
release: &'_ Release,
system: &'_ System,
user_pattern: &'_ str,
) -> Vec<Asset> {
release
.assets
.iter()
.filter_map(|asset| -> Option<Asset> {
if !user_pattern.is_empty() {
let r = Regex::new(user_pattern).unwrap_or_else(|_| {
panic!("{} is not a valid Regular Expression", user_pattern)
});
tracing::debug!("Matching '{}' against '{}'", r.as_str(), &asset.name);
if r.is_match(&asset.name) {
Some(asset.clone())
} else {
None
}
} else if system.is_match(&asset.name) {
tracing::debug!("Asset info: {:?}", asset.name);
Some(asset.clone())
} else {
None
}
})
.collect()
}
6 changes: 2 additions & 4 deletions crates/types/Cargo.toml → crates/vers-plugin/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
[package]
name = "types"
name = "vers-plugin"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
semver = "1.0"
test-case = "3.1"
regex = "1.9"
vers-types = { path = "../vers-types" }
22 changes: 22 additions & 0 deletions crates/vers-plugin/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use std::path::PathBuf;
use vers_types::{Asset, Version};

pub type PluginResult<T, E = Box<dyn std::error::Error>> = std::result::Result<T, E>;

pub trait VersPlugin: Default {
///
fn load(&self);
/// Retrieve a list of versions
fn list_versions(&self, name: Option<&str>) -> PluginResult<Vec<Version>>;
///
fn get_version(&self, name: Option<&str>, version: &str) -> PluginResult<String>;
///
fn get_version_assets(&self, name: Option<&str>, version: &Version) -> PluginResult<Vec<Asset>>;
///
fn get_download_path(&self, base_path: &PathBuf, name: Option<&str>, version: &Version) -> &PathBuf;

Check failure on line 16 in crates/vers-plugin/src/lib.rs

View workflow job for this annotation

GitHub Actions / Clippy [Linter] (ubuntu-latest)

writing `&PathBuf` instead of `&Path` involves a new object where a slice will do
}

#[macro_export]
macro_rules! register_plugin {
() => {};
}
11 changes: 11 additions & 0 deletions crates/vers-types/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "vers-types"
version = "0.1.0"
edition = "2021"

[dependencies]
regex = "1.9"
semver = "1.0"
test-case = "3.1"
thiserror = "1.0"
serde = "1.0"
4 changes: 4 additions & 0 deletions crates/vers-types/src/asset.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pub struct Asset {
pub name: String,
pub download_url: String,
}
Loading

0 comments on commit 1233817

Please sign in to comment.