Skip to content

Instantly share code, notes, and snippets.

@scrogson
Last active July 10, 2017 15:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scrogson/df719a6b4cc8d985a353a942c1342970 to your computer and use it in GitHub Desktop.
Save scrogson/df719a6b4cc8d985a353a942c1342970 to your computer and use it in GitHub Desktop.
A Mix Task to Publish docs to GitHub Pages
defmodule Mix.Tasks.Docs.Publish do
use Mix.Task
@shortdoc "Publishes docs to GitHub Pages"
@moduledoc """
Publishes docs to GitHub Pages.
mix docs.publish [git-remote]
"""
def run([]), do: run(["origin"])
def run([remote|_]) do
File.rm_rf("doc")
Mix.Task.run("docs")
remote_url = get_remote_url(remote)
Mix.shell.info "Git remote #{remote_url}"
File.cd!("doc")
run! "git init ."
run! "git add ."
run! "git commit -m \"Generating docs\""
run! "git remote add origin #{remote_url}"
run! "git push -f origin master:gh-pages"
end
defp get_remote_url(remote) do
push_url = :os.cmd('git remote show -n #{remote} | grep "Push\s.URL"') |> to_string
Regex.named_captures(~r/\: (?<git>.*)/, push_url)["git"]
end
defp run!(command) do
if Mix.shell.cmd(command) != 0 do
raise Mix.Error, message: "command `#{command}` failed"
end
:ok
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment