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:
-
prepare
-
publish
-
clean
@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
The repository branch (ALWAYS “gh-pages”).
Pages folder to use (defaults to ‘pages’).
Commit message.
The remote to use (defaults to ‘origin’).
Public Instance Methods
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
Remove temporary directory.
# File lib/detroit-github.rb, line 144 def clean rm_r File.join(Dir.tmpdir, 'detroit', 'github') end
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
# 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
We do not need to prepare if gh_pages directory is already created.
# File lib/detroit-github.rb, line 119 def prepare? !child end
# File lib/detroit-github.rb, line 45 def prerequisite @branch = PAGES_BRANCH @folder = DEFAULT_FOLDER @remote = DEFAULT_REMOTE @message = DEFAULT_MESSAGE end
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
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 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 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
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
Require Grit.
# File lib/detroit-github.rb, line 222 def initialize_requires require 'grit' end
Get a cached Grit::Repo instance.
# File lib/detroit-github.rb, line 227 def master @master ||= Grit::Repo.new(root) end
Location of child folder.
# File lib/detroit-github.rb, line 247 def pgdir @pgdir ||= File.join(root,folder) end
# File lib/detroit-github.rb, line 261 def root project ? project.root : Dir.pwd end
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
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
Remote URL for master.
# File lib/detroit-github.rb, line 232 def url @url ||= master.config["remote.#{remote}.url"] end