bin/nephos-generator
#!/usr/bin/env ruby
require 'colorize'
require 'optparse'
require_relative '../lib/nephos-server/version'
require_relative '../lib/nephos-server/bin-helpers'
GEMFILE = <<EOF
source 'https://rubygems.org'
gem 'nephos'
EOF
ROUTE_RB = <<EOF
#get url: "/", controller: "MainController", method: "root"
#get url: "/add", controller: "MainController", method: "add_url"
#get url: "/rm", controller: "MainController", method: "rm_url"
EOF
def raise_invalid_appli
if not Nephos::Bin.is_a_valid_application? and not $test
raise BinError, "You are not in a valid application directory"
end
end
module Nephos
module Generator
module Route
def self.exists? line
File.read("routes.rb").split("\n").include? line
end
def self.write! line
File.open("routes.rb", "a") do |f|
f.puts line
end
puts("Route created: ".green + line)
end
def self.generate!(verb, url, dest_c, dest_m=nil)
raise_invalid_appli
raise BinError, "\"#{verb}\" doesn't match with /\w+/" if not verb.to_s.match(/^\w+$/)
raise BinError, "\"#{url}\" is not a valid url" if not url.match(/^\/?(:?\w+)(\/:?\w+)*\/?$/)
raise BinError, "Option dest_c must match with \"controller#method\"" if not dest_c.match(/^\w+\#\w+$/) and dest_m.nil?
raise BinError, "Option dest_c must match with \"controllerName\"" if not dest_m.nil? and not dest_c.match(/^\w+$/)
raise BinError, "Option dest_m must match with \"methodName\"" if not dest_m.nil? and not dest_c.match(/^\w+$/)
controller, method = dest_c, dest_m
if dest_m.nil?
controller = dest_c.split("#")[0]
method = dest_c.split("#")[1]
end
verb = verb.upcase
line = "add_route \"#{verb}\", url: \"#{url}\", controller: \"#{controller}\", method: \"#{method}\""
if exists? line
if $remove
routes = File.read("routes.rb").split("\n")
puts "Success: ".green + "Route deleted: " + routes.delete(line).to_s
File.write("routes.rb", routes.join("\n") + "\n")
else
puts("Warning: ".yellow + "Route already exists: " + line)
end
else
if $remove
puts("Warning: ".yellow + "Route doesn't exists: " + line)
else
write!(line)
end
end
end
end
module Controller
def self.generate!(ctr)
uname = "#{ctr[0].upcase}#{ctr[1..-1]}Controller".gsub(/ControllerController$/, "Controller")
lname = uname.gsub(/([A-Z])/, '_\1')[1..-1].downcase
file = "app/#{lname}.rb"
raise_invalid_appli
if $remove
if File.exist?(file) and File.read(file).include? "class #{uname} < Nephos::Controller"
File.delete(file)
puts "Success: ".yellow + "\"#{file}\" removed"
else
raise BinError, "\"#{file}\" is not a valid Controller file."
end
else
if File.exist? file
print "Warning: ".yellow + "The file #{file} already exists. Are you sure to erase it ? (y/N) : "
r = STDIN.gets.to_s.chomp
raise BinError, "File #{file} already exists" unless r.match(/y(es)?/)
end
f = File.open(file, 'w')
f << <<EOF
class #{uname} < Nephos::Controller
def root
return {plain: "index"}
end
end
EOF
puts "Success: ".green + "Controller \"#{uname}\" created at location \"#{file}\""
end
end
end
module Application
def self.create_application_dir dir
raise BinError, "Directory #{dir} already exists" if Dir.exist? dir and dir != "."
Dir.mkdir dir
end
def self.move_to_application_dir dir
Dir.chdir dir
end
def self.initialize!
raise BinError, "Not an empty dir" unless Dir[File.expand_path "*"].empty?
File.write "routes.rb", ROUTE_RB
File.write "Gemfile", GEMFILE
Dir.mkdir "app"
begin
`git init .`
puts "Git repository initialized"
rescue Errno::ENOENT => err
puts "Git Init Error: ".yellow + "git repository not initialized"
rescue => err
puts "Git Init Error: ".yellow + " #{err.message}"
end if $git
exec("bundle install") if $build
end
end
def self.main opt
case $mode
when :controller
raise BinError, "Invalid Controller name" if not opt[0].to_s.match(/[\w\-\.]+/)
Controller.generate!(opt[0])
when :application
if not opt[0].to_s.empty?
Application.create_application_dir(opt[0])
puts "Application #{opt[0]} created"
Application.move_to_application_dir(opt[0])
end
Application.initialize!
puts "Application initialized"
when :route
raise BinError, "Need more arguments (verb url controller method)" if opt.size < 3
Route.generate!(*(opt[0..3]))
else
puts "nephos-generator --help"
end
end
end
end
begin
opt = OptionParser.new do |opts|
opts.banner = "Usage<#{Nephos::VERSION}>: nephos-generator <options>"
$git = true
opts.on("--no-git", "Disable the git initialization when create new application") do
$git = false
end
$build = true
opts.on("--no-build", "Disable the `bundle install` execution when create new application") do
$build = false
end
opts.on("--application", "-a", "Create new application") do
$mode = :application
end
opts.on("--route", "-r", "Create and remove routes") do
$mode = :route
end
opts.on("--controller", "-c", "Create and remove controller") do
$mode = :controller
end
opts.on("--rm", "Remove") do
$remove = true
end
opts.on("--debug", "Enable debugging mode") do
$debug = true
end
opts.on("--test", "Enable testing mode (for nephos developpers)") do
$test = true
end
end.parse!
Nephos::Generator.main(opt)
rescue BinError => err
puts "Error:".red + " #{err.message}"
rescue => err
puts "Error:".red + " #{err.message}"
puts err.backtrace
end