Sha256: ab1604c9e78aba0482ac6c7425daac4a1b4ba0545748df8ffd948619a888d89e

Contents?: true

Size: 1.82 KB

Versions: 44

Compression:

Stored size: 1.82 KB

Contents

require "foreman"

# Reads and writes Procfiles
#
# A valid Procfile entry is captured by this regex:
#
#   /^([A-Za-z0-9_]+):\s*(.+)$/
#
# All other lines are ignored.
#
class Foreman::Procfile

  # Initialize a Procfile
  #
  # @param [String] filename (nil)  An optional filename to read from
  #
  def initialize(filename=nil)
    @entries = []
    load(filename) if filename
  end

  # Yield each +Procfile+ entry in order
  #
  def entries(&blk)
    @entries.each do |(name, command)|
      yield name, command
    end
  end

  # Retrieve a +Procfile+ command by name
  #
  # @param [String] name  The name of the Procfile entry to retrieve
  #
  def [](name)
    @entries.detect { |n,c| name == n }.last
  end

  # Create a +Procfile+ entry
  #
  # @param [String] name     The name of the +Procfile+ entry to create
  # @param [String] command  The command of the +Procfile+ entry to create
  #
  def []=(name, command)
    delete name
    @entries << [name, command]
  end

  # Remove a +Procfile+ entry
  #
  # @param [String] name  The name of the +Procfile+ entry to remove
  #
  def delete(name)
    @entries.reject! { |n,c| name == n }
  end

  # Load a Procfile from a file
  #
  # @param [String] filename  The filename of the +Procfile+ to load
  #
  def load(filename)
    @entries.replace parse(filename)
  end

  # Save a Procfile to a file
  #
  # @param [String] filename  Save the +Procfile+ to this file
  #
  def save(filename)
    File.open(filename, 'w') do |file|
      file.puts self.to_s
    end
  end

  # Get the +Procfile+ as a +String+
  #
  def to_s
    @entries.map do |name, command|
      [ name, command ].join(": ")
    end.join("\n")
  end

private

  def parse(filename)
    File.read(filename).split("\n").map do |line|
      if line =~ /^([A-Za-z0-9_]+):\s*(.+)$/
        [$1, $2]
      end
    end.compact
  end

end

Version data entries

44 entries across 44 versions & 2 rubygems

Version Path
foreman-capistrano-0.53.4 lib/foreman/procfile.rb
foreman-capistrano-0.53.3 lib/foreman/procfile.rb
foreman-capistrano-0.53.2 lib/foreman/procfile.rb
foreman-capistrano-0.53.1 lib/foreman/procfile.rb
foreman-capistrano-0.53.0 lib/foreman/procfile.rb
foreman-capistrano-0.52.6 lib/foreman/procfile.rb
foreman-capistrano-0.52.5 lib/foreman/procfile.rb
foreman-capistrano-0.52.4 lib/foreman/procfile.rb
foreman-capistrano-0.52.3 lib/foreman/procfile.rb
foreman-capistrano-0.52.2 lib/foreman/procfile.rb
foreman-capistrano-0.52.1 lib/foreman/procfile.rb
foreman-capistrano-0.52.0 lib/foreman/procfile.rb
foreman-capistrano-0.51.5 lib/foreman/procfile.rb
foreman-0.60.2 lib/foreman/procfile.rb
foreman-capistrano-0.51.4 lib/foreman/procfile.rb
foreman-capistrano-0.51.3 lib/foreman/procfile.rb
foreman-0.60.0 lib/foreman/procfile.rb
foreman-0.59.0 lib/foreman/procfile.rb
foreman-0.58.0 lib/foreman/procfile.rb
foreman-0.57.0-java lib/foreman/procfile.rb