40
website/scripts/docsmg/Cargo.lock
generated
40
website/scripts/docsmg/Cargo.lock
generated
@ -17,6 +17,15 @@ version = "1.0.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
|
||||
|
||||
[[package]]
|
||||
name = "aho-corasick"
|
||||
version = "1.1.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916"
|
||||
dependencies = [
|
||||
"memchr",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anstream"
|
||||
version = "0.6.14"
|
||||
@ -162,6 +171,8 @@ dependencies = [
|
||||
"anyhow",
|
||||
"clap",
|
||||
"colored",
|
||||
"lazy_static",
|
||||
"regex",
|
||||
"tokio",
|
||||
]
|
||||
|
||||
@ -243,6 +254,35 @@ dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex"
|
||||
version = "1.10.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b91213439dad192326a0d7c6ee3955910425f441d7038e0d6933b0aec5c4517f"
|
||||
dependencies = [
|
||||
"aho-corasick",
|
||||
"memchr",
|
||||
"regex-automata",
|
||||
"regex-syntax",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex-automata"
|
||||
version = "0.4.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "38caf58cc5ef2fed281f89292ef23f6365465ed9a41b7a7754eb4e26496c92df"
|
||||
dependencies = [
|
||||
"aho-corasick",
|
||||
"memchr",
|
||||
"regex-syntax",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex-syntax"
|
||||
version = "0.8.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7a66a03ae7c801facd77a29370b4faec201768915ac14a721ba36f20bc9c209b"
|
||||
|
||||
[[package]]
|
||||
name = "rustc-demangle"
|
||||
version = "0.1.24"
|
||||
|
@ -9,4 +9,6 @@ edition = "2021"
|
||||
anyhow = "1.0.86"
|
||||
clap = { version = "4.5.9", features = ["derive", "env"] }
|
||||
colored = "2.1.0"
|
||||
lazy_static = "1.5.0"
|
||||
regex = "1.10.5"
|
||||
tokio = "1.38.0"
|
||||
|
@ -4,12 +4,10 @@
|
||||
|
||||
1. Verify that you have the latest version of rust installed
|
||||
- Install [rust](rustup.rs) or update rust to the latest version with `rustup update`
|
||||
2. Install the cli tool with `cargo install --git https://github.com/goauthentik/authentik --bin docsmg`
|
||||
2. Install the cli tool with `curl https://raw.githubusercontent.com/goauthentik/authentik/main/website/scripts/docsmg/install.sh | sh`
|
||||
|
||||
## Steps to use
|
||||
|
||||
1. Generate a migratefile with `docsmg generate`
|
||||
2. Find the files you want to move in `migratefile` and insert the path you want to move them to after the arrow; ex `path/to/move/from/file.md -> path/to/move/to/file.md` Note: make sure to put spaces on either side of the arrow or that line won't be recognized
|
||||
3. Once you have entered all the paths you want to move, migrate the files with `docsmg migrate`
|
||||
4. To revert the migration, use `docsmg unmigrate`; Note: DO NOT edit the migrate file inbetween steps 3 and 4
|
||||
5. Repeat steps 2-4 until you are satified with the result
|
||||
### Adding entries
|
||||
|
||||
Add entries to be moved with `map <MOVE_FROM> <MOVE_TO>`
|
||||
|
@ -1,14 +1,14 @@
|
||||
use std::path::PathBuf;
|
||||
|
||||
use crate::{migratefile::read_migrate_file_left_side, recurse_directory};
|
||||
use crate::{migratefile::read_migrate_file, recurse_directory};
|
||||
|
||||
pub fn generate(migratefile: Option<PathBuf>, migrate_path: PathBuf) {
|
||||
// if there is a migrate file, read it and get the paths from the left side
|
||||
let paths: Vec<PathBuf> = match migratefile {
|
||||
Some(i) => {
|
||||
let contents = read_migrate_file_left_side(i);
|
||||
let contents = read_migrate_file(i);
|
||||
if let Ok(contents) = contents {
|
||||
contents
|
||||
contents.iter().map(|x| x.0.clone()).collect()
|
||||
} else {
|
||||
vec![]
|
||||
}
|
||||
@ -20,12 +20,12 @@ pub fn generate(migratefile: Option<PathBuf>, migrate_path: PathBuf) {
|
||||
// get rid of paths already in the specified migrate file
|
||||
let paths: Vec<PathBuf> = recurse_directory(migrate_path.clone())
|
||||
.iter()
|
||||
.filter(|x| !paths.contains(x))
|
||||
.filter_map(|x| x.strip_prefix(migrate_path.clone()).ok())
|
||||
.filter(|x| !paths.contains(&x.to_path_buf()))
|
||||
.map(|x| x.to_path_buf())
|
||||
.collect();
|
||||
|
||||
for path in paths {
|
||||
println!("{} ->", path.display());
|
||||
println!("{} -> ", path.display());
|
||||
}
|
||||
}
|
||||
|
@ -1,10 +1,9 @@
|
||||
use std::{
|
||||
ffi::OsStr,
|
||||
fs::{read_to_string, write},
|
||||
path::PathBuf,
|
||||
collections::HashMap, ffi::OsStr, fmt::format, fs::{read_to_string, write}, path::{Component, Path, PathBuf}
|
||||
};
|
||||
|
||||
use colored::Colorize;
|
||||
use lazy_static::lazy_static;
|
||||
|
||||
use crate::{migratefile::read_migrate_file, recurse_directory};
|
||||
|
||||
@ -27,8 +26,6 @@ pub fn migrate(quiet: bool, migratefile: PathBuf, migrate_path: PathBuf) {
|
||||
}
|
||||
};
|
||||
|
||||
println!("len: {}", files.len());
|
||||
|
||||
replace_links(migrate_path.clone(), files.clone());
|
||||
let successful_moves = move_files(quiet, migrate_path.clone(), files);
|
||||
add_redirects(successful_moves.clone(), migrate_path.clone());
|
||||
@ -98,38 +95,31 @@ fn move_files(
|
||||
}
|
||||
|
||||
fn replace_links(migrate_path: PathBuf, successful_moves: Vec<(PathBuf, PathBuf)>) {
|
||||
lazy_static! {
|
||||
static ref find_link: regex::Regex =
|
||||
regex::Regex::new(r"\[(?<a>.*)\]\((?<b>.*)\)").unwrap();
|
||||
}
|
||||
let files = recurse_directory(migrate_path.clone());
|
||||
|
||||
for file in files {
|
||||
let relative_file = file
|
||||
.strip_prefix(migrate_path.clone())
|
||||
.unwrap()
|
||||
.to_path_buf();
|
||||
let relative_file = file.strip_prefix(migrate_path.clone()).unwrap().to_path_buf();
|
||||
let mut contents = match read_to_string(file.clone()) {
|
||||
Ok(i) => i,
|
||||
Err(_) => continue,
|
||||
};
|
||||
let mut replace = vec![];
|
||||
for successful_move in &successful_moves {
|
||||
if migrate_path
|
||||
.join(successful_move.0.clone())
|
||||
.canonicalize()
|
||||
.unwrap()
|
||||
== file.clone().canonicalize().unwrap()
|
||||
{
|
||||
if migrate_path.join(successful_move.0.clone()).canonicalize().unwrap()
|
||||
== file.clone().canonicalize().unwrap() {
|
||||
continue;
|
||||
}
|
||||
let new_successful_move_from =
|
||||
make_path_relative(successful_move.0.clone(), relative_file.clone());
|
||||
let new_successful_move_to =
|
||||
make_path_relative(successful_move.1.clone(), relative_file.clone());
|
||||
let new_successful_move_from = make_path_relative(successful_move.0.clone(), relative_file.clone());
|
||||
let new_successful_move_to = make_path_relative(successful_move.1.clone(), relative_file.clone());
|
||||
replace.push((new_successful_move_from, new_successful_move_to));
|
||||
}
|
||||
for i in replace {
|
||||
contents = contents.replace(
|
||||
&format!("({})", i.0.display()),
|
||||
&format!("({})", i.1.display()),
|
||||
);
|
||||
println!("{} : {} -> {}", file.display(), i.0.display(), i.1.display());
|
||||
contents = contents.replace(&format!("({})", i.0.display()), &format!("({})", i.1.display()));
|
||||
}
|
||||
write(file, contents).unwrap();
|
||||
}
|
||||
@ -142,16 +132,21 @@ fn make_path_relative(path: PathBuf, relative_to: PathBuf) -> PathBuf {
|
||||
loop {
|
||||
if path_components.len() <= subdirs {
|
||||
break;
|
||||
} else if path_components[subdirs] != relative_to_components[subdirs] {
|
||||
} else if path_components[subdirs]
|
||||
!= relative_to_components[subdirs]
|
||||
{
|
||||
break;
|
||||
}
|
||||
subdirs += 1;
|
||||
}
|
||||
let new_path = &path_components[subdirs..].iter().collect::<PathBuf>();
|
||||
let backouts = (0..relative_to_components.len() - subdirs - 1)
|
||||
.map(|_| PathBuf::from(".."))
|
||||
.reduce(|acc, e| acc.join(e))
|
||||
.unwrap_or(PathBuf::from(""));
|
||||
let new_path = &path_components[subdirs..]
|
||||
.iter()
|
||||
.collect::<PathBuf>();
|
||||
let backouts =
|
||||
(0..relative_to_components.len() - subdirs - 1)
|
||||
.map(|_| PathBuf::from(".."))
|
||||
.reduce(|acc, e| acc.join(e))
|
||||
.unwrap_or(PathBuf::from(""));
|
||||
//println!("{}, {}", relative_to_components.len() - subdirs - 1, backouts.display());
|
||||
let new_path = backouts.join(new_path);
|
||||
let new_path = if new_path
|
||||
@ -167,13 +162,12 @@ fn make_path_relative(path: PathBuf, relative_to: PathBuf) -> PathBuf {
|
||||
new_path
|
||||
};
|
||||
|
||||
let new_path = if new_path.file_name() == Some(OsStr::new("index.md"))
|
||||
|| new_path.file_name() == Some(OsStr::new("index.mdx"))
|
||||
{
|
||||
let new_path = if new_path.file_name() == Some(OsStr::new("index.md")) || new_path.file_name() == Some(OsStr::new("index.mdx")) {
|
||||
new_path.parent().unwrap().to_path_buf()
|
||||
} else {
|
||||
new_path
|
||||
};
|
||||
|
||||
|
||||
new_path
|
||||
}
|
||||
|
@ -19,17 +19,3 @@ pub fn read_migrate_file(file: PathBuf) -> anyhow::Result<Vec<(PathBuf, PathBuf)
|
||||
.collect::<Vec<_>>();
|
||||
Ok(migrations)
|
||||
}
|
||||
|
||||
pub fn read_migrate_file_left_side(file: PathBuf) -> anyhow::Result<Vec<PathBuf>> {
|
||||
let contents = read_to_string(file)?;
|
||||
let lines: Vec<String> = contents
|
||||
.split('\n')
|
||||
.map(|x| x.to_owned())
|
||||
.filter(|x| x != "")
|
||||
.collect();
|
||||
let migrations = lines
|
||||
.iter()
|
||||
.map(|x| x.split(" -> ").collect::<Vec<&str>>()[0].into())
|
||||
.collect::<Vec<_>>();
|
||||
Ok(migrations)
|
||||
}
|
||||
|
Reference in New Issue
Block a user