From 06b47440dd1a7e4617c3a5d341844ac1298fc899 Mon Sep 17 00:00:00 2001 From: QuirinEcker Date: Sat, 28 Jan 2023 18:32:13 +0100 Subject: [PATCH] bundling work in progress --- Cargo.lock | 27 +++++++++++++++++++ Cargo.toml | 1 + src/app/builder/asciidoctor.rs | 46 ++++++++++++++++++++++++++------- src/test/builder/asciidoctor.rs | 34 ++++++++++++++++++------ 4 files changed, 90 insertions(+), 18 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 08cd204..84880fa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8,6 +8,15 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" +[[package]] +name = "aho-corasick" +version = "0.7.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cc936419f96fa211c1b9166887b38e5e40b19958e5b895be7c1f93adec7071ac" +dependencies = [ + "memchr", +] + [[package]] name = "async-trait" version = "0.1.63" @@ -118,6 +127,7 @@ name = "docki" version = "0.1.1" dependencies = [ "async-trait", + "regex", "reqwest", "zip-extract", ] @@ -567,6 +577,23 @@ dependencies = [ "bitflags", ] +[[package]] +name = "regex" +version = "1.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "48aaa5748ba571fb95cd2c85c09f629215d3a6ece942baa100950af03a34f733" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.6.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "456c603be3e8d448b072f410900c09faf164fbce2d480456f50eea6e25f9c848" + [[package]] name = "remove_dir_all" version = "0.5.3" diff --git a/Cargo.toml b/Cargo.toml index 70a2f04..4157e22 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,5 +9,6 @@ license-file = "LICENSE.txt" [dependencies] async-trait = "0.1.63" +regex = "1.7.1" reqwest = {version = "0.11.14", features = ["blocking"]} zip-extract = "0.1.1" diff --git a/src/app/builder/asciidoctor.rs b/src/app/builder/asciidoctor.rs index 423b80e..1bf80e0 100644 --- a/src/app/builder/asciidoctor.rs +++ b/src/app/builder/asciidoctor.rs @@ -1,4 +1,6 @@ -use std::process; +use std::{process, fmt::format}; + +use regex::Regex; use super::Builder; @@ -28,32 +30,56 @@ fn asciidoctor_docs(in_path: &str, out_path: &str) -> process::Command { } fn asciidoctor_slides(in_path: &str, out_path: &str) -> process::Command { - let mut command = process::Command::new(format!("asciidoctor-revealjs-linux")); + let mut command = process::Command::new(format!("asciidoctor-revealjs")); let revealjs_path = path_between(out_path.to_string(), "./dist/slides/revealjs".to_string()); command .arg(format!("{in_path}")) + .arg(format!("-a revealjsdir={revealjs_path}")) .arg(format!("--out-file={out_path}")); return command; } -fn path_between(from: String, to: String) -> String { - let from_segments: Vec<&str> = from.split("/").collect(); - let to_segments: Vec<&str> = to.split("/").collect(); - let last_matching_index = last_matching_index(&from_segments, &to_segments); +pub fn path_between(from: String, to: String) -> String { + let from_segments = transform_input_to_clone_split(&from); + let to_segments = transform_input_to_clone_split(&to); + let last_matching_index = matching_from_start(&from_segments, &to_segments); let number_of_backs = from_segments.len() - last_matching_index; - return "".to_string(); + let mut path_between = path_back(number_of_backs); + dbg!(&path_between); + let path_to_to_path = &to_segments[last_matching_index..]; + path_between.push_str(&path_to_to_path.join("/")); + return path_between; } -pub fn last_matching_index(from_segments: &Vec<&str>, to_segments: &Vec<&str>) -> usize { +fn transform_input_to_clone_split(input: &String) -> Vec { + let regex = Regex::new(r"/$").unwrap(); + let first_transformation = input.clone().replace("./", ""); + return regex.replace_all(&first_transformation, "") + .to_string().split("/") + .collect::>() + .iter().map(|s| s.to_string()).collect() +} + +fn path_back(count: usize) -> String { + let mut path = "".to_string(); + + for _ in 0..count { + path.push_str("../"); + } + + return path; +} + +pub fn matching_from_start(from_segments: &Vec, to_segments: &Vec) -> usize { for (index, from_segment) in from_segments.iter().enumerate() { if let Some(to_segment) = to_segments.get(index){ if from_segment != to_segment { - return index - 1; + return index; } } else { - return index - 1; + return index; } } diff --git a/src/test/builder/asciidoctor.rs b/src/test/builder/asciidoctor.rs index 211fda0..883152e 100644 --- a/src/test/builder/asciidoctor.rs +++ b/src/test/builder/asciidoctor.rs @@ -2,18 +2,36 @@ use crate::app::builder::asciidoctor; #[test] fn last_matching_index_0() { - let vec1 = vec!["dings", "dings", "dingens"]; - let vec2 = vec!["dings", "dings", "dings"]; + let vec1 = vec!["dings", "dings", "dingens"].iter().map(|s| s.to_string()).collect(); + let vec2 = vec!["dings", "dings", "dings"].iter().map(|s| s.to_string()).collect(); - let last_maching = asciidoctor::last_matching_index(&vec1, &vec2); - assert_eq!(last_maching, 1); + let last_maching = asciidoctor::matching_from_start(&vec1, &vec2); + assert_eq!(last_maching, 2); } #[test] fn last_matching_index_1() { - let vec1 = vec!["dings", "dings", "dingens", "dings", "dingens"]; - let vec2 = vec!["dings", "dings", "dingens", "dings"]; + let vec1 = vec!["dings", "dings", "dingens", "dings", "dingens"].iter().map(|s| s.to_string()).collect(); + let vec2 = vec!["dings", "dings", "dingens", "dings"].iter().map(|s| s.to_string()).collect(); - let last_maching = asciidoctor::last_matching_index(&vec1, &vec2); - assert_eq!(last_maching, 3); + let last_maching = asciidoctor::matching_from_start(&vec1, &vec2); + assert_eq!(last_maching, 4); +} + +#[test] +fn path_between_0() { + let path1 = "./docs/dings"; + let path2 = "./dist/dings"; + + let path_between = asciidoctor::path_between(path1.to_string(), path2.to_string()); + assert_eq!(path_between, "../../dist/dings"); +} + +#[test] +fn path_between_1() { + let path1 = "./dist/slides/core/"; + let path2 = "./dist/slides/revealjs"; + + let path_between = asciidoctor::path_between(path1.to_string(), path2.to_string()); + assert_eq!(path_between, "../revealjs") }