class Detroit::GitHub

GitHub tool provides services for working with your project’s github repository.

Currently it only supports gh-pages publishing.

The following stations of the standard toolchain are targeted:

@note This tool is useless unless your project is hosted on GitHub!

Constants

DEFAULT_FOLDER

The project directory to store the gh-pages git repo.

DEFAULT_MESSAGE

Default commit message.

DEFAULT_REMOTE

Default remote name.

MANPAGE

Location of manpage for tool.

PAGES_BRANCH

Attributes

branch[R]

The repository branch (ALWAYS “gh-pages”).

folder[RW]

Pages folder to use (defaults to ‘pages’).

message[RW]

Commit message.

remote[RW]

The remote to use (defaults to ‘origin’).

Public Instance Methods

assemble?(station, options={}) click to toggle source

This tool ties into the ‘prepare`, `publish` and `clean` stations of the standard assembly.

@return [Boolean]

# File lib/detroit-github.rb, line 161
def assemble?(station, options={})
  return true if station == :prepare
  return true if station == :publish
  return true if station == :purge
  return false
end
clean() click to toggle source

Remove temporary directory.

# File lib/detroit-github.rb, line 144
def clean
  rm_r File.join(Dir.tmpdir, 'detroit', 'github')
end
keep=(entries) click to toggle source

Set keep list.

# File lib/detroit-github.rb, line 86
def keep=(entries)
  case entries
  when String
    @keep = [entries.to_str]
  else
    @keep = entries
  end
end
prepare() click to toggle source
# File lib/detroit-github.rb, line 99
def prepare
  return if child

  if File.exist?(pgdir)
    abort "Can't setup gh-pages at #{folder}. Directory already exists."
  end

  # does the master repo have a gh-pages branch?
  new = !master.branches.find{ |b| b.name == branch }

  if new
    create_branch
  else
    clone_branch
  end

  update_gitignore
end
prepare?() click to toggle source

We do not need to prepare if gh_pages directory is already created.

# File lib/detroit-github.rb, line 119
def prepare?
  !child
end
prerequisite() click to toggle source
# File lib/detroit-github.rb, line 45
def prerequisite
  @branch  = PAGES_BRANCH
  @folder  = DEFAULT_FOLDER
  @remote  = DEFAULT_REMOTE
  @message = DEFAULT_MESSAGE
end
publish() click to toggle source

Publish sitemap files to branch (gh-pages).

@todo Should we ‘git add –all` ?

@return [void]

# File lib/detroit-github.rb, line 128
def publish
  if !File.directory?(pgdir)
    report "No pages folder found (#{folder})."
    return
  end

  #copy_files  # post_generate assembly ?

  chdir(pgdir) do
    #sh %[git add -A]
    sh %[git commit -q -a -m "#{message}"]
    sh %[git push #{remote} #{branch}]
  end
end
sitemap=(entries) click to toggle source

Set sitemap.

# File lib/detroit-github.rb, line 76
def sitemap=(entries)
  case entries
  when String
    @sitemap = [entries.to_str]
  else
    @sitemap = entries
  end
end

Private Instance Methods

child() click to toggle source

Child Grit::Repo instance, i.e. a repo just for gh-pages.

# File lib/detroit-github.rb, line 237
def child
  @child ||= \
    begin
      Grit::Repo.new(pgdir) if File.directory?(pgdir)
    rescue Grit::InvalidGitRepositoryError
      nil          
    end
end
clone_branch() click to toggle source

Clone the repo to a local folder, checkout the pages branch and remove the master branch.

# File lib/detroit-github.rb, line 177
def clone_branch
  sh %[git clone #{url} #{pgdir}]
  Dir.chdir(pgdir) do
    sh %[git checkout #{branch}]
    sh %[git branch -d master]
  end
end
create_branch() click to toggle source

If the gh-pages branch doesn’t exist we need to create it.

# File lib/detroit-github.rb, line 188
def create_branch
  sh %[git clone #{url} #{pgdir}]
  Dir.chdir(pgdir) do
    sh %[git symbolic-ref HEAD refs/heads/#{branch}]
    sh %[rm .git/index]
    sh %[git clean -fdxq]
    sh %[echo "My GitHub Page" > index.html]
    sh %[git add .]
    sh %[git commit -a -m "First pages commit."]
    sh %[git push origin #{branch}]
    sh %[git checkout #{branch}]
    sh %[git branch -d master]
  end
end
initialize_requires() click to toggle source

Require Grit.

# File lib/detroit-github.rb, line 222
def initialize_requires
  require 'grit'
end
master() click to toggle source

Get a cached Grit::Repo instance.

# File lib/detroit-github.rb, line 227
def master
  @master ||= Grit::Repo.new(root)
end
pgdir() click to toggle source

Location of child folder.

# File lib/detroit-github.rb, line 247
def pgdir
  @pgdir ||= File.join(root,folder)
end
root() click to toggle source
# File lib/detroit-github.rb, line 261
def root
  project ? project.root : Dir.pwd
end
tmpdir() click to toggle source

Cached system temporary directory.

# File lib/detroit-github.rb, line 252
def tmpdir
  @tmpdir ||= (
    tmpdir = File.join(Dir.tmpdir, 'detroit', 'github', Time.now.to_i.to_s)
    mkdir_p(tmpdir)
    tmpdir
  )
end
update_gitignore() click to toggle source

Only updates .gitignore if folder is not already present.

# File lib/detroit-github.rb, line 204
def update_gitignore
  file = File.join(root, '.gitignore')
  if File.exist?(file)
    done = false
    File.readlines(file).each do |line|
      done = true if line.strip == folder
    end
    append(file, folder) unless done
  else
    write(file, folder)
  end
end
url() click to toggle source

Remote URL for master.

# File lib/detroit-github.rb, line 232
def url
  @url ||= master.config["remote.#{remote}.url"]
end