diff --git a/make_day_readme.sh b/make_day_readme.sh index 33e02e4..84d1ec4 100755 --- a/make_day_readme.sh +++ b/make_day_readme.sh @@ -12,82 +12,123 @@ FMT_WARN='\e[33m%s\e[0m\n' echo Making Readme of day $day in $readme function find_lang { - # 1: fileext, 2: lang, 3: howto - sourcefile=$(ls $day_dir/*.$1 2> /dev/null) + # args: 1: fileext, 2: lang, 3: howto + sources=($(IFS=$'\n' ls $day_dir/*.$1 2> /dev/null)) [[ $? != 0 ]] && return - lang=$2 - sourcefilebase=$(basename $sourcefile) - printf "$FMT_VAR" "Determined Langugae" "$2 ($sourcefilebase)" - sed -i "s/LANG/$2/" $readme - howto=$(echo "$3" | sed "s|SOURCE|$sourcefilebase|") - sed -i "s(HOWTORUN($howto(" $readme - loc=$(sed -r '/^\s*(#|\/\/|\/\*|;)/d;/^\s*$/d' $sourcefile | wc -l) - sed -i "s(LOC($loc(" $readme + if [[ ${#sources[@]} -gt 1 ]]; then + printf "$FMT_WARN" "Found multiple sources for language $2 (using first as main): ${sources[@]}" + fi + if [[ ${#sources[@]} -gt 1 ]]; then + printf "$FMT_WARN" "Found multiple sources for language $2 (using first as main): ${sources[@]}" + fi + mainsource=${sources[0]} + if [[ -z $lang ]]; then + lang=$2 + else + lang=$lang,$2 + fi + mainsource_base=$(basename $mainsource) + printf "$FMT_VAR" "Determined Language" "$2 ($mainsource_base)" + if [[ -n $3 ]]; then + howto=$(echo "$3" | sed "s|SOURCE|$mainsource_base|") + else + printf "$FMT_WARN" "No HOWTO provided for language $2" + fi + for file in ${sources[@]}; do + loc=$(( $loc + $(sed -r '/^\s*(#|\/\/|\/\*|;)/d;/^\s*$/d' "$file" | wc -l) )) + done } function get_time { cd $day_dir exe=$(find . -type f -executable) + if [[ -z $exe ]]; then + printf "$FMT_ERR" "Found no executable for day $day" + cd .. + return 2 + fi exe=$(basename $exe) if [[ $(echo $exe | wc -w) != 1 ]]; then - printf "$FMT_ERR" "Found multiple or no executables for day $day: '$exe'" + printf "$FMT_ERR" "Found multiple executables for day $day: '$exe'" cd .. return 1 - time=Error - else - time=$({ time ./$exe; } 2>&1) - if [[ $? != 0 ]]; then - printf "$FMT_ERR" "Execution error in './$exe' in '$(pwd)'. Output:" - printf "\e[34m" - echo -e "$time" - printf "\e[0m" - cd .. - return 1 - fi - time=$(echo -e "$time" | tail -3 | head -1 | awk '{print $2}') - re='([[:digit:]]+)m([[:digit:]]+),([[:digit:]]+)s' - [[ $time =~ $re ]] - min=${BASH_REMATCH[1]} - sec=${BASH_REMATCH[2]} - msec=${BASH_REMATCH[3]} - if [[ $min != 0 ]]; then - time="$min m $sec,$msec s" - else - time="$sec,$msec s" - fi - cd .. - # elif [[ $sec != 0 ]]; then - # time="$sec,$msec s" - # else - # time="$((msec+0)) ms" - # fi fi + time=$({ time ./$exe; } 2>&1) + if [[ $? != 0 ]]; then + printf "$FMT_ERR" "Execution error in './$exe' in '$(pwd)'. Output:" + printf "\e[34m" + echo -e "$time" + printf "\e[0m" + cd .. + return 1 + fi + time=$(echo -e "$time" | tail -3 | head -1 | awk '{print $2}') + re='([[:digit:]]+)m([[:digit:]]+),([[:digit:]]+)s' + [[ $time =~ $re ]] + min=${BASH_REMATCH[1]} + sec=${BASH_REMATCH[2]} + msec=${BASH_REMATCH[3]} + if [[ $min != 0 ]]; then + time="$min m $sec,$msec s" + else + time="$sec,$msec s" + fi + # elif [[ $sec != 0 ]]; then + # time="$sec,$msec s" + # else + # time="$((msec+0)) ms" + # fi + cd .. } gifts="" for i in $(seq $day); do gifts+=":gift:"; done -sed "s/DAY/$day/g" README.md.temp | sed "s/:gift:/$gifts/" > $day_dir/README.md +sed "s/DAY/$day/g" README.md.temp | sed "s/:gift:/$gifts/" > "$readme" get_time if [[ $? == 0 ]]; then - cd .. printf "$FMT_VAR" "exectime" "$time" - sed -i "s/EXECTIME/$time/" $readme + sed -i "s/EXECTIME/$time/" "$readme" else - cd .. printf "$FMT_WARN" "No execution time determined" - sed -i '/.*EXECTIME.*/d' $readme + sed -i '/.*EXECTIME.*/d' "$readme" fi if [[ -z $1 ]]; then printf "$FMT_WARN" "No comment provided" - sed -i "//d" $readme + sed -i '//d' "$readme" || echo "ERROR comment" else printf "$FMT_VAR" "Comment" "'$1'" - sed -i "s|COMMENT|$1|" $readme + sed -i "s|COMMENT|$1|" "$readme" || echo "ERROR comment2" fi +dayexec="day${day_dir}" +loc=0 find_lang py Python "python3 SOURCE" -find_lang rs Rust "rustc SOURCE\n./day${day_dir}" -find_lang cpp C++ "g++ SOURCE -o day${day_dir}\n ./day${day_dir}" +find_lang rs Rust "rustc SOURCE\n./$dayexec" +find_lang cpp C++ "g++ SOURCE -o ${dayexec}\n./${dayexec}" +find_lang c C "gcc SOURCE -o ${dayexec}\n./${dayexec}" +find_lang java Java "javac Main.java\njava Main" +find_lang s Assembly "" +find_lang s65 Assembly "javac Main.java\njava Main" +find_lang awk Awk "awk -f SOURCE input.txt" +find_lang sh Bash "./SOURCE" +find_lang php PhP "php -S localhost:8000 -t .\nfirefox http://localhost:8000" +find_lang js Javascript "" + +if [[ -z $lang ]]; then + printf "$FMT_WARN" "No languages found" +else + sed -i "s/LANG/$lang/" $readme +fi + +if [[ -n $howto ]]; then + sed -i "s|HOWTO|$howto|" $readme +fi + +if [[ $loc == 0 ]]; then + printf "$FMT_WARN" "No lines of code found" +else + sed -i "s(LOC($loc(" $readme +fi diff --git a/make_readme.sh b/make_readme.sh index f9cb82e..e5edac7 100755 --- a/make_readme.sh +++ b/make_readme.sh @@ -2,8 +2,16 @@ table="| Day | Language | Lines of code | Execuction time | Comment |\n" table+="|:---:|:---:| ---:| ---: |--- |\n" -for day in $(seq -f "%02g" 1 25); do - [[ ! -d "$day" ]] && continue +maxday=$(ls -d */ | sed -r 's|0*(.*)/|\1|g' | tail -1) +for day in $(seq -f "%02g" 1 $maxday); do + if [[ ! -d "$day" ]]; then + echo "Missing directory for day $day" + continue + fi + if [[ ! -f "$day/README.md" ]]; then + echo "Missing README for day $day" + continue + fi # echo $day lang=$(grep -E "Today's language:" $day/README.md | sed -r 's/.*\*\*(.+)\*\*.*/\1/') exectime=$(grep -E "Execution time:" $day/README.md | sed -r 's/.*\*\*(.+)\*\*.*/\1/')