Last active
July 10, 2017 15:48
-
-
Save scrogson/df719a6b4cc8d985a353a942c1342970 to your computer and use it in GitHub Desktop.
A Mix Task to Publish docs to GitHub Pages
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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