module Rubinjam::Tasks

Constants

API_BASE

Public Instance Methods

sh(*command) click to toggle source
# File lib/rubinjam/tasks.rb, line 38
def sh(*command)
  command = command.shelljoin
  result = `#{command}`
  raise "Command failed:\n#{command}\n#{result}" unless $?.success?
  result
end
upload_binary(tag, github_token) click to toggle source
# File lib/rubinjam/tasks.rb, line 10
def upload_binary(tag, github_token)
  # https://github.com/foo/bar or git@github.com:foo/bar.git -> foo/bar
  repo = sh("git", "remote", "get-url", "origin").strip
  repo.sub!(/\.git$/, "")
  repo = repo.split(/[:\/]/).last(2).join("/")

  auth = ["-H", "Authorization: token #{github_token}"]

  id = find_or_create_release(auth, repo, tag)
  puts "Release #{id}"

  # upload binary
  begin
    name = Rubinjam.write(Dir.pwd)
    puts "Uploading #{name} release asset"
    sh(
      "curl",
      "-X", "POST",
      "--data-binary", "@#{name}",
      "-H", "Content-Type: application/octet-stream",
      *auth,
      "https://uploads.github.com/repos/#{repo}/releases/#{id}/assets?name=#{name}"
    )
  ensure
    sh "rm", "-f", name.to_s
  end
end

Private Instance Methods

find_or_create_release(auth, repo, tag) click to toggle source
# File lib/rubinjam/tasks.rb, line 47
def find_or_create_release(auth, repo, tag)
  reply = sh("curl", *auth, "--data", {tag_name: tag}.to_json, "https://api.github.com/repos/#{repo}/releases")
  unless id = JSON.parse(reply)["id"]
    reply = sh("curl", *auth, "https://api.github.com/repos/#{repo}/releases/tags/#{tag}")
    id = JSON.parse(reply).fetch("id")
  end
  id
end