Permalink
Browse files

Pick random port when generating Rails app

We have lots of Suspenders-generated apps. We run them using Foreman.
Randomizing the port number can help us run multiple apps locally while avoiding
conflicts.

Convert `bin_setup` file to use ERb and interpolate `port_number` into template.
  • Loading branch information...
1 parent 29c9e26 commit 4caa6123c83e726d6fcd23956ce9883ca36b1d42 @croaky croaky committed Jul 8, 2014
Showing with 7 additions and 3 deletions.
  1. +6 −2 lib/suspenders/app_builder.rb
  2. +1 −1 templates/{bin_setup → bin_setup.erb}
@@ -26,7 +26,7 @@ def raise_on_unpermitted_parameters
end
def provide_setup_script
- copy_file 'bin_setup', 'bin/setup'
+ template 'bin_setup.erb', 'bin/setup', port_number: port_number
run 'chmod a+x bin/setup'
end
@@ -219,7 +219,7 @@ def configure_rack_timeout
end
def configure_action_mailer
- action_mailer_host 'development', "#{app_name}.local"
+ action_mailer_host 'development', "localhost:#{port_number}"
action_mailer_host 'test', 'www.example.com'
action_mailer_host 'staging', "staging.#{app_name}.com"
action_mailer_host 'production', "#{app_name}.com"
@@ -381,5 +381,9 @@ def factories_spec_rake_task
def generate_secret
SecureRandom.hex(64)
end
+
+ def port_number
+ @port_number ||= [3000, 4000, 5000, 6000, 7000, 8000, 9000].sample
+ end
end
end
View
@@ -21,7 +21,7 @@ bundle exec rake dev:prime
mkdir -p .git/safe
# Pick a port for Foreman
-echo "port: 7000" > .foreman
+echo "port: <%= config[:port_number] %>" > .foreman
# Print warning if Foreman is not installed
if ! command -v foreman &>/dev/null; then

0 comments on commit 4caa612

Please sign in to comment.