github_release.rb 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #!/usr/bin/env ruby
  2. require 'fileutils'
  3. require 'pathname'
  4. require 'tmpdir'
  5. require 'octokit'
  6. BUILD_SH = Pathname(__FILE__).+('../../build.sh').expand_path
  7. VERSION = `sh '#{BUILD_SH}' get-version`.strip
  8. RELEASE = "v#{VERSION}"
  9. BUILD = BUILD_SH.parent + 'build'
  10. OBJC_ZIP = BUILD + "realm-objc-#{VERSION}.zip"
  11. SWIFT_ZIP = BUILD + "realm-swift-#{VERSION}.zip"
  12. CARTHAGE_ZIP = BUILD + 'Carthage.framework.zip'
  13. REPOSITORY = 'realm/realm-cocoa'
  14. def release_notes(version)
  15. changelog = BUILD_SH.parent.+('CHANGELOG.md').readlines
  16. current_version_index = changelog.find_index { |line| line =~ (/^#{Regexp.escape version}/) }
  17. unless current_version_index
  18. raise "Update the changelog for the last version (#{version})"
  19. end
  20. current_version_index += 2
  21. previous_version_lines = changelog[(current_version_index+1)...-1]
  22. previous_version_index = current_version_index + (previous_version_lines.find_index { |line| line =~ /^\d+\.\d+\.\d+(-(alpha|beta|rc)(\.\d+)?)?\s+/ } || changelog.count)
  23. relevant = changelog[current_version_index..previous_version_index]
  24. relevant.join.strip
  25. end
  26. RELEASE_NOTES = release_notes(VERSION)
  27. github = Octokit::Client.new
  28. github.access_token = ENV['GITHUB_ACCESS_TOKEN']
  29. puts 'Creating GitHub release'
  30. prerelease = (VERSION =~ /alpha|beta|rc/) ? true : false
  31. response = github.create_release(REPOSITORY, RELEASE, name: RELEASE, body: RELEASE_NOTES, prerelease: prerelease)
  32. release_url = response[:url]
  33. uploads = [OBJC_ZIP, SWIFT_ZIP, CARTHAGE_ZIP]
  34. uploads.each do |upload|
  35. puts "Uploading #{upload.basename} to GitHub"
  36. github.upload_asset(release_url, upload.to_path, content_type: 'application/zip')
  37. end