Skip to content

Instantly share code, notes, and snippets.

@stevenklise
Forked from atduskgreg/gist:1258552
Created November 7, 2012 18:05
Show Gist options
  • Save stevenklise/4033284 to your computer and use it in GitHub Desktop.
Save stevenklise/4033284 to your computer and use it in GitHub Desktop.
new_sinatra_app.rb
#!/usr/bin/env ruby
require 'fileutils'
install_root = File.expand_path(File.dirname(__FILE__))
if !ARGV[0]
puts "ERROR: You must supply the name of the app you want to create. Like this:"
puts "./new_sinatra_app my_app"
exit 1
end
nid = install_root.split("/")[2]
app_name = ARGV[0]
app_name.gsub!(" ", "_")
puts "Creating new app: #{app_name}..."
puts "install_root: #{install_root}"
puts "NETID: #{nid}"
puts
puts
puts "Creating folder structure..."
puts
puts "creating public directory"
# create folder structure + always_retart.txt
FileUtils.mkdir_p "#{install_root}/#{app_name}/public"
puts "creating tmp directory"
FileUtils.mkdir_p "#{install_root}/#{app_name}/tmp"
puts "creating always_restart.txt "
FileUtils.touch "#{install_root}/#{app_name}/tmp/always_restart.txt"
puts "creating config.ru"
# install config.ru
File.open("#{install_root}/#{app_name}/config.ru", "w") do |f|
f.write <<-BLAH
require File.dirname(__FILE__) + '/app.rb'
before do
s = request.path_info
s[/^\\/~(\\w)+(\\d)+\\/sinatra\\/[^\\/|?]+/i] = ""
request.path_info = s
end
run Sinatra::Application
BLAH
end
puts "creating .htaccess file"
# install .htaccess
File.open("#{install_root}/#{app_name}/.htaccess", "w") do |f|
f.write <<-BLAH
PassengerEnabled on
RackBaseURI /sinatra
PassengerAppRoot #{install_root}/#{app_name}
RackEnv development
BLAH
end
puts
puts "creating app.rb"
# populate default app
File.open("#{install_root}/#{app_name}/app.rb", "w") do |f|
f.write <<-BLAH
require 'sinatra'
# Main route - this is the form where we take the input
get '/' do
form = ""
# The form variable contains the HTML for the form
# The "action" sets the url this form will go to
# The "method" sets the verb
form += '<form action="http://itp.nyu.edu/~#{nid}/sinatra/#{app_name}/get_rectangle" method="GET">'
# The 'name' of the input translates into where the variable
# ends up in the params in our other action
# An input with name="x" becomes params[:x]
form += '<p><label>text:</label> <input type="text" name="text" /></p>'
form += '<p><label>x:</label> <input type="text" name="x" /></p>'
form += '<p><label>y:</label> <input type="text" name="y" /></p>'
form += '<p><label>color:</label> <input type="text" name="color" /></p>'
# An input of type "submit" becomes a submit button for sending
# in the form
form += '<p><input type="submit" value="create" /></p>'
form += '</form>'
# The last line always gets returned
form
end
# This is the service which gets called by the form
get '/get_rectangle' do
rectangle = ""
# This will create a rectangle using in-line CSS
# Each of the values submitted in the form can be accessed by params[:name]
rectangle += '<p style="width: ' + params[:x] + '; '
rectangle += 'height: ' + params[:y] + '; '
rectangle += 'background-color: ' + params[:color] + '">'
rectangle += params[:text] + '</p>';
# The last line always gets returned
rectangle
end
BLAH
end
puts "checking for sinatra directory in public_html"
FileUtils.mkdir_p "/home/#{nid}/public_html/sinatra"
puts "installing symlink"
FileUtils.ln_s "#{install_root}/#{app_name}", "/home/#{nid}/public_html/sinatra/#{app_name}"
puts
puts "done!"
puts "edit you new app here:"
puts "#{install_root}/sinatra/#{app_name}/"
puts "and see it on the web here:"
puts "http://itp.nyu.edu/~#{nid}/sinatra/#{app_name}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment