Skip to content

Commit

Permalink
Merge pull request #18 from ink0rr/dev
Browse files Browse the repository at this point in the history
rgl v0.6.0
  • Loading branch information
ink0rr authored Mar 20, 2024
2 parents a99d762 + 4ea041b commit b519794
Show file tree
Hide file tree
Showing 29 changed files with 535 additions and 376 deletions.
238 changes: 124 additions & 114 deletions Cargo.lock

Large diffs are not rendered by default.

22 changes: 11 additions & 11 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
[package]
name = "rgl"
version = "0.5.2"
version = "0.6.0"
edition = "2021"

[[bin]]
name = "rgl"

[dependencies]
anyhow = "1.0.79"
clap = { version = "4.4.18", features = ["cargo"] }
anyhow = "1.0.80"
clap = { version = "4.5.2", features = ["cargo"] }
dialoguer = "0.11.0"
dunce = "1.0.4"
enum_dispatch = "0.3.12"
fslock = "0.2.1"
indexmap = { version = "2.2.0", features = ["serde"] }
indexmap = { version = "2.2.5", features = ["serde"] }
md5 = "0.7.0"
notify = "6.1.1"
notify-debouncer-mini = "0.4.1"
paris = { version = "1.5.15", features = ["macros"] }
rayon = "1.8.1"
semver = "1.0.21"
serde = { version = "1.0.196", features = ["derive"] }
serde_json = "1.0.112"
tempfile = "3.9.0"
ureq = "2.9.1"
rayon = "1.9.0"
semver = "1.0.22"
serde = { version = "1.0.197", features = ["derive"] }
serde_json = "1.0.114"
tempfile = "3.10.1"
ureq = "2.9.6"
uuid = { version = "1.7.0", features = ["v4"] }
walkdir = "2.4.0"
walkdir = "2.5.0"
which = "6.0.0"
zip = "0.6.6"
16 changes: 4 additions & 12 deletions src/commands/add.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,17 @@
use crate::info;
use crate::rgl::{ref_to_version, Config, FilterInstaller, RemoteFilter, Session};
use crate::rgl::{Config, FilterInstaller, FilterType, Session};
use anyhow::Result;
use std::path::Path;

pub fn add_filters(filters: Vec<&String>, force: bool) -> Result<()> {
let mut config = Config::load()?;
let mut session = Session::lock()?;
let data_path = Path::new(&config.regolith.data_path);
let data_path = config.get_data_path();
for arg in filters {
info!("Adding filter <b>{}</>...", arg);
let filter = FilterInstaller::from_arg(arg)?;
if filter.install(data_path, force)? {
if filter.install(FilterType::Remote, Some(&data_path), force)? {
info!("Filter <b>{}</> successfully added", filter.name);
let version = ref_to_version(&filter.git_ref);
config.regolith.filter_definitions.insert(
filter.name,
serde_json::to_value(RemoteFilter {
url: filter.url,
version,
})?,
);
config.add_filter(filter)?;
}
}
config.save()?;
Expand Down
26 changes: 26 additions & 0 deletions src/commands/apply.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use crate::fs::{empty_dir, try_symlink};
use crate::info;
use crate::rgl::{Config, Filter, FilterContext, Session};
use anyhow::Result;
use std::path::Path;

pub fn apply(filter_name: &str, run_args: Vec<String>) -> Result<()> {
let config = Config::load()?;
let mut session = Session::lock()?;

let temp = Path::new(".regolith").join("tmp");
let temp_bp = temp.join("BP");
let temp_rp = temp.join("RP");

empty_dir(&temp)?;
try_symlink(config.get_behavior_pack(), temp_bp)?;
try_symlink(config.get_resource_pack(), temp_rp)?;
try_symlink(config.get_data_path(), temp.join("data"))?;

let filter = config.get_filter(filter_name)?;
let context = FilterContext::new(filter.get_type(), filter_name)?;
filter.run(&context, &temp, &run_args)?;
info!("Successfully applied filter <b>{filter_name}</>");

session.unlock()
}
18 changes: 9 additions & 9 deletions src/commands/get.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
use crate::info;
use crate::rgl::{Config, Filter, FilterContext, FilterDefinition, FilterInstaller, Session};
use crate::rgl::{
Config, Filter, FilterContext, FilterDefinition, FilterInstaller, FilterType, Session,
};
use anyhow::Result;
use semver::Version;
use std::path::Path;

pub fn get_filters(force: bool) -> Result<()> {
let config = Config::load()?;
let mut session = Session::lock()?;
let data_path = Path::new(&config.regolith.data_path);
for (name, def) in config.regolith.filter_definitions {
let filter = FilterDefinition::from_value(def)?;
match filter {
let data_path = config.get_data_path();
for (name, value) in config.get_filters() {
match FilterDefinition::from_value(value)? {
FilterDefinition::Local(filter) => {
info!("Installing dependencies for <b>{name}</>...");
let context = FilterContext::new(&name, false)?;
let context = FilterContext::new(FilterType::Local, &name)?;
filter.install_dependencies(&context)?;
}
FilterDefinition::Remote(filter) => {
info!("Getting filter <b>{name}</>...");
let git_ref = Version::parse(&filter.version)
.map(|version| format!("{name}-{version}"))
.unwrap_or(filter.version);
let filter = FilterInstaller::new(&name, filter.url, git_ref)?;
filter.install(data_path, force)?;
let filter = FilterInstaller::new(&name, filter.url, git_ref);
filter.install(FilterType::Remote, Some(&data_path), force)?;
}
};
}
Expand Down
2 changes: 1 addition & 1 deletion src/commands/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub fn init(force: bool) -> Result<()> {

let min_engine_version = Input::<String>::with_theme(&ColorfulTheme::default())
.with_prompt("Minimum engine version")
.default("1.20.50".to_owned())
.default("1.20.60".to_owned())
.validate_with(|input: &String| -> Result<(), String> {
if Version::parse(input).is_ok() {
Ok(())
Expand Down
14 changes: 14 additions & 0 deletions src/commands/install.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use crate::info;
use crate::rgl::{FilterInstaller, FilterType};
use anyhow::Result;

pub fn install_tools(tools: Vec<&String>, force: bool) -> Result<()> {
for arg in tools {
info!("Installing tool <b>{}</>...", arg);
let tool = FilterInstaller::from_arg(arg)?;
if tool.install(FilterType::Tool, None, force)? {
info!("Tool <b>{}</> successfully installed", tool.name);
}
}
Ok(())
}
3 changes: 1 addition & 2 deletions src/commands/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ use paris::log;

pub fn list() -> Result<()> {
let config = Config::load()?;
let filters = config.regolith.filter_definitions;

let mut local_filters = vec![];
let mut remote_filters = vec![];
for (name, value) in filters {
for (name, value) in config.get_filters() {
let filter = FilterDefinition::from_value(value.to_owned())?;
match filter {
FilterDefinition::Local(_) => {
Expand Down
8 changes: 8 additions & 0 deletions src/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
mod add;
mod apply;
mod clean;
mod get;
mod init;
mod install;
mod list;
mod remove;
mod run;
mod tool;
mod uninstall;
mod update;

pub use self::add::*;
pub use self::apply::*;
pub use self::clean::*;
pub use self::get::*;
pub use self::init::*;
pub use self::install::*;
pub use self::list::*;
pub use self::remove::*;
pub use self::run::*;
pub use self::tool::*;
pub use self::uninstall::*;
pub use self::update::*;
7 changes: 5 additions & 2 deletions src/commands/remove.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
use crate::rgl::{Config, Session};
use crate::fs::rimraf;
use crate::rgl::{Config, FilterType, Session};
use crate::{info, warn};
use anyhow::Result;

pub fn remove_filters(filters: Vec<&String>) -> Result<()> {
let mut config = Config::load()?;
let mut session = Session::lock()?;
for name in filters {
if config.regolith.filter_definitions.remove(name).is_some() {
if config.remove_filter(name).is_some() {
let filter_dir = FilterType::Remote.cache_dir(name)?;
rimraf(filter_dir)?;
info!("Removed filter <b>{name}</>");
} else {
warn!("Filter <b>{name}</> not found");
Expand Down
53 changes: 25 additions & 28 deletions src/commands/run.rs
Original file line number Diff line number Diff line change
@@ -1,58 +1,55 @@
use crate::fs::{copy_dir, empty_dir, move_dir, rimraf, symlink};
use crate::rgl::{copy_dir_cached, Config, RunContext, Session};
use crate::fs::{copy_dir, empty_dir, move_dir, rimraf, try_symlink};
use crate::rgl::{copy_dir_cached, Config, Session};
use crate::{debug, info, measure_time, warn};
use anyhow::{Context, Result};
use std::{fs, io, time};
use std::time;

pub fn run_or_watch(profile_name: &str, watch: bool, cached: bool) -> Result<()> {
let start_time = time::Instant::now();
let config = Config::load()?;

let mut session = Session::lock()?;
let context = RunContext::new(config, profile_name)?;
let profile = context.get_profile(profile_name)?;
let (bp, rp, temp) = profile.get_export_paths(&context.name)?;

let bp = config.get_behavior_pack();
let rp = config.get_resource_pack();

let profile = config.get_profile(profile_name)?;
let (target_bp, target_rp, temp) = profile.get_export_paths(config.get_name())?;
let temp_bp = temp.join("BP");
let temp_rp = temp.join("RP");

measure_time!("Setup temp dir", {
empty_dir(&temp)?;
if cached {
copy_dir_cached(&context.behavior_pack, &temp_bp, &bp)?;
copy_dir_cached(&context.resource_pack, &temp_rp, &rp)?;
copy_dir_cached(bp, &temp_bp, &target_bp)?;
copy_dir_cached(rp, &temp_rp, &target_rp)?;
} else {
rimraf(&bp)?;
rimraf(&rp)?;
copy_dir(&context.behavior_pack, &temp_bp)?;
copy_dir(&context.resource_pack, &temp_rp)?;
}
if let Err(e) = symlink(&context.data_path, temp.join("data")) {
match e.downcast_ref::<io::Error>().map(|e| e.kind()) {
Some(io::ErrorKind::NotFound) => {
warn!("Data folder does not exists, creating...");
fs::create_dir_all(&context.data_path)?;
}
_ => return Err(e),
}
rimraf(&target_bp)?;
rimraf(&target_rp)?;
copy_dir(bp, &temp_bp)?;
copy_dir(rp, &temp_rp)?;
}
try_symlink(config.get_data_path(), temp.join("data"))?;
});

measure_time!(profile_name, {
info!("Running <b>{profile_name}</> profile");
profile.run(&context, &temp)?;
profile.run(&config, &temp, profile_name)?;
});

measure_time!("Export project", {
info!(
"Moving files to target location: \n\
\tBP: {} \n\
\tRP: {}",
bp.display(),
rp.display()
target_bp.display(),
target_rp.display()
);
let export = || -> Result<()> {
move_dir(temp_bp, bp)?;
move_dir(temp_rp, rp)
if !cached {
move_dir(temp_bp, target_bp)?;
move_dir(temp_rp, target_rp)?;
}
Ok(())
};
export().context("Failed to export project")?;
});
Expand All @@ -62,7 +59,7 @@ pub fn run_or_watch(profile_name: &str, watch: bool, cached: bool) -> Result<()>
if watch {
info!("Watching for changes...");
info!("Press Ctrl+C to stop watching");
context.watch_project_files()?;
config.watch_project_files()?;
warn!("Changes detected, restarting...");
session.unlock()?;
return run_or_watch(profile_name, watch, cached);
Expand Down
29 changes: 29 additions & 0 deletions src/commands/tool.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use crate::fs::{empty_dir, read_json, try_symlink};
use crate::rgl::{Config, Filter, FilterContext, FilterType, RemoteFilterConfig, Session};
use anyhow::{Context, Result};
use std::path::Path;

pub fn tool(tool_name: &str, run_args: Vec<String>) -> Result<()> {
let config = Config::load()?;
let mut session = Session::lock()?;

let temp = Path::new(".regolith").join("tmp");
let temp_bp = temp.join("BP");
let temp_rp = temp.join("RP");

empty_dir(&temp)?;
try_symlink(config.get_behavior_pack(), temp_bp)?;
try_symlink(config.get_resource_pack(), temp_rp)?;
try_symlink(config.get_data_path(), temp.join("data"))?;

let context = FilterContext::new(FilterType::Tool, tool_name)?;
let config_path = context.filter_dir.join("filter.json");
let config = read_json::<RemoteFilterConfig>(config_path)
.context(format!("Failed to load config for tool <b>{tool_name}</>"))?;

for filter in config.filters {
filter.run(&context, &temp, &run_args)?;
}

session.unlock()
}
17 changes: 17 additions & 0 deletions src/commands/uninstall.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use crate::fs::rimraf;
use crate::rgl::FilterType;
use crate::{info, warn};
use anyhow::Result;

pub fn uninstall_tools(tools: Vec<&String>) -> Result<()> {
for name in tools {
let filter_dir = FilterType::Tool.cache_dir(name)?;
if filter_dir.exists() {
rimraf(filter_dir)?;
info!("Uninstalled tool <b>{name}</>");
} else {
warn!("Tool <b>{name}</> not found");
}
}
Ok(())
}
13 changes: 13 additions & 0 deletions src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,19 @@ pub fn symlink(from: impl AsRef<Path>, to: impl AsRef<Path>) -> Result<()> {
))
}

pub fn try_symlink(from: impl AsRef<Path>, to: impl AsRef<Path>) -> Result<()> {
if let Err(e) = symlink(&from, &to) {
match e.downcast_ref::<io::Error>().map(|e| e.kind()) {
Some(io::ErrorKind::NotFound) => {
fs::create_dir_all(&from)?;
symlink(from, to)?;
}
_ => return Err(e),
}
}
Ok(())
}

pub fn write_file<P: AsRef<Path>, C: AsRef<[u8]>>(path: P, contents: C) -> Result<()> {
let path = path.as_ref();
fs::write(path, contents).context(format!(
Expand Down
Loading

0 comments on commit b519794

Please sign in to comment.