Skip to content

Commit

Permalink
Fixed setup
Browse files Browse the repository at this point in the history
  • Loading branch information
yatish27 committed May 14, 2024
1 parent 8092ccf commit 14822ef
Show file tree
Hide file tree
Showing 2 changed files with 251 additions and 2 deletions.
243 changes: 243 additions & 0 deletions bin/configure
Original file line number Diff line number Diff line change
@@ -0,0 +1,243 @@
#!/usr/bin/env ruby

puts ""
puts `gem install colorize`
puts ""

puts ""
puts `gem install activesupport`
puts ""

require 'colorize'
require 'active_support'

def replace_in_file(file, before, after, target_regexp = nil)
puts "Replacing in '#{file}'."
if target_regexp
target_file_content = ""
File.open(file).each_line do |l|
l.gsub!(before, after) if !!l.match(target_regexp)
l if !!l.match(target_regexp)
target_file_content += l
end
else
target_file_content = File.open(file).read
target_file_content.gsub!(before, after)
end
File.open(file, "w+") do |f|
f.write(target_file_content)
end
end

def stream(command, prefix = " ")
puts ""
IO.popen(command) do |io|
while (line = io.gets) do
puts "#{prefix}#{line}"
end
end
puts ""
end

def ask(string)
puts string.blue
return gets.strip
end

def not_installed?(package)
`brew info #{package} | grep "Not installed"`.strip.length > 0
end

def check_package(package)
if not_installed?(package)
puts "#{package} is not installed via Homebrew. Try running `brew install #{package}`.".red
input = ask "Try proceeding without #{package}? [y/n]"
if input.downcase[0] == "n"
exit
end
else
puts "#{package} is installed via Homebrew.".green
end
end

# Unless the shell's current version of Ruby is the same as what the application requires, we should flag it.
# rbenv produces strings like "3.1.2" while rvm produces ones like "ruby-3.1.2", so we account for that here.
required_ruby = `cat ./.ruby-version`.strip.gsub(/^ruby-/, "")
actual_ruby = `ruby -v`.strip
message = "Shore requires Ruby #{required_ruby} and `ruby -v` returns #{actual_ruby}."
if actual_ruby.include?(required_ruby)
puts message.green
else
puts message.red
input = ask "Try proceeding with with Ruby #{actual_ruby} anyway? [y/n]"
if input.downcase[0] == "n"
exit
end
end

if `brew info 2> /dev/null`.length > 0
puts "Homebrew is installed.".green
else
puts "You don't have Homebrew installed. This isn't necessarily a problem, you might not even be on macOS, but we can't check your dependencies without it.".red
input = ask "Try proceeding without Homebrew? [y/n]"
if input.downcase[0] == "n"
exit
end
end

case Gem::Platform.local.os
when "darwin"
check_package("postgresql@16")
when "linux"
system_packages = `dpkg -l | grep '^ii'`.split("\n").map do |package_information|
package_information.split("\s")[1]
end

psql_installed = system_packages.include?("postgresql")
if psql_installed
psql_version = `psql --version`.split("\s")[2]
if psql_version.match?(/^16/)
puts "You have PostgreSQL 16 installed.".green
else
puts "You have PostgreSQL installed, but you're using v#{psql_version} and not v16.".red
input = ask "Try proceeding without PostgreSQL 16? [y/n]"
if input.downcase[0] == "n"
exit
end
end
else
puts "You don't have PostgreSQL installed. Please see the installation instructions at https://ubuntu.com/server/docs/databases-postgresql".red
exit
end
else
puts "We currently don't support this platform to check if you have the following libraries installed:".red
puts "1. PostgreSQL"
puts ""
puts "Please ensure they are installed before proceeding."
input = ask "Proceed? [y/n]"
if input.downcase[0] == "n"
exit
end
end

if `bun -v 2> /dev/null`.length > 0
puts "Bun is installed.".green
else
puts "You don't have Yarn installed. We can't proceed without it. Try `brew install oven-sh/bun/bun` or see the installation instructions at https://bun.sh/docs/installation".red
exit
end

puts "Next, let's push your application to GitHub."
puts "If you would like to use another service like Gitlab to manage your repository,"
puts "you can opt out of this step and set up the repository manually."
puts "(If you're not sure, we suggest going with GitHub)"
skip_github = ask "Continue setting up with GitHub? [y/n]"
if skip_github.downcase[0] == "y"
if `git remote | grep shore`.strip.length > 0
puts "Repository already has a \`shore`\ remote.".yellow
else
if `git remote | grep origin`.strip.length > 0
puts "Renaming repository `origin` remote to `shore`.".green
`git remote rename origin shore`
else
puts "Repository has no `origin` remote, but also no `bullet-train` remote. Did something go wrong?".red
end
end

if `git remote | grep origin`.strip.length > 0
puts "Repository already has a \`origin`\ remote.".yellow
else
ask "Hit <Return> and we'll open a browser to GitHub where you can create a new repository. When you're done, copy the SSH path from the new repository and return here. We'll ask you to paste it to us in the next step."
command = if Gem::Platform.local.os == "linux"
"xdg-open"
else
"open"
end
`#{command} https://github.com/new`

ssh_path = ask "OK, what was the SSH path? (It should look like `[email protected]:your-account/your-new-repo.git`.)"
while ssh_path == ""
puts "You must provide a path for your new repository.".red
ssh_path = ask "What was the SSH path? (It should look like `[email protected]:your-account/your-new-repo.git`.)"
end
puts "Setting repository's `origin` remote to `#{ssh_path}`.".green
puts `git remote add origin #{ssh_path}`.chomp
end

local_branch = `git branch | grep "*"`.split.last

puts "Pushing repository to `origin`.".green
stream "git push origin #{local_branch}:main 2>&1"
end

puts "Running `bundle install`.".green
stream "bundle install"

puts "Running `yarn install`.".green
stream "bun install"

human = ask "What is the name of your new application in title case? (e.g. \"Some Great Application\")"
while human == ""
puts "You must provide a name for your application.".red
human = ask "What is the name of your new application in title case? (e.g. \"Some Great Application\")"
end

require "active_support/inflector"

variable = ActiveSupport::Inflector.parameterize(human.gsub("-", " "), separator: '_')
environment_variable = ActiveSupport::Inflector.parameterize(human.gsub("-", " "), separator: '_').upcase
class_name = variable.classify
kebab_case = variable.tr("_", "-")
connected_name = variable.gsub("_", "") # i.e. `bullettrain` as opposed to `bullet_train`

puts ""
puts "Replacing instances of \"Shore\" with \"#{human}\" throughout the codebase.".green
replace_in_file("./.circleci/config.yml", "untitled_application", variable)
replace_in_file("./config/application.rb", "untitled_application", connected_name)
replace_in_file("./config/database.yml", "untitled_application", variable)
replace_in_file("./config/database.yml", "UNTITLED_APPLICATION", environment_variable)
replace_in_file("./config/cable.yml", "untitled_application", variable)
replace_in_file("./config/initializers/session_store.rb", "untitled_application", variable)
replace_in_file("./config/environments/production.rb", "untitled_application", variable)
replace_in_file("./config/application.rb", "UntitledApplication", class_name)
replace_in_file("./config/locales/en/application.en.yml", "Untitled Application", human, /name/)
replace_in_file("./config/locales/en/application.en.yml", "untitled_application", variable)
replace_in_file("./config/locales/en/application.en.yml", "untitled application", human.downcase, /keywords/)
replace_in_file("./config/locales/en/user_mailer.en.yml", "Untitled Application", human)
replace_in_file("./zapier/package.json", "untitled-application", kebab_case)
replace_in_file("./zapier/package.json", "Untitled Application", human)
replace_in_file("./app/views/api/v1/open_api/index.yaml.erb", "Untitled Application", human)
replace_in_file("./app.json", "Untitled Application", human)
replace_in_file("./.redocly.yaml", "untitled_application", variable)

puts ""

unless skip_github
original_repo_link = "https://github.com/bullet-train-co/bullet_train"
new_repo_link = ask "What is the link to your repository? We will use this to enable the one-click deploy to Render button for your application."
replace_in_file("README.example.md", original_repo_link, new_repo_link, /repo=#{original_repo_link}/)
end

puts "Moving `./README.example.md` to `./README.md`.".green
puts `mv ./README.example.md ./README.md`.chomp

puts `rm .github/FUNDING.yml`.chomp

# We can only do this after the README is moved into place.
replace_in_file("./README.md", "Untitled Application", human)

if skip_github
puts ""
puts "Make sure you save your changes with Git.".yellow
else
puts "Committing all these changes to the repository.".green
stream "git add -A"
stream "git commit -m \"Run configuration script.\""
stream "git push origin #{local_branch}:main"
end

puts ""
puts "OK, we're done, but at some point you should edit `config/locales/en/application.en.yml`!".yellow
puts ""
puts "Next you can run `bin/setup`, then `bin/dev` to spawn a local instance, and then you can navigate to http://localhost:3000 to access your new application.".green
puts ""
10 changes: 8 additions & 2 deletions bin/setup
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ FileUtils.chdir APP_ROOT do
puts "Copied .env.sample to .env".green
end

# Install system dependencies if Homebrew is installed
if Gem::Platform.local.os.include?("darwin") && command?("brew")
puts "\n== Installing packages with Homebrew =="
system("brew bundle check --no-lock --no-upgrade") || system!("brew bundle --no-upgrade --no-lock")
end

puts "\n== Installing gems =="
system! "gem install bundler --conservative"
system("bundle check") || system!("bundle install")
Expand Down Expand Up @@ -75,6 +81,6 @@ FileUtils.chdir APP_ROOT do
puts ""
puts "You're set!! 🎉🎉🎉".green
puts ""
puts " #{"bin/dev".yellow} to start the server, visit http://localhost:3000"
puts " #{"bin/rake".yellow} to run tests"
puts " #{"bin/dev".yellow} to start the server, then visit http://localhost:3000"
puts " #{"bin/rails test:all".yellow} to run tests"
end

0 comments on commit 14822ef

Please sign in to comment.