class ReleaseProject UPDATE_TYPES = %w[major minor patch custom].freeze TEMP_FOLDER_NAME = ('_RENUO_RELEASE_TEMP_' + rand(100_000_000).to_s).freeze MOVE_TO_TEMP = "mkdir -p #{TEMP_FOLDER_NAME} && cd #{TEMP_FOLDER_NAME}".freeze def run(args) project_name, @update_type, @version = args validate_update_type(@update_type, @version) @version = validate_version(@version) validate_project_name(project_name) compare_develop_master(project_name) release(project_name, @update_type, @version) ensure cleanup end def validate_update_type(update_type, version) if version && update_type != UPDATE_TYPES[3] say('>> Do not specify a version for a non-custom release. Given version will be ignored.') end if !version && update_type == UPDATE_TYPES[3] @version = ask('>> Please enter your desired version for your custom release (X.Y.Z):') validate_update_type(@update_type, @version) end return if UPDATE_TYPES.include? update_type @update_type = ask('>> Please enter your desired update type (major minor patch custom):') validate_update_type(@update_type, version) end def validate_version(version) return nil if version.nil? version = version.match(/\d+\.\d+\.\d+/) return version if version abort('>> Invalid Version Number. Use format X.Y.Z for your version.') end def validate_project_name(project_name) abort('>> No project name given.') unless project_name unless system("#{MOVE_TO_TEMP} && hub clone #{project_name} && " \ "cd #{folder_name(project_name)} && git checkout master && git flow init -d") cleanup abort('>> Project not found on Github.') end end def compare_develop_master(project_name) say('Please double-check what is going to be deployed.') system "open https://github.com/#{project_name}/compare/master...develop" abort unless agree('Are you fine with those changes?') end def release(project_name, update_type, version) version ||= update_version_number(current_version(project_name), update_type) release_start project_name, update_type, version say ">> Project successfully released with version #{version}." end def release_start(project_name, update_type, version) abort('>> Unable to pull from Github. Cancelling release.') unless checkout? project_name if update_type != UPDATE_TYPES[3] && !version_bump?(project_name, current_version(project_name)) abort('>> Please bump the version in files manually. Cancelling release.') end ask_for_final_confirmation(project_name, update_type, version) return if release_flow(project_name, version) abort('>> Unable to finish release and push to master. Cancelling release.') end def checkout?(project_name) system("#{move_and_cd(project_name)} && " \ 'git checkout master && git pull origin master && ' \ 'git checkout develop && git pull origin develop') end def version_bump?(project_name, current_version) grep = "grep -R -F --color --exclude-dir=.git --exclude=Gemfile* '#{current_version}' ." if system("#{move_and_cd(project_name)} && #{grep}") agree(">> Current version number (#{current_version}) found in above files. " \ ' Do you wish to continue without changing it?') else true end end def release_flow(project_name, version) system("#{move_and_cd(project_name)} &&" \ "git flow release start #{version} && " \ "GIT_MERGE_AUTOEDIT=no git flow release finish -m #{version} #{version} && " \ 'git checkout master && git push origin master && ' \ 'git checkout develop && git push origin develop && ' \ 'git push --tags') end def current_version(project_name) describe = "#{move_and_cd(project_name)} && git describe --tags" tag = '`git rev-list --tags --max-count=1`' if `#{move_and_cd(project_name)} && git tag` == '' '0.0.0' else `#{describe} #{tag}`.strip end end def update_version_number(version, update_type) version = version.split('.').map(&:to_i) type_index = UPDATE_TYPES.index(update_type) version[type_index + 1..-1] = [0] * (2 - type_index) version[type_index] += 1 version.join('.') end def move_and_cd(project_name) "#{MOVE_TO_TEMP} && cd #{folder_name(project_name)}" end def folder_name(project_name) project_name.split('/').last end def cleanup system("rm -rf #{TEMP_FOLDER_NAME}") end def ask_for_final_confirmation(project_name, update_type, version) unless agree(">> Are you sure you wish to deploy '#{project_name}' " \ "as a #{update_type} release (#{current_version(project_name)} => #{version})?") abort('>> Cancelling Release.') end return unless Time.now.friday? && Time.now.hour >= 16 unless agree('>> Are you sure you want to deploy on late Friday afternoon? ' \ 'Did you think about your family...waiting for you at home?') abort('>> Very good. Go home now.') end end end