mirror of
https://github.com/rust-lang/rustlings.git
synced 2025-12-01 22:51:45 +02:00
Make cargo run work
This commit is contained in:
56
src/bin/gen-dev-cargo-toml.rs
Normal file
56
src/bin/gen-dev-cargo-toml.rs
Normal file
@@ -0,0 +1,56 @@
|
||||
use anyhow::{bail, Context, Result};
|
||||
use serde::Deserialize;
|
||||
use std::{
|
||||
fs::{self, create_dir},
|
||||
io::ErrorKind,
|
||||
};
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct Exercise {
|
||||
name: String,
|
||||
path: String,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct InfoToml {
|
||||
exercises: Vec<Exercise>,
|
||||
}
|
||||
|
||||
fn main() -> Result<()> {
|
||||
let exercises = toml_edit::de::from_str::<InfoToml>(
|
||||
&fs::read_to_string("info.toml").context("Failed to read `info.toml`")?,
|
||||
)
|
||||
.context("Failed to deserialize `info.toml`")?
|
||||
.exercises;
|
||||
|
||||
let mut buf = Vec::with_capacity(1 << 14);
|
||||
|
||||
buf.extend_from_slice(b"bin = [\n");
|
||||
|
||||
for exercise in exercises {
|
||||
buf.extend_from_slice(b" { name = \"");
|
||||
buf.extend_from_slice(exercise.name.as_bytes());
|
||||
buf.extend_from_slice(b"\", path = \"../");
|
||||
buf.extend_from_slice(exercise.path.as_bytes());
|
||||
buf.extend_from_slice(b"\" },\n");
|
||||
}
|
||||
|
||||
buf.extend_from_slice(
|
||||
br#"]
|
||||
|
||||
[package]
|
||||
name = "rustlings"
|
||||
version = "0.0.0"
|
||||
edition = "2021"
|
||||
publish = false
|
||||
"#,
|
||||
);
|
||||
|
||||
if let Err(e) = create_dir("dev") {
|
||||
if e.kind() != ErrorKind::AlreadyExists {
|
||||
bail!("Failed to create the `dev` directory: {e}");
|
||||
}
|
||||
}
|
||||
|
||||
fs::write("dev/Cargo.toml", buf).context("Failed to write `dev/Cargo.toml`")
|
||||
}
|
||||
@@ -91,9 +91,17 @@ pub struct ContextLine {
|
||||
|
||||
impl Exercise {
|
||||
fn cargo_cmd(&self, command: &str, args: &[&str]) -> Result<Output> {
|
||||
Command::new("cargo")
|
||||
.arg(command)
|
||||
.arg("--color")
|
||||
let mut cmd = Command::new("cargo");
|
||||
cmd.arg(command);
|
||||
|
||||
// A hack to make `cargo run` work when developing Rustlings.
|
||||
// Use `dev/Cargo.toml` when in the directory of the repository.
|
||||
#[cfg(debug_assertions)]
|
||||
if std::path::Path::new("tests").exists() {
|
||||
cmd.arg("--manifest-path").arg("dev/Cargo.toml");
|
||||
}
|
||||
|
||||
cmd.arg("--color")
|
||||
.arg("always")
|
||||
.arg("-q")
|
||||
.arg("--bin")
|
||||
|
||||
Reference in New Issue
Block a user