From 9575160f29fd0b3497c3474bc7ca2972eee1d5cd Mon Sep 17 00:00:00 2001 From: Bill Niblock Date: Sat, 14 Oct 2017 21:47:01 -0400 Subject: [PATCH] Implement Thor - Remove old sapling.rb file - Add new directory bin/ - Add new version of sapling.rb --- bin/sapling.rb | 45 ++++++++++++++++++++++++++++ sapling.rb | 79 -------------------------------------------------- 2 files changed, 45 insertions(+), 79 deletions(-) create mode 100644 bin/sapling.rb delete mode 100644 sapling.rb diff --git a/bin/sapling.rb b/bin/sapling.rb new file mode 100644 index 0000000..bcbc006 --- /dev/null +++ b/bin/sapling.rb @@ -0,0 +1,45 @@ +#!/usr/bin/env ruby + +require 'thor' +require 'yaml' + +Dir[File.join(__dir__, 'lib', '*.rb')].each { |file| require file } + +# The main Sapling interface. +class Sapling < Thor + desc 'read TREE', 'Load and traverse the TREE' + def read(tree) + exit if verify_tree(tree) + puts 'Welcome to Sapling, a Dialogue Tree Utility.' + speaker = Dialogue::Speaker.new(YAML.load_file(tree), false) + speaker.conversation + end + + desc 'edit TREE', 'Edit a new or existing TREE' + def edit(tree = '') + puts 'Welcome to Sapling, a Dialogue Tree Utility.' + if !tree.empty? + puts "Loading tree: #{tree}" + exit if verify_tree(tree) + gardner = Planter::Spade.new(YAML.load_file(tree, false)) + else + puts 'Creating a new tree!' + gardner = Planter::Spade.new(SKELETON_TREE) + end + gardner.plant + end + + desc 'serve TREE', 'Load TREE in a web-based interface' + def serve(tree) + exit if verify_tree(tree) + puts 'Sinatra will be cool.' + end + + desc 'export TREE', 'Save a portable HTML version of TREE' + def export(tree) + exit if verify_tree(tree) + puts 'Cool feature, bro!' + end +end + +Sapling.start(ARGV) diff --git a/sapling.rb b/sapling.rb deleted file mode 100644 index 5e3c9ae..0000000 --- a/sapling.rb +++ /dev/null @@ -1,79 +0,0 @@ -#!/usr/bin/env ruby - -require 'optparse' -require 'yaml' - -Dir[File.join(__dir__, 'lib', '*.rb')].each { |file| require file } - -# Sapling is the main module for the program. From here, the rest of the world -# starts building. -module Sapling - # CLI is the class for option parsing, and the gateway to the program, on the - # command line - class CLI - # Option parsing, and gateway to either reading and traversing a tree, or - # editing/creating a tree. - def talk(options) - opt_parser = OptionParser.new do |opt| - opt.banner = "Usage: sapling -t FILE\n" \ - "Usage: sapling -e [FILE]" - - opt.on_tail("-h", "--help", "Show this menu") do - puts opt - exit - end - - opt.on("-t", "--talk", - "Begin traversing the provided dialogue tree") do - - if ARGV.empty? - puts "No tree file provided. Please provide a tree file." - puts opt_parser - exit - end - - unless verify_tree(ARGV[0]) - puts "\n#{opt}\n" - exit - end - - puts "Welcome to Sapling, a Dialogue Tree Utility.\n" - speaker = Dialogue::Speaker.new(YAML.load_file(ARGV[0]),false) - speaker.conversation - end - - opt.on("-e", "--edit", - "Create or edit a dialogue tree") do - - if ARGV.empty? - puts "Creating a new tree." - tree = SKELETON_TREE - else - puts "Using tree at #{ARGV[0]}." - unless verify_tree(ARGV[0]) - puts "\n#{opt}\n" - exit - end - tree = YAML.load_file(ARGV[0]) - end - - puts "Welcome to Sapling, a Dialogue Tree Utility.\n" - gardner = Planter::Spade.new(tree) - gardner.plant - end - - end - - # Handle bad options gracefully - begin - opt_parser.parse!(options) - rescue OptionParser::InvalidOption - puts "Invalid option." - puts opt_parser - exit - end - end - end -end - -Sapling::CLI.new.talk(ARGV)