lib/susanoo/cli/project.rb in susanoo-0.5.1 vs lib/susanoo/cli/project.rb in susanoo-0.7.0
- old
+ new
@@ -4,14 +4,17 @@
module Susanoo
module CLI
class Project < Thor
+ include ::Thor::Actions
+
package_name 'Susanoo'
map 's' => :server
map 'g' => :generate
+ map 'r' => :run_in
def self.root=(path)
@@root = path
Susanoo::Project.path = path
end
@@ -31,23 +34,76 @@
exit 1
end
generator.start options
end
- method_option :debug, default: false
+ method_option :debug, default: true
+ method_option :built, default: false
desc 'server', 'Run development server.'
def server(port = 3000)
project_root = Susanoo::Project.path
+
+ if options[:built]
+ unless File.directory? File.join(project_root, 'www')
+ error "'www' directory is not present. Build you app first."
+ return
+ end
+
+ app = Rack::Directory.new File.join(project_root, 'www')
+
+ else
+ require File.join(project_root, 'config/routes')
+ # Set global debug flag
+ Susanoo::Project.debug = options[:debug]
+
+ app = ROUTER
+ end
+
+ Rack::Server.start(app: app, server: :thin, Port: port,
+ debug: options[:debug])
+ end
+
+ desc 'build', 'Build the application.'
+ def build
+ project_root = Susanoo::Project.path
+
require File.join(project_root, 'config/routes')
- # Set global debug flag
- Susanoo::Project.debug = options[:debug]
+ build_dir = File.join(project_root, 'www')
+ # setup build directory
- app = Rack::Server.start(app: ROUTER, server: :thin, Port: port,
- debug: options[:debug])
+ remove_file build_dir if File.exist? build_dir
+ # Create the www directory if there isn't
+ # WWW directory will be the build directory
+ # which will contains the static files.
+ #
+ # NOTE: cordova only uses this directory
+ # and we can't change it as far as I know
+ empty_directory build_dir
+
+ Susanoo::StaticGenerator.classes.each do |klass|
+ instance = klass.new
+ if instance.respond_to? :build
+ instance.build(self)
+ else
+ puts "[Warning]: '#{instance.class.to_s}' does not have 'build' method."
+ end
+ end
+
end
+ desc 'run PLATFORM', 'Run application on PLATFORM.'
+ def run_in(platform = :android)
+ # Build the project first
+ build
+
+ inside Susanoo::Project.path do
+ system "cordova run #{platform.to_s}"
+ end
+ end
+
private
+ # Private ---------------------------
def camelize(str)
str.split("_").each {|s| s.capitalize! }.join("")
end