feat!: decouple download and scaffold from main package into workspace

Added workspace structure to Cargo.toml. This way we don't need to build
all dependencies for helper binaries. Also changed .cargo/config
accordingly.
This commit is contained in:
Matej Janezic 2023-11-20 15:44:14 +01:00
parent 0000029068
commit 0000030025
Signed by: janezicmatej
GPG Key ID: 4298E230ED37B2C0
9 changed files with 85 additions and 38 deletions

View File

@ -1,6 +1,4 @@
[alias] [alias]
scaffold = "run --bin scaffold --quiet --release --" scaffold = "run -p scaffold --release --"
download = "run --bin download --quiet --release --" download = "run -p download --release --"
download_all = "run --bin download_all --quiet --release --" solve = "run --bin --release"
solve = "run --bin"

32
Cargo.lock generated
View File

@ -45,17 +45,11 @@ checksum = "0942ffc6dcaadf03badf6e6a2d0228460359d5e34b57ccdc720b7382dfbd5ec5"
[[package]] [[package]]
name = "aoc" name = "aoc"
version = "29.0.0" version = "30.0.0"
dependencies = [ dependencies = [
"dotenvy",
"elves", "elves",
"hashbrown 0.14.2", "hashbrown 0.14.2",
"hex-literal",
"itertools 0.12.0", "itertools 0.12.0",
"lazy_static",
"pico-args",
"regex",
"reqwest",
] ]
[[package]] [[package]]
@ -137,6 +131,15 @@ version = "0.15.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "03d8c417d7a8cb362e0c37e5d815f5eb7c37f79ff93707329d5a194e42e54ca0" checksum = "03d8c417d7a8cb362e0c37e5d815f5eb7c37f79ff93707329d5a194e42e54ca0"
[[package]]
name = "download"
version = "30.0.0"
dependencies = [
"dotenvy",
"pico-args",
"reqwest",
]
[[package]] [[package]]
name = "either" name = "either"
version = "1.8.0" version = "1.8.0"
@ -309,12 +312,6 @@ dependencies = [
"libc", "libc",
] ]
[[package]]
name = "hex-literal"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6fe2267d4ed49bc07b63801559be28c718ea06c4738b7a03c94df7386d2cde46"
[[package]] [[package]]
name = "http" name = "http"
version = "0.2.8" version = "0.2.8"
@ -740,6 +737,15 @@ version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7b4b9743ed687d4b4bcedf9ff5eaa7398495ae14e61cba0a295704edbc7decde" checksum = "7b4b9743ed687d4b4bcedf9ff5eaa7398495ae14e61cba0a295704edbc7decde"
[[package]]
name = "scaffold"
version = "30.0.0"
dependencies = [
"dotenvy",
"pico-args",
"reqwest",
]
[[package]] [[package]]
name = "schannel" name = "schannel"
version = "0.1.20" version = "0.1.20"

View File

@ -1,20 +1,31 @@
[package] [package]
name = "aoc" name = "aoc"
description.workspace = true
readme.workspace = true
version.workspace = true
edition.workspace = true
license.workspace = true
authors.workspace = true
repository.workspace = true
[dependencies]
# many cool stuff for iterators
itertools = "0.12.0"
# faster hashing algorithms for hashmaps and hashsets
hashbrown = "0.14.2"
# my library with some sorters and parses
elves = "0.2.0"
[workspace]
members = ["utils/download", "utils/scaffold"]
[workspace.package]
description = "template for advent of code" description = "template for advent of code"
readme = "README.md" readme = "README.md"
version = "29.0.0" version = "30.0.0"
edition = "2021" edition = "2021"
license = "MIT" license = "MIT"
authors = ["Matej Janežič <janezic.mj@gmail.com>"] authors = ["Matej Janežič <janezic.mj@gmail.com>"]
repository = "https://github.com/janezicmatej/aoc-template.git" repository = "https://github.com/janezicmatej/aoc-template.git"
[dependencies]
dotenvy = "0.15.6"
elves = "0.2.0"
hashbrown = "0.14.2"
hex-literal = "0.4.1"
itertools = "0.12.0"
lazy_static = "1.4.0"
pico-args = "0.5.0"
regex = "1.10.2"
reqwest = { version = "0.11.22", features = ["blocking"] }

View File

@ -8,19 +8,18 @@
- `src/` : - `src/` :
- `bin/`: - `bin/`:
- `<day>.rs`: solution files - `<day>.rs`: solution files
- `*.rs`: convenience scripts
- `inputs/`: this directory is gitignored, input files go here - `inputs/`: this directory is gitignored, input files go here
- `examples/`: example files go here; you can push this as test are run in ci - `examples/`: example files go here; you can push this as test are run in ci
- `utils/`: utils files go here - `utils/`: utils files go here
- `lib.rs`: contains framework code - `lib.rs`: contains framework code
- `.env.example`: example dotenv file - `.env.example`: example dotenv file
- `utils/`: binary packages with convenience scripts structured using cargo workspaces
### Cli ### Cli
- `cargo scaffold <day>`: prepare solution files for `day` - `cargo scaffold <day>`: prepare solution files for `day`
- `cargo download <day>`: download input file for `day` - `cargo download <day>`: download input file for `day`
- `cargo solve <day>`: run solution against input for `day` - `cargo solve <day>`: run solution against input for `day`
### dotenv ### dotenv
set `YEAR` to whichever year you are solving for and `TOKEN` to AoC session Cookie set `YEAR` to whichever year you are solving for and `TOKEN` to AoC session Cookie

View File

@ -47,8 +47,3 @@ pub fn read_file(folder: &str, day: u8) -> String {
let f = fs::read_to_string(filepath); let f = fs::read_to_string(filepath);
f.expect("could not open input file").trim().to_string() f.expect("could not open input file").trim().to_string()
} }
pub fn parse_args() -> Result<u8, pico_args::Error> {
let mut args = pico_args::Arguments::from_env();
args.free_from_str()
}

14
utils/download/Cargo.toml Normal file
View File

@ -0,0 +1,14 @@
[package]
name = "download"
description.workspace = true
readme.workspace = true
version.workspace = true
edition.workspace = true
license.workspace = true
authors.workspace = true
repository.workspace = true
[dependencies]
reqwest = { version = "0.11.22", features = ["blocking"] }
dotenvy = "0.15.6"
pico-args = "0.5.0"

View File

@ -3,8 +3,13 @@ use reqwest::blocking::Client;
use reqwest::header; use reqwest::header;
use std::{env, fs::OpenOptions, io::Write, process}; use std::{env, fs::OpenOptions, io::Write, process};
pub fn parse_args() -> Result<u8, pico_args::Error> {
let mut args = pico_args::Arguments::from_env();
args.free_from_str()
}
fn main() { fn main() {
let day: u8 = match aoc::parse_args() { let day: u8 = match parse_args() {
Ok(day) => day, Ok(day) => day,
Err(_) => { Err(_) => {
eprintln!("Need to specify a day (as integer). example: `cargo download 7`"); eprintln!("Need to specify a day (as integer). example: `cargo download 7`");

14
utils/scaffold/Cargo.toml Normal file
View File

@ -0,0 +1,14 @@
[package]
name = "scaffold"
description.workspace = true
readme.workspace = true
version.workspace = true
edition.workspace = true
license.workspace = true
authors.workspace = true
repository.workspace = true
[dependencies]
reqwest = { version = "0.11.22", features = ["blocking"] }
dotenvy = "0.15.6"
pico-args = "0.5.0"

View File

@ -35,6 +35,11 @@ mod tests {
} }
"#; "#;
pub fn parse_args() -> Result<u8, pico_args::Error> {
let mut args = pico_args::Arguments::from_env();
args.free_from_str()
}
fn safe_create_file(path: &str) -> Result<File, std::io::Error> { fn safe_create_file(path: &str) -> Result<File, std::io::Error> {
OpenOptions::new().write(true).create_new(true).open(path) OpenOptions::new().write(true).create_new(true).open(path)
} }
@ -44,7 +49,7 @@ fn create_file(path: &str) -> Result<File, std::io::Error> {
} }
fn main() { fn main() {
let day = match aoc::parse_args() { let day = match parse_args() {
Ok(day) => day, Ok(day) => day,
Err(_) => { Err(_) => {
eprintln!("Need to specify a day (as integer). example: `cargo scaffold 7`"); eprintln!("Need to specify a day (as integer). example: `cargo scaffold 7`");