Merge pull request #7154 from DavidEdwards1/allow-check-on-markdown

Initial Sketch of roc check on Markdown
This commit is contained in:
Anton-4 2024-10-15 13:54:09 +02:00 committed by GitHub
commit 837dc7b7cc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 126 additions and 18 deletions

View file

@ -18,10 +18,12 @@ use roc_load::{FunctionKind, LoadingProblem, Threading};
use roc_packaging::cache::{self, RocCacheDir};
use roc_target::Target;
use std::fs::{self, FileType};
use std::io::BufRead;
use std::io::{self, Read, Write};
use std::path::{Path, PathBuf};
use std::str::FromStr;
use target_lexicon::Triple;
use tempfile::Builder;
#[global_allocator]
static ALLOC: mimalloc::MiMalloc = mimalloc::MiMalloc;
@ -217,26 +219,89 @@ fn main() -> io::Result<()> {
let opt_main_path = matches.get_one::<PathBuf>(FLAG_MAIN);
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())
}
match roc_file_path.extension().and_then(OsStr::to_str) {
Some("md") => {
// Extract the blocks of roc code
let file = fs::File::open(roc_file_path.as_path())?;
let markdown_file_reader = io::BufReader::new(file);
let mut roc_blocks: Vec<String> = Vec::new();
let mut in_roc_block: bool = false;
let mut current_block = String::new();
Err(LoadingProblem::FormattedReport(report)) => {
print!("{report}");
for line in markdown_file_reader.lines() {
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:?}");
}
}
}
}
}

View 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)! 👋"
```

View file

@ -1078,7 +1078,15 @@ mod cli_run {
fn cli_form_check() {
let path = file_path_from_root("crates/cli/tests/cli", "form.roc");
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);
}