Skip to content

Instantly share code, notes, and snippets.

@alex-quiterio
Last active September 4, 2026 22:43
Show Gist options
  • Select an option

  • Save alex-quiterio/1bc58cf2e8551ddb445c664525dc9961 to your computer and use it in GitHub Desktop.

Select an option

Save alex-quiterio/1bc58cf2e8551ddb445c664525dc9961 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# install_gist.rb — installs a gist by ID into a directory
#
# Fetches the gist, downloads its file(s), and registers them in .gist-ids
#
# Setup:
# 1. Install gh CLI: https://cli.github.com
# 2. Run: gh auth login
#
# Usage:
# ruby install_gist.rb <gist_id> [directory] # defaults to current dir
require "open3"
require "json"
# ── args ──────────────────────────────────────────────────────────────────────
gist_id = ARGV[0]
dir = ARGV[1] || "."
unless gist_id
abort "Usage: ruby install_gist.rb <gist_id> [directory]"
end
mapping_file = File.join(dir, ".gist-ids")
# ── checks ────────────────────────────────────────────────────────────────────
unless system("which gh > /dev/null 2>&1")
abort "❌ gh CLI not found. Install it: https://cli.github.com"
end
Dir.mkdir(dir) unless Dir.exist?(dir)
# ── fetch gist metadata ───────────────────────────────────────────────────────
puts "🔍 Fetching gist #{gist_id}..."
stdout, stderr, status = Open3.capture3("gh", "api", "/gists/#{gist_id}")
unless status.success?
abort "❌ Could not fetch gist: #{stderr.strip}"
end
gist = JSON.parse(stdout.force_encoding("UTF-8"))
files = gist["files"]
if files.nil? || files.empty?
abort "❌ Gist has no files"
end
# ── load existing mappings ────────────────────────────────────────────────────
existing = {}
if File.exist?(mapping_file)
File.foreach(mapping_file, encoding: "UTF-8") do |line|
line.strip!
next if line.empty? || line.start_with?("#")
fname, gid = line.split("=", 2).map(&:strip)
existing[fname] = gid if fname && gid
end
end
# ── install each file ─────────────────────────────────────────────────────────
puts ""
installed = 0
skipped = 0
files.each do |filename, meta|
filepath = File.join(dir, filename)
if existing[filename]
if existing[filename] == gist_id
puts "⏭ '#{filename}' already registered — skipping"
else
puts "⚠️ '#{filename}' already registered with a different gist ID — skipping"
end
skipped += 1
next
end
# content is already in the metadata response, unless the file is too big
print "↓ Downloading '#{filename}'... "
content = meta["content"]
if meta["truncated"] || content.nil?
content, content_stderr, content_status = Open3.capture3(
"gh", "gist", "view", gist_id, "--filename", filename, "--raw"
)
unless content_status.success?
puts "❌ failed"
$stderr.puts " #{content_stderr.strip}"
next
end
end
File.write(filepath, content.force_encoding("UTF-8"), mode: "w:UTF-8")
puts "✅"
# register in .gist-ids
File.open(mapping_file, "a") { |f| f.puts "#{filename}=#{gist_id}" }
existing[filename] = gist_id
installed += 1
end
# ── summary ───────────────────────────────────────────────────────────────────
puts ""
puts "─────────────────────────────"
puts "✅ Installed : #{installed}"
puts "⏭ Skipped : #{skipped}" if skipped > 0
puts ""
puts "📄 .gist-ids updated: #{File.realpath(mapping_file)}" if installed > 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment