Skip to content

Commit

Permalink
Merge pull request #10 from auyer/feature/better-parametrization
Browse files Browse the repository at this point in the history
Main routine refactor + better parametrization
  • Loading branch information
auyer committed Apr 12, 2023
2 parents ed0eb19 + 23caaa3 commit d867054
Show file tree
Hide file tree
Showing 10 changed files with 400 additions and 373 deletions.
11 changes: 11 additions & 0 deletions .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,14 @@ jobs:
with:
token: ${{ secrets.GITHUB_TOKEN }}
args: --all-features

spell_check:
runs-on: ubuntu-latest
steps:
- name: Checkout Actions Repository
uses: actions/checkout@v2

- name: Writes changes in the local checkout
uses: crate-ci/typos@master
with:
write_changes: true
5 changes: 5 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
repos:
- repo: https://github.com/crate-ci/typos
rev: v1.14.5
hooks:
- id: typos
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Get the latest binary:
It is a single binary. You can just run it, or add it to your path so you can call it from anywhere.

Quick way to add it to your path:
or dowload the zip from the releases page
or download the zip from the releases page
```
cd Downloads
sudo unzip protonup-rs-linux-amd64.zip -d /usr/bin
Expand Down
10 changes: 6 additions & 4 deletions libprotonup/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ pub const DEFAULT_LUTRIS_INSTALL_DIR_FLATPAK: &str =
"~/.var/app/net.lutris.Lutris/data/lutris/runners/wine/";
pub const TEMP_DIR: &str = "/tmp/";

pub const GITHUB: &str = "https://api.github.com/repos";
pub const GITHUB_REPO: &str = "proton-ge-custom";
pub const LUTRIS_GITHUB_REPO: &str = "wine-ge-custom";
pub const GITHUB_ACCOUNT: &str = "GloriousEggroll";
pub const GITHUB_URL: &str = "https://api.github.com/repos";

pub const GEPROTON_GITHUB_REPO: &str = "proton-ge-custom";
pub const WINEGE_GITHUB_REPO: &str = "wine-ge-custom";
pub const GE_GITHUB_ACCOUNT: &str = "GloriousEggroll";

pub const USER_AGENT: &str = "protoup-rs";

// pub const CONFIG_FILE: &str = "~/.config/protonup/config.ini";
Expand Down
2 changes: 1 addition & 1 deletion libprotonup/src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ pub fn create_progress_trackers() -> (Arc<AtomicUsize>, Arc<AtomicBool>) {
)
}

pub fn check_if_exists(path: String, tag: String) -> bool {
pub fn check_if_exists(path: &str, tag: &str) -> bool {
let f_path = utils::expand_tilde(format!("{path}/{tag}")).unwrap();
let p = std::path::Path::new(&f_path);
p.is_dir()
Expand Down
160 changes: 106 additions & 54 deletions libprotonup/src/github.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::constants;
use crate::constants;
use crate::parameters::VariantParameters;
use anyhow::Result;
use serde::{Deserialize, Serialize};

Expand All @@ -24,18 +25,12 @@ pub struct Asset {
browser_download_url: String,
}

pub async fn list_releases(lutris: bool) -> Result<ReleaseList, reqwest::Error> {
pub async fn list_releases(source: &VariantParameters) -> Result<ReleaseList, reqwest::Error> {
let agent = format!("{}/v{}", constants::USER_AGENT, constants::VERSION,);

let url = format!(
"{}/{}/{}/releases",
constants::GITHUB,
constants::GITHUB_ACCOUNT,
if lutris {
constants::LUTRIS_GITHUB_REPO
} else {
constants::GITHUB_REPO
},
source.repository_url, source.repository_account, source.repository_name,
);

let client = reqwest::Client::builder().user_agent(agent).build()?;
Expand All @@ -54,7 +49,10 @@ pub struct Download {
pub created_at: String,
}

pub async fn fetch_data_from_tag(tag: &str, lutris: bool) -> Result<Download, reqwest::Error> {
pub async fn fetch_data_from_tag(
tag: &str,
source: &VariantParameters,
) -> Result<Download, reqwest::Error> {
let agent = format!("{}/v{}", constants::USER_AGENT, constants::VERSION,);

let client = reqwest::Client::builder().user_agent(agent).build()?;
Expand All @@ -64,28 +62,15 @@ pub async fn fetch_data_from_tag(tag: &str, lutris: bool) -> Result<Download, re
"latest" => {
let url = format!(
"{}/{}/{}/releases/latest",
constants::GITHUB,
constants::GITHUB_ACCOUNT,
if lutris {
constants::LUTRIS_GITHUB_REPO
} else {
constants::GITHUB_REPO
},
source.repository_url, source.repository_account, source.repository_name,
);
let rel: Release = client.get(url).send().await?.json().await?;
rel
}
_ => {
let url = format!(
"{}/{}/{}/releases/tags/{}",
constants::GITHUB,
constants::GITHUB_ACCOUNT,
if lutris {
constants::LUTRIS_GITHUB_REPO
} else {
constants::GITHUB_REPO
},
&tag
source.repository_url, source.repository_account, source.repository_name, &tag
);
let rel: Release = client.get(url).send().await?.json().await?;
rel
Expand All @@ -110,29 +95,88 @@ pub async fn fetch_data_from_tag(tag: &str, lutris: bool) -> Result<Download, re
}
Ok(download)
}

#[cfg(test)]
mod tests {
use crate::parameters;

use super::*;

#[tokio::test]
async fn test_data_fetch() {
let lutris = true;
let tag = "latest";
async fn test_fetch_data_from_tag() {
let conditions = &[
(
parameters::Variant::WineGE.parameters(),
"latest",
"Get Steam",
),
(
parameters::Variant::GEProton.parameters(),
"latest",
"Download Lutris",
),
];
for (source_parameters, tag, desc) in conditions {
let result = fetch_data_from_tag(tag, source_parameters).await;

assert!(
result.is_ok(),
"case :{} test: fetch_data_from_tag returned error",
desc
);

let result = match fetch_data_from_tag(tag, lutris).await {
Ok(data) => data,
Err(e) => {
eprintln!("Error: {}", e);
std::process::exit(1)
}
};
let result = result.unwrap();

assert!(
result.download.len() > 5,
"case : '{}' test: fetch_data_from_tag returned an wrong download link",
desc
);
assert!(
result.sha512sum.len() > 5,
"case : '{}' test: fetch_data_from_tag returned an wrong sha512sum",
desc
);
assert!(
result.size > 100,
"case : '{}' test: fetch_data_from_tag returned an wrong sha512sum",
desc
);
assert!(
result.version.len() > 2,
"case : '{}' test: fetch_data_from_tag returned an wrong version",
desc
);
}
}

println!("Got result: {:?}", result);
#[tokio::test]
async fn test_list_releases() {
let conditions = &[
(parameters::Variant::WineGE.parameters(), "List WineGE"),
(parameters::Variant::GEProton.parameters(), "List GEProton"),
];

for (source_parameters, desc) in conditions {
let result = list_releases(source_parameters).await;

assert!(
result.is_ok(),
"case : '{}' test: fetch_data_from_tag returned error",
desc
);

let result = result.unwrap();

assert!(
result.len() > 1,
"case : '{}' test: test_list_releases returned an empty list",
desc
);
}
}

#[tokio::test]
async fn test_releases() {
async fn test_get_release() {
let agent = format!("{}/v{}", constants::USER_AGENT, constants::VERSION,);

let client = match reqwest::Client::builder().user_agent(agent).build() {
Expand All @@ -143,24 +187,32 @@ mod tests {
}
};

let url = format!(
"{}/{}/{}/releases/latest",
constants::GITHUB,
constants::GITHUB_ACCOUNT,
constants::LUTRIS_GITHUB_REPO,
);
let conditions = &[
(parameters::Variant::WineGE.parameters(), "Get WineGE"),
(parameters::Variant::GEProton.parameters(), "Get GEProton"),
];
for (source_parameters, desc) in conditions {
let url = format!(
"{}/{}/{}/releases/latest",
source_parameters.repository_url,
source_parameters.repository_account,
source_parameters.repository_name
);

let rel: Release = match client.get(url).send().await {
Ok(res) => res,
Err(e) => {
eprintln!("Error: {}", e);
std::process::exit(1)
let rel = match client.get(url).send().await {
Ok(res) => res,
Err(e) => {
panic!("Error: {}", e);
}
}
}
.json()
.await
.unwrap();
.json::<Release>()
.await;

println!("Result: {:?}", rel);
assert!(
rel.is_ok(),
"case : '{}' test: test_get_release wrong",
desc
);
}
}
}
1 change: 1 addition & 0 deletions libprotonup/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod constants;
pub mod file;
pub mod github;
pub mod parameters;
pub mod utils;
90 changes: 90 additions & 0 deletions libprotonup/src/parameters.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
use super::constants::*;
use std::{fmt, str::FromStr};
// VariantParameters stores the parameters for a variant of Proton
pub struct VariantParameters {
/// this is a link back to the enum variant
variant_ref: Variant,
/// URL of the repository server (GitHub compatible URL only at the moment)
pub repository_url: String,
/// GitHub account for the variant
pub repository_account: String,
/// name of the repository
pub repository_name: String,
}

impl VariantParameters {
/// new_custom is a generator for custom VariantParameters
pub fn new_custom(
variant: Variant,
repository_url: String,
repository_account: String,
repository_name: String,
) -> VariantParameters {
VariantParameters {
variant_ref: variant,
repository_url,
repository_account,
repository_name,
}
}

///
pub fn variant_type(&self) -> &Variant {
return &self.variant_ref;
}
}

/// Variant is an enum with all supported "Proton" versions
pub enum Variant {
GEProton,
WineGE,
}

impl fmt::Display for Variant {
/// returns a string representation of this variant
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Variant::GEProton => write!(f, "GEProton"),
Variant::WineGE => write!(f, "WineGE"),
}
}
}

impl FromStr for Variant {
type Err = ();
fn from_str(input: &str) -> Result<Variant, Self::Err> {
match input {
"GEProton" => Ok(Variant::GEProton),
"WineGE" => Ok(Variant::WineGE),
_ => Err(()),
}
}
}

impl Variant {
/// returns the application target for the Variant. Steam and Lutris are the current options
pub fn intended_application(&self) -> &str {
match self {
Variant::GEProton => "Steam",
Variant::WineGE => "Lutris",
}
}

/// returns the default parameters for this Variant.
pub fn parameters(&self) -> VariantParameters {
match self {
Variant::GEProton => VariantParameters {
variant_ref: Variant::GEProton,
repository_url: GITHUB_URL.to_owned(),
repository_name: GEPROTON_GITHUB_REPO.to_owned(),
repository_account: GE_GITHUB_ACCOUNT.to_owned(),
},
Variant::WineGE => VariantParameters {
variant_ref: Variant::WineGE,
repository_url: GITHUB_URL.to_owned(),
repository_name: WINEGE_GITHUB_REPO.to_owned(),
repository_account: GE_GITHUB_ACCOUNT.to_owned(),
},
}
}
}
4 changes: 2 additions & 2 deletions protonup-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ description = "TUI Program for Custom Proton Download and installation written i
[dependencies]

# Use this to use the local changes in libprotonup.
# libprotonup = { path = "../libprotonup" }
libprotonup = { path = "../libprotonup" }
# This is necessary to publish to crates.io
libprotonup = { version = "0.4.2" }
# libprotonup = { version = "0.4.2" }
inquire = { version = "0.6", default-features = false, features = ["termion"] }
indicatif = { version = "0.17", features = [
"improved_unicode",
Expand Down

0 comments on commit d867054

Please sign in to comment.