Skip to content

Commit 54e21fd

Browse files
committed
Added support for app level settings
Also added a check for init.lua to make sure it exists as a file. Closes #241
1 parent 53898f7 commit 54e21fd

1 file changed

Lines changed: 33 additions & 11 deletions

File tree

src/lib.rs

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ pub struct AppState {
3333
pub init_args: Vec<String>,
3434
pub package_path: Option<String>,
3535
pub settings: Value,
36+
pub app_settings: Option<(String, Value)>,
3637
}
3738

3839
impl AppState {
@@ -72,6 +73,14 @@ impl AppState {
7273
lua.globals().set("torchbear", tb_table)?;
7374
}
7475

76+
// app table
77+
78+
if let Some((name, app_settings)) = &self.app_settings {
79+
let tb_table = lua.create_table()?;
80+
tb_table.set("settings", rlua_serde::to_value(&lua, app_settings).map_err(LuaError::external)?)?;
81+
lua.globals().set(name.as_str(), tb_table)?;
82+
}
83+
7584
// Lua package.path
7685
match self.package_path {
7786
Some(ref package_path) => {
@@ -169,28 +178,40 @@ impl ApplicationBuilder {
169178
None => ()
170179
}
171180

172-
let scl_path = match &init_path {
173-
Some(p) => p.parent().unwrap_or(Path::new(".")).join("torchbear.scl"),
174-
None => PathBuf::from("torchbear.scl"),
181+
fn get_or (map: &Value, key: &str, val: &str) -> String {
182+
map.get(key).map(|s| String::from(s.as_str().unwrap_or(val)) ).unwrap_or(String::from(val))
183+
}
184+
185+
let root_path = match &init_path {
186+
Some(p) => p.parent().unwrap_or(Path::new(".")),
187+
None => Path::new("."),
175188
};
176189

177-
let setting_file = scl_path.as_path();
190+
let config_path = root_path.join("torchbear.scl");
191+
192+
let setting_file = config_path.as_path();
178193

179194
let config = if setting_file.exists() {
180195
conf::Conf::load_file(&setting_file)?
181196
} else {
182197
SettingConfig::default()
183198
};
184199

185-
fn get_or (map: &Value, key: &str, val: &str) -> String {
186-
map.get(key).map(|s| String::from(s.as_str().unwrap_or(val)) ).unwrap_or(String::from(val))
187-
}
188-
189200
let general = config.general.unwrap_or_default();
190201

191-
let init_path = init_path.unwrap_or(Path::new(&get_or(&general, "init", "init.lua")).to_path_buf());
202+
let app_config: Option<(String, Value)> = general.get("app-name").and_then(Value::as_str).map(PathBuf::from).and_then(|name| {
203+
let mut config_path = root_path.join(&name);
204+
config_path.set_extension("scl");
205+
if config_path.exists() && config_path.is_file() {
206+
conf::Conf::load_file(&config_path).map(|s| (name.to_string_lossy().to_string(), s)).ok()
207+
} else {
208+
None
209+
}
210+
});
211+
212+
let init_path = init_path.unwrap_or(PathBuf::from(&get_or(&general, "init", "init.lua")));
192213

193-
if !init_path.exists() {
214+
if !init_path.exists() || !init_path.is_file() {
194215
println!("Error: Specified init.lua not found. You may have not completed installing your app");
195216
std::process::exit(1);
196217
}
@@ -206,7 +227,8 @@ impl ApplicationBuilder {
206227
init_path: init_path,
207228
init_args: init_args,
208229
package_path: package_path,
209-
settings: general
230+
settings: general,
231+
app_settings: app_config,
210232
};
211233

212234
if let Some(web) = config.web_server {

0 commit comments

Comments
 (0)