|
| 1 | +#!/usr/bin/env ruby |
| 2 | + |
| 3 | +# frozen_string_literal: true |
| 4 | + |
| 5 | +require 'optparse' |
| 6 | +require 'fileutils' |
| 7 | + |
| 8 | +class SetupScript |
| 9 | + WORKSPACE_FOLDERS = %w[ |
| 10 | + sentry-ruby |
| 11 | + sentry-rails |
| 12 | + sentry-sidekiq |
| 13 | + sentry-delayed_job |
| 14 | + sentry-resque |
| 15 | + sentry-opentelemetry |
| 16 | + ].freeze |
| 17 | + |
| 18 | + def initialize |
| 19 | + @options = { |
| 20 | + only_folders: [], |
| 21 | + only_bundle: false, |
| 22 | + only_npm: false, |
| 23 | + with_foreman: false |
| 24 | + } |
| 25 | + @workspace_root = '/workspace/sentry' |
| 26 | + end |
| 27 | + |
| 28 | + def run(args) |
| 29 | + parse_options(args) |
| 30 | + |
| 31 | + puts "🚀 Running post-create setup script..." |
| 32 | + |
| 33 | + Dir.chdir(@workspace_root) |
| 34 | + |
| 35 | + if should_run_bundle? |
| 36 | + cleanup_ruby_lsp_directories |
| 37 | + install_bundle_dependencies |
| 38 | + install_foreman_gem if @options[:with_foreman] |
| 39 | + end |
| 40 | + |
| 41 | + if should_run_npm? |
| 42 | + install_npm_dependencies |
| 43 | + end |
| 44 | + |
| 45 | + puts "✅ Post-create setup completed!" |
| 46 | + end |
| 47 | + |
| 48 | + private |
| 49 | + |
| 50 | + def parse_options(args) |
| 51 | + parser = OptionParser.new do |opts| |
| 52 | + opts.banner = "Usage: setup.rb [options]" |
| 53 | + |
| 54 | + opts.on("--only FOLDERS", Array, "Only process specified folders (comma-separated)") do |folders| |
| 55 | + @options[:only_folders] = folders |
| 56 | + end |
| 57 | + |
| 58 | + opts.on("--only-bundle", "Only run bundle operations (skip npm)") do |
| 59 | + @options[:only_bundle] = true |
| 60 | + end |
| 61 | + |
| 62 | + opts.on("--only-npm", "Only run npm operations (skip bundle)") do |
| 63 | + @options[:only_npm] = true |
| 64 | + end |
| 65 | + |
| 66 | + opts.on("--with-foreman", "Install foreman gem (default: false)") do |
| 67 | + @options[:with_foreman] = true |
| 68 | + end |
| 69 | + |
| 70 | + opts.on("-h", "--help", "Show this help message") do |
| 71 | + puts opts |
| 72 | + exit |
| 73 | + end |
| 74 | + end |
| 75 | + |
| 76 | + parser.parse!(args) |
| 77 | + |
| 78 | + if @options[:only_bundle] && @options[:only_npm] |
| 79 | + puts "❌ Error: --only-bundle and --only-npm are mutually exclusive" |
| 80 | + exit 1 |
| 81 | + end |
| 82 | + end |
| 83 | + |
| 84 | + def should_run_bundle? |
| 85 | + !@options[:only_npm] |
| 86 | + end |
| 87 | + |
| 88 | + def should_run_npm? |
| 89 | + !@options[:only_bundle] |
| 90 | + end |
| 91 | + |
| 92 | + def target_folders |
| 93 | + return @options[:only_folders] unless @options[:only_folders].empty? |
| 94 | + WORKSPACE_FOLDERS |
| 95 | + end |
| 96 | + |
| 97 | + def all_folders |
| 98 | + ['.'] + target_folders |
| 99 | + end |
| 100 | + |
| 101 | + def cleanup_ruby_lsp_directories |
| 102 | + puts "🧹 Cleaning up .ruby-lsp directories..." |
| 103 | + |
| 104 | + all_folders.each do |folder| |
| 105 | + ruby_lsp_path = File.join(folder, '.ruby-lsp') |
| 106 | + if Dir.exist?(ruby_lsp_path) |
| 107 | + puts " Removing #{folder}/.ruby-lsp" |
| 108 | + FileUtils.rm_rf(ruby_lsp_path) |
| 109 | + end |
| 110 | + end |
| 111 | + end |
| 112 | + |
| 113 | + def install_bundle_dependencies |
| 114 | + puts "📦 Installing bundle dependencies for workspace folders..." |
| 115 | + |
| 116 | + target_folders.each do |folder| |
| 117 | + folder_path = File.join(@workspace_root, folder) |
| 118 | + gemfile_path = File.join(folder_path, 'Gemfile') |
| 119 | + |
| 120 | + if Dir.exist?(folder_path) && File.exist?(gemfile_path) |
| 121 | + Dir.chdir(folder_path) do |
| 122 | + run_with_spinner(" #{folder}") do |
| 123 | + system('bundle install > /dev/null 2>&1') |
| 124 | + end |
| 125 | + end |
| 126 | + |
| 127 | + Dir.chdir(@workspace_root) |
| 128 | + else |
| 129 | + puts " Skipping #{folder} (no Gemfile found or directory doesn't exist)" |
| 130 | + end |
| 131 | + end |
| 132 | + end |
| 133 | + |
| 134 | + def install_foreman_gem |
| 135 | + run_with_spinner("💎 Installing foreman gem") do |
| 136 | + system('gem install foreman > /dev/null 2>&1') |
| 137 | + end |
| 138 | + end |
| 139 | + |
| 140 | + def install_npm_dependencies |
| 141 | + svelte_mini_path = File.join(@workspace_root, 'spec/apps/svelte-mini') |
| 142 | + |
| 143 | + if Dir.exist?(svelte_mini_path) |
| 144 | + Dir.chdir(svelte_mini_path) do |
| 145 | + run_with_spinner("📦 Installing npm dependencies for e2e tests") do |
| 146 | + system('npm install > /dev/null 2>&1') |
| 147 | + end |
| 148 | + end |
| 149 | + else |
| 150 | + puts "❌ Svelte mini app directory not found: #{svelte_mini_path}" |
| 151 | + exit 1 |
| 152 | + end |
| 153 | + end |
| 154 | + |
| 155 | + def run_with_spinner(message) |
| 156 | + print "#{message} " |
| 157 | + |
| 158 | + spinner_chars = %w[- \\ | /] |
| 159 | + spinner_index = 0 |
| 160 | + spinner_running = true |
| 161 | + |
| 162 | + spinner_thread = Thread.new do |
| 163 | + while spinner_running |
| 164 | + print "\r#{message} #{spinner_chars[spinner_index]}" |
| 165 | + spinner_index = (spinner_index + 1) % spinner_chars.length |
| 166 | + sleep 0.1 |
| 167 | + end |
| 168 | + end |
| 169 | + |
| 170 | + # Run the actual command |
| 171 | + success = yield |
| 172 | + |
| 173 | + # Stop spinner |
| 174 | + spinner_running = false |
| 175 | + spinner_thread.join |
| 176 | + |
| 177 | + if success |
| 178 | + puts "\r#{message} ✅" |
| 179 | + else |
| 180 | + puts "\r#{message} ❌" |
| 181 | + puts "❌ Command failed. Please check for missing dependencies or run manually." |
| 182 | + exit 1 |
| 183 | + end |
| 184 | + end |
| 185 | +end |
| 186 | + |
| 187 | +if __FILE__ == $0 |
| 188 | + SetupScript.new.run(ARGV) |
| 189 | +end |
0 commit comments