require 'yaml' module Soaring class Packager def initialize(options) @options = options end def package Dir.chdir(@options[:project_root]) do validate_project if not @options[:ignore_git_checks] build_output_location = generate_build_output_location(@options[:project_root]) update_project_release_information `mkdir -p #{@options[:project_root]}/build` `zip -r #{build_output_location} * .release_information .ebextensions --exclude=docker/* --exclude=jewels/* --exclude=spec/* --exclude=build/* --exclude=coverage/* --exclude=config/environment.yml` puts "Build packaged at #{build_output_location}" end end private def validate_project if not is_git_repo_up_to_date? puts "Local git repo is dirty and should not be packaged" exit 1 end puts 'Local git repo up to date.' if @options[:verbose] end def generate_build_output_location(project_folder) service_name = File.split(project_folder)[-1] timestamp = Time.now.strftime("%F-%H%M%S") "#{project_folder}/build/build_#{service_name}_#{get_short_commit_hash}_#{timestamp}.zip" end def update_project_release_information release_information = { 'commit_hash' => get_git_commit_hash, 'commit_date' => get_git_commit_date } File.open(".release_information", "w") do |file| file.write release_information.to_yaml end end def get_git_commit_hash git_hash = `git rev-parse HEAD`.chomp git_hash = /\A[0-9a-f]{5,40}\z/.match(git_hash) if git_hash.nil? $stderr.puts 'Failed to package, unable to get commit hash from local git repository' exit 1 end git_hash[0] end def get_git_commit_date `git show -s --format=%cI`.chomp end def is_git_repo_up_to_date? response, exit_status = Executor::execute("git status --porcelain") return true if ('' == response) and (exit_status.success?) false end def get_short_commit_hash response, exit_status = Executor::execute("git rev-parse --short HEAD") if not exit_status.success? puts 'Failed to package, unable to get short commit hash from local git repository' exit 1 end response.chomp end end end