Skip to content

Commit

Permalink
Merge branch 'unstable' into stable
Browse files Browse the repository at this point in the history
  • Loading branch information
markcda committed Jan 11, 2025
2 parents 07efd20 + 8a8142f commit 08cd90d
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 8 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/artifacts
/target
Cargo.lock
__pycache__
Cargo.lock
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
[package]
name = "smart-patcher"
version = "0.1.4"
version = "0.2.0"
edition = "2024"

[dependencies]
anyhow = "1.0"
clap = { version = "4.5", features = ["derive"] }
difference = { optional = true, git = "https://github.com/johannhof/difference.rs.git" }
mlua = { optional = true, version = "0.10", features = ["anyhow", "lua54", "vendored"] }
pyo3 = { optional = true, version = "0.23", features = ["auto-initialize"] }
regex = "1.11"
Expand All @@ -16,7 +17,7 @@ walkdir = "2.5"

[features]
default = ["tests", "generate-patches"]
tests = []
tests = ["dep:difference"]
generate-patches = ["python", "lua", "rhai"]
python = ["dep:pyo3"]
lua = ["dep:mlua"]
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ See the patch examples in `examples` folder. You can run tests with them by Depl
The usage is easy:

```bash
smart-patcher apply {patch-file.json} {folder-to-patch}
smart-patcher test {patch-file.json} {folder-to-patch} # to test if the patch is correct
smart-patcher apply {patch-file.json} {folder-to-patch} # to apply the patch
```

If you use any scripts, make sure that the path to the script files is relative to the patch file.
Expand Down
6 changes: 3 additions & 3 deletions deploy-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
],
"commands": [
{
"bash_c": "cargo clippy --no-default-features --features=python,lua,rhai",
"bash_c": "cargo clippy --no-default-features --features=tests,python,lua,rhai",
"ignore_fails": false,
"show_success_output": true,
"show_bash_c": true,
Expand All @@ -93,7 +93,7 @@
],
"commands": [
{
"bash_c": "cargo build --release --no-default-features --features=python,lua,rhai",
"bash_c": "cargo build --release --no-default-features --features=tests,python,lua,rhai",
"ignore_fails": false,
"show_success_output": false,
"show_bash_c": true,
Expand All @@ -119,7 +119,7 @@
],
"commands": [
{
"bash_c": "cargo test --no-default-features --features=python,lua,rhai",
"bash_c": "cargo test --no-default-features --features=tests,python,lua,rhai",
"ignore_fails": false,
"show_success_output": true,
"show_bash_c": true,
Expand Down
12 changes: 11 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![feature(let_chains)]
#![deny(warnings, clippy::todo, clippy::unimplemented)]

pub(crate) mod cmd;
pub(crate) mod patch;
Expand Down Expand Up @@ -31,7 +32,16 @@ fn main() -> anyhow::Result<()> {
patches.patch(&params.work_at, &patch_dir)?;
},
#[cfg(feature = "tests")]
SmartPatcherExecType::Test(_) => {},
SmartPatcherExecType::Test(params) => {
let mut patch_dir = params.patch.clone();
patch_dir.pop();

let file = std::fs::File::open(params.patch)?;
let buf = std::io::BufReader::new(file);
let patches: PatchFile = serde_json::from_reader(buf)?;

patches.test(&params.work_at, &patch_dir)?;
},
#[cfg(feature = "generate-patches")]
SmartPatcherExecType::GenerateExamples => {
crate::patch::generate_examples()?;
Expand Down
40 changes: 40 additions & 0 deletions src/patch.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#[cfg(feature = "tests")]
use difference::Changeset;
use serde::{Deserialize, Serialize};
use std::fs::{self, File};
use std::io::Read;
Expand Down Expand Up @@ -117,6 +119,44 @@ impl PatchFile {

Ok(cntr)
}

#[cfg(feature = "tests")]
pub fn test(&self, root: &Path, patch_dir: &Path) -> anyhow::Result<usize> {
let mut cntr = 0;

if root.is_dir() {
for entry in WalkDir::new(root).into_iter().filter_map(|e| e.ok()) {
let path = entry.path();
if !path.is_file() { continue }
for patch in &self.patches {
if self.should_process_file(path, &patch.files) {
let content = PatchFile::read(path, patch_dir, &patch.decoder)?;
if let Some(res) = PatchFile::apply_patch(content.clone(), patch_dir, patch, &mut cntr)? {
let diffs = Changeset::new(&content, &res, "");
println!("In file {:?}:", path);
println!("{}", diffs);
println!();
}
}
}
}
} else {
let path = root;
for patch in &self.patches {
if self.should_process_file(path, &patch.files) {
let content = PatchFile::read(path, patch_dir, &patch.decoder)?;
if let Some(res) = PatchFile::apply_patch(content.clone(), patch_dir, patch, &mut cntr)? {
let diffs = Changeset::new(&content, &res, "");
println!("In file {:?}:", path);
println!("{}", diffs);
println!();
}
}
}
}

Ok(cntr)
}

fn should_process_file(&self, path: &Path, file_patterns: &[FilePath]) -> bool {
if file_patterns.is_empty() { return true }
Expand Down

0 comments on commit 08cd90d

Please sign in to comment.