mirror of
https://github.com/notohh/rustlings.git
synced 2024-11-22 05:52:23 -05:00
Third-party exercises should be in a separate Git repo
This commit is contained in:
parent
61a84a2c11
commit
e93a99e19e
2 changed files with 61 additions and 38 deletions
|
@ -1,3 +1,5 @@
|
||||||
|
use std::path::PathBuf;
|
||||||
|
|
||||||
use anyhow::{bail, Context, Result};
|
use anyhow::{bail, Context, Result};
|
||||||
use clap::Subcommand;
|
use clap::Subcommand;
|
||||||
|
|
||||||
|
@ -9,7 +11,7 @@ mod update;
|
||||||
|
|
||||||
#[derive(Subcommand)]
|
#[derive(Subcommand)]
|
||||||
pub enum DevCommands {
|
pub enum DevCommands {
|
||||||
New { path: String },
|
New { path: PathBuf },
|
||||||
Check,
|
Check,
|
||||||
Update,
|
Update,
|
||||||
}
|
}
|
||||||
|
@ -22,7 +24,7 @@ impl DevCommands {
|
||||||
bail!("Disabled in the debug build");
|
bail!("Disabled in the debug build");
|
||||||
}
|
}
|
||||||
|
|
||||||
new::init().context(INIT_ERR)
|
new::new(&path).context(INIT_ERR)
|
||||||
}
|
}
|
||||||
DevCommands::Check => check::check(),
|
DevCommands::Check => check::check(),
|
||||||
DevCommands::Update => update::update(),
|
DevCommands::Update => update::update(),
|
||||||
|
|
|
@ -1,41 +1,72 @@
|
||||||
use anyhow::{Context, Result};
|
use anyhow::{bail, Context, Result};
|
||||||
use std::fs::{self, create_dir};
|
use std::{
|
||||||
|
env::set_current_dir,
|
||||||
|
fs::{self, create_dir},
|
||||||
|
path::Path,
|
||||||
|
process::Command,
|
||||||
|
};
|
||||||
|
|
||||||
use crate::CURRENT_FORMAT_VERSION;
|
use crate::CURRENT_FORMAT_VERSION;
|
||||||
|
|
||||||
pub fn init() -> Result<()> {
|
fn create_rel_dir(dir_name: &str, current_dir: &str) -> Result<()> {
|
||||||
create_dir("rustlings").context("Failed to create the directory `rustlings`")?;
|
create_dir(dir_name)
|
||||||
|
.with_context(|| format!("Failed to create the directory {current_dir}/{dir_name}"))?;
|
||||||
|
println!("Created the directory {current_dir}/{dir_name}");
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
create_dir("rustlings/exercises")
|
fn write_rel_file<C>(file_name: &str, current_dir: &str, content: C) -> Result<()>
|
||||||
.context("Failed to create the directory `rustlings/exercises`")?;
|
where
|
||||||
|
C: AsRef<[u8]>,
|
||||||
|
{
|
||||||
|
fs::write(file_name, content)
|
||||||
|
.with_context(|| format!("Failed to create the file {current_dir}/{file_name}"))?;
|
||||||
|
// Space to align with `create_rel_dir`.
|
||||||
|
println!("Created the file {current_dir}/{file_name}");
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
create_dir("rustlings/solutions")
|
pub fn new(path: &Path) -> Result<()> {
|
||||||
.context("Failed to create the directory `rustlings/solutions`")?;
|
let dir_name = path.to_string_lossy();
|
||||||
|
|
||||||
fs::write(
|
create_dir(path).with_context(|| format!("Failed to create the directory {dir_name}"))?;
|
||||||
"rustlings/info.toml",
|
println!("Created the directory {dir_name}");
|
||||||
|
|
||||||
|
set_current_dir(path)
|
||||||
|
.with_context(|| format!("Failed to set {dir_name} as the current directory"))?;
|
||||||
|
|
||||||
|
if !Command::new("git")
|
||||||
|
.arg("init")
|
||||||
|
.status()
|
||||||
|
.context("Failed to run `git init`")?
|
||||||
|
.success()
|
||||||
|
{
|
||||||
|
bail!("`git init` didn't run successfully. See the error message above");
|
||||||
|
}
|
||||||
|
|
||||||
|
write_rel_file(".gitignore", &dir_name, crate::init::GITIGNORE)?;
|
||||||
|
|
||||||
|
create_rel_dir("exercises", &dir_name)?;
|
||||||
|
create_rel_dir("solutions", &dir_name)?;
|
||||||
|
|
||||||
|
write_rel_file(
|
||||||
|
"info.toml",
|
||||||
|
&dir_name,
|
||||||
format!("{INFO_FILE_BEFORE_FORMAT_VERSION}{CURRENT_FORMAT_VERSION}{INFO_FILE_AFTER_FORMAT_VERSION}"),
|
format!("{INFO_FILE_BEFORE_FORMAT_VERSION}{CURRENT_FORMAT_VERSION}{INFO_FILE_AFTER_FORMAT_VERSION}"),
|
||||||
)
|
)?;
|
||||||
.context("Failed to create the file `rustlings/info.toml`")?;
|
|
||||||
|
|
||||||
fs::write("rustlings/Cargo.toml", CARGO_TOML)
|
write_rel_file("Cargo.toml", &dir_name, CARGO_TOML)?;
|
||||||
.context("Failed to create the file `rustlings/Cargo.toml`")?;
|
|
||||||
|
|
||||||
fs::write("rustlings/.gitignore", crate::init::GITIGNORE)
|
write_rel_file("README.md", &dir_name, README)?;
|
||||||
.context("Failed to create the file `rustlings/.gitignore`")?;
|
|
||||||
|
|
||||||
fs::write("rustlings/README.md", README)
|
create_rel_dir(".vscode", &dir_name)?;
|
||||||
.context("Failed to create the file `rustlings/README.md`")?;
|
write_rel_file(
|
||||||
|
".vscode/extensions.json",
|
||||||
create_dir("rustlings/.vscode")
|
&dir_name,
|
||||||
.context("Failed to create the directory `rustlings/.vscode`")?;
|
|
||||||
fs::write(
|
|
||||||
"rustlings/.vscode/extensions.json",
|
|
||||||
crate::init::VS_CODE_EXTENSIONS_JSON,
|
crate::init::VS_CODE_EXTENSIONS_JSON,
|
||||||
)
|
)?;
|
||||||
.context("Failed to create the file `rustlings/.vscode/extensions.json`")?;
|
|
||||||
|
|
||||||
println!("{INIT_DONE}");
|
println!("\nInitialization done ✓");
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -97,13 +128,3 @@ First,
|
||||||
|
|
||||||
Then, open your terminal in this directory and run `rustlings` to get started with the exercises 🚀
|
Then, open your terminal in this directory and run `rustlings` to get started with the exercises 🚀
|
||||||
";
|
";
|
||||||
|
|
||||||
const INIT_DONE: &str = r#"Initialization done!
|
|
||||||
You can start developing third-party Rustlings exercises in the `rustlings` directory :D
|
|
||||||
|
|
||||||
If the initialization was done in a Rust project which is a Cargo workspace, you need to add the
|
|
||||||
path to the `rustlings` directory to the `workspace.exclude` list in the project's `Cargo.toml`
|
|
||||||
file. For example:
|
|
||||||
|
|
||||||
[workspace]
|
|
||||||
exclude = ["rustlings"]"#;
|
|
||||||
|
|
Loading…
Reference in a new issue