use clap;
use platform_lp::Platform;
use version_lp::Version;
use lpsettings;
use love;
use std::path::{PathBuf,Path};
use smart_hash::traits::SmartHashSet;
use failure::Error;
use core;
use repo;
use binary;
pub fn process(matches : &clap::ArgMatches) -> Result<(),Error> {
if let Some(install) = matches.subcommand_matches("install") {
return process_install(&install);
}
let package_path : Option<PathBuf> = if let Some(value) = lpsettings::get_value("project.game-folder")? {
if let Some(mut path) = get_path(&matches) {
path.push(value.to_string());
Some(path) } else {
let mut temp_path = PathBuf::from(".");
temp_path.push(value.to_string());
Some(temp_path) }
} else { get_path(&matches) };
let plat : Platform = get_platform(&matches);
let ver : Option<Version> = get_version(&matches,&package_path);
match ver {
None => Err(format_err!("No version found, don't know what to run.")),
Some(ref ver) => {
core::run(&plat,&ver,package_path)
}
}
}
pub fn app() -> clap::App<'static,'static> {
clap::App::new("lpsettings")
.version(env!("CARGO_PKG_VERSION"))
.author("snsvrno<snsvrno@tuta.io>")
.about("Runs projects with different versions of LÖVE.")
.name("lprun")
.subcommand(clap::SubCommand::with_name("install")
.about("Installs different versions of LÖVE")
.subcommand(clap::SubCommand::with_name("list")
.about("Lists installed binaries.")
.arg(clap::Arg::with_name("list available")
.short("a")
.long("list-available")
.help("Lists available binaries.")))
.subcommand(clap::SubCommand::with_name("update")
.about("Updates the local repository of LOVE releases."))
.arg(clap::Arg::with_name("version")
.help("Version of LÖVE to use, overrides PROJECT defined version.")
.value_name("VERSION")
.required(true)
.index(1))
.arg(clap::Arg::with_name("platform")
.short("p")
.long("platform")
.help("Override what platform to use, can only choose 32bit varients on 64 bit machines.")
.value_name("platform")))
.arg(clap::Arg::with_name("version")
.short("v")
.long("version")
.help("Version of LÖVE to use, overrides PROJECT defined version.")
.value_name("version"))
.arg(clap::Arg::with_name("platform")
.short("p")
.long("platform")
.help("Override what platform to use, can only choose 32bit varients on 64 bit machines.")
.value_name("platform"))
.arg(clap::Arg::with_name("PROJECT")
.help("Path to LÖVE project folder or .love file")
.value_name("PROJECT")
.index(1))
}
fn process_install(matches : &clap::ArgMatches) -> Result<(),Error> {
if let Some(list) = matches.subcommand_matches("list") {
match list.is_present("list available") {
true => repo::list_available()?,
false => repo::list()?,
}
} else if let Some(_) = matches.subcommand_matches("update") {
repo::update_local_repo(true)?;
} else {
let platform = get_platform(matches);
if let Some(version) = matches.value_of("version") {
match Version::from_str(version) {
Some(version) => {
binary::install(&platform, &version)?;
println!("LOVE {} for {} installed.",version,platform);
},
None => { error!("Cannot parse version '{}'",version); },
}
} else {
return Err(format_err!("Cannot install LOVE if a version is not supplied."));
}
}
Ok(())
}
fn get_platform(matches : &clap::ArgMatches) -> Platform {
match matches.value_of("platform") {
None => { Platform::get_user_platform() },
Some(platform_override) => { return Platform::new(&platform_override); }
}
}
fn get_path(matches : &clap::ArgMatches) -> Option<PathBuf> {
match matches.value_of("PROJECT") {
None => { return None; },
Some(project) => {
let path = Path::new(&project);
return Some(path.to_path_buf());
}
}
}
fn get_version(matches : &clap::ArgMatches, game_path: &Option<PathBuf>) -> Option<Version> {
if let Some(version_override) = matches.value_of("version") {
if let Some(version) = Version::from_str(&version_override) {
return Some(version);
}
}
if let Some(ref path) = game_path {
if let Ok(version_override_project) = love::project::get_required_version(path) {
return Some(version_override_project);
}
}
get_latest_installed_version()
}
fn get_latest_installed_version() -> Option<Version> {
match binary::get_installed() {
Err(_) => None,
Ok(list) => {
if let Some(mut versions) = get_matching!(list,platform == Platform::get_user_platform()) {
if versions.len() > 0 {
versions.sort();
versions.reverse();
Some(versions[0].version.clone())
} else {
None
}
} else {
None
}
}
}
}