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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
require "json"

namespace :sync do
  desc "Sync David Lanham wallpapers (assumes the unzipped updates are in ~/Downloads)"
  task :dlanham do
    rm_rf File.expand_path("~/Dropbox/wallpapers/dlanham wallpapers/Newest Additions")
    FileList[File.expand_path("~/Downloads/dlanham*")].each do |dir|
      cmd = %w[ rsync -avz --exclude *.DS_Store . ]
      cmd << File.expand_path(dir.pathmap("~/Dropbox/wallpapers/%f"))
      Dir.chdir dir do
        sh *cmd
      end
      rm_r dir
    end
  end

  desc "Sync submodules"
  task :submodules do
    sh "git submodule sync"
    sh "git submodule foreach git pull"
  end

  desc "Sync puzzles from ~/Downloads"
  task :puzzles do
    Dir[File.expand_path("~/Downloads/*")].each do |file|
      dir = case file.pathmap("%f")
            when /^diagramless\d+.pdf$/i
              "diagramless"
            when /^201\dW(?:eek)?\d.*/
              "GM"
            end
      next if dir.nil?

      dir = File.expand_path(File.join("~/Dropbox/Shared/Puzzles", dir))
      mv file, dir
    end
  end

  desc "Sync a config"
  task :config, [:config, :to] do |t, args|
    config = File.expand_path(args[:config])
    to = args[:to] || config.sub(Dir.home, '\0/.dotfiles')

    mv config, to
    ln_s to, config
  end
end

namespace :dotslash do
  # The tag recorded in an existing bin/<name>, or nil when it has yet to be
  # generated.
  def dotslash_tag(file)
    return nil unless File.exist?(file)

    providers = JSON.parse(File.read(file).sub(/\A#!.*\n/, ""))
      .dig("platforms", "macos-aarch64", "providers") || []
    provider = providers.find { |p| p["type"] == "github-release" }
    provider && provider["tag"]
  rescue JSON::ParserError
    nil
  end

  # The block receives the tag and returns a hash of DotSlash platform to
  # asset name. A `path` proc receives each asset name.
  def update_dotslash_release(name:, repo:, path: name, tag: nil, &assets)
    unless tag
      tag = JSON.parse(`gh release view --repo #{repo} --json tagName`)["tagName"]
    end

    platforms = assets.call(tag).transform_values do |asset_name|
      url = "https://github.com/#{repo}/releases/download/#{tag}/#{asset_name}"
      output = `dotslash -- create-url-entry #{url}`
      raise "create-url-entry failed for #{url}" unless $?.success?

      entry = JSON.parse(output)

      platform = {
        size: entry["size"],
        hash: "blake3",
        digest: entry["digest"],
        path: path.respond_to?(:call) ? path.call(asset_name) : path,
        providers: [
          { url: url },
          { type: "github-release", repo: "https://github.com/#{repo}", tag: tag, name: asset_name },
        ],
      }
      platform[:format] = entry["format"] if entry["format"] && !entry["format"].start_with?("TODO")
      platform
    end

    dotslash = { name: name, platforms: platforms }

    file = "bin/#{name}"
    previous_tag = dotslash_tag(file)
    contents = "#!/usr/bin/env dotslash\n\n#{JSON.pretty_generate(dotslash)}\n"
    unchanged = File.exist?(file) && File.read(file) == contents
    File.write(file, contents) unless unchanged

    puts(if previous_tag.nil?
           "#{name}: added #{tag}"
         elsif unchanged
           "#{name}: up to date (#{tag})"
         elsif previous_tag == tag
           "#{name}: regenerated (#{tag})"
         else
           "#{name}: #{previous_tag} → #{tag}"
         end)
  end

  desc "Update jq"
  task(:jq) do
    update_dotslash_release(name: "jq", repo: "jqlang/jq") do
      {
        "macos-aarch64" => "jq-macos-arm64",
        "linux-aarch64" => "jq-linux-arm64",
        "linux-x86_64" => "jq-linux-amd64",
      }
    end
  end

  desc "Update jj"
  task(:jj) do
    update_dotslash_release(name: "jj", repo: "jj-vcs/jj") do |tag|
      {
        "macos-aarch64" => "jj-#{tag}-aarch64-apple-darwin.tar.gz",
        "linux-aarch64" => "jj-#{tag}-aarch64-unknown-linux-musl.tar.gz",
        "linux-x86_64" => "jj-#{tag}-x86_64-unknown-linux-musl.tar.gz",
      }
    end
  end

  desc "Update just"
  task(:just) do
    update_dotslash_release(name: "just", repo: "casey/just") do |tag|
      {
        "macos-aarch64" => "just-#{tag}-aarch64-apple-darwin.tar.gz",
        "linux-aarch64" => "just-#{tag}-aarch64-unknown-linux-musl.tar.gz",
        "linux-x86_64" => "just-#{tag}-x86_64-unknown-linux-musl.tar.gz",
      }
    end
  end

  desc "Update fzf"
  task(:fzf) do
    update_dotslash_release(name: "fzf", repo: "junegunn/fzf") do |tag|
      version = tag.delete_prefix("v")
      {
        "macos-aarch64" => "fzf-#{version}-darwin_arm64.tar.gz",
        "linux-aarch64" => "fzf-#{version}-linux_arm64.tar.gz",
        "linux-x86_64" => "fzf-#{version}-linux_amd64.tar.gz",
      }
    end
  end

  desc "Update zizmor"
  task(:zizmor) do
    update_dotslash_release(name: "zizmor", repo: "zizmorcore/zizmor") do
      {
        "macos-aarch64" => "zizmor-aarch64-apple-darwin.tar.gz",
        "linux-aarch64" => "zizmor-aarch64-unknown-linux-gnu.tar.gz",
        "linux-x86_64" => "zizmor-x86_64-unknown-linux-gnu.tar.gz",
      }
    end
  end

  desc "Update sops"
  task(:sops) do
    update_dotslash_release(name: "sops", repo: "getsops/sops") do |tag|
      {
        "macos-aarch64" => "sops-#{tag}.darwin.arm64",
        "linux-aarch64" => "sops-#{tag}.linux.arm64",
        "linux-x86_64" => "sops-#{tag}.linux.amd64",
      }
    end
  end

  age_assets = lambda do |tag|
    {
      "macos-aarch64" => "age-#{tag}-darwin-arm64.tar.gz",
      "linux-aarch64" => "age-#{tag}-linux-arm64.tar.gz",
      "linux-x86_64" => "age-#{tag}-linux-amd64.tar.gz",
    }
  end

  desc "Update age"
  task(:age) do
    update_dotslash_release(name: "age", repo: "FiloSottile/age", path: "age/age", &age_assets)
  end

  desc "Update age-keygen"
  task("age-keygen") do
    update_dotslash_release(name: "age-keygen", repo: "FiloSottile/age", path: "age/age-keygen", &age_assets)
  end

  desc "Update tree-sitter"
  task(:tree_sitter) do
    update_dotslash_release(name: "tree-sitter", repo: "tree-sitter/tree-sitter") do
      {
        "macos-aarch64" => "tree-sitter-macos-arm64.gz",
        "linux-aarch64" => "tree-sitter-linux-arm64.gz",
        "linux-x86_64" => "tree-sitter-linux-x64.gz",
      }
    end
  end

  desc "Update ramekin"
  task(:ramekin) do
    update_dotslash_release(name: "ramekin", repo: "kejadlen/ramekin") do
      { "macos-aarch64" => "ramekin-aarch64-apple-darwin.tar.gz" }
    end
  end

  desc "Update cq"
  task(:cq) do
    update_dotslash_release(name: "cq", repo: "technicalpickles/cq", path: ->(asset) { "#{asset.delete_suffix(".tar.gz")}/cq" }) do |tag|
      version = tag.delete_prefix("v")
      {
        "macos-aarch64" => "cq-#{version}-aarch64-apple-darwin.tar.gz",
        "linux-aarch64" => "cq-#{version}-aarch64-unknown-linux-gnu.tar.gz",
        "linux-x86_64" => "cq-#{version}-x86_64-unknown-linux-gnu.tar.gz",
      }
    end
  end

  desc "Update ranger"
  task(:ranger) do
    update_dotslash_release(name: "ranger", repo: "kejadlen/ranger") do
      {
        "macos-aarch64" => "ranger-aarch64-apple-darwin.tar.gz",
        "linux-aarch64" => "ranger-aarch64-unknown-linux-gnu.tar.gz",
      }
    end
  end

  desc "Update frork"
  task(:frork) do
    update_dotslash_release(name: "frork", repo: "kejadlen/frork") do
      { "macos-aarch64" => "frork-aarch64-apple-darwin.tar.gz" }
    end
  end

  desc "Update all dotslash files"
  task all: %i[
    age
    age-keygen
    cq
    fzf
    frork
    jj
    jq
    just
    ramekin
    ranger
    sops
    tree_sitter
    zizmor
  ]
end

desc "Upgrade neovim"
task :upgrade_neovim do
  # fetch.prunetags fetches tags despite brew's --no-tags, so the moving
  # nightly/stable tags would otherwise be rejected as clobbers.
  %w[tree-sitter neovim].each do |formula|
    chdir File.expand_path("~/Library/Caches/Homebrew/#{formula}--git") do
      sh "git tag --delete nightly stable" do
        # no-op so that `sh` doesn't throw an
        # exception if a tag doesn't exist
      end
    end
  end
  sh "brew upgrade tree-sitter --fetch-HEAD"
  sh "brew upgrade neovim --fetch-HEAD"
end

namespace :pave do
  PATHS = %w[
    Downloads/
  ]

  desc "Backup files for paving"
  task :backup, [:to_dir] do |t, args|
    to_dir = args[:to_dir]

    PATHS.each do |path|
      from = File.expand_path("~/#{path}")
      to = File.expand_path("#{to_dir}/#{path}")

      mkdir_p to.pathmap("%d")
      sh *%W[ rsync --archive --delete --verbose #{from} #{to} ]
    end
  end

  desc "Restore files for paving"
  task :restore, [:from_dir] do |t, args|
    from_dir = args[:from_dir]

    PATHS.each do |path|
      from = File.expand_path("#{from_dir}/#{path}")
      to = File.expand_path("~/#{path}")

      mkdir_p to.pathmap("%d")
      sh *%W[ rsync --archive --delete --verbose #{from} #{to} ]
    end
  end
end

namespace :init do
  desc "Set up local neovim overrides"
  task "local-nvim", [:dir] do |t, args|
    dir = File.expand_path(args.fetch(:dir))

    File.write(File.join(dir, ".vimrc.local"), <<~VIMRC_LOCAL)
      lua << EOF
        package.path = "./.dev.local/?.fnl;" .. package.path
        require("vimrc")
      EOF
    VIMRC_LOCAL

    mkdir_p File.join(dir, ".dev.local")

    File.write(File.join(dir, ".dev.local", "vimrc.fnl"), <<~VIMRC_FNL)
      (local {: setup-lsp} (require :lsp))
      ;; (setup-lsp :ruby_ls)
    VIMRC_FNL
  end
end