-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen.go
184 lines (167 loc) · 4.78 KB
/
gen.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
//go:build ignore
// +build ignore
package main
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"text/template"
"time"
"github.com/MarkusFreitag/advent-of-code/util"
)
var (
year = time.Now().Year()
day = time.Now().Day()
)
func yearPkg() string { return fmt.Sprintf("year%d", year) }
func dayPkg() string { return fmt.Sprintf("day%d", day) }
func main() {
if os.Args[1] != "" {
num, err := strconv.Atoi(os.Args[1])
if err != nil {
fmt.Printf("YEAR must be a number: %s\n", err.Error())
return
}
year = num
}
if os.Args[2] != "" {
num, err := strconv.Atoi(os.Args[2])
if err != nil {
fmt.Printf("DAY must be a number: %s\n", err.Error())
return
}
day = num
}
templates, err := template.ParseGlob("templates/*.tpl")
if err != nil {
fmt.Printf("error while loading templates: %s\n", err.Error())
return
}
path := filepath.Join("puzzles", yearPkg(), dayPkg())
// check whether this day has already been generated
if _, err := os.Stat(path); !os.IsNotExist(err) {
fmt.Printf("%d_%d already exists\n", year, day)
return
}
/*
1. download the days puzzle input and save it at inputs/YEAR_DAY.txt
2. ensure the days package directory exists
3. generate the code and test files at puzzles/YEAR/DAY
4. regenerate the years code file
5. regenerate the puzzles code file
*/
input, err := util.InputFromURL(year, day, util.WithAoCUserAgent("github.com/MarkusFreitag/advent-of-code", "[email protected]"))
if err != nil {
fmt.Printf("error while downloading puzzle input for %d:%d: %s\n", year, day, err.Error())
return
}
if err := os.MkdirAll("inputs", os.ModePerm); err != nil {
fmt.Printf("error while creating input directory: %s\n", err.Error())
return
}
if err := util.InputToFile(year, day, input); err != nil {
fmt.Printf("error while saving puzzle input into file: %s\n", err.Error())
return
}
if err := os.MkdirAll(path, os.ModePerm); err != nil {
fmt.Printf("error while creating package directories: %s\n", err.Error())
return
}
dayData := struct {
Package string
}{
Package: dayPkg(),
}
fd, err := os.OpenFile(filepath.Join(path, fmt.Sprintf("%s.go", dayPkg())), os.O_RDWR|os.O_CREATE, 0664)
if err != nil {
fmt.Printf("error while creating code file: %s\n", err.Error())
return
}
if err := templates.ExecuteTemplate(fd, "day.tpl", dayData); err != nil {
fmt.Printf("error while generating code: %s\n", err.Error())
return
}
if err := fd.Close(); err != nil {
fmt.Printf("error while saving code file: %s\n", err.Error())
return
}
fd, err = os.OpenFile(filepath.Join(path, fmt.Sprintf("%s_test.go", dayPkg())), os.O_RDWR|os.O_CREATE, 0664)
if err != nil {
fmt.Printf("error while creating testcode file: %s\n", err.Error())
return
}
if err := templates.ExecuteTemplate(fd, "day_test.tpl", dayData); err != nil {
fmt.Printf("error while generating testcode: %s\n", err.Error())
return
}
if err := fd.Close(); err != nil {
fmt.Printf("error while saving testcode file: %s\n", err.Error())
return
}
yearData := struct {
Package string
Imports []string
Days []string
}{
Package: yearPkg(),
Imports: make([]string, 0),
Days: make([]string, 0),
}
entries, err := ioutil.ReadDir(filepath.Join("puzzles", yearPkg()))
if err != nil {
fmt.Print("err: %s\n", err.Error())
return
}
for _, entry := range entries {
if entry.IsDir() {
yearData.Imports = append(
yearData.Imports,
fmt.Sprintf("github.com/MarkusFreitag/advent-of-code/puzzles/%s/%s", yearPkg(), entry.Name()),
)
yearData.Days = append(yearData.Days, entry.Name())
}
}
fd, err = os.OpenFile(filepath.Join("puzzles", yearPkg(), fmt.Sprintf("%s.go", yearPkg())), os.O_RDWR|os.O_CREATE, 0664)
if err != nil {
fmt.Printf("error while creating code file: %s\n", err.Error())
return
}
if err := templates.ExecuteTemplate(fd, "year.tpl", yearData); err != nil {
fmt.Printf("error while generating code: %s\n", err.Error())
return
}
if err := fd.Close(); err != nil {
fmt.Printf("error while saving code file: %s\n", err.Error())
return
}
puzzlesData := struct {
Years []string
}{
Years: make([]string, 0),
}
entries, err = ioutil.ReadDir("puzzles")
if err != nil {
fmt.Print("err: %s\n", err.Error())
return
}
for _, entry := range entries {
if entry.IsDir() {
puzzlesData.Years = append(puzzlesData.Years, entry.Name())
}
}
fd, err = os.OpenFile(filepath.Join("puzzles", "puzzles.go"), os.O_RDWR|os.O_CREATE, 0664)
if err != nil {
fmt.Printf("error while creating code file: %s\n", err.Error())
return
}
if err := templates.ExecuteTemplate(fd, "puzzles.tpl", puzzlesData); err != nil {
fmt.Printf("error while generating code: %s\n", err.Error())
return
}
if err := fd.Close(); err != nil {
fmt.Printf("error while saving code file: %s\n", err.Error())
return
}
}