Sapling: Add functionality for web and demo

- Web: Add `serve` functionality, which starts a simple Rack application
to serve a template page. Template still needs work.

- Demo: add `example` option, which starts the CLI using the "Example
Quest!" tree
This commit is contained in:
Bill Niblock 2017-10-30 00:22:31 -04:00
parent 522c23b206
commit ed521bfbcd

View file

@ -1,5 +1,7 @@
#!/usr/bin/env ruby #!/usr/bin/env ruby
# frozen string literal: true
require 'rack'
require 'thor' require 'thor'
require 'yaml' require 'yaml'
@ -7,6 +9,7 @@ Dir[File.join(__dir__, 'sapling', '*.rb')].each { |file| require file }
# The main Sapling interface. # The main Sapling interface.
class Sapling < Thor class Sapling < Thor
# CLI-based options
desc 'read TREE', 'Load and traverse the TREE' desc 'read TREE', 'Load and traverse the TREE'
def read(file) def read(file)
puts 'Welcome to Sapling, a Dialogue Tree Utility.' puts 'Welcome to Sapling, a Dialogue Tree Utility.'
@ -30,10 +33,16 @@ class Sapling < Thor
gardner.plant gardner.plant
end end
# Web-based options
desc 'serve TREE', 'Load TREE in a web-based interface' desc 'serve TREE', 'Load TREE in a web-based interface'
def serve(tree) def serve(file)
exit unless verify_tree(tree) exit unless verify_tree(file)
puts 'Sinatra will be cool.' tree = Gardner::Plot.new(YAML.load_file(file))
Rack::Server.new(
app: Greenhouse.new(tree),
server: 'webrick',
Port: 9000
).start
end end
desc 'export TREE', 'Save a portable HTML version of TREE' desc 'export TREE', 'Save a portable HTML version of TREE'
@ -41,4 +50,15 @@ class Sapling < Thor
exit unless verify_tree(tree) exit unless verify_tree(tree)
puts 'Cool feature, bro!' puts 'Cool feature, bro!'
end end
# Miscellaneous options
desc 'example', 'Play Example Quest!'
def example
file = File.join(__dir__, '..', 'var', 'trees', 'example_quest.yaml')
puts 'Welcome to Sapling, a Dialogue Tree Utility.'
exit unless verify_tree(file)
tree = Gardner::Plot.new(YAML.load_file(file))
speaker = Dialogue::Speaker.new(tree, false)
speaker.conversation
end
end end