Skip to content

Commit

Permalink
Start sketching out scripting ideas
Browse files Browse the repository at this point in the history
  • Loading branch information
simonsan committed May 21, 2023
1 parent e7d1bac commit b84d301
Show file tree
Hide file tree
Showing 13 changed files with 221 additions and 15 deletions.
111 changes: 111 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 8 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,19 @@ byte-unit = "4.0.19"
# dev dependencies
tempfile = "3.5.0"
aho-corasick = "1.0"
rstest = "0.17"
quickcheck = "1"
quickcheck_macros = "1"
pretty_assertions = "1"

# infer file and MIME type by magic number
infer = "0.13"
mime = "0.3"

rstest = "0.17"
quickcheck = "1"
quickcheck_macros = "1"
pretty_assertions = "1"
# Scripting
rhai = { version = "1.14.0", features = ["serde"] }

# TODO: Needed Dependencies
# rhai = { version = "1.14.0", features = ["serde"] }
# czkawka_core = "4.1.0"
# + Lazy-regex?
# https://crates.io/crates/lazy-regex
Expand Down Expand Up @@ -132,6 +133,8 @@ itertools = { workspace = true }
clap_complete = { workspace = true }
indicatif = { workspace = true }
winnow = { workspace = true }
rhai = { workspace = true }


[dev-dependencies]
abscissa_core = { workspace = true, features = ["testing"] }
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ This is a Rust implementation of the same concept.

- Filter `files`, which are `smaller than` 20KB in one location:

`organize filter size -l C:\Users\dailyuse\dev-src\organize\docs\screenshots -t files --condition ..20KB`
`organize filter size -l C:\Users\dailyuse\dev-src\organize\docs\screenshots -t files --range ..20KB`

This filter uses the `range` syntax (always inclusive) of Rust:

Expand Down
4 changes: 4 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,7 @@ inv-ft *ARGS:
# Checks the public api against the latest pubished version
pa:
cargo public-api diff latest

# Run a sample script
run:
cargo run -- run-script --path C:\Users\dailyuse\dev-src\organize\scripts\test.rhai
16 changes: 16 additions & 0 deletions scripts/test.rhai
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
print("Hello from rhai script!");

let folder = "C:\\Users\\dailyuse\\dev-src\\organize\\tests";

let apps_ext = ["exe", "msi", "apk"];

let entries = init_organize(folder);
let ext_entries = filter::by_extension(entries, apps_ext);
let last_modified = filter::by_last_modified(ext_entries, "..7d")

action::move(last_modified, "~/backup/INBOX/@Apps/")

let app_inbox_folder = "~/backup/INBOX/@Apps/";
filter::by_extension(entries, apps_ext) move_to app_inbox_folder;

filter(folder->by_extension(apps_ext)) => action(move->app_inbox_folder);
12 changes: 9 additions & 3 deletions src/commands.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
//! Organize Subcommands
mod action;
pub mod action;
mod completions;
mod docs;
mod filter;
pub mod filter;
mod run_script;

// Old approach
// mod edit;
Expand All @@ -13,7 +14,10 @@ mod filter;
// mod sim;

use crate::{
commands::{action::ActionCmd, completions::CompletionsCmd, docs::DocsCmd, filter::FilterCmd},
commands::{
action::ActionCmd, completions::CompletionsCmd, docs::DocsCmd, filter::FilterCmd,
run_script::RunScriptCmd,
},
config::OrganizeConfig,
};
use abscissa_core::{Command, Configurable, FrameworkError, Runnable};
Expand Down Expand Up @@ -48,6 +52,8 @@ pub enum OrganizeCmd {
Filter(FilterCmd),
/// Generate Completions for your shell
Completions(CompletionsCmd),
/// Run a `rhai` script
RunScript(RunScriptCmd),
}

#[allow(clippy::struct_excessive_bools)]
Expand Down
2 changes: 1 addition & 1 deletion src/commands/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use organize_rs_core::rules::actions::OrganizeAction;
/// for a more comprehensive example:
///
/// <https://docs.rs/clap/>
#[derive(Command, Debug, Args)]
#[derive(Command, Debug, Args, Clone)]
pub struct ActionCmd {
#[clap(subcommand)]
actions: OrganizeAction,
Expand Down
2 changes: 1 addition & 1 deletion src/commands/completions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use clap::{Args, CommandFactory, ValueEnum};
use clap_complete::{generate, shells, Generator};

/// `completions` subcommand
#[derive(Args, Command, Debug)]
#[derive(Args, Command, Debug, Clone)]
pub struct CompletionsCmd {
/// Shell to generate completions for
#[arg(value_enum)]
Expand Down
2 changes: 1 addition & 1 deletion src/commands/docs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use abscissa_core::{Command, Runnable};
use clap::Args;

/// Opens the documentation.
#[derive(Command, Debug, Args)]
#[derive(Command, Debug, Args, Clone)]
pub struct DocsCmd {}

impl Runnable for DocsCmd {
Expand Down
6 changes: 3 additions & 3 deletions src/commands/filter.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Filters that organize can apply
use std::fs::FileType;
use std::{fs::FileType, path::PathBuf};

use abscissa_core::{Command, Runnable};
use clap::Args;
Expand All @@ -20,14 +20,14 @@ use organize_rs_core::{
/// for a more comprehensive example:
///
/// <https://docs.rs/clap/>
#[derive(Command, Debug, Args)]
#[derive(Command, Debug, Args, Clone)]
pub struct FilterCmd {
#[clap(subcommand)]
filters: OrganizeFilter,

/// Locations to operate on
#[arg(short, long, global = true)]
locations: Vec<String>,
locations: Vec<PathBuf>,

/// Words in file names to be ignored
#[arg(long, global = true)]
Expand Down
48 changes: 48 additions & 0 deletions src/commands/run_script.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//! `run` subcommand
use std::{
fs::read_to_string,
path::{Path, PathBuf},
};

use abscissa_core::{status_err, Application, Command, Runnable, Shutdown};
use clap::Parser;
use rhai::{Engine, EvalAltResult};

use crate::{application::ORGANIZE_APP, commands::filter::FilterCmd, scripting::add};

/// Run a *.rhai script with organize
#[derive(Command, Debug, Parser)]
pub struct RunScriptCmd {
/// Path to the *.rhai script that should be run
#[clap(long)]
path: PathBuf,
}

impl Runnable for RunScriptCmd {
/// Start the application.
fn run(&self) {
match start_scripting_engine(&self.path) {
Ok(_) => {}
Err(err) => {
status_err!("failed to execute script: {}", err);
ORGANIZE_APP.shutdown(Shutdown::Crash)
}
};
}
}

fn start_scripting_engine(path: impl Into<PathBuf>) -> Result<(), Box<EvalAltResult>> {
// Create an 'Engine'
let mut engine = Engine::new();

engine.register_fn("add", add);

// engine.build_type::<FilterCmd>();

// Run the script
engine.run_file(path.into())?;

// Done!
Ok(())
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@ pub mod commands;
pub mod config;
pub mod error;
pub mod prelude;
pub mod scripting;
Loading

0 comments on commit b84d301

Please sign in to comment.