From a56c93ac0ae024651bf9d2928da653879ba93efd Mon Sep 17 00:00:00 2001 From: Oscar Molnar Date: Wed, 4 Dec 2024 18:06:13 +0000 Subject: [PATCH] Create kotlin template generator GHA --- .github/workflows/kotlin-template.yaml | 59 ++++++++++++++++++++++++++ templates/kotlin/day.kt | 13 ++++++ templates/kotlin/part1/part1.kt | 5 +++ templates/kotlin/part2/part2.kt | 5 +++ 4 files changed, 82 insertions(+) create mode 100644 .github/workflows/kotlin-template.yaml create mode 100644 templates/kotlin/day.kt create mode 100644 templates/kotlin/part1/part1.kt create mode 100644 templates/kotlin/part2/part2.kt diff --git a/.github/workflows/kotlin-template.yaml b/.github/workflows/kotlin-template.yaml new file mode 100644 index 0000000..3c73167 --- /dev/null +++ b/.github/workflows/kotlin-template.yaml @@ -0,0 +1,59 @@ +name: Kotlin template auto-generator + +on: + schedule: + - cron: "0 18 * * *" + workflow_dispatch: + +jobs: + setup-day: + runs-on: ubuntu-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v3 + + - name: Set up Git + run: | + git config user.name "GitHub Action" + git config user.email "action@github.com" + + - name: Calculate Next Day + id: day + run: | + TODAY=$(date -u +"%Y-%m-%d") + CURRENT_DAY=$(date -u -d "$TODAY" +%-d) + NEXT_DAY=$((CURRENT_DAY + 1)) + if [ "$NEXT_DAY" -gt 25 ]; then + echo "delete=true" >> $GITHUB_ENV + exit 0 + fi + echo "delete=false" >> $GITHUB_ENV + echo "DAY=$(printf '%02d' $NEXT_DAY)" >> $GITHUB_ENV + + - name: Generate folders and copy templates + if: env.delete == 'false' + run: | + DAY_FOLDER="2024/kotlin/src/main/kotlin/com/tymscar/day${{ env.DAY }}" + mkdir -p "$DAY_FOLDER/part1" "$DAY_FOLDER/part2" + + cp templates/kotlin/day.kt "$DAY_FOLDER/day${{ env.DAY }}.kt" + cp templates/kotlin/part1/part1.kt "$DAY_FOLDER/part1/part1.kt" + cp templates/kotlin/part2/part2.kt "$DAY_FOLDER/part2/part2.kt" + + sed -i "s/{{DAY}}/${{ env.DAY }}/g" "$DAY_FOLDER/day${{ env.DAY }}.kt" + sed -i "s/{{DAY}}/${{ env.DAY }}/g" "$DAY_FOLDER/part1/part1.kt" + sed -i "s/{{DAY}}/${{ env.DAY }}/g" "$DAY_FOLDER/part2/part2.kt" + + git add . + git commit -m "Setup files for Day ${{ env.DAY }}" + git push + + - name: Delete workflow if past December 25 + if: env.delete == 'true' + run: | + rm -rf templates + rm -f .github/workflows/kotlin-template.yaml + git add templates .github/workflows/kotlin-template.yaml + git commit -m "Kotlin template generator removed" + git push \ No newline at end of file diff --git a/templates/kotlin/day.kt b/templates/kotlin/day.kt new file mode 100644 index 0000000..6422645 --- /dev/null +++ b/templates/kotlin/day.kt @@ -0,0 +1,13 @@ +package com.tymscar.day{{DAY}} + +import java.io.File +import com.tymscar.day{{DAY}}.part1.solve as part1 +import com.tymscar.day{{DAY}}.part2.solve as part2 + +fun solve() { + var input: String = File("src/main/kotlin/com/tymscar/day{{DAY}}/input.txt").readText() + + println("Day {{DAY}}: ") + println("Part 1: ${part1(input)}") + println("Part 2: ${part2(input)}") +} diff --git a/templates/kotlin/part1/part1.kt b/templates/kotlin/part1/part1.kt new file mode 100644 index 0000000..df42a0a --- /dev/null +++ b/templates/kotlin/part1/part1.kt @@ -0,0 +1,5 @@ +package com.tymscar.day{{DAY}}.part1 + +fun solve(input: String): String { + return input +} diff --git a/templates/kotlin/part2/part2.kt b/templates/kotlin/part2/part2.kt new file mode 100644 index 0000000..7ec6883 --- /dev/null +++ b/templates/kotlin/part2/part2.kt @@ -0,0 +1,5 @@ +package com.tymscar.day{{DAY}}.part2 + +fun solve(input: String): String { + return input +}