mirror of
https://github.com/roc-lang/roc.git
synced 2025-12-11 11:32:28 +00:00
Merge pull request #7154 from DavidEdwards1/allow-check-on-markdown
Initial Sketch of roc check on Markdown
This commit is contained in:
commit
837dc7b7cc
3 changed files with 126 additions and 18 deletions
|
|
@ -18,10 +18,12 @@ use roc_load::{FunctionKind, LoadingProblem, Threading};
|
||||||
use roc_packaging::cache::{self, RocCacheDir};
|
use roc_packaging::cache::{self, RocCacheDir};
|
||||||
use roc_target::Target;
|
use roc_target::Target;
|
||||||
use std::fs::{self, FileType};
|
use std::fs::{self, FileType};
|
||||||
|
use std::io::BufRead;
|
||||||
use std::io::{self, Read, Write};
|
use std::io::{self, Read, Write};
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
use target_lexicon::Triple;
|
use target_lexicon::Triple;
|
||||||
|
use tempfile::Builder;
|
||||||
|
|
||||||
#[global_allocator]
|
#[global_allocator]
|
||||||
static ALLOC: mimalloc::MiMalloc = mimalloc::MiMalloc;
|
static ALLOC: mimalloc::MiMalloc = mimalloc::MiMalloc;
|
||||||
|
|
@ -217,26 +219,89 @@ fn main() -> io::Result<()> {
|
||||||
|
|
||||||
let opt_main_path = matches.get_one::<PathBuf>(FLAG_MAIN);
|
let opt_main_path = matches.get_one::<PathBuf>(FLAG_MAIN);
|
||||||
|
|
||||||
match check_file(
|
match roc_file_path.extension().and_then(OsStr::to_str) {
|
||||||
&arena,
|
Some("md") => {
|
||||||
roc_file_path.to_owned(),
|
// Extract the blocks of roc code
|
||||||
opt_main_path.cloned(),
|
let file = fs::File::open(roc_file_path.as_path())?;
|
||||||
emit_timings,
|
let markdown_file_reader = io::BufReader::new(file);
|
||||||
RocCacheDir::Persistent(cache::roc_cache_packages_dir().as_path()),
|
let mut roc_blocks: Vec<String> = Vec::new();
|
||||||
threading,
|
let mut in_roc_block: bool = false;
|
||||||
) {
|
let mut current_block = String::new();
|
||||||
Ok((problems, total_time)) => {
|
|
||||||
problems.print_error_warning_count(total_time);
|
|
||||||
Ok(problems.exit_code())
|
|
||||||
}
|
|
||||||
|
|
||||||
Err(LoadingProblem::FormattedReport(report)) => {
|
for line in markdown_file_reader.lines() {
|
||||||
print!("{report}");
|
let line = line.unwrap();
|
||||||
|
if line == "```roc" {
|
||||||
|
in_roc_block = true;
|
||||||
|
} else if (line == "```") & in_roc_block {
|
||||||
|
in_roc_block = false;
|
||||||
|
roc_blocks.push(current_block);
|
||||||
|
current_block = String::new();
|
||||||
|
} else if in_roc_block {
|
||||||
|
current_block.push_str(&line);
|
||||||
|
current_block.push_str("\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Ok(1)
|
// now check each block, we exit early if any single block does not check
|
||||||
|
let mut exit_code = 0;
|
||||||
|
|
||||||
|
for block in roc_blocks.iter() {
|
||||||
|
let mut file = Builder::new().suffix(".roc").tempfile()?;
|
||||||
|
write!(file, "{}", block)?;
|
||||||
|
|
||||||
|
match check_file(
|
||||||
|
&arena,
|
||||||
|
file.path().to_owned(),
|
||||||
|
opt_main_path.cloned(),
|
||||||
|
emit_timings,
|
||||||
|
RocCacheDir::Persistent(cache::roc_cache_packages_dir().as_path()),
|
||||||
|
threading,
|
||||||
|
) {
|
||||||
|
Ok((problems, total_time)) => {
|
||||||
|
problems.print_error_warning_count(total_time);
|
||||||
|
exit_code = problems.exit_code();
|
||||||
|
}
|
||||||
|
|
||||||
|
Err(LoadingProblem::FormattedReport(report)) => {
|
||||||
|
print!("{report}");
|
||||||
|
|
||||||
|
exit_code = 1;
|
||||||
|
}
|
||||||
|
Err(other) => {
|
||||||
|
panic!("build_file failed with error:\n{other:?}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if exit_code != 0 {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(exit_code)
|
||||||
}
|
}
|
||||||
Err(other) => {
|
_ => {
|
||||||
panic!("build_file failed with error:\n{other:?}");
|
match check_file(
|
||||||
|
&arena,
|
||||||
|
roc_file_path.to_owned(),
|
||||||
|
opt_main_path.cloned(),
|
||||||
|
emit_timings,
|
||||||
|
RocCacheDir::Persistent(cache::roc_cache_packages_dir().as_path()),
|
||||||
|
threading,
|
||||||
|
) {
|
||||||
|
Ok((problems, total_time)) => {
|
||||||
|
problems.print_error_warning_count(total_time);
|
||||||
|
Ok(problems.exit_code())
|
||||||
|
}
|
||||||
|
|
||||||
|
Err(LoadingProblem::FormattedReport(report)) => {
|
||||||
|
print!("{report}");
|
||||||
|
|
||||||
|
Ok(1)
|
||||||
|
}
|
||||||
|
Err(other) => {
|
||||||
|
panic!("build_file failed with error:\n{other:?}");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
35
crates/cli/tests/cli/form.md
Normal file
35
crates/cli/tests/cli/form.md
Normal file
|
|
@ -0,0 +1,35 @@
|
||||||
|
# A Simple Markdown Example
|
||||||
|
|
||||||
|
This file contains `form.roc` embedded as a block in Markdown. It lets us test that `roc check` works with Markdown.
|
||||||
|
|
||||||
|
```roc
|
||||||
|
app [main] { pf: platform "https://github.com/roc-lang/basic-cli/releases/download/0.15.0/SlwdbJ-3GR7uBWQo6zlmYWNYOxnvo8r6YABXD-45UOw.tar.br" }
|
||||||
|
|
||||||
|
import pf.Stdin
|
||||||
|
import pf.Stdout
|
||||||
|
|
||||||
|
main =
|
||||||
|
Stdout.line! "What's your first name?"
|
||||||
|
firstName = Stdin.line!
|
||||||
|
Stdout.line! "What's your last name?"
|
||||||
|
lastName = Stdin.line!
|
||||||
|
|
||||||
|
Stdout.line "Hi, $(firstName) $(lastName)! 👋"
|
||||||
|
```
|
||||||
|
|
||||||
|
Excitingly, we can have another block of Roc code as well! (In this case it is the same one...)
|
||||||
|
|
||||||
|
```roc
|
||||||
|
app [main] { pf: platform "https://github.com/roc-lang/basic-cli/releases/download/0.15.0/SlwdbJ-3GR7uBWQo6zlmYWNYOxnvo8r6YABXD-45UOw.tar.br" }
|
||||||
|
|
||||||
|
import pf.Stdin
|
||||||
|
import pf.Stdout
|
||||||
|
|
||||||
|
main =
|
||||||
|
Stdout.line! "What's your first name?"
|
||||||
|
firstName = Stdin.line!
|
||||||
|
Stdout.line! "What's your last name?"
|
||||||
|
lastName = Stdin.line!
|
||||||
|
|
||||||
|
Stdout.line "Hi, $(firstName) $(lastName)! 👋"
|
||||||
|
```
|
||||||
|
|
@ -1078,7 +1078,15 @@ mod cli_run {
|
||||||
fn cli_form_check() {
|
fn cli_form_check() {
|
||||||
let path = file_path_from_root("crates/cli/tests/cli", "form.roc");
|
let path = file_path_from_root("crates/cli/tests/cli", "form.roc");
|
||||||
let out = run_roc([CMD_CHECK, path.to_str().unwrap()], &[], &[]);
|
let out = run_roc([CMD_CHECK, path.to_str().unwrap()], &[], &[]);
|
||||||
dbg!(out.stdout, out.stderr);
|
assert_valid_roc_check_status(out.status);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[cfg_attr(windows, ignore)]
|
||||||
|
#[serial(cli_platform)]
|
||||||
|
fn cli_form_markdown_check() {
|
||||||
|
let path = file_path_from_root("crates/cli/tests/cli", "form.md");
|
||||||
|
let out = run_roc([CMD_CHECK, path.to_str().unwrap()], &[], &[]);
|
||||||
assert_valid_roc_check_status(out.status);
|
assert_valid_roc_check_status(out.status);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue