Merge branch 'dev' into 'main'

added health and reveal install command for better user experience

See merge request implo/docki-cli!3
This commit is contained in:
2023-03-06 21:10:04 +00:00
26 changed files with 1735 additions and 111 deletions

1
.dockerignore Normal file
View File

@@ -0,0 +1 @@
**

2
.gitignore vendored
View File

@@ -1,3 +1,3 @@
/target /target
dist dist
docs /docs

View File

@@ -3,7 +3,7 @@ workflow:
- if: '$CI_PIPELINE_SOURCE == "merge_request_event" && $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "main"' - if: '$CI_PIPELINE_SOURCE == "merge_request_event" && $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "main"'
default: default:
image: 'rust:slim' image: 'quirinecker/rust-openssl'
build: build:
script: script:

1153
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -1,6 +1,6 @@
[package] [package]
name = "docki" name = "docki"
version = "0.1.1" version = "0.3.0"
edition = "2021" edition = "2021"
description = "cli for building and publishing documentation using asciidoctor" description = "cli for building and publishing documentation using asciidoctor"
license-file = "LICENSE.txt" license-file = "LICENSE.txt"
@@ -8,3 +8,10 @@ license-file = "LICENSE.txt"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
bytes = "1.4.0"
colored = "2.0.0"
home = "0.5.4"
regex = "1.7.1"
reqwest = { version = "0.11.14", features = ["blocking"] }
text_io = "0.1.12"
zip-extract = "0.1.1"

7
Dockerfile Normal file
View File

@@ -0,0 +1,7 @@
FROM rust:slim
WORKDIR /opt/rust
RUN apt update \
&& apt-get -y upgrade \
&& apt-get -y install libssl-dev pkg-config

View File

View File

0
res/test/docs/fail.txt Normal file
View File

View File

0
res/test/docs/index.adoc Normal file
View File

1
sh/asciidoctor-revealjs-sh Executable file
View File

@@ -0,0 +1 @@
asciidoctor-revealjs $1 -a revealjsdir=$2 --out-file=$3

View File

@@ -1,29 +1,108 @@
use std::process; use std::process;
use regex::Regex;
use super::Builder; use super::Builder;
pub struct AsciiDoctorBuilder; fn exec_command(command: &mut process::Command) -> Result<(), String> {
let result = command.output();
impl Builder for AsciiDoctorBuilder { if let Ok(success) = result {
fn build(&self, in_path: &str, out_path: &str) -> Result<(), String> { if success.stderr.len() == 0 {
let result = process::Command::new("asciidoctor") return Ok(());
.arg(format!("{in_path}"))
.arg(format!("--out-file={out_path}"))
.output();
if let Ok(success) = result {
if success.stderr.len() == 0 {
return Ok(());
} else {
return Err(AsciiDoctorBuilder::from_utf8(success.stderr));
}
} else { } else {
return Err("command failed to execute".to_string()); return Err(AsciiDoctorDocsBuilder::from_utf8(success.stderr));
} }
} else {
println!("{}", result.unwrap_err());
return Err("asciidoctor not installed. For more information run docki health!".to_string());
} }
} }
impl AsciiDoctorBuilder { fn asciidoctor_docs(in_path: &str, out_path: &str) -> process::Command {
let mut command = process::Command::new(format!("asciidoctor"));
command
.arg(format!("--out-file={out_path}"))
.arg(format!("{in_path}"));
return command;
}
fn asciidoctor_slides(in_path: &str, out_path: &str) -> process::Command {
let mut command = process::Command::new(format!("asciidoctor-revealjs"));
let out_dir = parent_path(out_path);
let revealjs_path = path_between(out_dir.to_string(), "./dist/slides/revealjs".to_string());
command
.arg(format!("{in_path}"))
.arg(format!("-a"))
.arg(format!("revealjsdir={revealjs_path}"))
.arg(format!("--out-file={out_path}"));
return command;
}
fn parent_path(child_path: &str) -> String {
let split: Vec<&str> = child_path.split("/").collect();
let slice = &split[..split.len() - 1];
return slice.join("/");
}
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;
let mut path_between = path_back(number_of_backs);
let path_to_to_path = &to_segments[last_matching_index..];
path_between.push_str(&path_to_to_path.join("/"));
return path_between;
}
fn transform_input_to_clone_split(input: &String) -> Vec<String> {
let regex = Regex::new(r"/$").unwrap();
let first_transformation = input.clone().replace("./", "");
return regex.replace_all(&first_transformation, "")
.to_string().split("/")
.collect::<Vec<&str>>()
.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<String>, to_segments: &Vec<String>) -> 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;
}
} else {
return index;
}
}
return from_segments.len();
}
pub struct AsciiDoctorDocsBuilder;
impl Builder for AsciiDoctorDocsBuilder {
fn build(&self, in_path: &str, out_path: &str) -> Result<(), String> {
let mut command = asciidoctor_docs(in_path, out_path);
return exec_command(&mut command);
}
}
impl AsciiDoctorDocsBuilder {
fn from_utf8(input: Vec<u8>) -> String { fn from_utf8(input: Vec<u8>) -> String {
return match String::from_utf8(input) { return match String::from_utf8(input) {
Ok(m) => m, Ok(m) => m,
@@ -31,3 +110,12 @@ impl AsciiDoctorBuilder {
}; };
} }
} }
pub struct AsciiDoctorSlideBuilder;
impl Builder for AsciiDoctorSlideBuilder {
fn build(&self, in_path: &str, out_path: &str) -> Result<(), String> {
let mut command = asciidoctor_slides(in_path, out_path);
return exec_command(&mut command);
}
}

View File

@@ -1,99 +1,19 @@
use std::{collections::HashMap, fs, path::Path}; use std::collections::HashMap;
use crate::app::builder::{asciidoctor::AsciiDoctorBuilder, Builder}; use super::{executions::build_execution::BuildExecution, traits::Command};
use super::traits::Command; pub struct Build;
pub struct Build {
builder: Box<dyn Builder>,
}
impl Build {
fn build_dir(&self, path: &str) -> Vec<Result<(), String>> {
let mut results = vec![];
let Ok(dirs) = fs::read_dir(path) else {
return vec![Err(format!("direcotry {path} was not found. The filesystem was maybe updated while build"))]
};
for result in dirs {
let Ok(entry) = result else {
return vec![Err("could not read entry".to_string())];
};
let path = entry
.path()
.to_str()
.expect("could not get text path")
.to_string()
.clone();
if entry.path().is_dir() {
results = [results, self.build_dir(&path)].concat()
} else {
results.push(self.build_file(&path));
}
}
return results;
}
fn build_file(&self, path: &str) -> Result<(), String> {
let out_path = path
.clone()
.replace("docs", "dist")
.replace(".adoc", ".html");
return self.builder.build(&path, &out_path);
}
fn docs_directory_exists(&self, path: &String) -> bool {
Path::new(path).is_dir()
}
}
impl Command for Build { impl Command for Build {
fn execute(&self, _args: &HashMap<String, String>) -> Result<(), String> { fn execute(&self, _args: &HashMap<String, String>) -> Result<(), String> {
// let Ok(project_cwd_object) = env::current_dir() else { let mut build_execution = BuildExecution::new();
// return Err("current dirctory does not seem to exist".to_string()) return build_execution.execute();
// };
//
// let Some(project_cwd) = project_cwd_object.to_str() else {
// return Err("invalid path".to_string());
// };
let path = format!("./docs/");
let mut error_count = 0;
if !self.docs_directory_exists(&path) {
error_count += 1;
println!(
"docs directory does not exist. Either create it or clone the template from gitlab"
)
} else {
for result in self.build_dir(&path) {
match result {
Err(e) => {
error_count += 1;
println!("{e}");
}
Ok(()) => println!("success"),
};
}
}
if error_count > 0 {
return Err(format!("failed with {} errors", error_count));
}
return Ok(());
} }
fn new() -> Self fn new() -> Self
where where
Self: Sized, Self: Sized,
{ {
return Build { return Build {}
builder: Box::new(AsciiDoctorBuilder {}),
};
} }
} }

View File

@@ -0,0 +1,163 @@
use std::{fs, path::{Path, PathBuf}, io::Cursor};
use crate::app::{
builder::{
asciidoctor::{AsciiDoctorDocsBuilder, AsciiDoctorSlideBuilder},
Builder,
},
fs_util,
};
pub struct BuildExecution {
progress: usize,
goal: usize,
doc_builder: Box<dyn Builder>,
slide_builder: Box<dyn Builder>,
}
impl BuildExecution {
pub fn new() -> Self {
return BuildExecution {
progress: 0,
goal: 0,
slide_builder: Box::new(AsciiDoctorSlideBuilder {}),
doc_builder: Box::new(AsciiDoctorDocsBuilder {}),
};
}
pub fn execute(&mut self) -> Result<(), String> {
let path = "./docs/".to_string();
if !Self::directory_exists(&path) {
return Err("docs directory does not exist it. Create it or use the template".to_string())
}
if let Err(error) = Self::prepare() {
return Err(error);
}
return self.build_dir(&path);
}
fn build_file(
&self,
builder: &Box<dyn Builder>,
in_path: &str,
out_path: &str,
) -> Result<(), String> {
return builder.build(&in_path, &out_path);
}
fn build_file_and_status(
&self,
builder: &Box<dyn Builder>,
in_path: &str,
out_path: &str,
conversion_type: &str,
) {
let result = self.build_file(builder, in_path, out_path);
if result.is_ok() {
self.display_status(in_path, out_path, conversion_type)
} else {
self.display_status(in_path, out_path, "error");
let error = result.unwrap_err();
println!("{error}");
}
}
fn copy(&self, in_path: &str, out_path: &str) {
let segments: &Vec<&str> = &out_path.split("/").collect();
let parent_dir = &segments[0..segments.len() - 1].join("/");
Self::create_dir_recursive(parent_dir);
let result = fs::copy(in_path, out_path);
if result.is_ok() {
self.display_status(in_path, out_path, "copy");
} else {
self.display_status(in_path, out_path, "error");
let error = result.unwrap_err();
println!("{error}");
}
}
fn create_dir_recursive(path: &str) {
let mut validated_path = "./".to_string();
for segment in path.split("/") {
validated_path.push_str(format!("{segment}/").as_str());
if !Self::directory_exists(&validated_path) {
fs::create_dir(&validated_path).unwrap()
}
}
}
fn display_status(&self, in_path: &str, out_path: &str, conversion_type: &str) -> () {
println!(
"({} / {}) [{}] {} -> {}",
self.progress, self.goal, conversion_type, in_path, out_path
);
}
fn build_doc(&self, in_path: &str, out_path: &str) {
self.build_file_and_status(&self.doc_builder, in_path, out_path, "doc");
}
fn prepare() -> Result<(), String> {
let reveal_version = "3.9.2";
let target = format!("https://github.com/hakimel/reveal.js/archive/{reveal_version}.zip");
let Ok(response) = reqwest::blocking::get(target) else {
return Err("could not downlaod revealjs".to_string())
};
let Ok(bytes) = response.bytes() else {
return Err("could not extract bytes".to_string())
};
let out = PathBuf::from("./docs/slides/revealjs");
if zip_extract::extract(Cursor::new(bytes), &out, true).is_err() {
return Err("could not write extracted archive to disk".to_string());
}
return Ok(())
}
fn build_slide(&self, in_path: &str, out_path: &str) {
self.build_file_and_status(&self.slide_builder, in_path, out_path, "slide");
}
fn directory_exists(path: &String) -> bool {
Path::new(path).is_dir()
}
fn build_dir(&mut self, path: &str) -> Result<(), String> {
let result = fs_util::fetch_paths_recursive(&path);
let Ok(paths) = result else {
return Err(result.unwrap_err())
};
for (index, path) in paths.iter().enumerate() {
self.progress = index + 1;
self.goal = paths.len();
if path.ends_with(".adoc") && path.starts_with("./docs/slides") {
let out_path = path
.clone()
.replace("adoc", "html")
.replace("/docs/", "/dist/");
self.build_slide(&path, &out_path)
} else if path.ends_with(".adoc") {
let out_path = path
.clone()
.replace("adoc", "html")
.replace("/docs/", "/dist/");
self.build_doc(&path, &out_path)
} else {
let out_path = path.clone().replace("/docs/", "/dist/");
self.copy(&path, &out_path)
}
}
return Ok(());
}
}

View File

@@ -0,0 +1 @@
pub mod build_execution;

View File

@@ -0,0 +1,91 @@
use std::{collections::HashMap, process, io::ErrorKind};
use colored::Colorize;
use super::traits::Command;
pub struct Health;
const INFO_ASCIIDOC: &str = "
Install the binary with your package manager!
sudo apt install asciidoctor
brew install asciidoctor
gem install asciidoctor
sudo pacman -Syu asciidoctor
yay -Syu asciidoctor
";
const INFO_REVEAL: &str = "
There are two options to install it:
Option 1:
- run `docki install-reveal
Option 2:
- Install the binary from Github https://github.com/asciidoctor/asciidoctor-reveal.js/releases
- Move the downloaded binary in a folder included in the path
- Make sure the binary is called asciidoctor-revealjs and not asciidoctor-revealjs-linux or similar
";
impl Command for Health {
fn execute(&self, _args: &HashMap<String, String>) -> Result<(), String> {
Self::health();
return Ok(())
}
fn new() -> Self where Self: Sized {
return Self {}
}
}
impl Health {
fn health() {
Self::check_asciidoc();
Self::check_reveal();
}
fn check_reveal() -> () {
if Self::reveal_is_installed() {
Self::print_health_ok("asciidoctor-revealjs")
} else {
Self::print_health_not_ok("asciidoctor-revealjs", INFO_REVEAL)
}
}
fn reveal_is_installed() -> bool {
return Self::check_command("asciidoctor-revealjs")
}
fn check_asciidoc() -> () {
if Self::asciidoc_is_installed() {
Self::print_health_ok("asciidoctor")
} else {
Self::print_health_not_ok("asciidoctor", INFO_ASCIIDOC)
}
}
fn asciidoc_is_installed() -> bool {
return Self::check_command("asciidoctor")
}
fn check_command(command: &str) -> bool {
return match process::Command::new(command)
.output() {
Ok(_) => true,
Err(e) => ErrorKind::NotFound != e.kind()
}
}
fn print_health_ok(name: &str) {
println!("- ✔️ {}", name.bright_green());
}
fn print_health_not_ok(name: &str, info: &str) {
println!("- ❗{}", name.bright_red());
println!("{}", info.bright_black())
}
}

View File

@@ -2,10 +2,13 @@ use std::collections::HashMap;
use traits::Command; use traits::Command;
use self::build::Build; use self::{build::Build, health::Health, reveal::Reveal};
pub mod traits; pub mod traits;
pub mod executions;
mod build; mod build;
mod health;
mod reveal;
pub struct CommandRegistry { pub struct CommandRegistry {
commands: HashMap<String, Box<dyn Command>> commands: HashMap<String, Box<dyn Command>>
@@ -15,7 +18,10 @@ impl CommandRegistry {
pub fn register_all(&mut self) { pub fn register_all(&mut self) {
let registry = self; let registry = self;
registry.register("/build".to_string(), Box::new(Build::new()), true) registry.register("/build".to_string(), Box::new(Build::new()), true);
registry.register("/health".to_string(), Box::new(Health::new()), true);
registry.register("/install-reveal".to_string(), Box::new(Reveal::new()), true);
} }
pub fn register(&mut self, path: String, command: Box<dyn Command>, enabled: bool) { pub fn register(&mut self, path: String, command: Box<dyn Command>, enabled: bool) {

View File

@@ -0,0 +1,43 @@
use std::{fs::File, io::Write};
use crate::app::fs_util;
use super::traits::Command;
pub struct Reveal;
const ASCIIDOC_REVEAL_VERSION: &str= "v4.1.0-rc.5";
fn url() -> String {
return format!("https://github.com/asciidoctor/asciidoctor-reveal.js/releases/download/{}/asciidoctor-revealjs-linux", ASCIIDOC_REVEAL_VERSION);
}
impl Command for Reveal {
fn execute(&self, _args: &std::collections::HashMap<String, String>) -> Result<(), String> {
Self::install_asciidocto_revealjs();
return Ok(())
}
fn new() -> Self where Self: Sized {
return Self {}
}
}
impl Reveal {
fn install_asciidocto_revealjs() -> () {
let result = reqwest::blocking::get(url())
.expect("Could not download reveal. Make sure you are connected to the internet");
let binary = result.bytes().expect("could not get binary");
let home_path = home::home_dir().expect("could not find home dir");
let save_path = format!("{}/.docki/asciidoctor-revealjs", home_path.display());
let save_dir = format!("{}/.docki", home_path.display());
fs_util::create_dir_recursive(save_dir.as_str());
let mut file = File::create(save_path).expect("could not save binary");
file.write_all(&binary).expect("could not save binary");
}
}

84
src/app/fs_util/mod.rs Normal file
View File

@@ -0,0 +1,84 @@
use std::{env, fs, path::Path};
struct RecursivePathFetch {
paths: Vec<String>,
path: String,
}
impl RecursivePathFetch {
pub fn new_with_extension_filter(path: String) -> Self {
return Self {
paths: vec![],
path,
};
}
pub fn fetch(&mut self) -> Result<Vec<String>, String> {
if let Err(error) = self.read_dir(self.path.clone()) {
return Err(error);
} else {
return Ok(self.paths.clone());
}
}
fn read_dir(&mut self, path: String) -> Result<(), String> {
let Ok(entries) = fs::read_dir(path) else {
return self.dir_not_found();
};
for result in entries {
let entry = result.unwrap();
let path = entry.path();
let str_path = path.to_str().unwrap();
if path.is_file() {
self.paths.push(str_path.to_string())
} else if path.is_dir() {
let read_result = self.read_dir(str_path.to_string());
if read_result.is_err() {
return read_result;
}
}
}
return Ok(());
}
fn dir_not_found(&self) -> Result<(), String> {
return Err(format!(
"directory {} was not found or was changed while building",
self.path
));
}
}
pub fn fetch_paths_recursive(path: &str) -> Result<Vec<String>, String> {
let mut path_fetch = RecursivePathFetch::new_with_extension_filter(path.to_string());
return path_fetch.fetch();
}
pub fn create_dir_recursive(path: &str) {
let mut validated_path = "".to_string();
for segment in path.split("/") {
validated_path.push_str(format!("{segment}/").as_str());
if !directory_exists(&validated_path) {
fs::create_dir(&validated_path).unwrap()
}
}
}
pub fn directory_exists(path: &String) -> bool {
Path::new(path).is_dir()
}
pub fn expand_path(path: String) -> String {
let home_dir = env::var("HOME").expect("could not find home dir");
return path.replace("~", &home_dir);
}
pub fn docki_path_env() -> String {
let current = env::var("PATH").unwrap_or("".to_string());
return expand_path(format!("{}:~/.docki/", current));
}

View File

@@ -1,7 +1,9 @@
mod commands; mod commands;
pub mod builder; pub mod builder;
pub mod fs_util;
use std::collections::HashMap; use std::collections::HashMap;
use std::env;
use commands::traits::Command; use commands::traits::Command;
use commands::CommandRegistry; use commands::CommandRegistry;
@@ -17,7 +19,8 @@ impl App {
} }
} }
pub fn start(&self, args: Vec<String>) { pub fn start(self, args: Vec<String>) {
Self::preapare_env_path();
let command_args = &args[1..]; let command_args = &args[1..];
let mut path = String::from(""); let mut path = String::from("");
let mut argument_map = HashMap::new(); let mut argument_map = HashMap::new();
@@ -44,7 +47,11 @@ impl App {
self.execute_path(&path, &argument_map); self.execute_path(&path, &argument_map);
} }
fn execute_path(&self, path: &String, args: &HashMap<String, String>) { fn preapare_env_path() {
env::set_var("PATH", fs_util::docki_path_env());
}
fn execute_path(self, path: &String, args: &HashMap<String, String>) {
let command = self.command_regisrty.command_by(path); let command = self.command_regisrty.command_by(path);
if let Some(c) = command { if let Some(c) = command {
@@ -59,7 +66,7 @@ impl App {
match result { match result {
Ok(_) => println!("successfully executed"), Ok(_) => println!("successfully executed"),
Err(message) => println!("{message}"), Err(message) => println!("{message}")
} }
} }
} }

View File

@@ -1,5 +1,8 @@
mod app; mod app;
#[cfg(test)]
mod test;
use std::env; use std::env;
use app::App; use app::App;

View File

@@ -0,0 +1,37 @@
use crate::app::builder::asciidoctor;
#[test]
fn last_matching_index_0() {
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::matching_from_start(&vec1, &vec2);
assert_eq!(last_maching, 2);
}
#[test]
fn last_matching_index_1() {
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::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")
}

1
src/test/builder/mod.rs Normal file
View File

@@ -0,0 +1 @@
pub mod asciidoctor;

11
src/test/fs_util.rs Normal file
View File

@@ -0,0 +1,11 @@
use std::{fs, path::Path};
use crate::app::fs_util;
#[test]
fn test_fetch_asciidoctor_paths_recursive() {
let paths = fs_util::fetch_paths_recursive("res/test/docs").unwrap();
let len = paths.len();
dbg!(paths);
assert_eq!(len, 5);
}

2
src/test/mod.rs Normal file
View File

@@ -0,0 +1,2 @@
mod fs_util;
mod builder;