Skip to content

Instantly share code, notes, and snippets.

@runemadsen
Forked from atduskgreg/gist:1258552
Created September 4, 2012 17:56
Show Gist options
  • Save runemadsen/3624189 to your computer and use it in GitHub Desktop.
Save runemadsen/3624189 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
get '/' do
"Hello"
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