Sha256: a876c5e9f8fe956161b66abfdd412a9727f32b4684194ddd6e67029970f2705d

Contents?: true

Size: 1.71 KB

Versions: 2

Compression:

Stored size: 1.71 KB

Contents

require 'cymbal'
require 'fileutils'

module Menagerie
  ##
  # Connection object that contains releases
  class Collection
    def initialize(params = {})
      params = Cymbal.symbolize(params || {})
      @paths = default_paths.merge(params[:paths] || {})
      @options = default_options.merge(params[:options] || {})
      @logger = Menagerie.get_logger(@options[:verbosity])
    end

    def releases
      Dir.glob("#{@paths[:releases]}/*").map do |x|
        Release.new path: x, paths: @paths, logger: @logger
      end.sort
    end

    def orphans
      keepers = releases.map(&:artifacts).flatten.map(&:path).uniq
      Dir.glob("#{@paths[:artifacts]}/*/*").reject do |artifact|
        keepers.include? artifact
      end
    end

    def add_release(artifacts)
      rotate
      Release.new(
        artifacts: Cymbal.symbolize(artifacts),
        paths: @paths,
        logger: @logger
      )
      reap if @options[:reap]
      link_latest
    end

    private

    def rotate
      existing = releases.sort.reverse
      keepers = existing.pop(@options[:retention])
      existing.each(&:delete)
      keepers.each(&:rotate)
    end

    def reap
      orphans.each do |orphan|
        @logger.info "Reaping orphan: #{orphan}"
        FileUtils.rm_f orphan
      end
    end

    def link_latest
      FileUtils.rm_f @paths[:latest]
      FileUtils.ln_sf releases.sort.first.path, @paths[:latest]
      @logger.debug "Linked latest release to #{@paths[:latest]}"
    end

    def default_paths
      {
        artifacts: 'artifacts',
        releases: 'releases',
        latest: 'latest'
      }
    end

    def default_options
      {
        retention: 5,
        reap: true,
        verbose: true
      }
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
menagerie-1.1.0 lib/menagerie/collection.rb
menagerie-1.0.0 lib/menagerie/collection.rb