Sha256: 144b95ccdb7c097dd6c4c5d52f9965329fb2534692c02eaaffd4a01d9dabb6b9

Contents?: true

Size: 1.78 KB

Versions: 2

Compression:

Stored size: 1.78 KB

Contents

# encoding: UTF-8

module Tetra
  # implements a to_spec method
  module SpecGenerator
    # expected attributes:
    #   project (Tetra::Project)
    #   package_name (string)
    #   spec_dir (string)
    #   template_spec_name (string)

    # saves a specfile for this object in correct directories
    # returns the spec path and the conflict count with the previously generated
    # version, if any
    def to_spec
      project.from_directory do
        spec_name = "#{package_name}.spec"
        spec_path = File.join(spec_dir, spec_name)

        new_content = generate(template_spec_name, binding)
        conflict_count = project.merge_new_content(new_content, spec_path,
                                                   "Spec generated", "generate_#{package_name}_spec")

        output_dir = File.join("output", package_name)
        FileUtils.mkdir_p(output_dir)

        spec_link_path = File.join(output_dir, spec_name)
        FileUtils.symlink(File.expand_path(spec_path), spec_link_path, force: true)

        [spec_path, conflict_count]
      end
    end

    # returns the spec template path, exposed for testing
    def template_path
      File.join(File.dirname(__FILE__), "..", "template")
    end

    private

    # generates content from an ERB template and an object binding
    # if destination_path is given, write it to that file, otherwise just
    # return it
    def generate(template_name, object_binding, destination_path = nil)
      template_path = File.join(File.dirname(__FILE__), "..", "template")
      erb = ERB.new File.read(File.join(template_path, template_name)), nil, "<>"
      new_content =  erb.result(object_binding)

      unless destination_path.nil?
        File.open(destination_path, "w") { |io| io.write new_content }
      end

      new_content
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
tetra-0.42.0 lib/tetra/spec_generator.rb
tetra-0.41.0 lib/tetra/spec_generator.rb