Skip to content

Commit 6e17bb8

Browse files
use erb templates in new rake task
1 parent d97ca8c commit 6e17bb8

File tree

3 files changed

+64
-10
lines changed

3 files changed

+64
-10
lines changed

2024/ruby/Rakefile

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,60 @@
11
# frozen_string_literal: true
22

3-
require 'fileutils'
3+
require "date"
4+
require "erb"
5+
require "net/http"
6+
require "open-uri"
7+
require "fileutils"
48

59
def next_advent_number
6-
latest = Dir['./lib/advent*.rb'].map { |p| p.match(/\d{2}/)&.[](0)&.to_i }.compact.max || 0
10+
latest = Dir["./lib/advent*.rb"].map { |p| p.match(/\d{2}/)&.[](0)&.to_i }.compact.max || 0
711
latest + 1
812
end
913

10-
desc 'create class and spec file for DAY'
14+
desc "create class and spec file for DAY"
1115
task :new do
12-
day = ENV['DAY'].nil? ? next_advent_number : ENV['DAY'].to_i
16+
today = Date.today
1317

14-
raise 'invalid DAY' unless day.between?(1, 31)
18+
day = ENV["DAY"].nil? ? next_advent_number : ENV["DAY"].to_i
19+
year = ENV["YEAR"].nil? ? today.year : ENV["YEAR"].to_i
1520

16-
formatted_day = day.to_s.rjust(2, '0')
21+
raise "invalid DAY" unless day.between?(1, 31)
22+
raise "invalid YEAR" unless year.between?(2015, today.year)
1723

18-
FileUtils.touch("./lib/advent_#{formatted_day}.rb")
19-
FileUtils.touch("./spec/advent_#{formatted_day}_spec.rb")
20-
FileUtils.touch("./spec/fixtures/advent-#{formatted_day}.txt")
21-
FileUtils.touch("./spec/fixtures/advent-#{formatted_day}-sample.txt")
24+
html = Net::HTTP.get(URI.parse("https://adventofcode.com/#{year}/day/#{day}"))
25+
26+
title = html.match(%r{<h2>--- Day \d+: ([A-Za-z0-9\s-]+) ---</h2>}) { |m| m[1] }.to_s
27+
28+
formatted_title = title.tr("-", "").tr(" ", "").gsub(/(.)([A-Z])/, '\1_\2').downcase
29+
formatted_day = day.to_s.rjust(2, "0")
30+
31+
basename = "advent_#{formatted_day}_#{formatted_title}"
32+
33+
templates = [
34+
{ template: "./templates/klass.rb.erb", path: "./lib/#{basename}.rb" },
35+
{ template: "./templates/spec.rb.erb", path: "./spec/#{basename}_spec.rb" }
36+
]
37+
38+
templates.each do |h|
39+
path = h[:path]
40+
next if File.exist?(path)
41+
42+
erb = ERB.new(File.read(h[:template]))
43+
rendered = erb.result_with_hash(require_path: basename, day_number: formatted_day)
44+
45+
puts "creating #{path}"
46+
puts rendered.inspect
47+
puts "----------------"
48+
49+
File.write(path, rendered)
50+
end
51+
52+
puzzle_inputs = [
53+
"advent-#{formatted_day}-sample.txt",
54+
"advent-#{formatted_day}.txt"
55+
]
56+
57+
puzzle_inputs.each do |name|
58+
FileUtils.touch("./spec/fixtures/#{name}")
59+
end
2260
end

2024/ruby/templates/klass.rb.erb

Whitespace-only changes.

2024/ruby/templates/spec.rb.erb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
require "spec_helper"
2+
require "<%= require_path %>"
3+
4+
describe "Puzzle" do
5+
context "with sample data" do
6+
let(:data) do
7+
File.readlines("./spec/fixtures/advent-<%= day_number %>-sample.txt").map { |l| l.chomp.chars }
8+
end
9+
end
10+
11+
context "with puzzle data" do
12+
let(:data) do
13+
File.readlines("./spec/fixtures/advent-<%= day_number %>.txt").map { |l| l.chomp.chars }
14+
end
15+
end
16+
end

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy