Sha256: db0a3c28e74cef6d42a14e2fb6cb07461cd1540ccd1b2027c0e0114fbe2c0599

Contents?: true

Size: 1.42 KB

Versions: 3

Compression:

Stored size: 1.42 KB

Contents

require 'cymbal'
require 'fileutils'

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

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

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

    def create(artifacts)
      rotate
      Release.new artifacts: Cymbal.symbolize(artifacts), paths: @paths
      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 { |orphan| FileUtils.rm_f orphan }
    end

    def link_latest
      FileUtils.rm_f @paths[:latest]
      FileUtils.ln_sf releases.sort.first.path, @paths[:latest]
    end

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

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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
menagerie-0.1.3 lib/menagerie/collection.rb
menagerie-0.1.2 lib/menagerie/collection.rb
menagerie-0.1.1 lib/menagerie/collection.rb