Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions bin/setup
Original file line number Diff line number Diff line change
@@ -1,12 +1,30 @@
#!/usr/bin/env ruby
require "fileutils"
require "socket"
require "timeout"

REDIS_PORT = 6379
REDIS_HOST = "localhost"
APP_ROOT = File.expand_path("..", __dir__)

def system!(*args)
system(*args, exception: true)
end

def installed?(tool)
system("command -v #{tool} > /dev/null 2>&1")
end

def redis_running?
Timeout::timeout(3) do
socket = TCPSocket.new(REDIS_HOST, REDIS_PORT)
socket.close
return true
end
rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH, SocketError, Timeout::Error
return false
end

if ENV["RAILS_ENV"] == "production"
puts "RAILS_ENV is production; bailing out"
exit
Expand All @@ -15,6 +33,15 @@ end
FileUtils.chdir APP_ROOT do
puts "== Installing dependencies =="
system "mise install"

if installed?("brew")
system "brew install sqlite ffmpeg"
elsif installed?("pacman")
system "sudo pacman -S --noconfirm --needed sqlite ffmpeg"
elsif installed?("apt")
system "sudo apt-get install --no-install-recommends -y libsqlite3-0 ffmpeg"
end

system("bundle check") || system!("bundle install")

puts "\n== Preparing database =="
Expand All @@ -24,6 +51,16 @@ FileUtils.chdir APP_ROOT do
end
system! "bin/rails db:prepare"

unless redis_running?
if installed?("docker")
system("docker run -d --name campfire-redis -p #{REDIS_PORT}:#{REDIS_PORT} redis:7")
else
puts "Couldn't start Redis"
puts "Install either docker or redis and then run this command again"
exit 1
end
end

puts "\n== Removing old logs and tempfiles =="
system! "bin/rails log:clear tmp:clear"

Expand Down