Skip to content

Commit

Permalink
Config rework, only use when needed
Browse files Browse the repository at this point in the history
no longer shared around a global config variable, instead is only used
when needed, along with `Config::assert_is_setup`. this stops unrelated
commands from complaining about missing profiles

also removes old config migration, since that was supposed to be removed
in v3.0
  • Loading branch information
matcool committed Dec 8, 2024
1 parent e7c0018 commit 7e58ee6
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 132 deletions.
5 changes: 4 additions & 1 deletion src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,9 @@ pub fn get_mod_versions(
Ok(body.payload)
}

pub fn subcommand(config: &mut Config, cmd: Index) {
pub fn subcommand(cmd: Index) {
let mut _config = Config::new().assert_is_setup();
let config = &mut _config;
match cmd {
Index::Install { id, version } => {
install_mod(config, &id, &version.unwrap_or(VersionReq::STAR), false);
Expand All @@ -386,4 +388,5 @@ pub fn subcommand(config: &mut Config, cmd: Index) {
Index::Profile => index_dev::edit_profile(config),
Index::Admin { commands } => index_admin::subcommand(commands, config),
}
config.save();
}
15 changes: 12 additions & 3 deletions src/info.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::config::Config;
use crate::config::{self, Config};
use crate::logging::ask_value;
use crate::util::config::Profile;
use crate::{done, fail, info, NiceUnwrap};
use crate::{done, fail, info, warn, NiceUnwrap};
use clap::Subcommand;
use colored::Colorize;
use std::cell::RefCell;
Expand Down Expand Up @@ -53,9 +54,11 @@ fn get_bool(value: &str) -> Option<bool> {
}
}

pub fn subcommand(config: &mut Config, cmd: Info) {
pub fn subcommand(cmd: Info) {
match cmd {
Info::Set { field, value } => {
let mut config = Config::new().assert_is_setup();

let done_str = format!("Set {} to {}", field, &value);

if field == "default-developer" {
Expand All @@ -72,9 +75,12 @@ pub fn subcommand(config: &mut Config, cmd: Info) {
}

done!("{}", done_str);
config.save();
}

Info::Get { field, raw } => {
let config = Config::new().assert_is_setup();

let sdk_path;

let out = if field == "default-developer" {
Expand Down Expand Up @@ -109,6 +115,8 @@ pub fn subcommand(config: &mut Config, cmd: Info) {
}

Info::Setup {} => {
let mut config = Config::new();

if config.profiles.is_empty() {
let default = config::profile_platform_default();
let platform = ask_value(
Expand Down Expand Up @@ -185,6 +193,7 @@ pub fn subcommand(config: &mut Config, cmd: Info) {
Config::try_sdk_path().map_or(false, |path| path.join("bin/nightly").exists());

done!("Config setup finished");
config.save();
}
}
}
19 changes: 7 additions & 12 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,19 @@ fn main() {

let args = Args::parse();

let mut config = config::Config::new();

match args.command {
GeodeCommands::New { path } => template::build_template(&mut config, path),
GeodeCommands::Profile { commands } => profile::subcommand(&mut config, commands),
GeodeCommands::Config { commands } => info::subcommand(&mut config, commands),
GeodeCommands::Sdk { commands } => sdk::subcommand(&mut config, commands),
GeodeCommands::Package { commands } => package::subcommand(&mut config, commands),
GeodeCommands::Project { commands } => project::subcommand(&mut config, commands),
GeodeCommands::Index { commands } => index::subcommand(&mut config, commands),
GeodeCommands::New { path } => template::build_template(path),
GeodeCommands::Profile { commands } => profile::subcommand(commands),
GeodeCommands::Config { commands } => info::subcommand(commands),
GeodeCommands::Sdk { commands } => sdk::subcommand(commands),
GeodeCommands::Package { commands } => package::subcommand(commands),
GeodeCommands::Project { commands } => project::subcommand(commands),
GeodeCommands::Index { commands } => index::subcommand(commands),
GeodeCommands::Run {
background,
stay,
launch_args,
} => profile::run_profile(
&config,
None,
match (background, stay) {
(false, false) => RunBackground::Foreground,
Expand Down Expand Up @@ -79,6 +76,4 @@ fn main() {
let _ = man.render(&mut std::io::stdout());
}
}

config.save();
}
29 changes: 12 additions & 17 deletions src/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ pub enum Package {
},
}

pub fn install(config: &mut Config, pkg_path: &Path) {
pub fn install(config: &Config, pkg_path: &Path) {
let mod_path = config.get_current_profile().mods_dir();

if !mod_path.exists() {
Expand Down Expand Up @@ -142,7 +142,6 @@ fn zip_folder(path: &Path, output: &Path) {
}

fn create_resources(
#[allow(unused)] config: &mut Config,
mod_info: &ModFileInfo,
#[allow(unused_mut)] mut cache_bundle: &mut Option<CacheBundle>,
cache: &mut cache::ResourceCache,
Expand Down Expand Up @@ -217,12 +216,7 @@ fn create_resources(
}
}

fn create_package_resources_only(
config: &mut Config,
root_path: &Path,
output_dir: &PathBuf,
shut_up: bool,
) {
fn create_package_resources_only(root_path: &Path, output_dir: &PathBuf, shut_up: bool) {
// Parse mod.json
let mod_info = parse_mod_info(root_path);

Expand All @@ -231,7 +225,6 @@ fn create_package_resources_only(
let mut new_cache = cache::ResourceCache::new();

create_resources(
config,
&mod_info,
&mut cache_bundle,
&mut new_cache,
Expand All @@ -246,7 +239,6 @@ fn create_package_resources_only(
}

fn create_package(
config: &mut Config,
root_path: &Path,
binaries: Vec<PathBuf>,
raw_output: Option<PathBuf>,
Expand Down Expand Up @@ -287,7 +279,6 @@ fn create_package(

// Create resources
create_resources(
config,
&mod_file_info,
&mut cache_bundle,
&mut new_cache,
Expand Down Expand Up @@ -377,7 +368,8 @@ fn create_package(
zip_folder(&working_dir, &output);

if do_install {
install(config, &output);
let config = Config::new().assert_is_setup();
install(&config, &output);
}
}

Expand Down Expand Up @@ -455,16 +447,19 @@ fn merge_packages(inputs: Vec<PathBuf>) {
);
}

pub fn subcommand(config: &mut Config, cmd: Package) {
pub fn subcommand(cmd: Package) {
match cmd {
Package::Install { path } => install(config, &path),
Package::Install { path } => {
let config = Config::new().assert_is_setup();
install(&config, &path);
}

Package::New {
root_path,
binary: binaries,
output,
install,
} => create_package(config, &root_path, binaries, output, install),
} => create_package(&root_path, binaries, output, install),

Package::Merge { packages } => {
if packages.len() < 2 {
Expand All @@ -478,12 +473,12 @@ pub fn subcommand(config: &mut Config, cmd: Package) {
input,
output,
externals,
} => project::check_dependencies(config, input, output, None, externals),
} => project::check_dependencies(input, output, None, externals),

Package::Resources {
root_path,
output,
shut_up,
} => create_package_resources_only(config, &root_path, &output, shut_up),
} => create_package_resources_only(&root_path, &output, shut_up),
}
}
27 changes: 19 additions & 8 deletions src/profile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::config::{Config, Profile as CfgProfile};
use crate::{done, fail, info, warn, NiceUnwrap};
use clap::{Subcommand, ValueEnum};
use colored::Colorize;
use rustyline::config;
use std::cell::RefCell;
use std::process::Command;

Expand Down Expand Up @@ -96,12 +97,9 @@ fn is_valid_geode_dir(_dir: &Path) -> bool {
true
}

pub fn run_profile(
config: &Config,
profile: Option<String>,
background: RunBackground,
launch_args: Vec<String>,
) {
pub fn run_profile(profile: Option<String>, background: RunBackground, launch_args: Vec<String>) {
let config = Config::new().assert_is_setup();

let profile = &profile
.clone()
.map(|p| config.get_profile(&Some(p)).map(|p| p.borrow()))
Expand Down Expand Up @@ -158,9 +156,10 @@ pub fn run_profile(
}
}

pub fn subcommand(config: &mut Config, cmd: Profile) {
pub fn subcommand(cmd: Profile) {
match cmd {
Profile::List => {
let config = Config::new().assert_is_setup();
for profile in &config.profiles {
let name = &profile.borrow().name;
let path = &profile.borrow().gd_path;
Expand All @@ -181,6 +180,8 @@ pub fn subcommand(config: &mut Config, cmd: Profile) {
}

Profile::Path { profile, dir } => {
let config = Config::new();

let profile = profile
.clone()
.map(|p| config.get_profile(&Some(p)).map(|p| p.borrow()))
Expand All @@ -201,6 +202,8 @@ pub fn subcommand(config: &mut Config, cmd: Profile) {
}

Profile::Switch { profile } => {
let mut config = Config::new().assert_is_setup();

if config.get_profile(&Some(profile.to_owned())).is_none() {
fail!("Profile '{}' does not exist", profile);
} else if config.current_profile == Some(profile.to_owned()) {
Expand All @@ -209,13 +212,16 @@ pub fn subcommand(config: &mut Config, cmd: Profile) {
done!("'{}' is now the current profile", &profile);
config.current_profile = Some(profile);
}
config.save();
}

Profile::Add {
name,
location,
platform,
} => {
let mut config = Config::new().assert_is_setup();

if config.get_profile(&Some(name.to_owned())).is_some() {
fail!("A profile named '{}' already exists", name);
} else if !is_valid_geode_dir(&location) {
Expand Down Expand Up @@ -248,20 +254,26 @@ pub fn subcommand(config: &mut Config, cmd: Profile) {
location,
profile.to_string(),
)));
config.save();
}
}

Profile::Remove { name } => {
let mut config = Config::new().assert_is_setup();

if config.get_profile(&Some(name.to_owned())).is_none() {
fail!("Profile '{}' does not exist", name);
} else {
config.profiles.retain(|x| x.borrow().name != name);
done!("'{}' has been removed", name);
}
config.save();
}

Profile::Rename { old, new } => {
let mut config = Config::new().assert_is_setup();
config.rename_profile(&old, new);
config.save();
}

Profile::Run {
Expand All @@ -270,7 +282,6 @@ pub fn subcommand(config: &mut Config, cmd: Profile) {
stay,
launch_args,
} => run_profile(
config,
profile,
match (background, stay) {
(false, false) => RunBackground::Foreground,
Expand Down
10 changes: 5 additions & 5 deletions src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,12 +241,13 @@ fn find_dependency(
}

pub fn check_dependencies(
config: &Config,
input: PathBuf,
output: PathBuf,
platform: Option<PlatformName>,
externals: Vec<String>,
) {
let config = Config::new();

let mod_info = parse_mod_info(&input);

// If no dependencies, skippy wippy
Expand Down Expand Up @@ -328,7 +329,7 @@ pub fn check_dependencies(
// otherwise try to find it on installed mods and then on index

// check index
let found_in_index = match find_index_dependency(&dep, config) {
let found_in_index = match find_index_dependency(&dep, &config) {
Ok(f) => f,
Err(e) => {
warn!("Failed to fetch dependency {} from index: {}", &dep.id, e);
Expand Down Expand Up @@ -516,16 +517,15 @@ pub fn check_dependencies(
}
}

pub fn subcommand(config: &mut Config, cmd: Project) {
pub fn subcommand(cmd: Project) {
match cmd {
Project::New { path } => template::build_template(config, path),
Project::New { path } => template::build_template(path),
Project::ClearCache => clear_cache(&std::env::current_dir().unwrap()),
Project::Check {
install_dir,
platform,
externals,
} => check_dependencies(
config,
std::env::current_dir().unwrap(),
install_dir.unwrap_or("build".into()),
platform,
Expand Down
Loading

0 comments on commit 7e58ee6

Please sign in to comment.