Skip to content

Commit a65cad2

Browse files
committed
Create 'asciibinder' utility
1 parent 5af84cc commit a65cad2

File tree

6 files changed

+197
-11
lines changed

6 files changed

+197
-11
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,5 @@
1212
*.o
1313
*.a
1414
mkmf.log
15+
*.gem
16+
*.swp

Guardfile

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
require 'ascii_binder'
2+
gem_dir = Gem::Specification.find_by_name("ascii_binder").lib_dirs_glob
3+
instance_eval(File.read(File.join(gem_dir, 'ascii_binder/tasks/guards.rb')))

ascii_binder.gemspec

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ Gem::Specification.new do |spec|
3131
spec.add_dependency 'json'
3232
spec.add_dependency 'pandoc-ruby'
3333
spec.add_dependency 'sitemap_generator', '~> 5.1.0'
34+
spec.add_dependency 'trollop', '~> 2.1.2'
3435
spec.add_dependency 'yajl-ruby'
3536
spec.add_dependency 'tilt'
3637
end

bin/asciibinder

+155
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
#!/usr/bin/env ruby
2+
3+
require 'ascii_binder/helpers'
4+
require 'pathname'
5+
require 'trollop'
6+
7+
8+
include AsciiBinder::Helpers
9+
10+
SUB_COMMANDS = %w{help build watch package clean}
11+
Trollop::options do
12+
banner <<-EOF
13+
Usage:
14+
#$0 <command> <repo_dir>
15+
16+
Commands:
17+
build (default action)
18+
Builds the HTML docs in the indicated repo dir
19+
watch
20+
Starts Guard, which automatically regenerates changed HTML
21+
files on the working branch in the repo dir
22+
package
23+
Builds and packages the static HTML for all of the sites
24+
defined in the _distro_config.yml file
25+
clean
26+
Remove _preview, _publish and _package dirs created by
27+
other asciibinder operations.
28+
29+
Options:
30+
EOF
31+
stop_on SUB_COMMANDS
32+
end
33+
34+
cmd = ARGV.shift
35+
repo_dir = nil
36+
37+
if cmd.nil?
38+
cmd = "build"
39+
elsif not SUB_COMMANDS.include?(cmd)
40+
if not ARGV.empty?
41+
Trollop::die "'#{cmd}' is not a valid asciibinder command. Legal values are '#{SUB_COMMANDS.join('\', \'')}'."
42+
else
43+
repo_dir = Pathname.new(cmd)
44+
cmd = "build"
45+
end
46+
end
47+
48+
cmd_opts = case cmd
49+
when "build"
50+
Trollop::options do
51+
banner <<-EOF
52+
Usage:
53+
#$0 build <options> <repo_dir>
54+
55+
Description:
56+
This is the default behavior for the asciibinder utility. When run,
57+
asciibinder reads the _distro_config.yml file out of the working
58+
branch of the indicated repo directory and based on that, proceeds to
59+
build the working branch version of the documentation for each distro.
60+
61+
Once the working branch version is built, asciibinder cycles through
62+
the other branches named in the _distro_config.yml file until all of
63+
the permutations have been built.
64+
65+
Options:
66+
EOF
67+
opt :distro, "Instead of building all distros, build branches only for the specified distro.", :default => ''
68+
end
69+
#when "new"
70+
# Trollop::options do
71+
# opt :initialize, "Create a new AsciiBinder-ready git repo in the target directory.", :default => true
72+
# end
73+
when "watch"
74+
Trollop::options do
75+
banner <<-EOF
76+
Usage:
77+
#$0 watch <repo_dir>
78+
79+
Description:
80+
In watch mode, asciibinder starts a Guard process in the foreground.
81+
This process watches the repo_dir for changes to the AsciiDoc (.adoc)
82+
files. When a change occurs, asciibinder regenerates the specific
83+
HTML output of the file that was changed, for the working branch only.
84+
85+
This is meant to be used in conjunction with a web browser that is
86+
running a LiveReload plugin. If you are viewing the output HTML page
87+
in a browser where LiveReload is active, then every time you save a
88+
new version of the .adoc file, the new HTML is automatically
89+
regenrated and your page view is automatically refreshed.
90+
EOF
91+
end
92+
when "package"
93+
Trollop::options do
94+
banner <<-EOF
95+
Usage:
96+
#$0 package <options> <repo_dir>
97+
98+
Description:
99+
Publish mode is similar to 'build' mode, but once all of the branches' of
100+
HTML are generated, 'publish' goes on to organize the branch / distro
101+
combinations that are described in _distro_config.yml into their "site"
102+
layouts. As a final step, the site layouts are tarred and gzipped for
103+
easy placement onto a production web server.
104+
105+
Options:
106+
EOF
107+
opt :site, "Instead of packaging every docs site, package the specified site only.", :default => ''
108+
end
109+
when "help"
110+
Trollop::educate
111+
end
112+
113+
if (not repo_dir.nil? and not ARGV.empty?) or (repo_dir.nil? and ARGV.length > 1)
114+
Trollop::die "Too many arguments provided to ascii_binder: '#{ARGV.join(' ')}'. Exiting."
115+
elsif repo_dir.nil?
116+
if ARGV.length == 1
117+
repo_dir = Pathname.new(ARGV.shift)
118+
else
119+
repo_dir = Pathname.pwd
120+
end
121+
end
122+
123+
# Validate the repo_dir path
124+
if not repo_dir.exist?
125+
Trollop::die "The specified repo directory '#{repo_dir}' does not exist."
126+
elsif not repo_dir.directory?
127+
Trollop::die "The specified repo directory path '#{repo_dir}' is not a directory."
128+
elsif not repo_dir.readable?
129+
Trollop::die "The specified repo directory '#{repo_dir}' is not readable."
130+
elsif not repo_dir.writable?
131+
Trollop::die "The specified repo directory '#{repo_dir}' cannot be written to."
132+
end
133+
134+
# Set the repo root
135+
set_source_dir(File.expand_path(repo_dir))
136+
137+
# Do the things with the stuff
138+
case cmd
139+
when "build"
140+
build_distro = cmd_opts[:build] || ''
141+
generate_docs(build_distro)
142+
when "package"
143+
clean_up
144+
generate_docs('')
145+
package_site = cmd_opts[:site] || ''
146+
package_docs(package_site)
147+
when "watch"
148+
guardfile_path = File.join(Gem::Specification.find_by_name("ascii_binder").full_gem_path, 'Guardfile')
149+
exec("guard -G #{guardfile_path}")
150+
when "clean"
151+
clean_up
152+
puts "Cleaned up #{repo_dir}."
153+
end
154+
155+
exit

lib/ascii_binder/helpers.rb

+35-10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
require 'ascii_binder/template_renderer'
12
require 'asciidoctor'
23
require 'asciidoctor/cli'
34
require 'asciidoctor-diagram'
@@ -18,6 +19,10 @@ def self.source_dir
1819
@source_dir ||= `git rev-parse --show-toplevel`.chomp
1920
end
2021

22+
def self.set_source_dir(source_dir)
23+
@source_dir = source_dir
24+
end
25+
2126
def self.template_dir
2227
@template_dir ||= File.join(source_dir,'_templates')
2328
end
@@ -42,9 +47,7 @@ def self.package_dir
4247
end
4348
end
4449

45-
def_delegators self, :source_dir, :template_dir, :preview_dir, :package_dir
46-
47-
TemplateRenderer.initialize_cache(template_dir)
50+
def_delegators self, :source_dir, :set_source_dir, :template_dir, :preview_dir, :package_dir
4851

4952
BUILD_FILENAME = '_build_cfg.yml'
5053
DISTRO_MAP_FILENAME = '_distro_map.yml'
@@ -70,17 +73,17 @@ def git_checkout branch_name
7073

7174
def git_stash_all
7275
# See if there are any changes in need of stashing
73-
@stash_needed = `git status --porcelain` !~ /^\s*$/
76+
@stash_needed = `cd #{source_dir} && git status --porcelain` !~ /^\s*$/
7477
if @stash_needed
7578
puts "\nNOTICE: Stashing uncommited changes and files in working branch."
76-
`git stash -u`
79+
`cd #{source_dir} && git stash -u`
7780
end
7881
end
7982

8083
def git_apply_and_drop
8184
return unless @stash_needed
8285
puts "\nNOTE: Re-applying uncommitted changes and files to working branch."
83-
if system("git stash pop")
86+
if system("cd #{source_dir} && git stash pop")
8487
puts "NOTE: Stash application successful."
8588
else
8689
puts "ERROR: Could not apply stashed code. Run `git stash apply` manually."
@@ -184,7 +187,7 @@ def page(args)
184187
args[:subtopic_shim] = '../'
185188
end
186189

187-
TemplateRenderer.new.render("_templates/page.html.erb", args)
190+
TemplateRenderer.new.render(File.expand_path("#{source_dir}/_templates/page.html.erb"), args)
188191
end
189192

190193
def extract_breadcrumbs(args)
@@ -405,6 +408,9 @@ def generate_docs(build_distro,single_page=nil)
405408
puts "Building all distributions."
406409
end
407410

411+
# Cache the page templates
412+
TemplateRenderer.initialize_cache(template_dir)
413+
408414
# First, notify the user of missing local branches
409415
missing_branches = []
410416
distro_branches(build_distro).sort.each do |dbranch|
@@ -432,6 +438,9 @@ def generate_docs(build_distro,single_page=nil)
432438
end
433439
end
434440

441+
# Note the image files checked in to this branch.
442+
branch_image_files = Find.find(source_dir).select{ |path| not path.nil? and (path =~ /.*\.png$/ or path =~ /.*\.png\.cache$/) }
443+
435444
first_branch = single_page.nil?
436445

437446
if local_branch =~ /^\(detached from .*\)/
@@ -477,13 +486,13 @@ def generate_docs(build_distro,single_page=nil)
477486
system("mkdir -p #{branch_path}/images")
478487

479488
# Copy stylesheets into preview area
480-
system("cp -r _stylesheets/*css #{branch_path}/stylesheets")
489+
system("cp -r #{source_dir}/_stylesheets/*css #{branch_path}/stylesheets")
481490

482491
# Copy javascripts into preview area
483-
system("cp -r _javascripts/*js #{branch_path}/javascripts")
492+
system("cp -r #{source_dir}/_javascripts/*js #{branch_path}/javascripts")
484493

485494
# Copy images into preview area
486-
system("cp -r _images/* #{branch_path}/images")
495+
system("cp -r #{source_dir}/_images/* #{branch_path}/images")
487496

488497
# Build the landing page
489498
navigation = nav_tree(distro,branch_build_config)
@@ -573,6 +582,15 @@ def generate_docs(build_distro,single_page=nil)
573582
return
574583
end
575584

585+
# Remove DITAA-generated images
586+
ditaa_image_files = Find.find(source_dir).select{ |path| not path.nil? and not (path =~ /_preview/ or path =~ /_package/) and (path =~ /.*\.png$/ or path =~ /.*\.png\.cache$/) and not branch_image_files.include?(path) }
587+
if not ditaa_image_files.empty?
588+
puts "\nRemoving ditaa-generated files from repo before changing branches."
589+
ditaa_image_files.each do |dfile|
590+
File.unlink(dfile)
591+
end
592+
end
593+
576594
if local_branch == working_branch
577595
# We're moving away from the working branch, so save off changed files
578596
git_stash_all
@@ -652,6 +670,7 @@ def configure_and_generate_page options
652670
:images_path => "../../#{dir_depth}#{branch_config["dir"]}/images/",
653671
:site_home_path => "../../#{dir_depth}index.html",
654672
:css => ['docs.css'],
673+
:template_dir => template_dir,
655674
}
656675
full_file_text = page(page_args)
657676
File.write(tgt_file_path,full_file_text)
@@ -706,5 +725,11 @@ def package_docs(package_site)
706725
end
707726
end
708727
end
728+
729+
def clean_up
730+
if not system("rm -rf #{source_dir}/_preview/* #{source_dir}/_package/*")
731+
puts "Nothing to clean."
732+
end
733+
end
709734
end
710735
end

lib/ascii_binder/version.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module AsciiBinder
2-
VERSION = "0.0.6"
2+
VERSION = "0.0.7"
33
end

0 commit comments

Comments
 (0)