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

Using placeholder rustdoc generation for current crate #351

50 changes: 24 additions & 26 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ mod util;
use itertools::Itertools;
use rustdoc_cmd::RustdocCommand;
use semver::Version;
use std::path::{Path, PathBuf};
use std::path::PathBuf;

use clap::{Args, Parser, Subcommand};
use trustfall_rustdoc::{load_rustdoc, VersionedCrate};
Expand Down Expand Up @@ -91,7 +91,7 @@ fn main() -> anyhow::Result<()> {
Some(SemverChecksCommands::CheckRelease(args)) => {
let mut config = GlobalConfig::new().set_level(args.verbosity.log_level());

let loader: Box<dyn rustdoc_gen::RustdocGenerator> =
let baseline_loader: Box<dyn rustdoc_gen::RustdocGenerator> =
if let Some(path) = args.baseline_rustdoc.as_deref() {
Box::new(rustdoc_gen::RustdocFromFile::new(path.to_owned()))
} else if let Some(root) = args.baseline_root.as_deref() {
Expand Down Expand Up @@ -132,11 +132,13 @@ fn main() -> anyhow::Result<()> {
{
let name = "<unknown>";
let baseline_highest_allowed_version = None;
let current_loader =
rustdoc_gen::RustdocFromFile::new(current_rustdoc_path.to_path_buf());
let (current_crate, baseline_crate) = generate_versioned_crates(
&mut config,
CurrentCratePath::CurrentRustdocPath(current_rustdoc_path),
&*loader,
&rustdoc_cmd,
&current_loader,
&*baseline_loader,
name,
baseline_highest_allowed_version,
)?;
Expand All @@ -163,16 +165,16 @@ fn main() -> anyhow::Result<()> {
})?;
Ok(true)
} else {
config.shell_status(
"Parsing",
format_args!("{crate_name} v{current_version} (current)"),
let target = metadata.target_directory.as_std_path().join(util::SCOPE);
let current_loader = rustdoc_gen::RustdocFromProjectRoot::new(
&manifest::get_project_dir_from_manifest_path(manifest_path)?,
&target,
)?;

let (current_crate, baseline_crate) = generate_versioned_crates(
&mut config,
CurrentCratePath::ManifestPath(manifest_path),
&*loader,
&rustdoc_cmd,
&current_loader,
&*baseline_loader,
crate_name,
Some(current_version),
)?;
Expand Down Expand Up @@ -203,34 +205,30 @@ fn main() -> anyhow::Result<()> {
}
}

// Argument to the generate_versioned_crates function.
enum CurrentCratePath<'a> {
CurrentRustdocPath(&'a Path), // If rustdoc is passed, it is just loaded into the memory.
ManifestPath(&'a Path), // Otherwise, the function generates the rustdoc.
}

fn generate_versioned_crates(
config: &mut GlobalConfig,
current_crate_path: CurrentCratePath,
loader: &dyn rustdoc_gen::RustdocGenerator,
rustdoc_cmd: &RustdocCommand,
current_loader: &dyn rustdoc_gen::RustdocGenerator,
baseline_loader: &dyn rustdoc_gen::RustdocGenerator,
crate_name: &str,
version: Option<&Version>,
) -> anyhow::Result<(VersionedCrate, VersionedCrate)> {
let current_crate = match current_crate_path {
CurrentCratePath::CurrentRustdocPath(rustdoc_path) => load_rustdoc(rustdoc_path)?,
CurrentCratePath::ManifestPath(manifest_path) => {
let rustdoc_path = rustdoc_cmd.dump(manifest_path, None, true)?;
load_rustdoc(&rustdoc_path)?
}
};
let current_path = current_loader.load_rustdoc(
config,
rustdoc_cmd,
rustdoc_gen::CrateDataForRustdoc {
name: crate_name,
crate_type: rustdoc_gen::CrateType::Current,
},
)?;
let current_crate = load_rustdoc(&current_path)?;

// The process of generating baseline rustdoc can overwrite
// the already-generated rustdoc of the current crate.
// For example, this happens when target-dir is specified in `.cargo/config.toml`.
// That's the reason why we're immediately loading the rustdocs into memory.
// See: https://github.com/obi1kenobi/cargo-semver-checks/issues/269
let baseline_path = loader.load_rustdoc(
let baseline_path = baseline_loader.load_rustdoc(
config,
rustdoc_cmd,
rustdoc_gen::CrateDataForRustdoc {
Expand Down
16 changes: 16 additions & 0 deletions src/manifest.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use anyhow::Context as _;
tonowak marked this conversation as resolved.
Show resolved Hide resolved

#[derive(Debug, Clone)]
pub(crate) struct Manifest {
pub(crate) path: std::path::PathBuf,
Expand Down Expand Up @@ -67,3 +69,17 @@ pub(crate) fn get_first_bin_target_name(manifest: &Manifest) -> anyhow::Result<S
// the same name as the package but with dashes replaced with underscores.
Ok(get_package_name(manifest)?.replace('-', "_"))
}

pub(crate) fn get_project_dir_from_manifest_path(
manifest_path: &std::path::Path,
) -> anyhow::Result<std::path::PathBuf> {
assert!(
manifest_path.ends_with("Cargo.toml"),
"path {} isn't pointing to a manifest",
manifest_path.display()
);
let dir_path = manifest_path
.parent()
.context("manifest path doesn't have a parent")?;
Ok(dir_path.to_path_buf())
}
11 changes: 2 additions & 9 deletions src/rustdoc_gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,8 @@ fn create_placeholder_rustdoc_manifest(
},
CrateSource::ManifestPath { manifest } => DependencyDetail {
path: Some({
assert!(
manifest.path.ends_with("Cargo.toml"),
"path {} isn't pointing to a manifest",
manifest.path.display()
);
let dir_path = manifest
.path
.parent()
.context("manifest path doesn't have a parent")?;
let dir_path =
crate::manifest::get_project_dir_from_manifest_path(&manifest.path)?;
// The manifest will be saved in some other directory,
// so for convenience, we're using absolute paths.
dir_path
Expand Down