bin/nephos-generator in nephos-server-0.3.2 vs bin/nephos-generator in nephos-server-0.4.0

- old
+ new

@@ -1,7 +1,10 @@ #!/usr/bin/env ruby +require 'optparse' +require 'nephos-server/bin-helpers' + GEMFILE = <<EOF source 'https://rubygems.org' gem 'nephos-server' EOF @@ -10,11 +13,26 @@ #get url: "/", controller: "MainController", method: "root" #get url: "/add", controller: "MainController", method: "add_url" #get url: "/rm", controller: "MainController", method: "rm_url" EOF +def generate_route(verb, url, dest_c, dest_m=nil) + raise "You are not in a valid application directory" if not Nephos::Bin.is_a_valid_application? + raise InvalidVerb, "\"#{verb}\" doesn't match with /\w+/" if not verb.to_s.match(/^\w+$/) + raise InvalidUrl if not url.match(/^\/?(:?\w+)(\/:?\w+)*\/?$/) + raise "Option dest_m is not avaiable for now" if not dest_m.nil? + raise "Option dest_c must match with \"controller#method\"" if not dest_c.match(/^\w+\#\w+$/) + controller = dest_c.split("#")[0] + method = dest_c.split("#")[1] + line = "add_route \"#{verb}\", url: \"#{url}\", controller: \"#{controller}\", method: \"#{method}\"" + File.open("routes.rb", "a") do |f| + f.puts line + end +end + def generate_controller(name, file) + raise "You are not in a valid application directory" if not Nephos::Bin.is_a_valid_application? if File.exists? file print "The file #{file} already exists. Are you sure to erase it ? (y/N)" r = STDIN.gets.to_s.chomp raise "File #{file} already exists" unless r.match(/y(es)?/) end @@ -52,20 +70,18 @@ puts "Error: #{err.message}" end exec("bundle install") end -require 'optparse' - begin OptionParser.new do |opts| opts.banner = "Usage: nephos-generator [controller name] [appli [name]]" end.parse! case ARGV[0] when "c", "controller" - if ARGV[1].to_s.match(/[\w_\-\.]+/) + if ARGV[1].to_s.match(/[\w\-\.]+/) generate_controller("#{ARGV[1].capitalize}Controller", "app/#{ARGV[1].downcase}.rb") else puts "error" end when "a", "appli", "application" @@ -74,9 +90,11 @@ puts "Application #{ARGV[1]} created" move_to_application_dir(ARGV[1]) end initialize_application puts "Application initialized" + when "r", "route" + generate_route(*(ARGV[1..4])) else puts "\"#{ARGV[0]}\" not recognized has a command" end rescue => err