Skip to content

Commit

Permalink
Initial Implementation (blocking oll3/bita#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
MCOfficer committed May 6, 2020
1 parent d45b422 commit ba3321a
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 35 deletions.
3 changes: 2 additions & 1 deletion src/github.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use std::time::Duration;
#[derive(Deserialize, Debug)]
pub struct Release {
id: i64,
pub(crate) tag_name: String,
pub tag_name: String,
assets_url: String,
}

Expand Down Expand Up @@ -49,6 +49,7 @@ pub struct PRHead {
#[serde(alias = "ref")]
pub branch: String,
pub repo: Repo,
pub sha: String,
}

#[derive(Deserialize, Debug)]
Expand Down
23 changes: 14 additions & 9 deletions src/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub fn install(

fs::create_dir_all(&destination)?;

let archive_file = match instance_source.r#type {
let (archive_file, version) = match instance_source.r#type {
InstanceSourceType::Continuous => download_continuous_asset(&destination, instance_type)?,
InstanceSourceType::PR => download_pr_asset(
&destination,
Expand All @@ -46,6 +46,7 @@ pub fn install(
destination,
executable_path,
name,
version,
instance_type,
instance_source,
))
Expand All @@ -54,21 +55,24 @@ pub fn install(
fn download_continuous_asset(
destination: &PathBuf,
instance_type: InstanceType,
) -> Result<PathBuf> {
) -> Result<(PathBuf, String)> {
let assets = github::get_continuous_release_assets()?;
let asset = choose_artifact(assets, instance_type)?;
github::download(
&asset.browser_download_url,
asset.name(),
&destination.clone(),
)
Ok((
github::download(
&asset.browser_download_url,
asset.name(),
&destination.clone(),
)?,
String::from("continuous"), //TODO
))
}

fn download_pr_asset(
destination: &PathBuf,
instance_type: InstanceType,
pr_id: u16,
) -> Result<PathBuf> {
) -> Result<(PathBuf, String)> {
let pr = github::get_pr(pr_id)?;
let workflow = github::get_cd_workflow()?;
let run = github::get_latest_workflow_run(workflow.id, pr.head.branch, pr.head.repo.id)?;
Expand All @@ -83,9 +87,10 @@ fn download_pr_asset(
)?;
archive::unpack(&archive_path, destination)?;
fs::remove_file(archive_path)?;

let mut result_path = destination.clone();
result_path.push(artifact.name());
Ok(result_path)
Ok((result_path, pr.head.sha))
}

fn choose_artifact<A: Artifact>(artifacts: Vec<A>, instance_type: InstanceType) -> Result<A> {
Expand Down
35 changes: 15 additions & 20 deletions src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ use chrono::{DateTime, Local};
use iced::{button, Align, Button, Column, Element, Length, Row, Space, Text};
use platform_dirs::{AppDirs, AppUI};
use serde::{Deserialize, Serialize};
use std::fs;
use std::fs::File;
use std::io::Write;
use std::path::PathBuf;
use std::process::Command;
use std::time::SystemTime;
use std::{fs, thread};

#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub enum InstanceType {
Expand Down Expand Up @@ -56,26 +56,11 @@ pub struct Instance {
pub path: PathBuf,
pub executable: PathBuf,
pub name: String,
pub version: String,
pub instance_type: InstanceType,
pub source: InstanceSource,
}

impl Default for Instance {
fn default() -> Self {
Instance {
play_button: button::State::default(),
update_button: button::State::default(),
folder_button: button::State::default(),
delete_button: button::State::default(),
path: Default::default(),
executable: Default::default(),
name: Default::default(),
instance_type: InstanceType::Unknown,
source: InstanceSource::default(),
}
}
}

#[derive(Debug, Clone, Copy)]
pub enum InstanceMessage {
Play,
Expand All @@ -89,6 +74,7 @@ impl Instance {
path: PathBuf,
executable: PathBuf,
name: String,
version: String,
instance_type: InstanceType,
source: InstanceSource,
) -> Self {
Expand All @@ -100,6 +86,7 @@ impl Instance {
path,
executable,
name,
version,
instance_type,
source,
}
Expand All @@ -116,7 +103,7 @@ impl Instance {
Message::Dummy,
),
InstanceMessage::Update => {
iced::Command::perform(perform_update(self.clone()), Message::Dummy)
iced::Command::perform(perform_update(self.clone()), Message::Updated)
}
InstanceMessage::Folder => {
iced::Command::perform(open_folder(self.path.clone()), Message::Dummy)
Expand Down Expand Up @@ -206,7 +193,8 @@ pub async fn delete(path: PathBuf) -> Option<PathBuf> {
}
}

pub async fn perform_update(instance: Instance) {
pub async fn perform_update(instance: Instance) -> Option<Instance> {
/*
// Yes, this is terrible. Sue me. Bitar's objects don't implement Send, and i cannot figure out
// how to use them in the default executor (which is multithreaded, presumably). Since we don't
// need any sort of feedback other than logs, we can just update in new, single-threaded runtime.
Expand All @@ -219,7 +207,14 @@ pub async fn perform_update(instance: Instance) {
}
Err(e) => error!("Failed to spawn tokio runtime: {}", e),
};
});
});*/
match update::update_instance(instance).await {
Ok(instance) => Some(instance),
Err(e) => {
error!("Failed to update instance: {:#}", e);
None
}
}
}

pub async fn perform_play(path: PathBuf, executable: PathBuf, name: String) {
Expand Down
10 changes: 10 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ pub enum Message {
InstanceMessage(usize, InstanceMessage),
Installed(Option<Instance>),
Deleted(Option<PathBuf>),
Updated(Option<Instance>),
Dummy(()),
}

Expand Down Expand Up @@ -94,6 +95,15 @@ impl Application for ESLauncher {
instance::perform_save_instances(self.instances_frame.instances.clone());
}
}
Message::Updated(option) => {
if let Some(instance) = option {
self.instances_frame
.instances
.retain(|i| !i.path.eq(&instance.path));
self.instances_frame.instances.push(instance);
instance::perform_save_instances(self.instances_frame.instances.clone());
}
}
Message::Dummy(_) => (),
}
Command::none()
Expand Down
11 changes: 6 additions & 5 deletions src/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::path::PathBuf;
use tokio::fs::OpenOptions;
use url::Url;

pub async fn update_instance(instance: Instance) -> Result<()> {
pub async fn update_instance(instance: Instance) -> Result<Instance> {
if let InstanceType::Unknown = instance.instance_type {
return Err(anyhow!("Cannot update InstanceType::Unknown",));
}
Expand All @@ -18,15 +18,15 @@ pub async fn update_instance(instance: Instance) -> Result<()> {
return Err(anyhow!("{} doesn't exist", archive_path.to_string_lossy()));
}

match instance.source.r#type {
let new_instance = match instance.source.r#type {
InstanceSourceType::PR => {
info!("Incremental update isn't supported for PRs, triggering reinstall");
install::install(
instance.path.clone(),
instance.name,
instance.instance_type,
instance.source,
)?;
)?
}
InstanceSourceType::Continuous => {
let url = format!(
Expand All @@ -37,11 +37,12 @@ pub async fn update_instance(instance: Instance) -> Result<()> {
if !archive_path.ends_with(InstanceType::AppImage.archive().unwrap()) {
archive::unpack(&archive_path, &instance.path)?;
}
instance // TODO
}
}
};

info!("Done!");
Ok(())
Ok(new_instance)
}

async fn bitar_update_archive(target_path: &PathBuf, url: String) -> Result<()> {
Expand Down

0 comments on commit ba3321a

Please sign in to comment.