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
$LOAD_PATH.unshift File.expand_path("lib", __dir__)

require "minitest/test_task"
require "pathname"

Minitest::TestTask.create

directory ".direnv"

desc "Create bundler binstubs (runs when Gemfile.lock changes)"
file ".direnv/.bundled" => ["Gemfile.lock"] do
  sh "bundle binstubs --all"
  touch ".direnv/.bundled"
end

task binstubs: ".direnv/.bundled"

desc "Start dev server with auto-restart, served via Tailscale"
task :dev do
  sh "tailscale serve --bg 9292"
  sh "fd | entr -r rackup"
end

desc "Seed database with sample series and tasks"
task :seed do
  require "ketchup/seed"

  Ketchup::DB[:tasks].delete
  Ketchup::DB[:series].delete

  user = Ketchup::User.first || abort("No users yet — visit the app first to create one")

  Ketchup::Seed.call(user: user, series: Ketchup::Seed::DATA)
  puts "Seeded #{Ketchup::Seed::DATA.length} series for #{user.login}"
end

namespace :snapshots do
  cache_dir = Pathname(ENV.fetch("XDG_CACHE_HOME", "~/.cache")).expand_path / "ketchup" / "snapshots"
  css_sources = {
    "public/css/reset.css" => "reset.css",
    "public/css/utopia.css" => "utopia.css",
    "templates/snapshots.css" => "snapshots.css",
  }

  directory cache_dir

  css_targets = css_sources.map { |src, basename|
    target = cache_dir / basename
    file target => [cache_dir, src] do
      cp src, target
    end
    target
  }

  desc "Capture screenshots of the app in key states"
  task :capture do
    ENV["DATABASE_URL"] = ":memory:"
    ENV["CHANGE_ID"] ||= "abcd1234"
    ENV["COMMIT_SHA"] ||= "abc1234"
    ENV["BUILD_DATE"] ||= "2025-01-01"
    require "ketchup/snapshots"

    output_dir = cache_dir / "current"
    Ketchup::Snapshots::Capture.new(output_dir: output_dir).call

    if ENV["CI"]
      require "json"
      puts({ output_dir: output_dir }.to_json)
    end
  end

  desc "Compare current screenshots against baseline from latest release"
  task diff: [:capture, *css_targets] do
    require "erb"
    require "ketchup/snapshots"

    base_dir = cache_dir
    baseline_dir = base_dir / "baseline"
    current_dir = base_dir / "current"

    tag_file = base_dir / ".baseline-tag"
    latest_tag = `gh release view --repo kejadlen/ketchup --json tagName -q .tagName 2>/dev/null`.strip
    cached_tag = tag_file.exist? ? tag_file.read.strip : nil

    if latest_tag.empty?
      puts "No release found — showing current screenshots only"
    elsif cached_tag == latest_tag && baseline_dir.exist? && baseline_dir.glob("*/manifest.json").any?
      puts "Baseline #{latest_tag} already cached"
    else
      FileUtils.rm_rf(baseline_dir)
      baseline_dir.mkpath

      tarball = base_dir / "baseline.tar.gz"
      system("gh", "release", "download", latest_tag, "--repo", "kejadlen/ketchup", "--pattern", "snapshots.tar.gz", "--output", tarball.to_s, "--clobber", exception: true)
      system("tar", "xzf", tarball.to_s, "-C", baseline_dir.to_s, exception: true)
      tarball.delete
      tag_file.write(latest_tag)
      puts "Downloaded baseline from release #{latest_tag}"
    end

    snapshots_by_viewport = Ketchup::Snapshots::Diff.new(baseline_dir: baseline_dir, current_dir: current_dir).comparisons_by_viewport

    template = (Pathname(__dir__) / "templates/snapshot_diff.erb").read
    output_path = base_dir / "diff.html"
    output_path.write(ERB.new(template, trim_mode: "-").result_with_hash(snapshots_by_viewport: snapshots_by_viewport))
    puts "Diff viewer: #{output_path}"
  end

  desc "Capture, diff, and open the viewer"
  task review: :diff do
    system("open", (cache_dir / "diff.html").to_s)
  end

  desc "Generate gallery HTML from images in a directory"
  task :gallery, [:images_dir, :output_path] do |_t, args|
    require "erb"
    require "ketchup/snapshots"

    images_dir = Pathname(args.fetch(:images_dir) { cache_dir / "current" })
    output_path = Pathname(args.fetch(:output_path) { cache_dir / "gallery.html" })

    css_sources.each do |src, _basename|
      cp src, output_path.dirname.to_s
    end

    title = "Ketchup Snapshots"
    images_by_viewport = Ketchup::Snapshots::VIEWPORTS.to_h do |viewport, _size|
      viewport_dir = images_dir / viewport
      entries = Ketchup::Snapshots::Entry.read_manifest(viewport_dir)
      images_rel = viewport_dir.relative_path_from(output_path.dirname)
      [viewport, entries.map { |entry| { entry: entry, filename: (images_rel / "#{entry.name}.png").to_s } }]
    end

    template = (Pathname(__dir__) / "templates/snapshot_gallery.erb").read
    output_path.write(ERB.new(template, trim_mode: "-").result_with_hash(title: title, images_by_viewport: images_by_viewport))
    puts output_path
  end
end

namespace :cdn do
  desc "Update integrity hashes for CDN scripts in layout.rb"
  task :rehash do
    layout = "lib/ketchup/views/layout.rb"
    content = File.read(layout)

    content.gsub!(%r{src:\s*"(https://[^"]+)".*\n\s*integrity:\s*"sha384-[^"]+"}) do |match|
      url = match[/"(https:\/\/[^"]+)"/, 1]
      hash = `curl -sL '#{url}' | openssl dgst -sha384 -binary | openssl base64 -A`.strip
      abort "Failed to fetch #{url}" if hash.empty?
      puts "#{url} -> sha384-#{hash}"
      match.sub(/sha384-[^"]+/, "sha384-#{hash}")
    end

    File.write(layout, content)
  end
end

desc "Generate RBS from inline annotations and run Steep type checker"
task :check do
  sh "rbs-inline --output lib/"
  sh "steep check"
end

task default: %i[ test check binstubs ]