Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update domain model to AVPRIndex package, add WIP AVPRClient support #90

Merged
merged 8 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/ARCValidationPackages/ARCValidationPackages.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<Compile Include="Results.fs" />
<Compile Include="Defaults.fs" />
<Compile Include="Domain.fs" />
<Compile Include="AVPRAPI.fs" />
<Compile Include="GitHubAPI.fs" />
<Compile Include="PackageCache.fs" />
<Compile Include="Config.fs" />
Expand All @@ -22,6 +23,8 @@
<ItemGroup>
<PackageReference Include="Fake.DotNet.Cli" Version="[6.0.0]" />
<PackageReference Include="FsHttp" Version="[11.0.0]" />
<PackageReference Include="AVPRClient" Version="[0.0.2]" />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reminder to bump these in the final PR

<PackageReference Include="AVPRIndex" Version="[0.0.2]" />
</ItemGroup>

</Project>
34 changes: 34 additions & 0 deletions src/ARCValidationPackages/AVPRAPI.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
namespace ARCValidationPackages

open AVPRClient
open ARCValidationPackages
open AVPRIndex
open System
open System.Net.Http

type AVPRAPI () =
member private this.BaseUri = Uri("https://avpr.nfdi4plants.org")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer these to be refactored into Globals namespace. I am also quite sure that the default base url is already set to exactly this url when you call the client constructor

member private this.HttpClienHandler = new HttpClientHandler (UseCookies = false)
member private this.HttpClient = new HttpClient(this.HttpClienHandler, true, BaseAddress=this.BaseUri)
member this.Client = AVPRClient.Client(this.HttpClient)
member this.GetAllPackages (): ValidationPackage [] =
this.Client.GetAllPackagesAsync(System.Threading.CancellationToken.None)
|> Async.AwaitTask
|> Async.RunSynchronously
|> Seq.toArray
member this.GetPackageByName (packageName: string): ValidationPackage =
this.Client.GetLatestPackageByNameAsync(packageName)
|> Async.AwaitTask
|> Async.RunSynchronously
member this.GetPackageByNameAndVersion (packageName: string) (version: string): ValidationPackage =
this.Client.GetPackageByNameAndVersionAsync(packageName, version)
|> Async.AwaitTask
|> Async.RunSynchronously

//member this.downloadPackageScript (packageIndex: ValidationPackageIndex) =

// let validationPackage =
// this.GetPackageByNameAndVersion
// packageIndex.Metadata.Name
// (ValidationPackageMetadata.getSemanticVersionString packageIndex.Metadata)
// Text.Encoding.UTF8.GetString validationPackage.PackageContent
3 changes: 2 additions & 1 deletion src/ARCValidationPackages/Config.fs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace ARCValidationPackages
open System.IO
open System.Text.Json
open AVPRIndex.Domain

type Config = {
PackageIndex: ValidationPackageIndex []
Expand All @@ -25,7 +26,7 @@ type Config = {
Config.create(
packageIndex = GitHubAPI.getPackageIndex(?Token = Token),
indexLastUpdated = System.DateTimeOffset.Now,
packageCacheFolder = defaultArg CacheFolder (Defaults.PACKAGE_CACHE_FOLDER()),
packageCacheFolder = defaultArg CacheFolder (Defaults.PACKAGE_CACHE_FOLDER_PREVIEW()),
configFilePath = defaultArg ConfigPath (Defaults.CONFIG_FILE_PATH())
)
static member indexContainsPackages (packageName: string) (config: Config) =
Expand Down
23 changes: 18 additions & 5 deletions src/ARCValidationPackages/Defaults.fs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ module Defaults =

let [<Literal>] CONFIG_FILE_NAME = "validation-packages-config.json"

let [<Literal>] PACKAGE_CACHE_FOLDER_NAME = "package-cache"
let [<Literal>] PACKAGE_CACHE_FOLDER_NAME_PREVIEW = "package-cache-preview"

let [<Literal>] PACKAGE_CACHE_FOLDER_NAME_RELEASE = "package-cache-release"

let [<Literal>] PACKAGE_CACHE_FILE_NAME = "validation-packages-cache.json"

Expand All @@ -44,15 +46,26 @@ module Defaults =
Path.Combine(CONFIG_FOLDER(), CONFIG_FILE_NAME)
.Replace("\\", "/")

let PACKAGE_CACHE_FOLDER () =
let PACKAGE_CACHE_FOLDER_PREVIEW () =
let path =
Path.Combine(CONFIG_FOLDER(), PACKAGE_CACHE_FOLDER_NAME_PREVIEW)
.Replace("\\", "/")
Directory.CreateDirectory(path) |> ignore
path

let PACKAGE_CACHE_FOLDER_RELEASE () =
let path =
Path.Combine(CONFIG_FOLDER(), PACKAGE_CACHE_FOLDER_NAME)
Path.Combine(CONFIG_FOLDER(), PACKAGE_CACHE_FOLDER_NAME_RELEASE)
.Replace("\\", "/")
Directory.CreateDirectory(path) |> ignore
path

let PACKAGE_CACHE_FILE_PATH () =
Path.Combine(PACKAGE_CACHE_FOLDER(), PACKAGE_CACHE_FILE_NAME)
let PACKAGE_CACHE_FILE_PATH_PREVIEW () =
Path.Combine(PACKAGE_CACHE_FOLDER_PREVIEW(), PACKAGE_CACHE_FILE_NAME)
.Replace("\\", "/")

let PACKAGE_CACHE_FILE_PATH_RELEASE () =
Path.Combine(PACKAGE_CACHE_FOLDER_RELEASE(), PACKAGE_CACHE_FILE_NAME)
.Replace("\\", "/")

let SERIALIZATION_OPTIONS = JsonSerializerOptions(WriteIndented = true)
Expand Down
175 changes: 13 additions & 162 deletions src/ARCValidationPackages/Domain.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3,168 +3,19 @@
open System.IO
open System.Text.Json
open System.Text.Json.Serialization
open AVPRIndex.Domain
open System.Runtime.CompilerServices


// must be classes to be deserializable with YamlDotNet

/// <summary>
/// Represents the author of a validation package
/// </summary>
type Author() =
// mandatory fields
member val FullName = "" with get,set
member val Email = "" with get,set
// optional fields
member val Affiliation = "" with get,set
member val AffiliationLink = "" with get,set

override this.GetHashCode() = hash (this.FullName, this.Email, this.Affiliation, this.AffiliationLink)

override this.Equals(other) =
match other with
| :? Author as a ->
(this.FullName, this.Email, this.Affiliation, this.AffiliationLink) = (a.FullName, a.Email, a.Affiliation, a.AffiliationLink)
| _ -> false

static member create(
fullName: string,
email: string,
?Affiliation: string,
?AffiliationLink: string
) =
let tmp = Author()
tmp.FullName <- fullName
tmp.Email <- email
Affiliation |> Option.iter (fun x -> tmp.Affiliation <- x)
AffiliationLink |> Option.iter (fun x -> tmp.AffiliationLink <- x)
tmp


/// <summary>
/// Represents the metadata of a validation package, e.g. version, name and description.
/// </summary>
type ValidationPackageMetadata() =
// mandatory fields
member val Name = "" with get,set
member val Description = "" with get,set
member val MajorVersion = 0 with get,set
member val MinorVersion = 0 with get,set
member val PatchVersion = 0 with get,set
// optional fields
member val Publish = false with get,set
member val Authors: Author [] = Array.empty<Author> with get,set
member val Tags: string [] = Array.empty<string> with get,set
member val ReleaseNotes = "" with get,set

override this.GetHashCode() =
hash (
this.Name,
this.Description,
this.MajorVersion,
this.MinorVersion,
this.PatchVersion,
this.Publish,
this.Authors,
this.Tags,
this.ReleaseNotes
)

override this.Equals(other) =
match other with
| :? ValidationPackageMetadata as vpm ->
(
this.Name,
this.Description,
this.MajorVersion,
this.MinorVersion,
this.PatchVersion,
this.Publish,
this.Authors,
this.Tags,
this.ReleaseNotes
) = (
vpm.Name,
vpm.Description,
vpm.MajorVersion,
vpm.MinorVersion,
vpm.PatchVersion,
vpm.Publish,
vpm.Authors,
vpm.Tags,
vpm.ReleaseNotes
)
| _ -> false

static member create (
name: string,
description: string,
majorVersion: int,
minorVersion: int,
patchVersion: int,
?Publish: bool,
?Authors: Author [],
?Tags: string [],
?ReleaseNotes
) =
let tmp = ValidationPackageMetadata()
tmp.Name <- name
tmp.Description <- description
tmp.MajorVersion <- majorVersion
tmp.MinorVersion <- minorVersion
tmp.PatchVersion <- patchVersion
Publish |> Option.iter (fun x -> tmp.Publish <- x)
Authors |> Option.iter (fun x -> tmp.Authors <- x)
Tags |> Option.iter (fun x -> tmp.Tags <- x)
ReleaseNotes |> Option.iter (fun x -> tmp.ReleaseNotes <- x)

tmp

static member getSemanticVersionString(m: ValidationPackageMetadata) = $"{m.MajorVersion}.{m.MinorVersion}.{m.PatchVersion}";

/// <summary>
/// represents a remotely available version of a validation package, e.g. the path to the file on GitHub and the date it was last updated.
/// </summary>
type ValidationPackageIndex =
{
RepoPath: string
FileName:string
LastUpdated: System.DateTimeOffset
Metadata: ValidationPackageMetadata
} with
static member create (
repoPath: string,
fileName: string,
lastUpdated: System.DateTimeOffset,
metadata: ValidationPackageMetadata

) =
{
RepoPath = repoPath
FileName = fileName
LastUpdated = lastUpdated
Metadata = metadata
}
static member create (
repoPath: string,
lastUpdated: System.DateTimeOffset,
metadata: ValidationPackageMetadata
) =
ValidationPackageIndex.create(
repoPath = repoPath,
fileName = Path.GetFileNameWithoutExtension(repoPath),
lastUpdated = lastUpdated,
metadata = metadata
)

static member getSemanticVersionString(i: ValidationPackageIndex) = $"{i.Metadata.MajorVersion}.{i.Metadata.MinorVersion}.{i.Metadata.PatchVersion}";

member this.PrettyPrint() =
$" {this.Metadata.Name} @ version {this.Metadata.MajorVersion}.{this.Metadata.MinorVersion}.{this.Metadata.PatchVersion}{System.Environment.NewLine}{_.Metadata.Description}{System.Environment.NewLine}Last Updated: {this.LastUpdated}{System.Environment.NewLine}"
module ValidationPackageMetadata =
let getSemanticVersionString(m: ValidationPackageMetadata) = $"{m.MajorVersion}.{m.MinorVersion}.{m.PatchVersion}"

module ValidationPackageIndex =
let getSemanticVersionString(i: ValidationPackageIndex) = $"{i.Metadata.MajorVersion}.{i.Metadata.MinorVersion}.{i.Metadata.PatchVersion}"
/// <summary>
/// represents the locally installed version of a validation package, e.g. the path to the local file and the date it was cached.
/// </summary>
type ARCValidationPackage =
type CachedValidationPackage =
{
FileName: string
CacheDate: System.DateTimeOffset
Expand All @@ -190,8 +41,8 @@ type ARCValidationPackage =
/// <param name="packageIndex">The input package index entry</param>
/// <param name="Date">Optional. The date to set the CacheDate to. Defaults to the current date.</param>
static member ofPackageIndex (packageIndex: ValidationPackageIndex, ?Date: System.DateTimeOffset, ?CacheFolder: string) =
let path = defaultArg CacheFolder (Defaults.PACKAGE_CACHE_FOLDER())
ARCValidationPackage.create(
let path = defaultArg CacheFolder (Defaults.PACKAGE_CACHE_FOLDER_PREVIEW())
CachedValidationPackage.create(
fileName = packageIndex.FileName,
cacheDate = (defaultArg Date System.DateTimeOffset.Now),
localPath = (System.IO.Path.Combine(path, packageIndex.FileName).Replace("\\","/")),
Expand All @@ -205,8 +56,8 @@ type ARCValidationPackage =
/// <param name="Date"></param>
/// <param name="Path"></param>
static member ofPackageName (packageName: string, ?Date: System.DateTimeOffset, ?Path: string) =
let path = defaultArg Path (Defaults.PACKAGE_CACHE_FOLDER())
ARCValidationPackage.create(
let path = defaultArg Path (Defaults.PACKAGE_CACHE_FOLDER_PREVIEW())
CachedValidationPackage.create(
fileName = packageName,
cacheDate = (defaultArg Date System.DateTimeOffset.Now),
localPath = (System.IO.Path.Combine(path, $"{packageName}.fsx").Replace("\\","/")),
Expand All @@ -218,10 +69,10 @@ type ARCValidationPackage =
/// </summary>
/// <param name="date">The date to set the CacheDate to</param>
/// <param name="package">The input package</param>
static member updateCacheDate (date: System.DateTimeOffset) (package: ARCValidationPackage) =
static member updateCacheDate (date: System.DateTimeOffset) (package: CachedValidationPackage) =
{package with CacheDate = date}

static member getSemanticVersionString(vp: ARCValidationPackage) = $"{vp.Metadata.MajorVersion}.{vp.Metadata.MinorVersion}.{vp.Metadata.PatchVersion}";
static member getSemanticVersionString(vp: CachedValidationPackage) = $"{vp.Metadata.MajorVersion}.{vp.Metadata.MinorVersion}.{vp.Metadata.PatchVersion}";

member this.PrettyPrint() =
$" {this.Metadata.Name} @ version {this.Metadata.MajorVersion}.{this.Metadata.MinorVersion}.{this.Metadata.PatchVersion}{System.Environment.NewLine}{this.Metadata.Description}{System.Environment.NewLine}CacheDate: {this.CacheDate}{System.Environment.NewLine}Installed at: {this.LocalPath}{System.Environment.NewLine}"
1 change: 1 addition & 0 deletions src/ARCValidationPackages/GitHubAPI.fs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ open System
open System.IO
open System.Text
open System.Text.Json
open AVPRIndex.Domain

module GitHubAPI =

Expand Down
Loading
Loading