Skip to content

Commit

Permalink
refactor: Move plugin repository models to the plugin driver
Browse files Browse the repository at this point in the history
  • Loading branch information
rholshausen committed Apr 3, 2023
1 parent bbeda70 commit 6ed7c4d
Show file tree
Hide file tree
Showing 11 changed files with 1,449 additions and 305 deletions.
349 changes: 189 additions & 160 deletions cli/Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ exclude = [
]

[dependencies]
pact-plugin-driver = "0.3.2"
pact-plugin-driver = { version = "0.4", path = "../drivers/rust/driver" }
clap = { version = "4.1.8", features = [ "derive", "cargo" ] }
comfy-table = "6.1.3"
home = "0.5.3"
Expand Down
129 changes: 6 additions & 123 deletions cli/src/repository.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
use std::collections::HashMap;
use std::fs;
use std::fs::File;
use std::io::{BufReader, Read, Write};
use std::path::PathBuf;

use anyhow::{anyhow, bail, Context};
use chrono::{DateTime, Local, Utc};
use chrono::{DateTime, Local};
use comfy_table::presets::UTF8_FULL;
use comfy_table::Table;
use itertools::Itertools;
use pact_plugin_driver::plugin_models::PactPluginManifest;
use semver::Version;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use sha2::{Digest, Sha256};
use tracing::{debug, info, trace, warn};

use pact_plugin_driver::plugin_models::PactPluginManifest;
use pact_plugin_driver::repository::{ManifestSource, PluginEntry, PluginRepositoryIndex};

use crate::{PluginVersionCommand, RepositoryCommands, resolve_plugin_dir};
use crate::install::{download_json_from_github, fetch_json_from_url, json_to_string};
use crate::repository::ManifestSource::GitHubRelease;
Expand All @@ -34,123 +33,6 @@ pub(crate) fn handle_command(repository_command: &RepositoryCommands) -> anyhow:
}
}

/// Struct representing the plugin repository index file
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct PluginRepositoryIndex {
/// Version of this index file
index_version: usize,

/// File format version of the index file
format_version: usize,

/// Timestamp (in UTC) that the file was created/updated
timestamp: DateTime<Utc>,

/// Plugin entries
pub entries: HashMap<String, PluginEntry>
}

/// Struct to store the plugin version entries
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct PluginEntry {
/// Name of the plugin
pub name: String,
/// Latest version
pub latest_version: String,
/// All the plugin versions
pub versions: Vec<PluginVersion>
}

/// Struct to store the plugin versions
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct PluginVersion {
/// Version of the plugin
pub version: String,
/// Source the manifest was loaded from
pub source: ManifestSource,
/// Manifest
pub manifest: Option<PactPluginManifest>
}

/// Struct to store the plugin versions
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(tag = "type", content = "value")]
pub enum ManifestSource {
/// Loaded from a file
File(String),

/// Loaded from a GitHub release
GitHubRelease(String)
}

impl ManifestSource {
pub fn name(&self) -> String {
match self {
ManifestSource::File(_) => "file".to_string(),
ManifestSource::GitHubRelease(_) => "GitHub release".to_string()
}
}

pub fn value(&self) -> String {
match self {
ManifestSource::File(v) => v.clone(),
ManifestSource::GitHubRelease(v) => v.clone()
}
}
}

impl PluginEntry {
fn new(manifest: &PactPluginManifest, source: &ManifestSource) -> PluginEntry {
PluginEntry {
name: manifest.name.clone(),
latest_version: manifest.version.clone(),
versions: vec![PluginVersion {
version: manifest.version.clone(),
source: source.clone(),
manifest: Some(manifest.clone())
}]
}
}

fn add_version(&mut self, manifest: &PactPluginManifest, source: &ManifestSource) {
if let Some(version) = self.versions.iter_mut()
.find(|m| m.version == manifest.version) {
version.source = source.clone();
version.manifest = Some(manifest.clone());
} else {
self.versions.push(PluginVersion {
version: manifest.version.clone(),
source: source.clone(),
manifest: Some(manifest.clone())
});
}
self.update_latest_version();
}

fn update_latest_version(&mut self) {
let latest_version = self.versions.iter()
.max_by(|m1, m2| {
let a = Version::parse(&m1.version).unwrap_or_else(|_| Version::new(0, 0, 0));
let b = Version::parse(&m2.version).unwrap_or_else(|_| Version::new(0, 0, 0));
a.cmp(&b)
})
.map(|m| m.version.clone())
.unwrap_or_default();
self.latest_version = latest_version.clone();
}
}

impl Default for PluginRepositoryIndex {
fn default() -> Self {
PluginRepositoryIndex {
index_version: 0,
format_version: 0,
timestamp: Utc::now(),
entries: Default::default()
}
}
}

/// Create a new repository file
fn new_repository(filename: &Option<String>, overwrite: bool) -> anyhow::Result<()> {
debug!(?filename, "Creating new repository file");
Expand Down Expand Up @@ -617,9 +499,10 @@ mod tests {
use std::io::Write;

use expectest::prelude::*;
use pact_plugin_driver::plugin_models::PactPluginManifest;
use tempfile::NamedTempFile;

use pact_plugin_driver::plugin_models::PactPluginManifest;

use crate::repository::{add_manifest_to_index, load_index_file, ManifestSource, new_repository, PluginRepositoryIndex};

#[test_log::test]
Expand Down
Loading

0 comments on commit 6ed7c4d

Please sign in to comment.