Skip to main content

dxm_artifacts/
lib.rs

1//! A crate for installing and FXServer installations.
2
3use std::{error::Error, io::Write, path::Path};
4
5use cfx::ArtifactsPlatform;
6use github::get_version_commit_sha;
7use reqwest::blocking::Client;
8use tempfile::NamedTempFile;
9
10pub mod cfx;
11pub mod github;
12pub mod jg;
13
14/// Downloads and installs the given installation version to the given directory
15/// path.
16pub fn install<S, P>(
17    client: &Client,
18    platform: &ArtifactsPlatform,
19    version: S,
20    path: P,
21) -> Result<(), Box<dyn Error>>
22where
23    S: AsRef<str>,
24    P: AsRef<Path>,
25{
26    let path = path.as_ref();
27
28    fs_err::create_dir_all(path)?;
29
30    let mut file = NamedTempFile::with_suffix(platform.archive_name())?;
31    download(client, platform, version, file.as_file_mut())?;
32
33    log::trace!("extracting fxserver archive");
34    platform.decompress(file.reopen()?, path)?;
35
36    Ok(())
37}
38
39/// Downloads the given installation version archive, and writes it to the given
40/// writer.
41pub fn download<S, W>(
42    client: &Client,
43    platform: &ArtifactsPlatform,
44    version: S,
45    mut writer: W,
46) -> Result<(), Box<dyn Error>>
47where
48    S: AsRef<str>,
49    W: Write,
50{
51    let version = version.as_ref();
52
53    let commit_sha = get_version_commit_sha(client, version)?;
54    let url = platform.runtime_url(version, commit_sha);
55
56    log::trace!("download fxserver archive");
57    let bytes = client.get(url).send()?.error_for_status()?.bytes()?;
58    writer.write_all(&bytes)?;
59
60    Ok(())
61}