#!/usr/bin/env ruby require 'ascii_binder/helpers' require 'pathname' require 'trollop' include AsciiBinder::Helpers def call_generate(distro,page=nil) if page == '' page = nil end begin generate_docs(distro,page) rescue Exception => e Trollop::die "Could not generate docs: #{e.message}" end end SUB_COMMANDS = %w{help build watch package clean create} Trollop::options do banner <<-EOF Usage: #$0 Commands: build (default action) Builds the HTML docs in the indicated repo dir create Generates a new AsciiBinder repo at the indicated dir watch Starts Guard, which automatically regenerates changed HTML files on the working branch in the repo dir package Builds and packages the static HTML for all of the sites defined in the _distro_config.yml file clean Remove _preview, _publish and _package dirs created by other AsciiBinder operations. Options: EOF stop_on SUB_COMMANDS end cmd = ARGV.shift repo_dir = nil if cmd.nil? cmd = "build" elsif not SUB_COMMANDS.include?(cmd) if not ARGV.empty? Trollop::die "'#{cmd}' is not a valid asciibinder subcommand. Legal values are '#{SUB_COMMANDS.join('\', \'')}'." else repo_dir = Pathname.new(cmd) cmd = "build" end end cmd_opts = case cmd when "build" Trollop::options do banner <<-EOF Usage: #$0 build Description: This is the default behavior for the asciibinder utility. When run, AsciiBinder reads the _distro_config.yml file out of the working branch of the indicated repo directory and based on that, proceeds to build the working branch version of the documentation for each distro. Once the working branch version is built, AsciiBinder cycles through the other branches named in the _distro_config.yml file until all of the permutations have been built. The available options enable you to limit the scope of the build work, as described by the options themselves. Note that the format for the "--page" option is: : or for subtopics: /: However, if you want to use the --page option extensively, then be aware of the `asciibinder watch` function, which does this for you automatically as you change .adoc files in your working branch. Options: EOF opt :distro, "Instead of building all distros, build branches only for the specified distro.", :default => '' opt :page, "Build only the specified page for all distros and only the current working branch.", :default => '' conflicts :distro, :page end when "create" Trollop::options do banner <<-EOF Usage: #$0 create Description: Creates a new, bare AsciiBinder repo in the specified directory. EOF end when "watch" Trollop::options do banner <<-EOF Usage: #$0 watch Description: In watch mode, AsciiBinder starts a Guard process in the foreground. This process watches the repo_dir for changes to the AsciiDoc (.adoc) files. When a change occurs, AsciiBinder regenerates the specific HTML output of the file that was changed, for the working branch only. This is the equivalent of running: $ asciibinder build --page=':' ...except that the Guardfile automatically detects and runs this as you work. This is meant to be used in conjunction with a web browser that is running a LiveReload plugin. If you are viewing the output HTML page in a browser where LiveReload is active, then every time you save a new version of the .adoc file, the new HTML is automatically regenerated and your page view is automatically refreshed. EOF end when "package" Trollop::options do banner <<-EOF Usage: #$0 package Description: Publish mode is similar to 'build' mode, but once all of the branches' of HTML are generated, 'publish' goes on to organize the branch / distro combinations that are described in _distro_config.yml into their "site" layouts. As a final step, the site layouts are tarred and gzipped for easy placement onto a production web server. Options: EOF opt :site, "Instead of packaging every docs site, package the specified site only.", :default => '' end when "help" Trollop::educate end if (not repo_dir.nil? and not ARGV.empty?) or (repo_dir.nil? and ARGV.length > 1) Trollop::die "Too many arguments provided to ascii_binder: '#{ARGV.join(' ')}'. Exiting." elsif repo_dir.nil? if ARGV.length == 1 repo_dir = Pathname.new(ARGV.shift) else if not cmd == 'create' repo_dir = Pathname.pwd else Trollop::die "Specify a name for the new repo directory." end end end # Validate the repo_dir path if cmd == 'create' if repo_dir.exist? Trollop::die "The specified new repo directory '#{repo_dir}' already exists." end else if not repo_dir.exist? Trollop::die "The specified repo directory '#{repo_dir}' does not exist." elsif not repo_dir.directory? Trollop::die "The specified repo directory path '#{repo_dir}' is not a directory." elsif not repo_dir.readable? Trollop::die "The specified repo directory '#{repo_dir}' is not readable." elsif not repo_dir.writable? Trollop::die "The specified repo directory '#{repo_dir}' cannot be written to." else ['.git','_build_cfg.yml','_distro_map.yml','_templates'].each do |file| if not File.exist?(File.join(repo_dir, file)) Trollop::die "The specified repo directory '#{repo_dir}' does not appear to be an AsciiBinder repo." end end end end # Set the repo root set_source_dir(File.expand_path(repo_dir)) # Change to the repo dir. This is necessary in order for # AsciiDoctor to work properly. if not cmd == 'create' Dir.chdir source_dir end # Do the things with the stuff case cmd when "build" build_distro = cmd_opts[:build] || '' refresh_page = cmd_opts[:page] || '' call_generate(build_distro,refresh_page) when "package" clean_up call_generate('') package_site = cmd_opts[:site] || '' package_docs(package_site) when "watch" if not dir_empty?(preview_dir) guardfile_path = File.join(Gem::Specification.find_by_name("ascii_binder").full_gem_path, 'Guardfile') exec("guard -G #{guardfile_path}") else Trollop::die "Run 'asciibinder build' at least once before running 'asciibinder watch'." end when "clean" clean_up puts "Cleaned up #{repo_dir}." when "create" create_new_repo puts "Created new repo in #{repo_dir}." end exit