Skip to content

Commit

Permalink
Merge pull request #237 from moonbitlang/add-packages-json-for-moondoc
Browse files Browse the repository at this point in the history
add --packages-json for moondoc
  • Loading branch information
lijunchen authored Aug 30, 2024
2 parents 3335c65 + 5d1f1b3 commit 3d2ec03
Show file tree
Hide file tree
Showing 10 changed files with 111 additions and 11 deletions.
24 changes: 13 additions & 11 deletions crates/moon/src/cli/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
// For inquiries, you can contact us via e-mail at [email protected].

use anyhow::bail;
use moonbuild::dry_run::print_commands;
use mooncake::pkg::sync::auto_sync;
use moonutil::common::{
read_module_desc_file_in_dir, CargoPathExt, FileLock, MoonbuildOpt, MooncOpt, RunMode,
Expand Down Expand Up @@ -64,16 +65,6 @@ pub fn run_doc(cli: UniversalFlags, cmd: DocSubcommand) -> anyhow::Result<i32> {
let bind = cmd.bind;
let port = cmd.port;

if cli.dry_run {
println!(
"moondoc {} -o {}{}",
source_dir.display(),
static_dir.display(),
if serve { " -serve-mode" } else { "" }
);
return Ok(0);
}

let mod_desc = read_module_desc_file_in_dir(&source_dir)?;

let mut moonc_opt = MooncOpt::default();
Expand Down Expand Up @@ -105,7 +96,6 @@ pub fn run_doc(cli: UniversalFlags, cmd: DocSubcommand) -> anyhow::Result<i32> {
&moonc_opt,
&moonbuild_opt,
)?;
moonbuild::entry::run_check(&moonc_opt, &moonbuild_opt, &module)?;

let mut args = vec![
source_dir.display().to_string(),
Expand All @@ -115,10 +105,22 @@ pub fn run_doc(cli: UniversalFlags, cmd: DocSubcommand) -> anyhow::Result<i32> {
moonutil::moon_dir::core_bundle(moonc_opt.link_opt.target_backend)
.display()
.to_string(),
"-packages-json".to_string(),
moonbuild_opt
.target_dir
.join("packages.json")
.display()
.to_string(),
];
if serve {
args.push("-serve-mode".to_string())
}
if cli.dry_run {
print_commands(&module, &moonc_opt, &moonbuild_opt)?;
println!("moondoc {}", args.join(" "));
return Ok(0);
}
moonbuild::entry::run_check(&moonc_opt, &moonbuild_opt, &module)?;
let output = std::process::Command::new("moondoc").args(&args).output()?;
if output.status.code().unwrap() != 0 {
eprintln!("{}", String::from_utf8_lossy(&output.stderr));
Expand Down
67 changes: 67 additions & 0 deletions crates/moon/tests/test_cases/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5986,3 +5986,70 @@ fn test_js() {
"#]],
);
}

#[test]
fn test_moon_doc_dry_run() {
let dir = TestDir::new("moon_doc.in");
check(
&get_stdout_with_args_and_replace_dir(&dir, ["doc", "--dry-run"]),
expect![[r#"
moonc check ./src/lib/hello.mbt -o ./target/wasm-gc/release/check/lib/lib.mi -pkg username/hello/lib -std-path $MOON_HOME/lib/core/target/wasm-gc/release/bundle -pkg-sources username/hello/lib:./src/lib -target wasm-gc
moonc check ./src/main/main.mbt -o ./target/wasm-gc/release/check/main/main.mi -pkg username/hello/main -is-main -std-path $MOON_HOME/lib/core/target/wasm-gc/release/bundle -i ./target/wasm-gc/release/check/lib/lib.mi:lib -pkg-sources username/hello/main:./src/main -target wasm-gc
moonc check ./src/lib/hello_test.mbt -o ./target/wasm-gc/release/check/lib/lib.blackbox_test.mi -pkg username/hello/lib_blackbox_test -std-path $MOON_HOME/lib/core/target/wasm-gc/release/bundle -i ./target/wasm-gc/release/check/lib/lib.mi:lib -pkg-sources username/hello/lib_blackbox_test:./src/lib -target wasm-gc
moondoc $ROOT -o $ROOT/target/doc -std-path $MOON_HOME/lib/core/target/wasm-gc/release/bundle -packages-json $ROOT/target/wasm-gc/release/check/packages.json
"#]],
);
}

#[test]
#[ignore = "wait for the latest release of moonc"]
fn test_moon_doc() {
let dir = TestDir::new("moon_doc.in");
let _ = get_stderr_on_success_with_args_and_replace_dir(&dir, ["doc"]);
check(
&read(&dir.join("target/doc/username/hello/lib/members.md")),
expect![[r##"
# username/hello/lib
* * *
# Index
|Value|Description|
|---|---|
|[hello](#hello)||
* * *
# Value and Function
## hello
```moonbit
:::source,username/hello/lib/hello.mbt,1:::pub fn hello() -> <a href="moonbitlang/predef#string">String</a>
```
"##]],
);
check(
&read(&dir.join("target/doc/username/hello/main/members.md")),
expect![[r#"
# username/hello/main
* * *
# Index
"#]],
);
check(
&read(&dir.join("target/doc/username/hello/_sidebar.md")),
expect![[r#"
- [Contents](username/hello/)
- Packages
- [username/hello/lib](username/hello/lib/members)
- [username/hello/main](username/hello/main/members)
- Dependencies
- [moonbitlang/core](moonbitlang/core/)
"#]],
);
}
2 changes: 2 additions & 0 deletions crates/moon/tests/test_cases/moon_doc.in/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
target/
.mooncakes/
1 change: 1 addition & 0 deletions crates/moon/tests/test_cases/moon_doc.in/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# username/hello
10 changes: 10 additions & 0 deletions crates/moon/tests/test_cases/moon_doc.in/moon.mod.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "username/hello",
"version": "0.1.0",
"readme": "README.md",
"repository": "",
"license": "",
"keywords": [],
"description": "",
"source": "src"
}
3 changes: 3 additions & 0 deletions crates/moon/tests/test_cases/moon_doc.in/src/lib/hello.mbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub fn hello() -> String {
"Hello, world!"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
test "hello" {
if @lib.hello() != "Hello, world!" {
fail!("@lib.hello() != \"Hello, world!\"")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
3 changes: 3 additions & 0 deletions crates/moon/tests/test_cases/moon_doc.in/src/main/main.mbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main {
println(@lib.hello())
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"is-main": true,
"import": [
"username/hello/lib"
]
}

0 comments on commit 3d2ec03

Please sign in to comment.