Skip to content

Commit

Permalink
Checkout and init git repositories for books
Browse files Browse the repository at this point in the history
  • Loading branch information
Coi-l committed Mar 26, 2024
1 parent 1ae3973 commit 2e32b69
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 38 deletions.
96 changes: 96 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ tempfile = "3.4.0"
toml = "0.5.11" # Do not update, see https://github.com/rust-lang/mdBook/issues/2037
topological-sort = "0.2.2"
resolve-path = "0.1.0"
git2 = "0.18.3"

# Watch feature
notify = { version = "6.1.1", optional = true }
Expand Down
52 changes: 34 additions & 18 deletions src/cmd/shelf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,19 @@ pub fn make_subcommand() -> Command {
Command::new("shelf").about("Build a bookshelf from shelf.toml file")
}

fn build_book(path: &str) -> Result<()> {
fn process_book(path: &str, index_file: &mut File, summary: &mut File) -> Result<()> {
let book_dir = path.try_resolve()?;
let book_dir = std::fs::canonicalize(book_dir)?;
println!("{book_dir:?}");
let mut book = MDBook::load(book_dir)?;

// Build book
let mut path = current_dir()?;
let title = book.config.book.title.clone().unwrap();
path.push("books/");
path.push(title);
book.config.build.build_dir = path;
book.config.book.shelf_url = Some(PathBuf::from(r"../../shelf/book/index.html"));
book.build()?;
Ok(())
}

fn process_book(path: &str, index_file: &mut File, summary: &mut File) -> Result<()> {
let book_dir = path.try_resolve()?;
let book_dir = std::fs::canonicalize(book_dir)?;
let book = MDBook::load(book_dir)?;

// Create post in index file
let title = book.config.book.title.unwrap_or_default();
Expand Down Expand Up @@ -62,12 +56,7 @@ pub fn execute(_args: &ArgMatches) -> Result<()> {
let mut contents = String::new();
file.read_to_string(&mut contents)?;
let shelf: Shelf = toml::from_str(&contents)?;
if let Some(local) = &shelf.book.local {
for sb in local {
println!("{:?}", sb);
build_book(&sb.path)?;
}
}

let _ = std::fs::remove_dir_all("shelf");
let _ = MDBook::init("shelf").create_gitignore(false).build();

Expand All @@ -79,10 +68,37 @@ pub fn execute(_args: &ArgMatches) -> Result<()> {

writeln!(index_file, "# Bookshelf")?;
writeln!(index_file)?;
if let Some(local) = &shelf.book.local {
for sb in local {

for sb in &shelf.book {
if let Some(url) = &sb.git_url {
println!("{:?}", sb);
process_book(&sb.path, &mut index_file, &mut summary)?
let path = sb.path.clone().unwrap_or("root".to_owned());
let repo_raw_name = url.split('/').last().unwrap_or(&path);
let repo_name = format!("{repo_raw_name}-{path}");
let mut checkout_path = PathBuf::from("repositories");
checkout_path.push(repo_name);

let book_path = if let Some(path) = &sb.path {
let mut bp = checkout_path.clone();
bp.push(path);
bp
} else {
checkout_path.clone()
};

let _repo = match git2::Repository::open(&checkout_path) {
Ok(repo) => repo,
Err(_) => match git2::Repository::clone(&url, &checkout_path) {
Ok(repo) => repo,
Err(e) => panic!("failed to clone: {}", e),
},
};

process_book(&book_path.to_str().unwrap(), &mut index_file, &mut summary)?
} else if let Some(path) = &sb.path {
process_book(path, &mut index_file, &mut summary)?
} else {
warn!("Neither path or git specified. Invalid book");
}
}

Expand Down
26 changes: 6 additions & 20 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -640,33 +640,19 @@ impl HtmlConfig {
#[derive(Deserialize, Debug)]
/// Represents a book in a shelf
pub struct ShelfBook {
/// local book
pub local: Option<Vec<ShelfLocalBook>>,
/// git book
pub git: Option<Vec<ShelfGitBook>>,
}

#[derive(Deserialize, Debug)]
/// Properties for defining a git based mdbook for a shelf
pub struct ShelfGitBook {
/// url to the git
pub url: String,
/// Optional path inside the git where to find the book
/// Path to filesystem local book
/// or if git_url is specified, the path inside the git
/// where the book is located
pub path: Option<String>,
}

#[derive(Deserialize, Debug)]
/// Properties for a book found on disk
pub struct ShelfLocalBook {
/// Path to the book
pub path: String,
/// git url
pub git_url: Option<String>,
}

#[derive(Deserialize, Debug)]
/// Represents a shelf that contains a lot of books
pub struct Shelf {
/// The books in the shelf
pub book: ShelfBook,
pub book: Vec<ShelfBook>,
}

/// Configuration for how to render the print icon, print.html, and print.css.
Expand Down

0 comments on commit 2e32b69

Please sign in to comment.