Skip to content

Commit

Permalink
Completed Installer
Browse files Browse the repository at this point in the history
  • Loading branch information
suptejas committed Mar 31, 2021
1 parent 2221a5f commit cd9e666
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 38 deletions.
114 changes: 77 additions & 37 deletions shc-installer/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
use std::{env, fs::{self, create_dir}, io::{self, Read, Write, copy}, path::Path, process};
use std::{
env,
fs::{self, create_dir, File},
io::{self, copy, Read, Write},
path::Path,
process::{self, Command},
};

use colored::*;
use reqwest::Url;
use exitfailure::ExitFailure;
use indicatif::{ProgressBar, ProgressStyle};
use reqwest::Url;
use reqwest::{header, Client};

struct DownloadProgress<R> {
Expand All @@ -20,7 +26,7 @@ impl<R: Read> Read for DownloadProgress<R> {
}
}

fn download(url: &str, destination: &str) -> Result<(), ExitFailure> {
fn download(url: &str, destination: &str, file_name: &str) -> Result<(), ExitFailure> {
let url = Url::parse(url)?;
let client = Client::new();

Expand All @@ -37,16 +43,19 @@ fn download(url: &str, destination: &str) -> Result<(), ExitFailure> {
"Couldn't download URL: {}. Error: {:?}",
url,
resp.status(),
)).into());
))
.into());
}
};

let mut request = client.get(url.as_str());
let pb = ProgressBar::new(total_size- 150000);
let pb = ProgressBar::new(total_size - 150000);

pb.set_style(ProgressStyle::default_bar()
.template("[{elapsed_precise}] [{bar:40.cyan/blue}] {bytes}/{total_bytes} ({eta})")
.progress_chars("#>-"));
pb.set_style(
ProgressStyle::default_bar()
.template("[{elapsed_precise}] [{bar:40.cyan/blue}] {bytes} / {total_bytes} ({eta})")
.progress_chars("#>-"),
);

let file = Path::new(destination);

Expand All @@ -65,46 +74,77 @@ fn download(url: &str, destination: &str) -> Result<(), ExitFailure> {
.create(true)
.append(true)
.open(&file)?;

println!(
"Downloading from {}",
url
"Downloading {} from {}",
file_name.bright_purple(),
url.as_str().truecolor(255, 200, 156)
);

let _ = copy(&mut source, &mut dest)?;

Ok(())
}

fn main() {
match env::consts::OS {
"windows" => {
ansi_term::enable_ansi_support();
let home = env::var("USERPROFILE").unwrap();
let target = format!("{}{}", &home, r"\.shc\shc.exe");
let file_path: String = format!("{}{}" , &home, r"\.shc").to_string();
let parent_dir = Path::new(file_path.as_str());

if !parent_dir.exists() {
create_dir(file_path).unwrap();
}
match env::consts::OS {
"windows" => {
ansi_term::enable_ansi_support()
.expect("Something Went Wrong While Enabling Ansi Support");

let home = env::var("USERPROFILE").unwrap();
let target = format!("{}{}", &home, r"\.shc\shc.exe");

let file_path: String = format!("{}{}", &home, r"\.shc").to_string();
let parent_dir = Path::new(file_path.as_str());

if !Path::new(&target).exists() {
match download("https://xtreme-private-cdn.herokuapp.com/dl/shc", &target) {
Ok(_) => {
if !parent_dir.exists() {
create_dir(file_path).unwrap();
}

if !Path::new(&target).exists() {
match download(
"https://xtreme-cdn.herokuapp.com/dl/shc",
&target,
"shc.exe",
) {
Ok(_) => {
println!("{}", "Installing Shc".bright_cyan());
println!("{}", "Setting Environment Variables".bright_yellow());

let mut file =
File::create(format!(r"{}\temp.ps1", env::var("TEMP").unwrap()))
.unwrap();

file.write(b"[Environment]::SetEnvironmentVariable(\"Path\", $env:Path + \";C:\\Users\\xtrem\\.shc\", \"User\")").unwrap();
drop(file);

Command::new("powershell.exe")
.arg("-NoProfile")
.arg("-NonInteractive")
.arg("-File")
.arg(format!(r"{}\temp.ps1", env::var("TEMP").unwrap()))
.output()
.unwrap();

println!(
"{} {}",
"Successfully Installed".bright_green(),
"shc".bright_magenta()
);
}
Err(_) => {}
}
} else {
println!(
"{}", "Installing Shc".bright_cyan(),
);
},
Err(_) => {},
}
"{}",
format!("shc.exe was already found at {}", &target.bright_purple())
)
}
}


},
&_ => {
&_ => {
println!("{}", "OS Not Supported!".bright_yellow());
process::exit(1);
}
}
}

}
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ fn main() {
} else if args.len() == 2 {
println!(
"{}",
"Specify A Shortcut To Install\nUsage: shc add shorcut1,shortcut2".bright_yellow()
"Specify A Shortcut To Install\nUsage: shc add shorcut1,shortcut2"
.bright_yellow()
);
} else if args.len() == 4 {
let alias = &args[2];
Expand Down

0 comments on commit cd9e666

Please sign in to comment.