Skip to content
This repository has been archived by the owner on Nov 19, 2024. It is now read-only.

Commit

Permalink
Add multirepo support
Browse files Browse the repository at this point in the history
  • Loading branch information
dshevtsov committed Feb 28, 2019
1 parent 1c5fd7e commit 275963b
Show file tree
Hide file tree
Showing 6 changed files with 186 additions and 3 deletions.
9 changes: 6 additions & 3 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ require 'launchy'
require 'colorator'

# Load ruby files with helper methods from the 'rakelib/' directory
require_relative 'rakelib/link-checker.rb'
require_relative 'rakelib/converter.rb'
require_relative 'rakelib/double-slash-check.rb'
require_relative 'rakelib/lib/link-checker.rb'
require_relative 'rakelib/lib/converter.rb'
require_relative 'rakelib/lib/double-slash-check.rb'

desc "Same as 'rake', 'rake preview'"
task default: %w[preview]
Expand Down Expand Up @@ -56,3 +56,6 @@ task build: %w[clean] do
sh 'bundle exec jekyll build --verbose --trace'
puts 'Built!'.green
end

# desc 'Pull docs from external repositories'
# task init: %w[multirepo:init]
72 changes: 72 additions & 0 deletions rakelib/lib/converter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# frozen_string_literal: true

# Helper methods to convert markdown reports to HTML and open them in a browser.
module Converter
# The CSS to append to the HTML report with broken links
CSS = %q(
<link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet">
<style>
/* Lists
–––––––––––––––––––––––––––––––––––––––––––––––––– */
ul {
list-style: disc;
font-family: \'Roboto\', sans-serif;
color: red; }
ol {
list-style: decimal inside; }css
ol, ul {
padding-left: 0;
margin-top: 0; }
ul ul,
ul ol,
ol ol,
ol ul {
margin: 1.5rem 0 1.5rem 3rem;
font-size: 90%;
color: black;}
li {
margin-bottom: 1rem;
font-weight: bold;}
</style>
)

# Define the Kramdown method to convert markdown to HTML.
def self.kramdown(text)
Kramdown::Document.new(text).to_html
end

# Read content in the given path
def self.content(path)
File.read(path)
end

# Automatically open the HTML report in a browser.
def self.open_in_browser(path)
Launchy.open(path) do |exception|
warn "Attempted to open #{path} and failed because #{exception}".red
end
end

# Locate the output directory, and convert the latest markdown file to HTML.
def self.to_html
latest_md =
FileList["#{LinkChecker::DIR}/*.md"].max_by { |file| File.mtime file }

print "Reading the #{latest_md} ... ".magenta
# Change a file extension to .html
html_file = latest_md.ext('html')
File.open(html_file, 'w') do |file|
print 'converting to HTML ... '.magenta
file.write kramdown(content(latest_md))
file.write CSS
end

# Open the HTML report in browser
print 'opening the converted report in browser ... '.magenta
open_in_browser(html_file)
puts 'Done!'.green
end
end
20 changes: 20 additions & 0 deletions rakelib/lib/double-slash-check.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Custom check for html-proofer to find double forward slashes in URLs.
module LinkChecker
class DoubleSlashCheck < ::HTMLProofer::Check
def slash?
return false if @link.href.nil?
@link.href.match %r{\w//}
end

def run
@html.css('a').each do |node|
@link = create_element(node)
line = node.line

if slash?
return add_issue("Remove double forward slashes from URLs", line: line)
end
end
end
end
end
41 changes: 41 additions & 0 deletions rakelib/lib/link-checker.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# frozen_string_literal: true

# Helper methods to configure and run html-proofer
module LinkChecker
# Run html-proofer to check the generated HTML pages
def self.check_site
HTMLProofer.check_directory('_site', options).run
end

# Read options from the '_config.checks.yml'
# Ignore baseurl if $branch is available in environment
def self.options
config = YAML.load_file('_config.checks.yml')
baseurl = ENV['branch']
return config['html-proofer'] unless baseurl
url_swap = { url_swap: { %r{\A/#{baseurl}} => '' } }
config['html-proofer'].merge(url_swap)
end

# Relative path for the link checker report
def self.md_report_path
# Create the directory if it doesn't exist
FileUtils.mkdir_p DIR
File.join DIR, file_name
end

# Name the directory for the link checker reports
DIR = File.join 'tmp', '.htmlproofer'

# Read the current Git branch
def self.current_branch
`git symbolic-ref --short HEAD`.strip
end

# Name the file for the link checker report
def self.file_name
prefix = 'broken-links-in-'
timestamp = Time.now.strftime('_%m-%d_%H-%M-%S')
prefix + current_branch + timestamp + '.md'
end
end
22 changes: 22 additions & 0 deletions rakelib/multirepo.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace :multirepo do
desc 'Add docs from external repositories (mftf, page-builder)'
task :init do
sh './scripts/docs-from-code.sh mftf git@github.com:magento-devdocs/magento2-functional-testing-framework.git docs-in-code'
sh './scripts/docs-from-code.sh page-builder git@github.com:magento-devdocs/magento2-page-builder.git ds_docs-in-code'
sh './scripts/docs-from-code.sh mbi git@github.com:magento/devdocs-mbi.git master'
end

desc 'Add multirepo docs providing arguments "dir", "repo", and "branch". Example: rake multirepo:add dir=mftf repo=git@github.com:magento-devdocs/magento2-functional-testing-framework.git branch=docs-in-code'
task :add do
dir = ENV['dir']
repo = ENV['repo']
branch = ENV['branch']

abort 'Provide a directory name for the multirepo docs. Example: dir=mftf' unless dir
abort "'#{dir}' directory already exists" if Dir.exist? dir
abort 'Provide a repository cloning URL (SSH).Example: repo=git@github.com:magento-devdocs/magento2-functional-testing-framework.git' unless repo
abort 'Provide a branch name for the multirepo docs. Example: branch=master' unless branch

sh "./scripts/docs-from-code.sh #{dir} #{repo} #{branch}"
end
end
25 changes: 25 additions & 0 deletions scripts/docs-from-code.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env bash

dir=$1
repo=$2
branch=$3

echo "Creating a directory: $dir"
mkdir "$dir"
cd "$dir" || exit

echo 'Initiating git in the directory'
git init

echo "Adding a remote repository: $repo"
git remote add origin -f "$repo"

echo 'Enabling sparse checkout'
git config core.sparseCheckout true

echo 'Adding /docs/* to sparse checkout'
echo '/docs/*' >> .git/info/sparse-checkout

echo "Checkouting a branch: $branch"
git checkout "$branch"
cd ..

0 comments on commit 275963b

Please sign in to comment.