Skip to content

Commit

Permalink
Create kotlin template generator GHA
Browse files Browse the repository at this point in the history
  • Loading branch information
tymscar committed Dec 4, 2024
1 parent 4d75d70 commit a56c93a
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 0 deletions.
59 changes: 59 additions & 0 deletions .github/workflows/kotlin-template.yaml
Original file line number Diff line number Diff line change
@@ -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 "[email protected]"
- 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
13 changes: 13 additions & 0 deletions templates/kotlin/day.kt
Original file line number Diff line number Diff line change
@@ -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)}")
}
5 changes: 5 additions & 0 deletions templates/kotlin/part1/part1.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.tymscar.day{{DAY}}.part1

fun solve(input: String): String {
return input
}
5 changes: 5 additions & 0 deletions templates/kotlin/part2/part2.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.tymscar.day{{DAY}}.part2

fun solve(input: String): String {
return input
}

0 comments on commit a56c93a

Please sign in to comment.