Skip to content

Commit

Permalink
Merge pull request #159 from sanders41/package-versions
Browse files Browse the repository at this point in the history
Refactor default version builder and bump pre-commit 3.5.0 -> 3.6.0
  • Loading branch information
sanders41 authored Dec 11, 2023
2 parents 74aaf68 + 81442d2 commit 43f3631
Show file tree
Hide file tree
Showing 2 changed files with 221 additions and 99 deletions.
122 changes: 117 additions & 5 deletions src/package_version.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,57 @@
use std::fmt;
use std::time::Duration;

use anyhow::Result;

#[derive(Debug)]
#[derive(Debug, PartialEq, Eq)]
pub enum PythonPackage {
Maturin,
MyPy,
PreCommit,
Pytest,
PytestCov,
Ruff,
Tomli,
}

impl fmt::Display for PythonPackage {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
PythonPackage::Maturin => write!(f, "maturin"),
PythonPackage::MyPy => write!(f, "mypy"),
PythonPackage::PreCommit => write!(f, "pre-commit"),
PythonPackage::Pytest => write!(f, "pytest"),
PythonPackage::PytestCov => write!(f, "pytest-cov"),
PythonPackage::Ruff => write!(f, "ruff"),
PythonPackage::Tomli => write!(f, "tomli"),
}
}
}

#[derive(Debug, PartialEq, Eq)]
pub enum PreCommitHook {
PreCommit,
MyPy,
Ruff,
}

impl fmt::Display for PreCommitHook {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
PreCommitHook::MyPy => write!(f, "mypy"),
PreCommitHook::PreCommit => write!(f, "pre-commit"),
PreCommitHook::Ruff => write!(f, "ruff"),
}
}
}

pub trait LatestVersion {
fn get_latest_version(&mut self) -> Result<()>;
}

#[derive(Debug)]
pub struct PreCommitHookVersion {
pub id: PreCommitHook,
pub hook: PreCommitHook,
pub repo: String,
pub rev: String,
}
Expand Down Expand Up @@ -46,29 +82,45 @@ impl LatestVersion for PreCommitHookVersion {
}
}

impl PreCommitHookVersion {
pub fn new(hook: PreCommitHook) -> Self {
let rev = default_pre_commit_rev(&hook);
let repo = pre_commit_repo(&hook);
PreCommitHookVersion { hook, repo, rev }
}
}

#[derive(Debug)]
pub struct PythonPackageVersion {
pub name: String,
pub package: PythonPackage,
pub version: String,
}

impl LatestVersion for PythonPackageVersion {
fn get_latest_version(&mut self) -> Result<()> {
let url = format!("https://pypi.org/pypi/{}/json", self.name);
let name = self.package.to_string();
let url = format!("https://pypi.org/pypi/{}/json", name);
let client = reqwest::blocking::Client::new();
let response = client
.get(url)
.timeout(Duration::new(5, 0))
.send()?
.text()?;
let info: serde_json::Value = serde_json::from_str(&response)?;
self.name = info["info"]["name"].to_string().replace('"', "");
self.version = info["info"]["version"].to_string().replace('"', "");

Ok(())
}
}

impl PythonPackageVersion {
pub fn new(package: PythonPackage, min_python_version: &str) -> Self {
let version = default_version(&package, min_python_version);

PythonPackageVersion { package, version }
}
}

#[derive(Debug)]
pub struct RustPackageVersion {
pub name: String,
Expand All @@ -95,3 +147,63 @@ impl LatestVersion for RustPackageVersion {
Ok(())
}
}

pub fn default_version(package: &PythonPackage, min_python_version: &str) -> String {
match package {
PythonPackage::Maturin => "1.4.0".to_string(),
PythonPackage::MyPy => "1.7.1".to_string(),

// TODO: This isn't a good resolver but it will work for now. Should be imporoved.
PythonPackage::PreCommit => {
let version_info: Vec<&str> = min_python_version.split('.').collect();
if let Ok(minor) = version_info[1].parse::<i32>() {
if minor < 10 {
"3.5.0".to_string()
} else {
"3.6.0".to_string()
}
} else {
"3.6.0".to_string()
}
}
PythonPackage::Pytest => "7.4.2".to_string(),
PythonPackage::PytestCov => "4.1.0".to_string(),
PythonPackage::Ruff => "0.1.7".to_string(),
PythonPackage::Tomli => "2.0.1".to_string(),
}
}

pub fn default_pre_commit_rev(hook: &PreCommitHook) -> String {
match hook {
PreCommitHook::MyPy => "v1.7.1".to_string(),
PreCommitHook::PreCommit => "v4.5.0".to_string(),
PreCommitHook::Ruff => "v0.1.7".to_string(),
}
}

pub fn pre_commit_repo(hook: &PreCommitHook) -> String {
match hook {
PreCommitHook::MyPy => "https://github.com/pre-commit/mirrors-mypy".to_string(),
PreCommitHook::PreCommit => "https://github.com/pre-commit/pre-commit-hooks".to_string(),
PreCommitHook::Ruff => "https://github.com/astral-sh/ruff-pre-commit".to_string(),
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_less_than_three_ten() {
let version = default_version(&PythonPackage::PreCommit, "3.9");

assert_eq!("3.5.0", version)
}

#[test]
fn test_three_ten() {
let version = default_version(&PythonPackage::PreCommit, "3.10");

assert_eq!("3.6.0", version)
}
}
Loading

0 comments on commit 43f3631

Please sign in to comment.