#!/usr/bin/env ruby

require 'erb'
require 'json'
require 'ostruct'

class Generator
  attr_reader :name, :cases
  def initialize(name, cases)
    @name = name
    @cases = cases
  end

  def metadata_dir
    File.expand_path(File.join('..', '..', '..', 'x-common'), __FILE__)
  end

  def data
    File.read(File.join(metadata_dir, "#{name}.json"))
  end

  def path_to(file)
    File.expand_path(File.join('..', '..', 'exercises', name, file), __FILE__)
  end

  def version
    @version ||= File.read(path_to('.version')).strip.to_i
  end

  def sha1
    `cd #{metadata_dir} && git log -1 --oneline`
  end

  def test_cases
    cases.call(data)
  end

  def generate
    File.open(path_to("#{name.gsub(/[ -]/, '_')}_test.rb"), 'w') do |f|
      f.write ERB.new(File.read(path_to('example.tt'))).result binding
    end
    File.open(path_to('.version'), 'w') do |f|
      f.write version + 1
    end
    contents = File.read(path_to('example.rb'))
    File.open(path_to('example.rb'), 'w') do |f|
      f.write contents.gsub("VERSION = #{version}", "VERSION = #{version + 1}")
    end
  end
end

#!/usr/bin/env ruby

require 'erb'
require 'json'
require 'ostruct'

class Generator
  attr_reader :name, :cases
  def initialize(name, cases)
    @name = name
    @cases = cases
  end

  def metadata_dir
    File.expand_path(File.join('..', '..', '..', 'x-common'), __FILE__)
  end

  def data
    File.read(File.join(metadata_dir, "#{name}.json"))
  end

  def path_to(file)
    File.expand_path(File.join('..', '..', 'exercises', name, file), __FILE__)
  end

  def version
    @version ||= File.read(path_to('.version')).strip.to_i
  end

  def sha1
    `cd #{metadata_dir} && git log -1 --pretty=format:"%h"`
  end

  def test_cases
    cases.call(data)
  end

  def generate
    File.open(path_to("#{name.gsub(/[ -]/, '_')}_test.rb"), 'w') do |f|
      f.write ERB.new(File.read(path_to('example.tt'))).result binding
    end
    File.open(path_to('.version'), 'w') do |f|
      f.write version + 1
    end
    contents = File.read(path_to('example.rb'))
    File.open(path_to('example.rb'), 'w') do |f|
      f.write contents.gsub("VERSION = #{version}", "VERSION = #{version + 1}")
    end
  end
end

Remove commit message from git version indicator.
Remove commit message from git version indicator.

Change git log format from oneline ("%h %s"), to abbreviated hash only ("%h")

The code for generated tests often includes a comment giving the git
version of the test data from which the test was generated, such as:
`# deb225e Implement canonical dataset for scrabble-score problem (#255)`

The same version string is used for all tests generated at that time and
will often have no relevance to the test you are looking at and only
adds confusion.

"Why is it telling me about scrabble-score? This is the test file for
hamming!"

By including only the hash, anyone interested enough in the commit can
easily look up the exact commit message and everybody else can just
ignore what appears to be a random hexidecimal string.
