class Giblish::GitRepoConverter

Converts all adoc files within a git repo

Public Class Methods

new(options) click to toggle source
Calls superclass method Giblish::FileTreeConverter::new
# File lib/giblish/core.rb, line 292
def initialize(options)
  super(options)
  # cache the top of the tree since we need to redefine the
  # paths per branch/tag later on.
  @master_paths = @paths.dup
  @master_deployment_info = @deploy_info.dup
  @git_repo_root = options[:gitRepoRoot]
  @git_repo = init_git_repo @git_repo_root, options[:localRepoOnly]
  @user_branches = select_user_branches(options[:gitBranchRegexp])
  @user_tags = select_user_tags(options[:gitTagRegexp])
end

Public Instance Methods

convert() click to toggle source

Convert the docs from each branch/tag and add info to the summary page. return true if all conversions went ok, false if at least one failed

# File lib/giblish/core.rb, line 308
def convert
  conv_error = false
  (@user_branches + @user_tags).each do |co|
    has_error = convert_one_checkout(co)
    if has_error == true
      conv_error = true
    end
  rescue
    conv_error = true
    next
  end

  # Render the summary page
  index_builder = GitSummaryIndexBuilder.new @git_repo,
                                             @user_branches,
                                             @user_tags

  conv_error ||= @converter.convert_str(
    index_builder.source,
    @master_paths.dst_root_abs,
    "index"
  )

  # clean up
  GC.start

  conv_error
end

Protected Instance Methods

add_doc(adoc, adoc_stderr) click to toggle source
Calls superclass method Giblish::FileTreeConverter#add_doc
# File lib/giblish/core.rb, line 349
def add_doc(adoc, adoc_stderr)
  info = super(adoc, adoc_stderr)

  # Redefine the srcFile to mean the relative path to the git repo root
  src_file = Pathname.new(info.src_file).relative_path_from(@git_repo_root).to_s
  # Get the commit history of the doc
  # (use a homegrown git log to get 'follow' flag)
  gi = Giblish::GitItf.new(@git_repo_root)
  gi.file_log(src_file).each do |i|
    h = DocInfo::DocHistory.new
    h.date = i["date"]
    h.message = i["message"]
    h.author = i["author"]
    info.history << h
  end
end
graph_builder_factory() click to toggle source
# File lib/giblish/core.rb, line 344
def graph_builder_factory
  Giblish::GitGraphBuilderGraphviz.new @processed_docs, @paths, @deploy_info,
                                       @converter.converter_options, @git_repo
end
index_factory() click to toggle source
# File lib/giblish/core.rb, line 339
def index_factory
  GitRepoIndexBuilder.new(@processed_docs, @converter, @paths, @deploy_info,
                          @options[:resolveDocid], @options[:gitRepoRoot])
end

Private Instance Methods

convert_one_checkout(checkout) click to toggle source

convert all docs from one particular git commit returns true if at least one doc failed to convert and false if everything went ok.

# File lib/giblish/core.rb, line 415
def convert_one_checkout(checkout)
  # determine if we are called with a tag or a branch
  is_tag = (checkout.respond_to?(:tag?) && checkout.tag?)

  Giblog.logger.info { "Checking out #{checkout.name}" }
  @git_repo.checkout checkout.name

  unless is_tag
    # if this is a branch, make sure it is up-to-date
    Giblog.logger.info { "Merging with origin/#{checkout.name}" }
    @git_repo.merge "origin/#{checkout.name}"
  end

  # assign a checkout-unique dst-dir
  dir_name = checkout.name.tr("/", "_") << "/"

  # Update needed base class members before converting a new checkout
  @processed_docs = []
  @paths.dst_root_abs = @master_paths.dst_root_abs.realpath.join(dir_name)

  if @options[:makeSearchable] && !@master_deployment_info.search_assets_path.nil?
    @paths.search_assets_abs = @master_paths.search_assets_abs.join(dir_name)
    @deploy_info.search_assets_path = @master_deployment_info.search_assets_path.join(dir_name)
    Giblog.logger.info { "will store search data in #{@paths.search_assets_abs}" }
  end

  # Parse and convert docs using given args
  Giblog.logger.info { "Convert docs into dir #{@paths.dst_root_abs}" }
  # parent_convert
  begin
    FileTreeConverter.instance_method(:convert).bind_call(self)
  rescue => e
    raise "convert_one_checkout has a failure with #{co}!\n\n(#{e.message})"
  end
end
init_git_repo(git_repo_root, local_only) click to toggle source
# File lib/giblish/core.rb, line 368
def init_git_repo(git_repo_root, local_only)
  # Sanity check git repo root
  git_repo_root || raise(ArgumentError("No git repo root dir given"))

  # Connect to the git repo
  begin
    git_repo = Git.open(git_repo_root)
  rescue StandardError => e
    raise "Could not find a git repo at #{git_repo_root} !"\
        "\n\n(#{e.message})"
  end

  # fetch all remote refs if ok with user
  begin
    git_repo.fetch unless local_only
  rescue StandardError => e
    raise "Could not fetch from origin"\
        "(do you need '--local-only'?)!\n\n(#{e.message})"
  end
  git_repo
end
select_user_branches(checkout_regexp) click to toggle source

Get the branches/tags the user wants to parse

# File lib/giblish/core.rb, line 391
def select_user_branches(checkout_regexp)
  return [] unless @options[:gitBranchRegexp]

  regexp = Regexp.new checkout_regexp
  user_checkouts = @git_repo.branches.remote.select do |b|
    # match branches but remove eventual HEAD -> ... entry
    regexp.match b.name unless b.name =~ /^HEAD/
  end
  Giblog.logger.debug { "selected git branches: #{user_checkouts}" }
  user_checkouts
end
select_user_tags(tag_regexp) click to toggle source
# File lib/giblish/core.rb, line 403
def select_user_tags(tag_regexp)
  return [] unless tag_regexp

  regexp = Regexp.new @options[:gitTagRegexp]
  @git_repo.tags.select do |t|
    regexp.match t.name
  end
end