-
Notifications
You must be signed in to change notification settings - Fork 61
Multi-year support #77
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
Open
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9ca3e7b
Added `switch-year` command to support multiple years (Multi-year sup...
FXCourel 2154129
Remove `today` feature as its only goal was to make `chrono` an optio...
FXCourel cdd07bc
Decided to move all data files rather than bins and examples only.
FXCourel 2fec68c
Made year system compatible with benchmarks
FXCourel ea685ea
Update README.md
FXCourel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
3 changes: 2 additions & 1 deletion
.cargo/config.toml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
Cargo.lock
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,5 @@ pub mod download; | |
pub mod read; | ||
pub mod scaffold; | ||
pub mod solve; | ||
pub mod switchyear; | ||
pub mod time; |
91 changes: 91 additions & 0 deletions
src/template/commands/switchyear.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
use crate::template::year::Year; | ||
use std::{env, fs, path::PathBuf}; | ||
|
||
extern crate fs_extra; | ||
|
||
pub fn handle(year: Year) { | ||
let env_year = Year::__new_unchecked(env::var("AOC_YEAR").unwrap().parse().unwrap()); | ||
if year == env_year { | ||
println!("🔔 You are already in the year you want to switch to."); | ||
} else { | ||
switch_to_year(year, env_year); | ||
println!( | ||
"---\n🎄 Successfully switched to year {}.", | ||
year.into_inner() | ||
); | ||
} | ||
} | ||
|
||
pub fn handle_today() { | ||
let year = Year::this_year().unwrap(); | ||
let env_year = Year::new(env::var("AOC_YEAR").unwrap().parse().unwrap()).unwrap(); | ||
if year != env_year { | ||
switch_to_year(year, env_year); | ||
println!( | ||
"🎄 Automatically switched to this year: {}.", | ||
year.into_inner() | ||
); | ||
} | ||
} | ||
|
||
fn create_folder_with_gitkeep(path: PathBuf) { | ||
fs_extra::dir::create_all(&path, false).unwrap(); | ||
fs::write(path.join(".keep"), "").unwrap(); | ||
} | ||
|
||
pub fn switch_to_year(year: Year, previous_year: Year) { | ||
let cwd = env::current_dir().unwrap(); | ||
|
||
// Move src and data files to years/ | ||
let src = cwd.join("src"); | ||
let data = cwd.join("data"); | ||
let bin = src.join("bin"); | ||
let examples = data.join("examples"); | ||
let inputs = data.join("inputs"); | ||
let puzzles = data.join("puzzles"); | ||
let years = cwd.join("years"); | ||
let destination = years.join(previous_year.into_inner().to_string()); | ||
|
||
let default_copy = fs_extra::dir::CopyOptions::new(); | ||
let mut inside_copy = fs_extra::dir::CopyOptions::new(); | ||
inside_copy.content_only = true; | ||
fs_extra::dir::create_all(&destination, false).unwrap(); | ||
fs_extra::dir::move_dir(&bin, &destination, &default_copy).unwrap(); | ||
fs_extra::dir::move_dir(&data, &destination, &inside_copy).unwrap(); | ||
|
||
// Move years/ to src and data files | ||
let source = years.join(year.into_inner().to_string()); | ||
if source.exists() { | ||
let source_bin = source.join("bin"); | ||
fs_extra::dir::move_dir(&source_bin, &src, &default_copy).unwrap(); | ||
fs_extra::dir::move_dir(&source, &data, &inside_copy).unwrap(); | ||
println!( | ||
"Found existing files for year {}, moved them.", | ||
year.into_inner() | ||
); | ||
} else { | ||
println!( | ||
"No existing files for year {}, generating blank folders.", | ||
year.into_inner() | ||
); | ||
create_folder_with_gitkeep(bin); | ||
create_folder_with_gitkeep(examples); | ||
create_folder_with_gitkeep(inputs); | ||
create_folder_with_gitkeep(puzzles); | ||
} | ||
|
||
// Set the environment variable | ||
std::env::set_var("AOC_YEAR", year.into_inner().to_string()); | ||
|
||
// Write Cargo.toml | ||
let config_toml = cwd.join(".cargo").join("config.toml"); | ||
let config_toml_content = fs::read_to_string(&config_toml).unwrap(); | ||
let config_toml_updated_content = config_toml_content.replace( | ||
&previous_year.into_inner().to_string(), | ||
&year.into_inner().to_string(), | ||
); | ||
fs::write(config_toml, config_toml_updated_content).unwrap(); | ||
|
||
// Update benchmarks in README.md | ||
crate::template::readme_benchmarks::update_after_switch_year().unwrap(); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.