Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor template #59

Merged
merged 7 commits into from
Dec 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,7 @@ pub enum AocError
#[cfg(feature = "tally")]
#[error("{0}")]
RunError(String),

#[error("Setup for year already exists")]
SetupExists,
}
5 changes: 0 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,6 @@ async fn main() -> Result<(), AocError>
.required(false)
.default_value(OsStr::from(chrono::Utc::now().day().to_string()))
.help("Day to run"),
Arg::new("year")
.short('y')
.required(false)
.default_value(OsStr::from(chrono::Utc::now().year().to_string()))
.help("Year for automatic download of input if not present"),
Arg::new("test")
.short('t')
.long("test")
Expand Down
8 changes: 4 additions & 4 deletions src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
error::AocError,
util::{
file::{cargo_path, day_path, download_input_file},
get_day, get_time_symbol, get_year,
get_day, get_time_symbol, get_year_from_root,
},
};

Expand All @@ -34,7 +34,7 @@ pub async fn run(matches: &ArgMatches) -> Result<(), AocError>

if !dir.join("input").exists()
{
let year = get_year(matches)?;
let year = get_year_from_root().await?;
let current_year = Utc::now().year();
let current_month = Utc::now().month();

Expand Down Expand Up @@ -84,15 +84,15 @@ pub async fn run(matches: &ArgMatches) -> Result<(), AocError>

if matches.get_flag("assert")
{
let year = get_year(matches)?;
let year = get_year_from_root().await?;
assert_answer(&out, day, year).await?;
}

// Only try to submit if the submit flag is passed
#[cfg(feature = "submit")]
if let Some(task) = get_submit_task(matches).transpose()?
{
let year = get_year(matches)?;
let year = get_year_from_root().await?;
let output = submit::submit(&out, task, day, year).await?;
println!("Task {}: {}", task, output);
}
Expand Down
50 changes: 44 additions & 6 deletions src/setup.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,47 @@
use std::path::Path;

use clap::ArgMatches;
use tokio::{fs::OpenOptions, io::AsyncWriteExt};

use crate::{error::AocError, util::get_year};
use crate::error::AocError;

async fn setup_template_project(year: i32) -> Result<(), AocError>
{
tokio::process::Command::new("cargo")
if Path::new(&format!("{year}")).exists()
{
return Err(AocError::SetupExists);
}

let res = tokio::process::Command::new("cargo")
.args(["new", &format!("year_{}", year)])
.output()
.await?;

let template = format!("{}/template/template.rs", env!("CARGO_MANIFEST_DIR"));
if !res.status.success()
{
return Err(AocError::SetupExists);
}

tokio::fs::rename(format!("year_{year}"), format!("{year}")).await?;

let template_dir = format!("{}/template", env!("CARGO_MANIFEST_DIR"));
let bins = tokio::fs::read(Path::new(&template_dir).join("Cargo.toml.template")).await?;

OpenOptions::new()
.append(true)
.open(format!("{}/Cargo.toml", year))
.await?
.write_all(&bins)
.await?;

for i in 1..=25
{
let dir = format!("year_{year}/src/bin/day_{:0>2}", i);
let dir = format!("{year}/src/day_{:0>2}", i);
tokio::fs::create_dir_all(&dir).await?;
tokio::fs::copy(&template, format!("{dir}/main.rs")).await?;
tokio::fs::copy(Path::new(&template_dir).join("template.rs"), format!("{dir}/main.rs"))
.await?;
}
tokio::fs::remove_file(format!("year_{year}/src/main.rs")).await?;
tokio::fs::remove_file(format!("{year}/src/main.rs")).await?;
Ok(())
}

Expand All @@ -40,6 +65,19 @@ async fn get_session_token() -> Result<(), AocError>
Ok(())
}

fn get_year(matches: &ArgMatches) -> Result<i32, AocError>
seblyng marked this conversation as resolved.
Show resolved Hide resolved
{
let year = matches.get_one::<String>("year").ok_or(AocError::ArgMatches)?;
if year.chars().count() == 2
{
Ok(format!("20{}", year).parse()?)
}
else
{
Ok(year.parse()?)
}
}

pub async fn setup(args: &ArgMatches) -> Result<(), AocError>
{
let year = get_year(args)?;
Expand Down
16 changes: 13 additions & 3 deletions src/util/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::path::PathBuf;
use std::{
io::{Error, ErrorKind},
path::PathBuf,
};

use clap::ArgMatches;

Expand Down Expand Up @@ -32,9 +35,16 @@ impl std::fmt::Display for Task
}


pub fn get_year(matches: &ArgMatches) -> Result<i32, AocError>
pub async fn get_year_from_root() -> Result<i32, AocError>
{
let year = matches.get_one::<String>("year").ok_or(AocError::ArgMatches)?;
let root = cargo_path().await?;
let year = root
.file_name()
.ok_or_else(|| {
AocError::StdIoErr(Error::new(ErrorKind::NotFound, "could not find Cargo.toml file"))
})?
.to_string_lossy();

if year.chars().count() == 2
{
Ok(format!("20{}", year).parse()?)
Expand Down
2 changes: 1 addition & 1 deletion src/util/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub struct AocRequest

impl AocRequest
{
const AOC_USER_AGENT: &str =
const AOC_USER_AGENT: &'static str =
"github.com/seblj/cargo-aoc by [email protected] and [email protected]";

pub fn new() -> AocRequest
Expand Down
99 changes: 99 additions & 0 deletions template/Cargo.toml.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
[[bin]]
name = "day_01"
path = "src/day_01/main.rs"

[[bin]]
name = "day_02"
path = "src/day_02/main.rs"

[[bin]]
name = "day_03"
path = "src/day_03/main.rs"

[[bin]]
name = "day_04"
path = "src/day_04/main.rs"

[[bin]]
name = "day_05"
path = "src/day_05/main.rs"

[[bin]]
name = "day_06"
path = "src/day_06/main.rs"

[[bin]]
name = "day_07"
path = "src/day_07/main.rs"

[[bin]]
name = "day_08"
path = "src/day_08/main.rs"

[[bin]]
name = "day_09"
path = "src/day_09/main.rs"

[[bin]]
name = "day_10"
path = "src/day_10/main.rs"

[[bin]]
name = "day_11"
path = "src/day_11/main.rs"

[[bin]]
name = "day_12"
path = "src/day_12/main.rs"

[[bin]]
name = "day_13"
path = "src/day_13/main.rs"

[[bin]]
name = "day_14"
path = "src/day_14/main.rs"

[[bin]]
name = "day_15"
path = "src/day_15/main.rs"

[[bin]]
name = "day_16"
path = "src/day_16/main.rs"

[[bin]]
name = "day_17"
path = "src/day_17/main.rs"

[[bin]]
name = "day_18"
path = "src/day_18/main.rs"

[[bin]]
name = "day_19"
path = "src/day_19/main.rs"

[[bin]]
name = "day_20"
path = "src/day_20/main.rs"

[[bin]]
name = "day_21"
path = "src/day_21/main.rs"

[[bin]]
name = "day_22"
path = "src/day_22/main.rs"

[[bin]]
name = "day_23"
path = "src/day_23/main.rs"

[[bin]]
name = "day_24"
path = "src/day_24/main.rs"

[[bin]]
name = "day_25"
path = "src/day_25/main.rs"