Skip to content

Commit 34b74f3

Browse files
committed
add docs command
1 parent 0f0daec commit 34b74f3

File tree

2 files changed

+64
-38
lines changed

2 files changed

+64
-38
lines changed

.github/workflows/bevy_mod_scripting.yml

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -55,24 +55,4 @@ jobs:
5555
command: xtest ci-check
5656
components: clippy, rustfmt, grcov
5757
target: ${{ matrix.run_args.cross }}
58-
args: --workspace --features=lua54,rhai,rune --profile=ephemeral-build
59-
# docs:
60-
# name: Docs
61-
# runs-on: ubuntu-latest
62-
# steps:
63-
# - name: Install alsa and udev
64-
# run: sudo apt-get update; sudo apt-get install --no-install-recommends libasound2-dev libudev-dev
65-
# - uses: actions/checkout@v2
66-
# - uses: actions-rs/toolchain@v1
67-
# with:
68-
# toolchain: stable
69-
# override: true
70-
# - name: Rust Cache
71-
# uses: Swatinem/rust-cache@v2.7.3
72-
# - name: Find docs.rs features
73-
# run: echo "DOCS_FEATURES=$(cargo metadata --no-deps | python -c "import sys,json; [print(','.join(x['metadata']['docs.rs']['features'])) for x in json.load(sys.stdin)['packages'] if x['name'] == 'bevy_mod_scripting']")" >> $GITHUB_OUTPUT
74-
# id: features
75-
# - uses: actions-rs/cargo@v1
76-
# with:
77-
# command: doc
78-
# args: --workspace --features=${{ steps.features.outputs.DOCS_FEATURES }} --profile=ephemeral-build
58+
args: --workspace --features=lua54,rhai,rune --profile=ephemeral-build

crates/xtask/src/main.rs

Lines changed: 63 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,12 @@ enum Xtasks {
178178
/// Build the main workspace, apply all prefferred lints
179179
Check,
180180
/// Build the rust crates.io docs as well as any other docs
181-
Docs,
181+
Docs {
182+
/// Open in browser
183+
/// This will open the generated docs in the default browser
184+
#[clap(long, short)]
185+
open: bool,
186+
},
182187
/// Build the main workspace, and then run all tests
183188
Test,
184189
/// Perform a full check as it would be done in CI
@@ -190,7 +195,7 @@ impl Xtasks {
190195
match self {
191196
Xtasks::Build => Self::build(features),
192197
Xtasks::Check => Self::check(features),
193-
Xtasks::Docs => Self::docs(),
198+
Xtasks::Docs { open } => Self::docs(open),
194199
Xtasks::Test => Self::test(features),
195200
Xtasks::CiCheck => Self::cicd(),
196201
Xtasks::Init => Self::init(),
@@ -212,22 +217,35 @@ impl Xtasks {
212217
Ok(workspace_root.into())
213218
}
214219

220+
fn relative_workspace_dir<P: AsRef<Path>>(dir: P) -> Result<std::path::PathBuf> {
221+
let workspace_dir = Self::workspace_dir()?;
222+
Ok(workspace_dir.join(dir))
223+
}
224+
215225
fn run_system_command<I: IntoIterator<Item = impl AsRef<OsStr>>>(
216226
command: &str,
217227
context: &str,
218228
add_args: I,
229+
dir: Option<&Path>,
219230
) -> Result<()> {
220231
info!("Running system command: {}", command);
221232

233+
let working_dir = match dir {
234+
Some(d) => Self::relative_workspace_dir(d)?,
235+
None => Self::workspace_dir()?,
236+
};
237+
222238
let mut cmd = Command::new(command);
223239
cmd.args(add_args)
224240
.stdout(std::process::Stdio::inherit())
225241
.stderr(std::process::Stdio::inherit())
226-
.current_dir(Self::workspace_dir()?);
242+
.current_dir(working_dir);
227243

228244
info!("Using command: {:?}", cmd);
229245

230-
let output = cmd.output().with_context(|| context.to_owned())?;
246+
let output = cmd.output();
247+
info!("Command output: {:?}", output);
248+
let output = output.with_context(|| context.to_owned())?;
231249
match output.status.code() {
232250
Some(0) => Ok(()),
233251
_ => bail!(
@@ -257,17 +275,17 @@ impl Xtasks {
257275
.expect("invalid command argument")
258276
.to_owned()
259277
}));
260-
let workspace_dir = Self::workspace_dir()?;
261-
let workspace_dir = match dir {
262-
Some(d) => workspace_dir.join(d),
263-
None => workspace_dir,
278+
279+
let working_dir = match dir {
280+
Some(d) => Self::relative_workspace_dir(d)?,
281+
None => Self::workspace_dir()?,
264282
};
265283

266284
let mut cmd = Command::new("cargo");
267285
cmd.args(args)
268286
.stdout(std::process::Stdio::inherit())
269287
.stderr(std::process::Stdio::inherit())
270-
.current_dir(workspace_dir);
288+
.current_dir(working_dir);
271289

272290
info!("Using command: {:?}", cmd);
273291

@@ -309,21 +327,37 @@ impl Xtasks {
309327
"cargo",
310328
"Failed to run cargo fmt",
311329
vec!["fmt", "--all", "--", "--check"],
330+
None,
312331
)?;
313332

314333
Ok(())
315334
}
316335

317-
fn docs() -> Result<()> {
336+
fn docs(open: bool) -> Result<()> {
318337
// find [package.metadata."docs.rs"] key in Cargo.toml
319338
let metadata = Self::cargo_metadata()?;
320-
let package = metadata.root_package().expect("no root package");
339+
340+
let package = metadata
341+
.packages
342+
.iter()
343+
.find(|p| p.name == "bevy_mod_scripting")
344+
.expect("Could not find bevy_mod_scripting package in metadata");
345+
346+
info!("Building with root package: {}", package.name);
347+
321348
let docs_rs = package
322349
.metadata
323350
.get("docs.rs")
324351
.expect("no docs.rs metadata");
325352

326-
let string_list = docs_rs
353+
let features = docs_rs
354+
.as_object()
355+
.expect("docs.rs metadata is not an object")
356+
.get("features")
357+
.expect("no 'features' in docs.rs metadata");
358+
359+
info!("Using docs.rs metadata: {:?}", docs_rs);
360+
let string_list = features
327361
.as_array()
328362
.expect("docs.rs metadata is not an array")
329363
.iter()
@@ -333,22 +367,29 @@ impl Xtasks {
333367

334368
let features = Features(string_list);
335369

370+
let mut args = Vec::default();
371+
args.push("--all");
372+
if open {
373+
args.push("--open");
374+
}
336375
Self::run_workspace_command(
337376
"doc",
338377
"Failed to build crates.io docs",
339378
features.clone(),
340-
vec!["--all"],
379+
args,
341380
None,
342381
)?;
343382

344383
// build mdbook
345-
Self::run_workspace_command(
384+
let args = if open { vec!["build"] } else { vec!["serve"] };
385+
386+
Self::run_system_command(
346387
"mdbook",
347-
"Failed to build mdbook docs",
348-
features,
349-
vec!["--all"],
388+
"Failed to build or serve mdbook docs",
389+
args,
350390
Some(Path::new("docs")),
351391
)?;
392+
352393
Ok(())
353394
}
354395

@@ -395,6 +436,7 @@ impl Xtasks {
395436
"-o",
396437
"target/coverage/html",
397438
],
439+
None,
398440
)?;
399441

400442
Self::run_system_command(
@@ -417,6 +459,7 @@ impl Xtasks {
417459
"-o",
418460
"target/coverage/lcov.info",
419461
],
462+
None,
420463
)
421464
}
422465

@@ -454,6 +497,9 @@ impl Xtasks {
454497
let all_features = Features::all_features();
455498
Self::check(all_features.clone())?;
456499

500+
// run docs
501+
Self::docs(false)?;
502+
457503
// run tests
458504
Self::test(all_features)?;
459505

0 commit comments

Comments
 (0)