module AppVersion class Release def self.is_pre_release?(tag) default = TRUE #default to true if running locally begin if tag.empty? p 'No tag, so unable to check Github release status.' default else require 'octokit' repo = CI.repo || Git.repo p "Repo slug is #{repo}." if repo.empty? || ENV['GITHUB_TOKEN'].nil? #for local builds p 'GITHUB_TOKEN missing, running locally.' default else client = Octokit::Client.new(:access_token => ENV['GITHUB_TOKEN']) p 'Connecting to Github.' client.user.login client.default_media_type = "application/vnd.github.moondragon+json" release = client.release_for_tag(repo, tag) if !release default else release[:prerelease] end end end rescue LoadError p 'Octokit gem missing, running locally.' default end end end class Version def to_s "#{version} (#{build_no})" end #TODO: refactor so that every permutation can be tested. E.g. github release where is_pre_release=false, what is the commit count? def version(semantic = false) version_suffix = CI.version_suffix if Git.installed? latest_tag = Git.tag if latest_tag == 'HEAD' commit_count = Git.commit_count clean_tag = '0.0.1' elsif latest_tag.empty? #not a git directory commit_count = 0 clean_tag = '0.0.1' else commit_count = Git.commit_count_since_tag(latest_tag) clean_tag = Git.clean_tag end #Only increment version after production release, so that we retain a single version during beta and RCs #TODO: check if this is a tagged build, and then always use the clean_tag. Not need to check pre_release or increment if commit_count == 0 || Release.is_pre_release?(latest_tag) short_version = clean_tag else short_version = clean_tag.increment_version end target = !version_suffix.empty? ? ":#{version_suffix.upcase}" : '' if semantic short_version else short_version + target + suffix end else $stderr.puts 'Git required, not installed.' exit 1 end end def build_no CI.build_no end def suffix branch = CI.branch || Git.branch p "Branch = #{branch}." case branch when /develop/ suffix = '-develop' when /master/ suffix = '-master' when /feature*/ suffix = "-#{branch.split('/').last.downcase}" when /release*/ suffix = "-#{branch.split('/').last.downcase}" when /hotfix*/ suffix = "-#{branch.split('/').last.downcase}" else suffix = '-debug' end #special case for github releases if CI.tagged_build? p 'Tagged build, suppressing branch suffix.' suffix = '' end return suffix end end end