class Sinistra::CUI

Constants

CONFIG_RU
LIB_APPNAME
LIB_CTRLMAIN
LIB_HLP_MAIN
LIB_MDL_MAIN
LICENSE
README
TODO

Public Instance Methods

init(name) click to toggle source
# File lib/sinistra/cui.rb, line 68
def init(name)
  if not File.exists?("#{name}.sinistra")
    # default configuration
    default_yaml = {
      "name" => name,
      "readme"  => true,
      "todo"    => true,
      "license" => false,
      "bundler" => {
        "enabled" => true,
        "source"  => "https://rubygems.org",
        "ruby"    => "2.0.0",
        "gems"    => {"sinatra"=>"", "thin"=>""}
      },
      "proc" => {
        "enabled" => true,
        "env" => {
          "PORT"  => "8080"
        },
        "tasks" => {
          "web" => "bundle exec rackup -I ./ -I ./lib -I ./%appname% -I ./thrd -P $PORT"
        }
      },
      "hooks" => {
        "git"     => true,
        "model"   => true,
        "helper"  => true,
        "hg"      => false,
        "sql"     => false,
        "rdoc"    => false,
        "tests"   => false,
        "vendor"  => false
      },
    }

    # saves config.yml
    IO.write("config.yml", default_yaml.to_yaml)

    # warns about completion
    puts " ! Edit you config.yml and then run `sinistra install'"
  else
    puts " ! Failed to create the app: already found a config.yml"
  end
end
install() click to toggle source
# File lib/sinistra/cui.rb, line 114
def install
  config = YAML.load(IO.read("config.yml"))
  name   = config["name"]

  makdir = [name.downcase, File.join(name.downcase, "controller"), File.join(name.downcase, "views"), File.join(name.downcase, "views", "layouts"), "lib", "public", "tmp"]

  # optional but default model
  makdir << File.join(name.downcase, "models") if config["hooks"]["model"]
  makdir << File.join(name.downcase, "helpers") if config["hooks"]["helper"]

  # extra hooks
  makdir << "db" if config["hooks"]["sql"]
  makdir << "doc" if config["hooks"]["rdoc"]

  # extra things
  makdir << "misc" if config["hooks"]["tests"] || config["hooks"]["vendor"]

  # tests, init scripts and vendor code
  makdir << "misc/tests" if config["hooks"]["tests"]
  makdir << "misc/thrd" if config["hooks"]["vendor"]

  # create dirs
  makdir.each do |dir|
    begin
      Dir.mkdir(dir)
    rescue
    end
  end

  # creating base files
  CONFIG_RU.gsub!(/\%appname\%/, name.downcase)
  CONFIG_RU.gsub!(/\%Appname\%/, name.capitalize)
  CONFIG_RU.gsub!(/\%APPNAME\%/, name.upcase)
  CONFIG_RU.gsub!(/^.*require\s\'models\/main\'$/, "") unless config["hooks"]["model"]
  CONFIG_RU.gsub!(/^.*require\s\'helpers\/main\'$/, "") unless config["hooks"]["helper"]

  # library name
  LIB_APPNAME.gsub!(/\%appname\%/, name.downcase)
  LIB_APPNAME.gsub!(/\%Appname\%/, name.capitalize)
  LIB_APPNAME.gsub!(/\%APPNAME\%/, name.upcase)

  # writing files
  IO.write("config.ru", CONFIG_RU) unless File.exists?("config.ru")
  IO.write("./lib/#{name.downcase}.rb", LIB_APPNAME) unless File.exists?("./lib/#{name.downcase}.rb")
  IO.write("./#{name.downcase}/controller/main.rb", LIB_CTRLMAIN) unless File.exists?("./#{name.downcase}/controller/main.rb")
  
  # optional base files
  IO.write(File.join(name.downcase,"models","main.rb"), LIB_MDL_MAIN) if config["hooks"]["model"] && !File.exists?(File.join(name.downcase,"models","main.rb"))
  IO.write(File.join(name.downcase,"helpers","main.rb"), LIB_HLP_MAIN) if config["hooks"]["helper"] && !File.exists?(File.join(name.downcase,"helpers","main.rb"))

  # create misc files
  IO.write("README", README) if config["hooks"]["readme"] && !File.exists?("README")
  IO.write("LICENSE", LICENSE) if config["hooks"]["license"]  && !File.exists?("LICENSE")
  IO.write("TODO", TODO) if config["hooks"]["todo"]  && !File.exists?("TODO")

  # gemfile creation
  if config["bundler"]["enabled"] && !File.exists?("Gemfile")
    puts "Gemfile.lock found, overwriting it anyway" if File.exists?("Gemfile.lock")
    gemfile  = "ruby '#{config["bundler"]["ruby"]}'\n"
    gemfile << "source '#{config["bundler"]["source"]}'\n"
    config["bundler"]["gems"].each do |name_, version|
      gemfile << "gem '#{name_}'"
      gemfile << ", '#{version}'" unless version == ""
      gemfile << "\n"
    end
    IO.write("Gemfile", gemfile)
    puts " ! Run `bundle install' to install #{name} dependencies get a proper Gemfile.lock"
  end

  # git creation args
  if config["hooks"]["git"]
    %x{ git init } unless File.exists?(".git")
    IO.write(".gitignore", "/tmp/*\n") unless File.exists?(".gitignore")
  end

  # mercurial creation args
  if config["hooks"]["hg"]
    %x{ hg init } unless File.exists?(".hg")
    IO.write(".hgignore", "syntax: glob\n/tmp/*\n") unless File.exists?(".hgignore")
  end

  if config["proc"]["enabled"]
    unless File.exists?("Procfile")
      procfile = ""
      config["proc"]["tasks"].each do |task, action|
        procfile << task << ": " << action << "\n"
      end
      procfile.gsub!(/\%appname\%/, name.downcase)
      procfile.gsub!(/\%Appname\%/, name.capitalize)
      procfile.gsub!(/\%APPNAME\%/, name.upcase)
      IO.write("Procfile", procfile)
    end
    unless File.exists?(".env")
      env = ""
      config["proc"]["env"].each do |nm, value|
        env << nm << "=" << value << "\n"
      end
      env.gsub!(/\%appname\%/, name.downcase)
      env.gsub!(/\%Appname\%/, name.capitalize)
      env.gsub!(/\%APPNAME\%/, name.upcase)
      IO.write(".env", env)
    end
  end

  # Lastest warnings
  puts " ! Run `foreman start' (or the built-in `sinistra start') to launch #{config["name"]}'s Procfile"
end
start() click to toggle source
# File lib/sinistra/cui.rb, line 223
def start
  script = File.join(File.dirname(File.expand_path(__FILE__)),"shoreman.sh")
  exec "\"#{script}\""
end