|
| 1 | +package solutions |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "strconv" |
| 8 | + "text/template" |
| 9 | + |
| 10 | + "github.com/obalunenko/advent-of-code/internal/puzzles/solutions/templates" |
| 11 | +) |
| 12 | + |
| 13 | +func createNewFromTemplate(purl string) error { |
| 14 | + const ( |
| 15 | + perms = 0o655 |
| 16 | + yearLen = 4 |
| 17 | + dayLen = 2 |
| 18 | + ) |
| 19 | + |
| 20 | + pd, err := parsePuzzleURL(purl) |
| 21 | + if err != nil { |
| 22 | + return fmt.Errorf("parse puzzle url %q: %w", purl, err) |
| 23 | + } |
| 24 | + |
| 25 | + day := strconv.Itoa(pd.day) |
| 26 | + if len(day) < dayLen { |
| 27 | + day = "0" + day |
| 28 | + } |
| 29 | + |
| 30 | + if len(day) != dayLen { |
| 31 | + return fmt.Errorf("invalid day: %s", day) |
| 32 | + } |
| 33 | + |
| 34 | + year := strconv.Itoa(pd.year) |
| 35 | + |
| 36 | + if len(year) != yearLen { |
| 37 | + return fmt.Errorf("invalid year: %s", year) |
| 38 | + } |
| 39 | + |
| 40 | + params := templates.Params{ |
| 41 | + Year: year, |
| 42 | + Day: pd.day, |
| 43 | + DayStr: day, |
| 44 | + URL: purl, |
| 45 | + } |
| 46 | + |
| 47 | + path := filepath.Clean(filepath.Join(year, "day"+day)) |
| 48 | + |
| 49 | + if err = createPuzzleDir(path, perms); err != nil { |
| 50 | + return fmt.Errorf("failed to create puzzle dir: %w", err) |
| 51 | + } |
| 52 | + |
| 53 | + testdata := filepath.Clean(filepath.Join(path, "testdata")) |
| 54 | + |
| 55 | + if err = createTestdata(testdata, perms); err != nil { |
| 56 | + return fmt.Errorf("failed to create testdata: %w", err) |
| 57 | + } |
| 58 | + |
| 59 | + tmplsFns := []func() (*template.Template, error){ |
| 60 | + templates.SolutionTmpl, templates.SolutionTestTmpl, templates.SpecTmpl, |
| 61 | + } |
| 62 | + |
| 63 | + for _, tmplFn := range tmplsFns { |
| 64 | + var tmpl *template.Template |
| 65 | + |
| 66 | + tmpl, err = tmplFn() |
| 67 | + if err != nil { |
| 68 | + return fmt.Errorf("failed to get template: %w", err) |
| 69 | + } |
| 70 | + |
| 71 | + if err = createFromTemplate(tmpl, path, perms, params); err != nil { |
| 72 | + return fmt.Errorf("failed to create from template: %w", err) |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + return nil |
| 77 | +} |
| 78 | + |
| 79 | +func createFromTemplate(tmpl *template.Template, path string, perms os.FileMode, params templates.Params) error { |
| 80 | + fpath := filepath.Clean(filepath.Join(path, tmpl.Name())) |
| 81 | + |
| 82 | + if isExist(fpath) { |
| 83 | + return nil |
| 84 | + } |
| 85 | + |
| 86 | + var content []byte |
| 87 | + |
| 88 | + content, err := templates.SubstituteTemplate(tmpl, params) |
| 89 | + if err != nil { |
| 90 | + return fmt.Errorf("failed to substitute template: %w", err) |
| 91 | + } |
| 92 | + |
| 93 | + if err = os.WriteFile(fpath, content, perms); err != nil { |
| 94 | + return fmt.Errorf("failed to write file: %w", err) |
| 95 | + } |
| 96 | + |
| 97 | + return nil |
| 98 | +} |
| 99 | + |
| 100 | +func createPuzzleDir(path string, perms os.FileMode) error { |
| 101 | + if !isExist(path) { |
| 102 | + if err := os.MkdirAll(path, perms); err != nil { |
| 103 | + return fmt.Errorf("failed to create dir: %w", err) |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + return nil |
| 108 | +} |
| 109 | + |
| 110 | +func createTestdata(path string, perms os.FileMode) error { |
| 111 | + if !isExist(path) { |
| 112 | + if err := os.MkdirAll(path, perms); err != nil { |
| 113 | + return fmt.Errorf("failed to create dir: %w", err) |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + input := filepath.Clean(filepath.Join(path, "input.txt")) |
| 118 | + |
| 119 | + if !isExist(input) { |
| 120 | + var f *os.File |
| 121 | + |
| 122 | + f, err := os.Create(input) |
| 123 | + if err != nil { |
| 124 | + return fmt.Errorf("failed to create file: %w", err) |
| 125 | + } |
| 126 | + |
| 127 | + if err = f.Close(); err != nil { |
| 128 | + return fmt.Errorf("failed to close file: %w", err) |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + return nil |
| 133 | +} |
| 134 | + |
| 135 | +func isExist(path string) bool { |
| 136 | + stat, err := os.Stat(path) |
| 137 | + if err != nil && !os.IsNotExist(err) { |
| 138 | + return false |
| 139 | + } |
| 140 | + |
| 141 | + return stat != nil && stat.Name() != "" |
| 142 | +} |
| 143 | + |
| 144 | +type puzzleDate struct { |
| 145 | + year int |
| 146 | + day int |
| 147 | +} |
| 148 | + |
| 149 | +func parsePuzzleURL(url string) (puzzleDate, error) { |
| 150 | + const ( |
| 151 | + urlFmt = "https://adventofcode.com/%d/day/%d" |
| 152 | + paramsNum = 2 |
| 153 | + ) |
| 154 | + |
| 155 | + var year, day int |
| 156 | + |
| 157 | + n, err := fmt.Sscanf(url, urlFmt, &year, &day) |
| 158 | + if err != nil { |
| 159 | + return puzzleDate{}, fmt.Errorf("parse puzzle url: %w", err) |
| 160 | + } |
| 161 | + |
| 162 | + if n != paramsNum { |
| 163 | + return puzzleDate{}, fmt.Errorf("invalid puzzle url: %s", url) |
| 164 | + } |
| 165 | + |
| 166 | + return puzzleDate{ |
| 167 | + year: year, |
| 168 | + day: day, |
| 169 | + }, nil |
| 170 | +} |
0 commit comments