Skip to content

Commit 8df66f7

Browse files
committed
Allow initialization in a workspace
1 parent 3958038 commit 8df66f7

File tree

3 files changed

+65
-42
lines changed

3 files changed

+65
-42
lines changed

src/init.rs

Lines changed: 49 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,40 @@ use ratatui::crossterm::style::Stylize;
33
use std::{
44
env::set_current_dir,
55
fs::{self, create_dir},
6-
io::ErrorKind,
6+
io::{self, Write},
77
path::Path,
88
process::{Command, Stdio},
99
};
1010

11-
use crate::{cargo_toml::updated_cargo_toml, embedded::EMBEDDED_FILES, info_file::InfoFile};
11+
use crate::{
12+
cargo_toml::updated_cargo_toml, embedded::EMBEDDED_FILES, info_file::InfoFile,
13+
term::press_enter_prompt,
14+
};
1215

1316
pub fn init() -> Result<()> {
14-
// Prevent initialization in a directory that contains the file `Cargo.toml`.
15-
// This can mean that Rustlings was already initialized in this directory.
16-
// Otherwise, this can cause problems with Cargo workspaces.
17-
if Path::new("Cargo.toml").exists() {
18-
bail!(CARGO_TOML_EXISTS_ERR);
17+
let rustlings_dir = Path::new("rustlings");
18+
if rustlings_dir.exists() {
19+
bail!(RUSTLINGS_DIR_ALREADY_EXISTS_ERR);
1920
}
2021

21-
let rustlings_path = Path::new("rustlings");
22-
if let Err(e) = create_dir(rustlings_path) {
23-
if e.kind() == ErrorKind::AlreadyExists {
24-
bail!(RUSTLINGS_DIR_ALREADY_EXISTS_ERR);
22+
let mut stdout = io::stdout().lock();
23+
let mut init_git = true;
24+
25+
if Path::new("Cargo.toml").exists() {
26+
if Path::new("exercises").exists() && Path::new("solutions").exists() {
27+
bail!(IN_INITIALIZED_DIR_ERR);
2528
}
26-
return Err(e.into());
29+
30+
stdout.write_all(CARGO_TOML_EXISTS_PROMPT_MSG)?;
31+
press_enter_prompt(&mut stdout)?;
32+
init_git = false;
2733
}
2834

29-
set_current_dir("rustlings")
35+
stdout.write_all(b"This command will create the directory `rustlings/` which will contain the exercises.\nPress ENTER to continue ")?;
36+
press_enter_prompt(&mut stdout)?;
37+
38+
create_dir(rustlings_dir).context("Failed to create the `rustlings/` directory")?;
39+
set_current_dir(rustlings_dir)
3040
.context("Failed to change the current directory to `rustlings/`")?;
3141

3242
let info_file = InfoFile::parse()?;
@@ -75,18 +85,21 @@ pub fn init() -> Result<()> {
7585
fs::write(".vscode/extensions.json", VS_CODE_EXTENSIONS_JSON)
7686
.context("Failed to create the file `rustlings/.vscode/extensions.json`")?;
7787

78-
// Ignore any Git error because Git initialization is not required.
79-
let _ = Command::new("git")
80-
.arg("init")
81-
.stdin(Stdio::null())
82-
.stderr(Stdio::null())
83-
.status();
88+
if init_git {
89+
// Ignore any Git error because Git initialization is not required.
90+
let _ = Command::new("git")
91+
.arg("init")
92+
.stdin(Stdio::null())
93+
.stderr(Stdio::null())
94+
.status();
95+
}
8496

85-
println!(
97+
writeln!(
98+
stdout,
8699
"\n{}\n\n{}",
87100
"Initialization done ✓".green(),
88101
POST_INIT_MSG.bold(),
89-
);
102+
)?;
90103

91104
Ok(())
92105
}
@@ -104,7 +117,7 @@ target/
104117

105118
pub const VS_CODE_EXTENSIONS_JSON: &[u8] = br#"{"recommendations":["rust-lang.rust-analyzer"]}"#;
106119

107-
const CARGO_TOML_EXISTS_ERR: &str = "The current directory contains the file `Cargo.toml`.
120+
const IN_INITIALIZED_DIR_ERR: &str = "It looks like Rustlings is already initialized in this directory.
108121
109122
If you already initialized Rustlings, run the command `rustlings` for instructions on getting started with the exercises.
110123
Otherwise, please run `rustlings init` again in another directory.";
@@ -115,5 +128,19 @@ You probably already initialized Rustlings.
115128
Run `cd rustlings`
116129
Then run `rustlings` again";
117130

131+
const CARGO_TOML_EXISTS_PROMPT_MSG: &[u8] = br#"You are about to initialize Rustlings in a directory that already contains a `Cargo.toml` file!
132+
133+
=> It is recommended to abort with CTRL+C and initialize Rustlings in another directory <=
134+
135+
If you know what you are doing and want to initialize Rustlings in a Cargo workspace,
136+
then you need to add its directory to `members` in the `workspace` section of the `Cargo.toml` file:
137+
138+
```toml
139+
[workspace]
140+
members = ["rustlings"]
141+
```
142+
143+
Press ENTER if you are sure that you want to continue after reading the warning above "#;
144+
118145
const POST_INIT_MSG: &str = "Run `cd rustlings` to go into the generated directory.
119146
Then run `rustlings` to get started.";

src/main.rs

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ use anyhow::{bail, Context, Result};
22
use app_state::StateFileStatus;
33
use clap::{Parser, Subcommand};
44
use std::{
5-
io::{self, BufRead, IsTerminal, StdoutLock, Write},
5+
io::{self, IsTerminal, Write},
66
path::Path,
77
process::exit,
88
};
9+
use term::{clear_terminal, press_enter_prompt};
910

1011
use self::{app_state::AppState, dev::DevCommands, info_file::InfoFile, watch::WatchExit};
1112

@@ -20,20 +21,12 @@ mod init;
2021
mod list;
2122
mod progress_bar;
2223
mod run;
24+
mod term;
2325
mod terminal_link;
2426
mod watch;
2527

2628
const CURRENT_FORMAT_VERSION: u8 = 1;
2729

28-
fn clear_terminal(stdout: &mut StdoutLock) -> io::Result<()> {
29-
stdout.write_all(b"\x1b[H\x1b[2J\x1b[3J")
30-
}
31-
32-
fn press_enter_prompt() -> io::Result<()> {
33-
io::stdin().lock().read_until(b'\n', &mut Vec::new())?;
34-
Ok(())
35-
}
36-
3730
/// Rustlings is a collection of small exercises to get you used to writing and reading Rust code
3831
#[derive(Parser)]
3932
#[command(version)]
@@ -79,14 +72,6 @@ fn main() -> Result<()> {
7972

8073
match args.command {
8174
Some(Subcommands::Init) => {
82-
{
83-
let mut stdout = io::stdout().lock();
84-
stdout.write_all(b"This command will create the directory `rustlings/` which will contain the exercises.\nPress ENTER to continue ")?;
85-
stdout.flush()?;
86-
press_enter_prompt()?;
87-
stdout.write_all(b"\n")?;
88-
}
89-
9075
return init::init().context("Initialization failed");
9176
}
9277
Some(Subcommands::Dev(dev_command)) => return dev_command.run(),
@@ -118,8 +103,7 @@ fn main() -> Result<()> {
118103

119104
let welcome_message = welcome_message.trim_ascii();
120105
write!(stdout, "{welcome_message}\n\nPress ENTER to continue ")?;
121-
stdout.flush()?;
122-
press_enter_prompt()?;
106+
press_enter_prompt(&mut stdout)?;
123107
clear_terminal(&mut stdout)?;
124108
// Flush to be able to show errors occuring before printing a newline to stdout.
125109
stdout.flush()?;

src/term.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
use std::io::{self, BufRead, StdoutLock, Write};
2+
3+
pub fn clear_terminal(stdout: &mut StdoutLock) -> io::Result<()> {
4+
stdout.write_all(b"\x1b[H\x1b[2J\x1b[3J")
5+
}
6+
7+
pub fn press_enter_prompt(stdout: &mut StdoutLock) -> io::Result<()> {
8+
stdout.flush()?;
9+
io::stdin().lock().read_until(b'\n', &mut Vec::new())?;
10+
stdout.write_all(b"\n")?;
11+
Ok(())
12+
}

0 commit comments

Comments
 (0)