Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
318 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#!/usr/bin/env ruby | ||
# | ||
# mongify_mongoid helps output Mongoid | ||
# Visit http://github.com/anlek/mongify_mongoid for more information. | ||
# | ||
# Author: Andrew Kalek | ||
# | ||
|
||
$:.unshift File.join(File.dirname(File.dirname(File.expand_path(__FILE__))), 'lib') | ||
|
||
require 'mongify/mongoid/cli' | ||
|
||
begin | ||
exit Mongify::Mongoid::CLI::Application.new(ARGV).execute! | ||
rescue Mongify::Mongoid::Error => error | ||
$stderr.puts "Error: #{error}" | ||
exit MongifyMongoid::CLI::Application.execution_error_status | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
require 'mongify/mongoid' | ||
|
||
require 'mongify/mongoid/cli/command/version' | ||
require 'mongify/mongoid/cli/command/help' | ||
require 'mongify/mongoid/cli/command/worker' | ||
|
||
|
||
require 'mongify/mongoid/cli/options' | ||
require 'mongify/mongoid/cli/application' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
module Mongify | ||
module Mongoid | ||
# The Command Line Interface module | ||
module CLI | ||
# | ||
# Represents an instance of a Mongify Mongoid application. | ||
# This is the entry point for all invocations of Mongify from the | ||
# command line. | ||
# | ||
class Application | ||
|
||
# Successful execution exit code | ||
STATUS_SUCCESS = 0 | ||
# Failed execution exit code | ||
STATUS_ERROR = 1 | ||
|
||
def initialize(arguments=[], stdin=$stdin, stdout=$stdout) | ||
arguments = ['-h'] if arguments.empty? | ||
@options = Options.new(arguments) | ||
@status = STATUS_SUCCESS | ||
end | ||
|
||
# Runs the application | ||
def execute! | ||
begin | ||
cmd = @options.parse | ||
return cmd.execute(self) | ||
rescue Error => error | ||
$stderr.puts "Error: #{error}" | ||
report_error | ||
rescue Exception => error | ||
report_error | ||
raise error | ||
end | ||
end | ||
|
||
# Sends output to the UI | ||
def output(message) | ||
UI.puts(message) | ||
end | ||
|
||
# Sets status code as successful | ||
def report_success | ||
@status = STATUS_SUCCESS | ||
end | ||
|
||
# Sets status code as failure (or error) | ||
def report_error | ||
@status = STATUS_ERROR | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
module Mongify | ||
module Mongoid | ||
module CLI | ||
module Command | ||
# | ||
# A command to display usage information for this application. | ||
# | ||
class Help | ||
def initialize(parser) | ||
@parser = parser | ||
end | ||
#Executes the help command | ||
def execute(view) | ||
view.output(@parser.to_s) | ||
view.report_success | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
module Mongify | ||
module Mongoid | ||
module CLI | ||
module Command | ||
# | ||
# A command to report the application's current version number. | ||
# | ||
class Version | ||
def initialize(progname) | ||
@progname = progname | ||
end | ||
#Executes version command | ||
def execute(view) | ||
view.output("#{@progname} #{Mongify::Mongoid::VERSION}\n") | ||
view.report_success | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
module Mongify | ||
module Mongoid | ||
module CLI | ||
module Command | ||
# | ||
# A command to run the different commands in the application (related to Mongifying). | ||
# | ||
class Worker | ||
attr_accessor :view | ||
|
||
|
||
def initialize(translation_file=nil, output_dir=nil, parser="") | ||
@translation_file = translation_file | ||
@output_dir = output_dir | ||
@parser = parser | ||
end | ||
|
||
#Executes the worked based on a given command | ||
def execute(view) | ||
self.view = view | ||
|
||
raise TranslationFileNotFound, "Translation file is required" unless @translation_file | ||
raise TranslationFileNotFound, "Unable to find Translation File at #{@translation_file}" unless File.exists?(@translation_file) | ||
|
||
#TODO: Check if output exists | ||
|
||
view.output("Mongify::Mongoid::Worker => SHOULD RUN SOMETHING") | ||
|
||
#TODO call main command | ||
|
||
view.report_success | ||
end | ||
|
||
# Passes find command to parent class | ||
|
||
####### | ||
private | ||
####### | ||
|
||
|
||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
require 'optparse' | ||
module Mongify | ||
module Mongoid | ||
module CLI | ||
# | ||
# Used to parse the options for an application | ||
# | ||
class Options | ||
def initialize(argv) | ||
@parsed = false | ||
@argv = argv | ||
@parser = OptionParser.new | ||
set_options | ||
parse_options | ||
end | ||
|
||
# Banner for help output | ||
def banner | ||
progname = @parser.program_name | ||
return <<EOB | ||
Usage: #{progname} translation_file.rb [--output ~/output_dir] | ||
Examples: | ||
#{progname} database_translation.rb | ||
#{progname} database_translation.rb -O ~/output_dir | ||
#{progname} process database.config database_translation.rb | ||
See http://github.com/anlek/mongify for more details | ||
EOB | ||
end | ||
|
||
# Sets the options for CLI | ||
# Also used for help output | ||
def set_options | ||
@parser.banner = banner | ||
@parser.separator "Common options:" | ||
@parser.on("-h", "--help", "Show this message") do | ||
@command_class = Command::Help | ||
end | ||
@parser.on("-v", "--version", "Show version") do | ||
@command_class = Command::Version | ||
end | ||
@parser.on("-O", "--output DIR", "Output Directory") do |dir| | ||
@output_dir = dir | ||
end | ||
end | ||
|
||
# Parses CLI passed attributes and figures out what command user is trying to run | ||
def parse | ||
case | ||
when @command_class == Command::Help | ||
Command::Help.new(@parser) | ||
when @command_class == Command::Version | ||
Command::Version.new(@parser.program_name) | ||
else | ||
Command::Worker.new(translation_file, output_dir, @parser) | ||
end | ||
end | ||
|
||
####### | ||
private | ||
####### | ||
|
||
# Returns the translation_file or nil | ||
def translation_file(argv=@argv) | ||
argv[0] if argv.length >= 1 | ||
end | ||
|
||
# Returns the config file | ||
def output_dir(argv=@argv) | ||
@output_dir if @output_dir && File.exist?(@output_dir) && File.directory?(@output_dir) | ||
end | ||
|
||
# option parser, ensuring parse_options is only called once | ||
def parse_options | ||
@parser.parse!(@argv) | ||
rescue OptionParser::InvalidOption => er | ||
raise Mongify::InvalidOption, er.message, er.backtrace | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
module Mongify | ||
module Mongoid | ||
# | ||
# Used to output messages to the UI | ||
# | ||
class UI | ||
class << self | ||
# Outputs to stream using puts method | ||
def puts(msg) | ||
out_stream.puts(msg) if out_stream | ||
end | ||
|
||
# Outputs to stream using print method | ||
def print(msg) | ||
out_stream.print(msg) if out_stream | ||
end | ||
|
||
# Gets input from user | ||
def gets | ||
in_stream ? in_stream.gets : '' | ||
end | ||
|
||
# Outputs a question and gets input | ||
def request(msg) | ||
print(msg) | ||
gets.chomp | ||
end | ||
|
||
# Asks a yes or no question and waits for reply | ||
def ask(msg) | ||
request("#{msg} [yn] ") == 'y' | ||
end | ||
|
||
# Outputs a Warning (using puts command) | ||
def warn(msg) | ||
puts "WARNING: #{msg}" | ||
end | ||
|
||
# Outputs a message and aborts execution of app | ||
def abort(message) | ||
UI.puts "PROGRAM HALT: #{message}" | ||
Kernel.abort message | ||
end | ||
|
||
# Incoming stream | ||
def in_stream | ||
$stdin | ||
end | ||
# Output stream | ||
def out_stream | ||
$stdout | ||
end | ||
|
||
# Creates an instance of HighLine | ||
# which lets us figure out width of console | ||
# plus a whole lot more | ||
# @return [HighLine] instance | ||
def terminal_helper | ||
@terminal_helper ||= HighLine.new | ||
end | ||
end | ||
end | ||
end | ||
end |