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

rustdoc: auto create output directory when "--output-format json" #93099

Merged
merged 1 commit into from
Jan 21, 2022
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
12 changes: 8 additions & 4 deletions src/librustdoc/json/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
mod conversions;

use std::cell::RefCell;
use std::fs::File;
use std::fs::{create_dir_all, File};
use std::path::PathBuf;
use std::rc::Rc;

Expand All @@ -18,13 +18,14 @@ use rustc_session::Session;

use rustdoc_json_types as types;

use crate::clean;
use crate::clean::types::{ExternalCrate, ExternalLocation};
use crate::config::RenderOptions;
use crate::docfs::PathError;
use crate::error::Error;
use crate::formats::cache::Cache;
use crate::formats::FormatRenderer;
use crate::json::conversions::{from_item_id, IntoWithTcx};
use crate::{clean, try_err};

#[derive(Clone)]
crate struct JsonRenderer<'tcx> {
Expand Down Expand Up @@ -256,10 +257,13 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> {
.collect(),
format_version: types::FORMAT_VERSION,
};
let mut p = self.out_path.clone();
let out_dir = self.out_path.clone();
try_err!(create_dir_all(&out_dir), out_dir);

let mut p = out_dir;
p.push(output.index.get(&output.root).unwrap().name.clone().unwrap());
p.set_extension("json");
let file = File::create(&p).map_err(|error| Error { error: error.to_string(), file: p })?;
let file = try_err!(File::create(&p), p);
serde_json::ser::to_writer(&file, &output).unwrap();
Ok(())
}
Expand Down