aocinit

Perl utility to initialize directories and files for the Advent of Code
git clone git://git.samirparikh.com/aocinit
Log | Files | Refs | README

commit 4357e6ab5c3f94ba9a4fe3b485cbbd19c3d1c6ea
Author: Samir Parikh <noreply@samirparikh.com>
Date:   Thu, 15 Dec 2022 17:57:00 +0000

add README and initial version of aocinit files

Diffstat:
AREADME.md | 16++++++++++++++++
Aaocinit | 94+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 110 insertions(+), 0 deletions(-)

diff --git a/README.md b/README.md @@ -0,0 +1,16 @@ +`aocinit` is a Perl utility to help organize your Advent of Code directories +and subdirectories. It automatically creates folders for each year's events +and each day's exercises. It also provides some boilerplate code so you can +spend more time solving the challenges. + +Usage: + +`aocinit [year] [day]` + +where: + + `[year]` is the two-digit year (e.g. '22') + '[day]` is the two-digit day (e.g. '08' or '15') + +You can configure the location of where to put the directories and any +directory prefix by changing the `$PATH_FROM_HOME` and `$DIR_PREFIX` variables. diff --git a/aocinit b/aocinit @@ -0,0 +1,94 @@ +#!/usr/bin/env perl + +# +# Make sure you place this file somewhere in your $PATH +# + +use strict; +use warnings; +use v5.32; +use Term::ANSIColor; +use Scalar::Util qw(looks_like_number); +use File::HomeDir; +use File::Spec::Functions; +use Mojo::UserAgent; + +my $AOC_FIRST_YEAR = 15; # first event was in 2015 +my $HOME = File::HomeDir->my_home; +my $PATH_FROM_HOME = "programs"; +my $DIR_PREFIX = "zaoc_20"; + +sub invalid_arguments { + die "Usage (with two-digit year and two-digit day): $0 ", + colored( '[year]', 'underline'), ' ', + colored( '[day]', 'underline'); +} + +sub process_arguments { + my @arguments = @{ shift() }; + invalid_arguments if ( scalar @arguments != 2 ); + my ( $year, $day ) = @arguments; + invalid_arguments unless ( looks_like_number( $year ) && + looks_like_number( $day ) ); + invalid_arguments if ( $year < $AOC_FIRST_YEAR or + $year > (localtime)[5] % 100 ); + invalid_arguments if ( $day < 1 or $day > 24 ); + $day = '0' . $day if ( $day =~ m/^\d$/ ); + #say "@arguments"; + return ( $year, $day ); +} + +sub create_year_directory { + my $year_directory = shift; + if ( -e $year_directory ) { + say "$year_directory already exists"; + } else { + say "creating $year_directory..."; + mkdir $year_directory, 0755 or die "Cannot create directory $year_directory: $!"; + } +} + +sub create_day_subdirectory { + my $day_subdir = shift; + if ( -e -d $day_subdir ) { + say "$day_subdir already exists."; + } else { + say "creating $day_subdir..."; + mkdir $day_subdir, 0755 or warn "Cannot create directory $day_subdir: $!"; + } +} + +my ( $year, $day ) = process_arguments( \@ARGV ); +# todo: Check if we have the right number of arguments +# todo: Check if $year and $day are integers +#$day = '0' . $day if ( $day =~ m/^\d$/ ); + +#my $home = File::HomeDir->my_home; +#my $path = "$home/programs"; +#my $directory_prefix = "zaoc_20"; +#my $year_directory = "$path/$directory_prefix$year"; +my $year_directory = "$HOME/$PATH_FROM_HOME/$DIR_PREFIX$year"; +create_year_directory( $year_directory ); +#if ( -e $year_directory ) { +# say "$year_directory already exists"; +#} else { +# say "creating $year_directory..."; +# mkdir $year_directory, 0755 or warn "Cannot create directory $year_directory: $!"; +#} + +#opendir my $dh, $path or die "cannot open $path: $!"; +#foreach my $file ( readdir $dh ) { +# if ( -d catfile( $path, $file )) { +# say "$file is a directory"; +# } else { +# say "$file is NOT a directory"; +# } +#} +my $day_subdir = catfile ( $year_directory, "day" . $day ); +create_day_subdirectory( $day_subdir ); +#if ( -e -d $day_subdir ) { +# say "$day_subdir already exists."; +#} else { +# say "creating $day_subdir..."; +# mkdir $day_subdir, 0755 or warn "Cannot create directory $day_subdir: $!"; +#}