Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build local #14

Merged
merged 2 commits into from
Oct 15, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions src/crate_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::{Args, Result};
use std::env;
use std::fs::read_to_string;
use std::path::Path;
use std::process::Command;

enum CrateSource {
Std,
Expand All @@ -12,10 +13,7 @@ enum CrateSource {

impl CrateSource {
fn is_local(&self) -> bool {
match self {
&CrateSource::Local(_) => true,
_ => false,
}
matches!(self, &CrateSource::Local(_))
}
}

Expand Down Expand Up @@ -78,7 +76,7 @@ fn make_url(crate_info: &CrateInfo) -> String {
let base = if let Some(version) = &crate_info.version {
format!("https://doc.rust-lang.org/{}/std/", version)
} else {
format!("https://doc.rust-lang.org/stable/std/")
"https://doc.rust-lang.org/stable/std/".to_string()
};

if let Some(ref query) = crate_info.query {
Expand Down Expand Up @@ -141,7 +139,18 @@ pub fn open(crate_info: CrateInfo) -> Result<()> {
}
Err(e) => {
if crate_info.source.is_local() && !is_locally_available(&url) {
println!("The crate is not available locally");
println!("Trying to build the documentation locally...");
let build_local_doc = Command::new("cargo").arg("doc").status();
match build_local_doc {
Ok(status) => {
if status.success() && is_locally_available(&url) {
return open(crate_info);
} else {
println!("The crate is not available locally")
}
}
Err(build_error) => println!("Oh no {}!", build_error),
}
} else {
println!("Seems like you've lost your way, 学生, try again.");
}
Expand All @@ -156,14 +165,14 @@ fn get_manifest_version(name: &str) -> std::io::Result<String> {
let toml = std::env::current_dir()?.join("Cargo.toml");
let version: String = read_to_string(toml)?
.lines()
.filter(|l| l.replace(" ", "").contains(format!("{}=", name).as_str()))
.filter(|l| l.replace(' ', "").contains(format!("{}=", name).as_str()))
.collect();
Ok(version.trim_matches(|c: char| !c.is_numeric()).to_string())
}

/// Converts the first letter of a crate's name to upper case.
fn first_letter_to_upper(c: &str) -> String {
if c.len() > 0 {
if !c.is_empty() {
format!("{}{}", &c[0..1].to_uppercase(), &c[1..])
} else {
c.to_string()
Expand Down
Loading