Skip to content

Commit

Permalink
Change how guides are built so they work like manageiq_docs.
Browse files Browse the repository at this point in the history
  • Loading branch information
hayesr committed Jan 10, 2017
1 parent e2fa3b0 commit 1b297e7
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 32 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -4,6 +4,7 @@ _site
.env

dest/
site/docs/guides
site/docs/reference/latest/
site/.asset-cache

Expand Down
3 changes: 0 additions & 3 deletions .gitmodules

This file was deleted.

4 changes: 3 additions & 1 deletion Dockerfile
Expand Up @@ -25,7 +25,9 @@ ENV MIQ_BUNDLER=/root/.rbenv/shims/bundle

ENV MIQ_BASE_DIR=/srv/build
ENV MIQ_DOCS_DIR=/srv/build/site/docs
ENV MIQ_GUIDES_DIR=${MIQ_DOCS_DIR}/guides

ENV MIQ_GUIDES_REPO=https://github.com/manageiq/guides
ENV MIQ_GUIDES_BRANCH=master

ENV MIQ_SITE_DEST=/srv/manageiq_org
ENV MIQ_SITE_SOURCE=${MIQ_BASE_DIR}/site
Expand Down
12 changes: 2 additions & 10 deletions exe/miq
Expand Up @@ -81,9 +81,8 @@ module Miq
Guides.build
end

desc "site", "Build Jekyll site (and guides)"
desc "site", "Build Jekyll site"
def site
invoke :guides
say "Building Jekyll site", :green
Site.build
end
Expand All @@ -99,7 +98,7 @@ module Miq
desc "menus", "Regenerate menu yaml files"
def menus
say "Rebuilding menu yaml files", :red
say "This will clobber existing files.", :red
say "This will clobber existing files in tmp.", :red
answer = ask("Are you sure? Y/n")

if answer == "Y"
Expand Down Expand Up @@ -135,15 +134,8 @@ module Miq
desc "generate lwimiq YYYY-MM-DD", "Generate a new blog post"
subcommand "generate", Generate

# desc "deploy", "Prepares and deploys site. Don't use unless your changes have been pushed to repos."
# def deploy
# invoke 'miq:update:all'
# invoke 'miq:build:all'
# end

desc "serve", "Does Jekyll serve with appropriate args"
def serve
invoke 'miq:build:guides'
Site.serve
end
end
Expand Down
90 changes: 79 additions & 11 deletions lib/miq/guides.rb
@@ -1,3 +1,5 @@
require "active_support/core_ext/object/blank"

module Miq
class Guides < Executor
def self.reset
Expand All @@ -12,39 +14,105 @@ def self.build
new.build
end

attr_reader :guides_dir
attr_reader :repo, :branch, :tmp_dir, :src_dir, :dst_dir

def initialize
@guides_dir = Miq.site_dir.join( (ENV["MIQ_GUIDES_DIR"] || "docs/guides") )
# Where do the docs live?
@repo = ENV["MIQ_GUIDES_REPO"] || "https://github.com/ManageIQ/guides.git"

# What branch to checkout?
@branch = ENV["MIQ_GUIDES_BRANCH"] || "master"

# Where should we cache and build?
@tmp_dir = ENV["MIQ_GUIDES_TMP"] || "/tmp/guides"

# What subdirectory do we want?
@src_dir = ENV["MIQ_GUIDES_SRC"]

# Where should the files end up?
@dst_dir = ENV["MIQ_GUIDES_DST"] || Miq.docs_dir.join("guides")
end

def reset
shell "cd #{guides_dir} && git reset --hard HEAD"
end
logger.info "Removing #{tmp_dir}"
logger.info "Removing files and directories from #{dst_dir}"

def update
reset
shell "git submodule update"
unless debug?
rm_dir(tmp_dir)

Dir["#{dst_dir}/*"].each do |path|
if File.directory?(path)
rm_dir(path)
else
File.delete(path)
end
end
end
end

def build
clone_or_update_repo
sync_files
add_front_matter
end

# Adds front matter for Jekyll compat
def update
clone_or_update_repo
end

def clone_or_update_repo
if File.directory?(tmp_dir)
logger.info "Updating local guides repo"
shell "cd #{tmp_dir} && git pull origin"
else
logger.info "Cloning #{repo} to #{tmp_dir}"
shell "git clone #{repo} --depth 1 #{tmp_dir}"
end
end

# Adds YAML front matter for Jekyll compat
def add_front_matter
Dir["#{guides_dir}/**/*.md"].each do |path|
logger.info "Adding front matter"

Dir["#{dst_dir}/**/*.md"].each do |path|
content = File.readlines(path)

unless content[0] == "---\n"
File.open(path, "w") do |f|
f.puts "---\n---\n"
content.each {|line| f.puts line }
f.puts "---\n" * 2
content.each { |line| f.puts line }
end
end
end
end

def sync_files
if File.directory?("#{tmp_dir}/#{src_dir}") || debug?
prep dst_dir
logger.info "Syncing files to #{dst_dir}"
cmd = "rsync -av "
cmd << exclude_files.map{|x| "--exclude '#{x}'"}.join(' ')
cmd << " #{sync_target}/* #{dst_dir}/"
shell cmd
else
logger.error "Reference docs source directory not present."
end
end

def sync_target
if src_dir.blank?
tmp_dir
else
"#{tmp_dir}/#{src_dir}"
end
end

private

# Relative to src_dir
def exclude_files
[]
end
# /Guides
end
end
6 changes: 3 additions & 3 deletions lib/miq/ref_docs.rb
Expand Up @@ -52,7 +52,7 @@ def build
clone_or_update_repo
setup_ascii_binder
build_ref_docs
move_files
sync_files
end

def update
Expand All @@ -65,7 +65,7 @@ def clone_or_update_repo
shell "cd #{tmp_dir} && git pull origin"
else
logger.info "Cloning #{repo} to #{tmp_dir}"
shell "git clone #{repo} #{tmp_dir}"
shell "git clone #{repo} --depth 1 #{tmp_dir}"
end
end

Expand All @@ -86,7 +86,7 @@ def build_ref_docs
].join(" && ")
end

def move_files
def sync_files
if File.directory?("#{tmp_dir}/#{src_dir}") || debug?
prep dst_dir
logger.info "Syncing files to #{dst_dir}"
Expand Down
1 change: 0 additions & 1 deletion site/docs/guides
Submodule guides deleted from 3db529
8 changes: 5 additions & 3 deletions test/lib/miq/guides_test.rb
Expand Up @@ -3,16 +3,18 @@
class GuidesTest < Minitest::Test
def setup
@dir = fixtures_path.join("docs", "guides")
ENV["MIQ_GUIDES_DIR"] = @dir.to_s
ENV["MIQ_GUIDES_TMP"] = @dir.to_s
ENV["MIQ_GUIDES_DST"] = @dir.to_s
@subject = Miq::Guides.new
end

def teardown
reset_fixture
end

def test_setting_dir_from_env
assert_equal @dir, @subject.guides_dir
def test_setting_dirs_from_env
assert_equal @dir.to_s, @subject.tmp_dir
assert_equal @dir.to_s, @subject.dst_dir
end

def test_adding_front_matter
Expand Down

0 comments on commit 1b297e7

Please sign in to comment.