Skip to content

Commit 47d5eee

Browse files
committed
make use of GEODE_SDK env var instead of setting it
1 parent 6a21df6 commit 47d5eee

File tree

4 files changed

+40
-68
lines changed

4 files changed

+40
-68
lines changed

src/info.rs

+5-37
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,6 @@ pub fn subcommand(config: &mut Config, cmd: Info) {
6464

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

8078
Info::Get { field, raw } => {
79+
let sdk_path;
80+
8181
let out = if field == "default-developer" {
8282
config.default_developer.as_deref().unwrap_or("")
8383
} else if field == "sdk-path" {
84-
config.sdk_path.as_ref().and_then(|x| Some(x.to_str().unwrap())).unwrap_or("")
84+
sdk_path = Config::sdk_path();
85+
sdk_path.to_str().unwrap_or("")
8586
} else if field == "sdk-nightly" {
8687
if config.sdk_nightly {
8788
"true"
@@ -158,40 +159,7 @@ pub fn subcommand(config: &mut Config, cmd: Info) {
158159
done!("Profile added");
159160
}
160161

161-
if config.sdk_path.is_none() {
162-
info!(
163-
"Please enter the path to the Geode repository folder \
164-
(https://github.com/geode-sdk/geode):"
165-
);
166-
config.sdk_path = Some(loop {
167-
let mut buf = String::new();
168-
match std::io::stdin().lock().read_line(&mut buf) {
169-
Ok(_) => {},
170-
Err(e) => {
171-
fail!("Unable to read input: {}", e);
172-
continue;
173-
}
174-
};
175-
176-
// Verify path is valid
177-
let path = PathBuf::from(buf.trim());
178-
if !path.is_dir() {
179-
fail!("The path must point to a folder");
180-
continue;
181-
}
182-
if !path.join("README.md").exists() {
183-
fail!(
184-
"Given path doesn't seem to be the Geode repo, \
185-
make sure to enter the path to the root (where \
186-
README.md is)"
187-
);
188-
continue;
189-
}
190-
break path;
191-
});
192-
config.sdk_nightly = config.sdk_path.as_ref().unwrap().join("bin/nightly").exists();
193-
done!("SDK path added");
194-
}
162+
config.sdk_nightly = Config::sdk_path().join("bin/nightly").exists();
195163

196164
done!("Config setup finished");
197165
},

src/sdk.rs

+13-26
Original file line numberDiff line numberDiff line change
@@ -76,17 +76,7 @@ fn parse_version(str: &str) -> Result<Version, semver::Error> {
7676
}
7777

7878
fn uninstall(config: &mut Config) -> bool {
79-
let sdk_path: &Path = if let Some(p) = &config.sdk_path {
80-
if !p.exists() {
81-
fail!("SDK path \"{}\" does not exist", p.display());
82-
return false;
83-
}
84-
85-
p
86-
} else {
87-
fail!("Unable to uninstall SDK as it is not installed");
88-
return false;
89-
};
79+
let sdk_path = Config::sdk_path();
9080

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

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

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

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

143-
config.sdk_path = Some(path);
132+
// TODO: set GEODE_SDK enviroment var
133+
134+
info!("Please set the GEODE_SDK enviroment variable to {}", path.to_str().unwrap());
144135

145136
switch_to_tag(config, &repo);
146137

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

168159
// Initialize repository
169160
let repo = Repository::open(
170-
config.sdk_path.as_ref().nice_unwrap("Unable to update SDK as it is not installed")
161+
Config::sdk_path()
171162
).nice_unwrap("Could not initialize local SDK repository");
172163

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

254245
fn install_binaries(config: &mut Config) {
255-
config.sdk_path.as_ref().nice_unwrap(
256-
"SDK not installed! Use `geode sdk install` to install \
257-
Geode or `geode config setup` to set up the CLI."
258-
);
259246
update(config, None);
260247
let release_tag: String;
261248
let target_dir: PathBuf;
262249
if config.sdk_nightly {
263250
info!("Installing nightly binaries");
264251
release_tag = "Nightly".into();
265-
target_dir = config.sdk_path.as_ref().unwrap().join("bin/nightly");
252+
target_dir = Config::sdk_path().join("bin/nightly");
266253
} else {
267-
let ver = get_version(config);
254+
let ver = get_version();
268255
info!("Installing binaries for {}", ver);
269256
release_tag = format!("v{}", ver);
270-
target_dir = config.sdk_path.as_ref().unwrap().join(format!("bin/{}", ver));
257+
target_dir = Config::sdk_path().join(format!("bin/{}", ver));
271258
}
272259
let url = format!("https://api.github.com/repos/geode-sdk/geode/releases/tags/{}", release_tag);
273260

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

320-
pub fn get_version(config: &mut Config) -> Version {
307+
pub fn get_version() -> Version {
321308
Version::parse(
322309
fs::read_to_string(
323-
config.sdk_path.as_ref().nice_unwrap("SDK not installed!").join("VERSION")
310+
Config::sdk_path().join("VERSION")
324311
).nice_unwrap("Unable to read SDK version, make sure you are using SDK v0.4.2 or later").as_str()
325312
).nice_unwrap("Invalid SDK version")
326313
}
@@ -346,7 +333,7 @@ pub fn subcommand(config: &mut Config, cmd: Sdk) {
346333
},
347334
Sdk::Uninstall => { uninstall(config); },
348335
Sdk::Update { branch } => update(config, branch),
349-
Sdk::Version => info!("Geode SDK version: {}", get_version(config)),
336+
Sdk::Version => info!("Geode SDK version: {}", get_version()),
350337
Sdk::InstallBinaries => install_binaries(config),
351338
}
352339
}

src/template.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ fn create_template(
5858

5959
// Default mod.json
6060
let mod_json = json!({
61-
"geode": get_version(config).to_string(),
61+
"geode": get_version().to_string(),
6262
"version": version,
6363
"id": id,
6464
"name": name,

src/util/config.rs

+21-4
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ pub struct Config {
2424
pub current_profile: Option<String>,
2525
pub profiles: Vec<RefCell<Profile>>,
2626
pub default_developer: Option<String>,
27-
pub sdk_path: Option<PathBuf>,
2827
pub sdk_nightly: bool,
2928
#[serde(flatten)]
3029
other: HashMap<String, Value>,
@@ -66,7 +65,6 @@ impl OldConfig {
6665
).map(|i| i.borrow().name.clone()),
6766
profiles,
6867
default_developer: self.default_developer.to_owned(),
69-
sdk_path: None,
7068
sdk_nightly: false,
7169
other: HashMap::new()
7270
}
@@ -108,6 +106,27 @@ impl Config {
108106
}
109107
}
110108

109+
pub fn sdk_path() -> PathBuf {
110+
let sdk_var = std::env::var("GEODE_SDK")
111+
.nice_unwrap("Unable to find Geode SDK. Please define the GEODE_SDK enviroment variable to point to the Geode SDK");
112+
113+
let path = PathBuf::from(sdk_var);
114+
if !path.is_dir() {
115+
fail!("The GEODE_SDK enviroment variable must point to the folder containing the Geode SDK");
116+
std::process::exit(1)
117+
}
118+
if !path.join("VERSION").exists() {
119+
fail!(
120+
"The GEODE_SDK enviroment variable doesn't seem to point to the Geode SDK (Could not find VERSION)
121+
Perhaps you are on a version older than v0.4.2?"
122+
);
123+
std::process::exit(1)
124+
}
125+
126+
path
127+
}
128+
129+
111130
pub fn new() -> Config {
112131
if !geode_root().exists() {
113132
warn!("It seems you don't have Geode installed. Some operations will not work");
@@ -117,7 +136,6 @@ impl Config {
117136
current_profile: None,
118137
profiles: Vec::new(),
119138
default_developer: None,
120-
sdk_path: None,
121139
sdk_nightly: false,
122140
other: HashMap::<String, Value>::new()
123141
};
@@ -132,7 +150,6 @@ impl Config {
132150
current_profile: None,
133151
profiles: Vec::new(),
134152
default_developer: None,
135-
sdk_path: None,
136153
sdk_nightly: false,
137154
other: HashMap::<String, Value>::new()
138155
}

0 commit comments

Comments
 (0)