#!/usr/bin/env ruby $: << File.expand_path(File.join(File.dirname(__FILE__), "../lib")) begin require 'ninjs' require 'optparse' rescue LoadError require 'rubygems' require 'ninjs' require 'optparse' end header = <<-HEADER ninjs #{Ninjs::VERSION} Copyright (c) #{Time.new.year} Dayton Nolan Released under the MIT License Description: The ninjs command line application is a simple way to quickly develop and manage your application. With it you can create an application, generate scaffolding, compile, and upgrade your application. Usage: ninjs [command] [arguments] [options (-h, --help)] Commands: HEADER create = <<-CREATE create Creates a new ninjs application in the current working directory or sub directory within. Arguments: application name - Name of the ninjs application subdirectory* - Directory where the application will be installed (created if non existent) examples: ninjs create myapp ninjs create myapp subdirectory CREATE generate = <<-GEN generate Generates scoffolding for the given component file type. Arguments: type - Type of application scaffold to create (module, elements, model). name* - Name of the module to generate the scaffold for Options: -a, --alias name* - Create an alias of the application object (defaults to "app"), passing a name is optional -e, --elements - Generate an elements file -m, --model - Generate a model file examples: ninjs generate module mymodule -em (generates module, elements and model) ninjs generate module mymodule -a (generates an application alias) ninjs generate elements mymodule -m (generates an elements and a model file) ninjs generate model mymodule (generates an model file) GEN compile = <<-COMP compile Compiles the ninjs project in the current working directory. Options: -c, --compress - Compress output with JsMin example: ninjs compile COMP watch = <<-WATCH watch Watches the current working directory for file changes and compiles when changes are detected. example: ninjs watch WATCH upd = <<-UPDATE update Updates your application's core files to the latest version. UPDATE footer = <<-FOOTER * optional argument FOOTER help = { :header => header, :create => create, :generate => generate, :compile => compile, :watch => watch, :update => upd, :footer => footer } command = ARGV[0] options = { :alias => false, :elements => false, :model => false, :help => false, :compress => nil } optparse = OptionParser.new do|opts| opts.on( '-h', '--help', 'Display this screen' ) do options[:help] = true if command.nil? || command.match(/^-h/) help.each do |section| puts section end exit end end opts.on( '-v', '--version', 'Display the version') do time = Time.now Ninjs::Notification.notice 'ninjs ' + Ninjs::VERSION Ninjs::Notification.notice "Copyright (c) #{time.year} Dayton Nolan" Ninjs::Notification.notice "Released under the MIT License" exit end opts.on( '-a', '--alias', 'Generate application alias') do options[:alias] = true end opts.on('-e', '--elements', 'Generate elements file') do options[:elements] = true end opts.on('-m', '--model', 'Generate model file') do options[:model] = true end opts.on('-c', '--compress', 'Compress with JsMin') do options[:compress] = true end end optparse.parse! case command # create when "create" if options[:help] puts help[:create] exit end app_name = ARGV[1] sub_dir = ARGV[2] || nil if app_name.nil? puts "Error! Application name is required (ninjs create app_name)" exit end sub_dir.nil? ? Ninjs::Command.create(app_name) : Ninjs::Command.create(app_name, sub_dir) # generate when "generate" if options[:help] puts help[:generate] exit end errors = Hash.new type = ARGV[1] name = ARGV[2] als = ARGV[3] || 'app' errors[:type] = "Error! Scaffold type is required (ninjs generate module mymodule)" if type.nil? errors[:name] = "Error! Module name is required (ninjs generate module mymodule)" if name.nil? unless errors.empty? errors.each do |error| puts error end exit end with = { :model => options[:model], :elements => options[:elements] } if options[:alias] Ninjs::Command.generate_with_alias(type, name, with, als) else Ninjs::Command.generate(type, name, with) end # compile when "compile" if options[:help] puts help[:compile] exit end unless options[:compress].nil? Ninjs::Command.compile(options[:compress]) else Ninjs::Command.compile() end # watch when "watch" if options[:help] puts help[:watch] exit end Ninjs::Command.watch # update when "update" if options[:help] puts help[:update] exit end Ninjs::Command.update # help else help.each do |section| puts section end end