Permalink
Browse files

Better directory options and handling

  • Loading branch information...
1 parent 629596a commit 77a22adb4f6f21ab938e45af62bafae3935f1d2f @wagenet wagenet committed May 19, 2011
Showing with 53 additions and 31 deletions.
  1. +38 −16 lib/sc_docs/cli.rb
  2. +15 −15 lib/sc_docs/generator.rb
View
@@ -5,43 +5,49 @@
module ScDocs
class CLI < Thor
- class_option :input_dir, :aliases => ['-i'], :type => :string,
- :banner => "Directory to generate docs for"
- class_option :output_dir, :aliases => ['-o'], :type => :string,
- :banner => "Directory to output docs to"
class_option :update, :aliases => ['-u'], :type => :boolean, :default => false,
:banner => "If input is a git repo, pull and rebase"
class_option :template, :aliases => ['-t'], :type => :string,
:banner => "Path to jsdoc template (forces HTML output)"
class_option :verbose, :aliases => ['-v'], :type => :boolean, :default => false
- desc "generate", "Generate docs"
+ desc "generate DIRECTORY", "Generate docs"
+ method_option :output_dir, :aliases => ['-o'], :type => :string, :required => true,
+ :banner => "Directory to output docs to"
method_option :project, :aliases => ['-p'], :type => :string,
:banner => "SproutCore Project Name"
- def generate
+ def generate(directory)
+
puts "Generating Documentation...\n\n"
- run_generator
+ update_repo
+ generator(directory).generate
end
- desc "preview", "Preview docs output"
- def preview
+ desc "preview DIRECTORY", "Preview docs output"
+ method_option :output_dir, :aliases => ['-o'], :type => :string, :required => false,
+ :banner => "Directory to output docs to (defaults to a tempfile)"
+ def preview(directory)
puts "Building Documentation Preview...\n\n"
- run_generator(true)
+ update_repo
+ with_temp_output{ generator(directory).preview }
end
private
- def run_generator(preview=false)
- update_repo if options[:update]
+ def output_dir
+ @output_dir || options[:output_dir]
+ end
- puts options[:template]
- generator = (options[:template] ? HtmlGenerator : ScGenerator).new(options)
- preview ? generator.preview : generator.generate
+ def generator(directory)
+ opts = options.merge(:output_dir => output_dir)
+ (opts[:template] ? HtmlGenerator : ScGenerator).new(directory, opts)
end
def update_repo
+ return unless options[:update]
+
puts "Updating repository...\n\n" if options[:verbose]
-
+
if File.directory? input_dir and File.directory? "#{input_dir}/.git"
Dir.chdir input_dir do
run("git fetch", print_output)
@@ -50,5 +56,21 @@ def update_repo
end
end
+ def with_temp_output
+ using_temp_dir = output_dir.nil? || output_dir.empty?
+
+ if using_temp_dir
+ require 'tempfile' # For Dir.tmpdir
+ @output_dir = File.join(Dir.tmpdir, "docs#{rand(100000)}")
+ end
+
+ yield
+ ensure
+ if using_temp_dir
+ FileUtils.rm_rf output_dir
+ @output_dir = nil # Probably not necessary
+ end
+ end
+
end
end
View
@@ -13,8 +13,8 @@ class Generator
attr_reader :verbose
- def initialize(options={})
- @input_dir = File.expand_path(options[:input_dir])
+ def initialize(directory, options={})
+ @input_dir = File.expand_path(directory)
@output_dir = File.expand_path(options[:output_dir])
@verbose = options[:verbose]
end
@@ -58,10 +58,11 @@ def copy_files
path = File.join(template, "output")
if File.directory?(path)
puts "Copying additional files" if verbose
+ # This is stupid, but necessary to copy only the contents
Dir["#{path}/*"].each{|p| FileUtils.cp_r(p, output_dir) }
end
end
-
+
def run_server
Server.new(output_dir).start
end
@@ -70,7 +71,7 @@ def run_server
class HtmlGenerator < Generator
- def initialize(options={})
+ def initialize(directory, options={})
super
@template = File.expand_path(options[:template])
end
@@ -80,10 +81,10 @@ def initialize(options={})
class ScGenerator < Generator
attr_reader :app_dir
-
+
attr_reader :project_name
- def initialize(options={})
+ def initialize(directory, options={})
super
@template = File.expand_path("../templates/sc_fixture", __FILE__)
@app_dir = File.expand_path(options[:output_dir])
@@ -100,10 +101,10 @@ def generate
private
def prep
- target_dir = File.dirname(app_dir)
FileUtils.rm_rf app_dir
- FileUtils.mkdir_p target_dir
- FileUtils.cp_r File.expand_path("../docs", __FILE__), target_dir
+ FileUtils.mkdir_p app_dir
+ # This is stupid, but necessary to copy only the contents
+ Dir[File.expand_path("../docs/*", __FILE__)].each{|f| FileUtils.cp_r f, app_dir }
end
def run_server
@@ -118,11 +119,11 @@ def deploy
tmp_path = File.join(Dir.tmpdir, "docs#{rand(100000)}")
FileUtils.mv app_dir, tmp_path
-
+
Dir.chdir tmp_path
build_cmd = "sc-build -r --languages=en --build-targets=docs --build=#{project_name}"
-
+
puts "Deploying...\n\n"
puts "#{build_cmd}\n\n" if verbose
@@ -131,15 +132,14 @@ def deploy
FileUtils.rm_rf app_dir
FileUtils.mkdir_p app_dir
- docs_dir = File.join(app_dir, "sc_docs")
- FileUtils.cp_r File.join(tmp_path, "tmp", "build", "sc_docs"), docs_dir
- FileUtils.cp File.join(docs_dir, "docs", "en", project_name, "index.html"), docs_dir
+ FileUtils.cp_r File.join(tmp_path, "tmp", "build", "sc_docs"), app_dir
+ FileUtils.cp File.join(app_dir, "sc_docs", "docs", "en", project_name, "index.html"), app_dir
puts "Deployed"
ensure
FileUtils.rm_rf tmp_path
end
-
+
end
end

0 comments on commit 77a22ad

Please sign in to comment.