Skip to content

Commit

Permalink
neko installation support (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
AltronMaxX authored Jan 19, 2024
1 parent f648ad8 commit 5efbefc
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 6 deletions.
9 changes: 4 additions & 5 deletions src/cache_directory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl Cache {
*/
pub fn get_haxe_dir_name(&self, file_name: &str) -> Result<String> {
if cfg!(target_os = "windows") {
Self::get_extracted_dir_zip(self, file_name)
Self::get_extracted_dir_zip(self)
} else {
Self::get_extracted_dir_tar(self, file_name)
}
Expand Down Expand Up @@ -64,9 +64,8 @@ impl Cache {
Ok(name)
}

pub fn get_extracted_dir_zip(&self, file_name: &str) -> Result<String> {
fn get_extracted_dir_zip(&self) -> Result<String> {
// When unzipped, it doesn't need extra processing to get directory (like tar->gz does)
println!("{}/bin/", self.location);
let extracted_dir_path = format!("{}\\bin\\", self.location);
let mut extracted_dir = fs::read_dir(extracted_dir_path)?;

Expand Down Expand Up @@ -193,9 +192,9 @@ impl Cache {
*/
pub fn extract_archive(&self, file_name: &str, to: &str) -> Result<()> {
if cfg!(target_os = "windows") {
Self::extract_zip(&self, file_name, to)?;
Self::extract_zip(self, file_name, to)?;
} else {
Self::extract_tarball(&self, file_name, to)?;
Self::extract_tarball(self, file_name, to)?;
}

Ok(())
Expand Down
9 changes: 8 additions & 1 deletion src/install_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub async fn run_install(version: String) -> Result<()> {
let download = match version.as_str() {
"ceramic" => executor::block_on(packages::ceramic::download(&cache)),
"nightly" => executor::block_on(packages::haxe_nightly::download(&cache)),
"neko" => executor::block_on(packages::neko::download(&cache)),
_ => executor::block_on(packages::haxe_stable::download(&cache, &version)),
};

Expand All @@ -30,9 +31,15 @@ pub async fn run_install(version: String) -> Result<()> {
let _ = std::fs::create_dir(ceramic_dir);
cache.extract_zip(file_name.as_str(), "bin/ceramic").unwrap();
"ceramic".to_string()
} else if version.eq("neko") {
let neko_dir = Cache::get_path().unwrap() + "/bin/neko";
let _ = std::fs::remove_dir_all(&neko_dir);
let _ = std::fs::create_dir(neko_dir);
cache.extract_archive(file_name.as_str(), "bin/neko").unwrap();
packages::neko::get_neko_dir_name(&cache, file_name.as_str()).unwrap()
} else {
cache.extract_archive(file_name.as_str(), "bin").unwrap();
cache.get_haxe_dir_name(&file_name.as_str()).unwrap()
cache.get_haxe_dir_name(file_name.as_str()).unwrap()
};

cache.add_version(&version, location);
Expand Down
1 change: 1 addition & 0 deletions src/packages/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ pub mod common;
pub mod haxe_nightly;
pub mod haxe_stable;
pub mod haxeget;
pub mod neko;
138 changes: 138 additions & 0 deletions src/packages/neko.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
use std::fs;
use flate2::read::GzDecoder;
use tar::Archive;
use super::common;
use crate::cache_directory::Cache;
use color_eyre::eyre::{eyre, Result};

pub async fn download(cache: &Cache) -> Result<String> {
let client = reqwest::Client::new();

println!("Downloading latest Neko");

let file_name: String =
get_neko_archive().expect("Unable to infer the file name of the tar file");

// Now we can find the url that matches that file name
let binary_url = format!(
"https://build.haxe.org/builds/neko/{}/{file}",
get_sys_name().unwrap(),
file = file_name
);

let path = format!("{}/bin/{file_name}", cache.location);
common::download_file(&client, binary_url.as_str(), &path)
.await
.unwrap();

Ok(file_name)
}

pub fn link_neko(cache: &Cache) -> Result<()> {
// Check if not installed
let tar_version = cache
.find_version(&"neko".to_string())
.ok_or_else(|| eyre!("Neko is not installed. Try running `haxeget install neko`"))?;

common::link(cache, &tar_version, "neko", "neko")?;

if cfg!(target_os = "windows") {
println!("Note: You will need to run `setx /M NEKO_INSTPATH {}` and add `%NEKO_INSTPATH%` to your PATH vars to use Neko!", Cache::get_path().unwrap() + "\\neko");
} /*else if std::env::var("HAXE_STD_PATH").is_err() { I don't know if there are similar variables for non windows systems
println!("Note: You will need to add `export HAXE_STD_PATH={}/std/` to your shell config (i.e ~/.bashrc or ~/.zshrc)", Cache::get_path().unwrap());
}*/

Ok(())
}

pub fn get_neko_dir_name(cache: &Cache, file_name: &str) -> Result<String> {
if cfg!(target_os = "windows") {
get_extracted_dir_zip(cache)
} else {
get_extracted_dir_tar(cache, file_name)
}
}

fn get_extracted_dir_tar(cache: &Cache, file_name: &str) -> Result<String> {
let tarball = fs::File::open(format!("{}/bin/neko/{file_name}", cache.location))?;
let tar = GzDecoder::new(tarball);
let mut archive = Archive::new(tar);
let mut name = String::from("neko/");

// Get the name of the directory extracted
if let Some(file) = archive.entries().unwrap().next() {
let file = file.unwrap();
name.push_str(
file.header()
.path()
.unwrap()
.as_ref()
.to_str()
.expect("Unable to get extracted directory name"),
);
name.truncate(name.len() - 1);
};

Ok(name)
}

fn get_extracted_dir_zip(cache: &Cache) -> Result<String> {
let mut name = String::from("neko\\");

let extracted_dir_path = format!("{}\\bin\\neko", cache.location);

let mut extracted_dir = std::fs::read_dir(extracted_dir_path)?;

// Get the name of the already extracted directory
if let Some(dir) = extracted_dir.next() {
let dir = dir.unwrap();
name.push_str(
dir.path()
.file_name()
.unwrap()
.to_str()
.expect("Unable to get extracted directory name"),
);
};

Ok(name)
}

fn get_neko_archive() -> Result<String> {
let mut file_name = String::new();

file_name.push_str("neko_latest");
if (cfg!(target_os = "linux") && cfg!(target_arch = "x86_64")) || cfg!(target_os = "macos")
{
file_name.push_str(".tar.gz");
} else if cfg!(target_os = "windows") {
file_name.push_str(".zip");
} else {
return Err(eyre!(
"Your operating system and/or architecture is unsupported".to_owned()
));
}

Ok(file_name)
}

fn get_sys_name() -> Result<String> {
let mut sys = String::new();
if cfg!(target_os = "linux") && cfg!(target_arch = "x86_64") {
sys.push_str("linux64");
} else if cfg!(target_os = "macos") {
sys.push_str("mac");
} else if cfg!(target_os = "windows") {
if cfg!(target_arch = "x86_64") {
sys.push_str("windows64");
} else {
sys.push_str("windows");
}
} else {
return Err(eyre!(
"Your operating system and/or architecture is unsupported".to_owned()
));
}

Ok(sys)
}
1 change: 1 addition & 0 deletions src/use_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pub fn run_use(version: String) -> Result<()> {
let cache = Cache::new().expect("Cache was unable to be read");
match version.as_str() {
"ceramic" => crate::packages::ceramic::link_ceramic(&cache),
"neko" => crate::packages::neko::link_neko(&cache),
_ => crate::packages::common::link_haxe(&cache, version),
}
}

0 comments on commit 5efbefc

Please sign in to comment.