#!/usr/bin/env ruby require 'rubygems' require 'commander/import' require 'socks' require 'fileutils' program :version, Socks::VERSION program :description, 'A Ruby-Rack-based microframework' default_command :about command :new do |c| c.syntax = 'socks new [options]' c.summary = 'Create a new Socks app' c.description = 'Use Rack and other utilities to make a new Socks app from scratch' c.action do |args, opt| # Pull out the first arg in args[] app = args[0] # ------------------------------------------------------------------------- # File Contents | # ------------------------------------------------------------------------- config_ru = "# config.ru is used for running rack-based applications. Please do not edit this file without proper understanding of Rack # or some bad things may happen. # Bundler setup require \"bundler/setup\" Bundler.setup # http_router require File.expand_path(\"../config/router.rb\", __FILE__) # Controllers Dir[\"../app/controllers\" + \"*.rb\"].each do |file| require file end # Used so Rack can automatically get updated to file contents without # a ten second cooldown use Rack::Reloader, 0 # Used to run your router (app!) # Go to config/router.rb to declare routes run #{app.capitalize}::Router" gemfile = "# List dependencies for your app in your Gemfile, then run the `bundle` command source \"http://rubygems.org\" # Socks itself! gem \"socks\", \"~> #{Socks::VERSION}\" # Rack HTTP gem \"rack\" # Startup your server with thin gem \"thin\" # Or use Foreman gem \"foreman\" # Use HttpRouter for routes (required) gem \"http_router\" # Use Warden for authentication # gem \"warden\" # Use RSpec for tests gem \"rspec\"" rakefile = "# Socks generates many helpful Rake tasks for you! require \"socks\" Socks::Tasks.install" gitignore = ".DS_Store" procfile = "web: bundle exec rackup config.ru -p 4000" app_template = "require \"socks\"" router_template = "# Declare the paths to your routes here. require \"http_router\" module #{app.capitalize} Router = HttpRouter.new do # See what you can do at https://github.com/joshbuddy/http_router end end " # ------------------------------------------------------------------------- # Creation | # ------------------------------------------------------------------------- FileUtils.mkdir app FileUtils.cd app FileUtils.mkdir %w( app config ) FileUtils.mkdir %w( app/controllers app/views ) # Sub-dirs FileUtils.touch %w( config.ru Gemfile Rakefile .gitignore Procfile ) # ------------------------------------------------------------------------- # File Initialization | # ------------------------------------------------------------------------- system("echo '#{config_ru}' >> config.ru") system("echo '#{gemfile}' >> Gemfile") system("echo '#{rakefile}' >> Rakefile") system("echo '#{gitignore}' >> .gitignore") system("echo '#{procfile}' >> Procfile") system("echo '#{app_template}' >> #{app}.rb") system("echo '#{router_template}' >> config/router.rb") system("bundle") end end command :controller do |c| c.syntax = 'socks controller [options]' c.description = 'Create a template for something' c.action do |args, options| cont = args[0] cont_template = "# Specify pages in the controller # e.g. # # def index # \"This is a socks app!\" # end # class #{cont.capitalize}Controller < Socks::BaseController # ... end " FileUtils.touch "app/controllers/#{cont}_controller.rb" system("echo '#{cont_template}' >> app/controllers/#{cont}_controller.rb") end end command :start do |c| c.syntax = 'socks start [options]' c.description = 'Startup a Rack server' c.action do |args, options| system('foreman start') # Startup server including code in lib/ that runs on port 4000 notify "Socks server is up" end end command :version do |c| c.syntax = 'socks version [options]' c.description = 'Display the current version (altertative to --version)' c.action do |args, options| Socks::VERSION end end command :about do |c| c.syntax = 'socks about [options]' c.action do |args, options| system("echo \"\e[32m Usage: socks new APP_NAME\"") end end # ------------------------------------------------------------------------- # Helpers | # -------------------------------------------------------------------------