Skip to content

Commit

Permalink
make use of GEODE_SDK env var instead of setting it
Browse files Browse the repository at this point in the history
  • Loading branch information
matcool committed Oct 14, 2022
1 parent 6a21df6 commit 47d5eee
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 68 deletions.
42 changes: 5 additions & 37 deletions src/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@ pub fn subcommand(config: &mut Config, cmd: Info) {

if field == "default-developer" {
config.default_developer = Some(value);
} else if field == "sdk-path" {
config.sdk_path = Some(PathBuf::from(value));
} else if field == "sdk-nightly" {
config.sdk_nightly = get_bool(&value)
.nice_unwrap(format!("'{}' cannot be parsed as a bool", value));
Expand All @@ -78,10 +76,13 @@ pub fn subcommand(config: &mut Config, cmd: Info) {
},

Info::Get { field, raw } => {
let sdk_path;

let out = if field == "default-developer" {
config.default_developer.as_deref().unwrap_or("")
} else if field == "sdk-path" {
config.sdk_path.as_ref().and_then(|x| Some(x.to_str().unwrap())).unwrap_or("")
sdk_path = Config::sdk_path();
sdk_path.to_str().unwrap_or("")
} else if field == "sdk-nightly" {
if config.sdk_nightly {
"true"
Expand Down Expand Up @@ -158,40 +159,7 @@ pub fn subcommand(config: &mut Config, cmd: Info) {
done!("Profile added");
}

if config.sdk_path.is_none() {
info!(
"Please enter the path to the Geode repository folder \
(https://github.com/geode-sdk/geode):"
);
config.sdk_path = Some(loop {
let mut buf = String::new();
match std::io::stdin().lock().read_line(&mut buf) {
Ok(_) => {},
Err(e) => {
fail!("Unable to read input: {}", e);
continue;
}
};

// Verify path is valid
let path = PathBuf::from(buf.trim());
if !path.is_dir() {
fail!("The path must point to a folder");
continue;
}
if !path.join("README.md").exists() {
fail!(
"Given path doesn't seem to be the Geode repo, \
make sure to enter the path to the root (where \
README.md is)"
);
continue;
}
break path;
});
config.sdk_nightly = config.sdk_path.as_ref().unwrap().join("bin/nightly").exists();
done!("SDK path added");
}
config.sdk_nightly = Config::sdk_path().join("bin/nightly").exists();

done!("Config setup finished");
},
Expand Down
39 changes: 13 additions & 26 deletions src/sdk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,7 @@ fn parse_version(str: &str) -> Result<Version, semver::Error> {
}

fn uninstall(config: &mut Config) -> bool {
let sdk_path: &Path = if let Some(p) = &config.sdk_path {
if !p.exists() {
fail!("SDK path \"{}\" does not exist", p.display());
return false;
}

p
} else {
fail!("Unable to uninstall SDK as it is not installed");
return false;
};
let sdk_path = Config::sdk_path();

warn!("Are you sure you want to uninstall SDK?");
print!(" (type 'Yes' to proceed) ");
Expand All @@ -106,7 +96,6 @@ fn uninstall(config: &mut Config) -> bool {
return false;
}

config.sdk_path = None;
done!("Uninstalled Geode SDK");
return true;
}
Expand All @@ -115,7 +104,7 @@ fn install(config: &mut Config, path: PathBuf) {

let parent = path.parent().unwrap();

if config.sdk_path.is_some() {
if std::env::var("GEODE_SDK").is_ok() {
fail!("SDK is already installed");
info!("Use --reinstall if you want to remove the existing installation");
} else if !parent.exists() {
Expand All @@ -140,7 +129,9 @@ fn install(config: &mut Config, path: PathBuf) {
let repo = builder.clone("https://github.com/geode-sdk/geode", &path)
.nice_unwrap("Could not download SDK");

config.sdk_path = Some(path);
// TODO: set GEODE_SDK enviroment var

info!("Please set the GEODE_SDK enviroment variable to {}", path.to_str().unwrap());

switch_to_tag(config, &repo);

Expand All @@ -167,7 +158,7 @@ fn update(config: &mut Config, branch: Option<Branch>) {

// Initialize repository
let repo = Repository::open(
config.sdk_path.as_ref().nice_unwrap("Unable to update SDK as it is not installed")
Config::sdk_path()
).nice_unwrap("Could not initialize local SDK repository");

// Fetch
Expand Down Expand Up @@ -200,7 +191,7 @@ fn update(config: &mut Config, branch: Option<Branch>) {
} else if !merge_analysis.is_fast_forward() {
fail!("Cannot update SDK, it has local changes");
info!("Go into the repository at {} and manually run `git pull`",
config.sdk_path.as_ref().unwrap().to_str().unwrap()
Config::sdk_path().to_str().unwrap()
);
} else {
// Change head and checkout
Expand Down Expand Up @@ -252,22 +243,18 @@ fn switch_to_tag(config: &mut Config, repo: &Repository) {
}

fn install_binaries(config: &mut Config) {
config.sdk_path.as_ref().nice_unwrap(
"SDK not installed! Use `geode sdk install` to install \
Geode or `geode config setup` to set up the CLI."
);
update(config, None);
let release_tag: String;
let target_dir: PathBuf;
if config.sdk_nightly {
info!("Installing nightly binaries");
release_tag = "Nightly".into();
target_dir = config.sdk_path.as_ref().unwrap().join("bin/nightly");
target_dir = Config::sdk_path().join("bin/nightly");
} else {
let ver = get_version(config);
let ver = get_version();
info!("Installing binaries for {}", ver);
release_tag = format!("v{}", ver);
target_dir = config.sdk_path.as_ref().unwrap().join(format!("bin/{}", ver));
target_dir = Config::sdk_path().join(format!("bin/{}", ver));
}
let url = format!("https://api.github.com/repos/geode-sdk/geode/releases/tags/{}", release_tag);

Expand Down Expand Up @@ -317,10 +304,10 @@ fn install_binaries(config: &mut Config) {
done!("Binaries installed");
}

pub fn get_version(config: &mut Config) -> Version {
pub fn get_version() -> Version {
Version::parse(
fs::read_to_string(
config.sdk_path.as_ref().nice_unwrap("SDK not installed!").join("VERSION")
Config::sdk_path().join("VERSION")
).nice_unwrap("Unable to read SDK version, make sure you are using SDK v0.4.2 or later").as_str()
).nice_unwrap("Invalid SDK version")
}
Expand All @@ -346,7 +333,7 @@ pub fn subcommand(config: &mut Config, cmd: Sdk) {
},
Sdk::Uninstall => { uninstall(config); },
Sdk::Update { branch } => update(config, branch),
Sdk::Version => info!("Geode SDK version: {}", get_version(config)),
Sdk::Version => info!("Geode SDK version: {}", get_version()),
Sdk::InstallBinaries => install_binaries(config),
}
}
2 changes: 1 addition & 1 deletion src/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ fn create_template(

// Default mod.json
let mod_json = json!({
"geode": get_version(config).to_string(),
"geode": get_version().to_string(),
"version": version,
"id": id,
"name": name,
Expand Down
25 changes: 21 additions & 4 deletions src/util/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ pub struct Config {
pub current_profile: Option<String>,
pub profiles: Vec<RefCell<Profile>>,
pub default_developer: Option<String>,
pub sdk_path: Option<PathBuf>,
pub sdk_nightly: bool,
#[serde(flatten)]
other: HashMap<String, Value>,
Expand Down Expand Up @@ -66,7 +65,6 @@ impl OldConfig {
).map(|i| i.borrow().name.clone()),
profiles,
default_developer: self.default_developer.to_owned(),
sdk_path: None,
sdk_nightly: false,
other: HashMap::new()
}
Expand Down Expand Up @@ -108,6 +106,27 @@ impl Config {
}
}

pub fn sdk_path() -> PathBuf {
let sdk_var = std::env::var("GEODE_SDK")
.nice_unwrap("Unable to find Geode SDK. Please define the GEODE_SDK enviroment variable to point to the Geode SDK");

let path = PathBuf::from(sdk_var);
if !path.is_dir() {
fail!("The GEODE_SDK enviroment variable must point to the folder containing the Geode SDK");
std::process::exit(1)
}
if !path.join("VERSION").exists() {
fail!(
"The GEODE_SDK enviroment variable doesn't seem to point to the Geode SDK (Could not find VERSION)
Perhaps you are on a version older than v0.4.2?"
);
std::process::exit(1)
}

path
}


pub fn new() -> Config {
if !geode_root().exists() {
warn!("It seems you don't have Geode installed. Some operations will not work");
Expand All @@ -117,7 +136,6 @@ impl Config {
current_profile: None,
profiles: Vec::new(),
default_developer: None,
sdk_path: None,
sdk_nightly: false,
other: HashMap::<String, Value>::new()
};
Expand All @@ -132,7 +150,6 @@ impl Config {
current_profile: None,
profiles: Vec::new(),
default_developer: None,
sdk_path: None,
sdk_nightly: false,
other: HashMap::<String, Value>::new()
}
Expand Down

0 comments on commit 47d5eee

Please sign in to comment.