Sha256: fd906d7e78e46d45470297cb814ace69af5ee951810d56384743c81dcb9f4dcf

Contents?: true

Size: 1.88 KB

Versions: 1

Compression:

Stored size: 1.88 KB

Contents

module Groundwork
  class Recipe
    def initialize &block
      if block_given?
        instance_eval &block
      end
    end

    def tar= data
      @tar = TarWrapper.new(data)
    end

    # Creates a directory. If a block is passed,
    # the block will be run inside that directory
    def directory *name, &block
      FileUtils.mkdir_p File.join(name)
      if block_given?
        FileUtils.cd File.join(name), &block
      end
    end

    # If opts is a string, it's the contents of the file, verbatim
    # If opts is a hash, it must contain either:
    # * :from, copies the text of the given filename verbatim
    # * :from_erb, uses the text of the filename as an erb template
    # * :erb, uses the given string as an erb template
    def file name, opts = nil
      name = File.join(name)
      File.open(File.join(name),"w") do |file|
        if opts.is_a? String
          file.print opts
        elsif opts.is_a? Hash
          file.print( if opts[:from]
                        read_file opts[:from]
                      elsif opts[:from_erb]
                        ERB.new(read_file(opts[:from_erb])).result(binding)
                      elsif opts[:erb]
                        ERB.new(opts[:erb]).result(binding)
                      end )
        elsif opts.nil?
          # write nothing
        else
          raise ArgumentError.new
        end
      end
    end

    # When you compile a recipe, it runs it to determine which files to bake in.
    # If you have a file that might not be generated by running your script with
    # no options, you can call possible on it to ensure it gets baked into
    # the script. This function only has an effect during compilation, it does
    # nothing when the script is actually run.
    def possible name ; end

    private

    def read_file name
      if @tar
        @tar[name]
      else
        File.read(name)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
groundwork-0.0.4 lib/recipe.rb