Sha256: 748dd70d68461b39b51840fea556370bd7fcc84033a49ef9cbd40665b8c5d4b4

Contents?: true

Size: 1.79 KB

Versions: 4

Compression:

Stored size: 1.79 KB

Contents

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_commit_hash
        `mkdir -p #{@options[:project_root]}/build`
        `zip -r #{build_output_location} * .ebextensions --exclude=build/* --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_commit_hash
      response, exit_status = Executor::execute("git rev-parse HEAD > commit-hash")
      if not exit_status.success?
        puts 'Failed to package, unable to get commit hash from local git repository'
        exit 1
      end
    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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
soaring-0.1.17 lib/soaring/packager.rb
soaring-0.1.16 lib/soaring/packager.rb
soaring-0.1.15 lib/soaring/packager.rb
soaring-0.1.14 lib/soaring/packager.rb