Sha256: 8917cde0722408fea0aa13a0b41458138a37ca7fab0ef20a06a760399a057168

Contents?: true

Size: 1.84 KB

Versions: 25

Compression:

Stored size: 1.84 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).gsub("\r\n","\n").split("\n").map do |line|
      if line =~ /^([A-Za-z0-9_-]+):\s*(.+)$/
        [$1, $2]
      end
    end.compact
  end

end

Version data entries

25 entries across 25 versions & 3 rubygems

Version Path
foreman-systemd-0.78.0 lib/foreman/procfile.rb
foreman-0.78.0 lib/foreman/procfile.rb
foreman-0.77.0 lib/foreman/procfile.rb
foreman-0.76.0 lib/foreman/procfile.rb
foreman-0.75.0 lib/foreman/procfile.rb
foreman-0.74.0 lib/foreman/procfile.rb
foreman-0.73.0 lib/foreman/procfile.rb
foreman-0.71.0 lib/foreman/procfile.rb
foreman-0.70.0 lib/foreman/procfile.rb
foreman-0.69.0-mingw32 lib/foreman/procfile.rb
foreman-0.69.0 lib/foreman/procfile.rb
foreman-0.67.0-mingw32 lib/foreman/procfile.rb
foreman-0.67.0-java lib/foreman/procfile.rb
foreman-0.67.0 lib/foreman/procfile.rb
foreman-0.66.0-mingw32 lib/foreman/procfile.rb
foreman-0.66.0-java lib/foreman/procfile.rb
foreman-0.66.0 lib/foreman/procfile.rb
foreman-0.65.0 lib/foreman/procfile.rb
foreman-0.64.0 lib/foreman/procfile.rb
mango-0.8.0 vendor/bundler/ruby/2.1.0/gems/foreman-0.63.0/lib/foreman/procfile.rb