#!/usr/bin/env ruby # encoding: UTF-8 while (index = ARGV.index('-I')) _, path = ARGV.slice!(index, 2) $LOAD_PATH << path end # Always add ./lib to the load path as that is the default location for Ruby # files in a project. $LOAD_PATH << File.expand_path('./lib') require 'optparse' require 'fileutils' require 'logger' require 'wab' require 'wab/impl' # The options for this application are determined by the # +WAB::Impl::Configuration+ class which takes an +usage+ string and an # +options+ hash. The returned object is a +WAB::Impl::Configuration+ # object that is used by the Shell to configure itself before being started. # Prepare the +usage+ string. # Basically a banner text and description passed on +OptionParser+. usage = %{ Usage: wabur [run|new|init] [options] [types] A pure Ruby WAB Runner and multi-purpose tool for WABuR development. Configured directly via command-line options or via a configuration file which can either be a UNIX-style conf file, or a JSON file, or a YAML file. The configuration typically includes designating Controller classes for each URL path to be handled. Modes (the first non-option) available are: run: Runs wabur as a runner and shell. Also the default. Any types specified will be ignored. new: New project directory created and set up. The `--base` options must be provided to specify the directory to create and initialize. Not providing the `--base` switch will revert to the `init` mode. e.g.: wabur new Entry Article --base my_app init: Initialize a project into the current directory. e.g.: wabur init Entry Article } # Prepare the +options+ hash. Basically a mapping of supported switches and # their description that gets parsed by +OptionParser+. It is also used to # generate default values. options = { base: { val: '.', type: String, doc: 'App root directory which is $BASE.', arg: 'PATH', short: '-b' }, store: { dir: { val: '$BASE/data', type: String, doc: "Directory to use for data storage.", arg: 'PATH', } }, path_prefix: { val: '/v1', type: String, doc: 'URL path prefix for relative handler routing.', arg: 'PREFIX', }, handler: { val: [], doc: 'Type and handler/controller class in the form =.', short: '-t', parse: [:type, :handler], arg: 'PAIR', }, type_key: { val: 'kind', type: String, doc: 'Shell type_key.', arg: 'KEY', }, http: { dir: { val: '$BASE/pages', type: String, doc: 'Directory where HTTP content resides.', arg: 'PATH', }, port: { val: 6363, type: Integer, doc: 'HTTP Port to listen on.', arg: 'PORT', }, }, indent: { val: 0, type: Integer, doc: 'JSON indentation.', arg: 'INDENT', }, verbosity: { val: 'WARN', type: String, doc: 'Log level. (ERROR, WARN, INFO, DEBUG)', arg: 'LEVEL', }, } config = WAB::Impl::Configuration.new(usage, options) if 'new' == config[:mode] dir = File.expand_path(config[:base] || '.') FileUtils.mkdir_p(dir) unless File.expand_path('.') == dir WAB::Impl::Init.setup(dir, config) puts "Successfully created a new WAB workspace at #{dir}" elsif 'init' == config[:mode] dir = File.expand_path('.') WAB::Impl::Init.setup(dir, config) puts "Successfully initialized a new WAB workspace at #{dir}" else # The Configuration object can be modified before initializing the Shell. By # setting the +config[:logger]+ the Shell will use that as the logger. The # +config[:handler]+ array can also be modified by setting path values along # with a Controller instance, a Controller class, or the name of a # Controller class. config[:logger] = Logger.new(STDOUT) # Start a shell initialized with the final configuration. WAB::Impl::Shell.new(config).start end