Sha256: 81e7a506fbb68797da6b11a828f03ccdd28e34eeec258d1835f1642da6649f42

Contents?: true

Size: 1.44 KB

Versions: 6

Compression:

Stored size: 1.44 KB

Contents

module Groundwork
  module Commands
    def self.compile args
      cmd_opts = Trollop::options(args) do
        banner <<-STR
Usage:
      groundwork compile [options] filename.recipe.rb

Compiles a .recipe.rb, including all referenced files into it.

Options are:
STR
        opt :force, "Overwrite .recipe file if exists", :default=>false
        opt :print, "Instead of storing in a file, print the recipe to stdout", :default=>false
        opt :chdir, "Before running, cd to the given directory", :default=>FileUtils.pwd
      end

      input = args.shift
      raise RuntimeError.new("Cannot open #{input}") if input && !File.exists?(input)

      compiled = ""
      FileUtils.cd(cmd_opts[:chdir]) do
        compiled = if input
                     Groundwork::Recipe.compile_file input
                   else
                     Groundwork::Recipe.compile STDIN.read, FileUtils.pwd
                   end
      end

      if cmd_opts[:print]
        puts compiled
      else
        name = args.shift || if input
                               File.basename(input,".recipe.rb")
                             else
                               File.basename(FileUtils.pwd)
                             end

        if File.exists?(name+".recipe") && !cmd_opts[:force]
          raise RuntimeError.new("File already exists: #{name}.recipe\nUse -f to overwrite")
        end

        File.open(name+".recipe","w"){|f| f.print compiled}
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
groundwork-1.0.2 commands/compile.rb
groundwork-1.0.1 commands/compile.rb
groundwork-1.0.0 commands/compile.rb
groundwork-0.0.6 commands/compile.rb
groundwork-0.0.5 commands/compile.rb
groundwork-0.0.4 commands/compile.rb