|
| 1 | +#!/usr/bin/env ruby |
| 2 | +# frozen_string_literal: true |
| 3 | + |
| 4 | +require "date" |
| 5 | +require "fileutils" |
| 6 | + |
| 7 | +def usage |
| 8 | + puts <<~USAGE |
| 9 | + Usage: bin/event <command> [options] |
| 10 | +
|
| 11 | + Commands: |
| 12 | + new <event_date> <title> Create a new event post. <event_date> must be YYYY-MM-DD. |
| 13 | + Example: bin/event new 2026-08-20 "Intro to Hotwire" |
| 14 | + publish Commit and push the new events to the repository to trigger a deployment. |
| 15 | + USAGE |
| 16 | +end |
| 17 | + |
| 18 | +def slugify(string) |
| 19 | + string.downcase.strip.gsub(' ', '-').gsub(/[^\w-]/, '') |
| 20 | +end |
| 21 | + |
| 22 | +def cmd_new(args) |
| 23 | + if args.length < 2 |
| 24 | + puts "Error: Missing event date or title." |
| 25 | + usage |
| 26 | + exit 1 |
| 27 | + end |
| 28 | + |
| 29 | + date_str = args[0] |
| 30 | + title = args[1..].join(" ") |
| 31 | + |
| 32 | + begin |
| 33 | + Date.parse(date_str) |
| 34 | + rescue Date::Error |
| 35 | + puts "Error: Invalid date format. Please use YYYY-MM-DD." |
| 36 | + exit 1 |
| 37 | + end |
| 38 | + |
| 39 | + slug = slugify(title) |
| 40 | + publish_date = Date.today.strftime("%Y-%m-%d") |
| 41 | + filename = "src/_posts/#{publish_date}-#{slug}.md" |
| 42 | + |
| 43 | + if File.exist?(filename) |
| 44 | + puts "Error: File #{filename} already exists." |
| 45 | + exit 1 |
| 46 | + end |
| 47 | + |
| 48 | + content = <<~POST |
| 49 | + --- |
| 50 | + layout: post |
| 51 | + title: "#{title}" |
| 52 | + event_date: "#{date_str}" |
| 53 | + tags: ["meetup"] |
| 54 | + # meetup_link: "https://www.meetup.com/ruby-az/events/..." |
| 55 | + --- |
| 56 | +
|
| 57 | + Join us for **#{title}**! |
| 58 | +
|
| 59 | + More details coming soon. |
| 60 | + POST |
| 61 | + |
| 62 | + File.write(filename, content) |
| 63 | + puts "Created new event post at #{filename}" |
| 64 | +end |
| 65 | + |
| 66 | +def cmd_publish(args) |
| 67 | + puts "Staging changes..." |
| 68 | + unless system("git add src/_posts/") |
| 69 | + puts "Failed to add changes to git." |
| 70 | + exit 1 |
| 71 | + end |
| 72 | + |
| 73 | + puts "Committing changes..." |
| 74 | + unless system("git commit -m 'Add new event post'") |
| 75 | + puts "No new changes to commit, or commit failed." |
| 76 | + end |
| 77 | + |
| 78 | + puts "Pushing to remote to trigger deployment..." |
| 79 | + unless system("git push") |
| 80 | + puts "Failed to push to remote." |
| 81 | + exit 1 |
| 82 | + end |
| 83 | + |
| 84 | + puts "Successfully published events!" |
| 85 | +end |
| 86 | + |
| 87 | +case ARGV[0] |
| 88 | +when "new" |
| 89 | + cmd_new(ARGV[1..]) |
| 90 | +when "publish" |
| 91 | + cmd_publish(ARGV[1..]) |
| 92 | +when "-h", "--help", "help", nil |
| 93 | + usage |
| 94 | +else |
| 95 | + puts "Unknown command: #{ARGV[0]}" |
| 96 | + usage |
| 97 | + exit 1 |
| 98 | +end |
0 commit comments