class SchemaEvolutionManager::Library

Constants

TMPFILE_DIR
TMPFILE_PREFIX

Public Class Methods

assert_dir_exists(dir) click to toggle source
# File lib/schema-evolution-manager/library.rb, line 21
def Library.assert_dir_exists(dir)
  Preconditions.assert_class(dir, String)

  if !File.directory?(dir)
    raise "Dir[#{dir}] does not exist"
  end
end
assert_valid_tag(tag) click to toggle source
# File lib/schema-evolution-manager/library.rb, line 45
def Library.assert_valid_tag(tag)
  Preconditions.check_state(Version.is_valid?(tag), "Invalid tag[%s]. Format must be x.x.x (e.g. 1.1.2)" % tag)
end
base_dir() click to toggle source

Returns the relative path to the base directory (root of this git repo)

# File lib/schema-evolution-manager/library.rb, line 112
def Library.base_dir
  @@base_dir
end
delete_file_if_exists(path) click to toggle source
# File lib/schema-evolution-manager/library.rb, line 93
def Library.delete_file_if_exists(path)
  if File.exist?(path)
    FileUtils.rm_r(path)
  end
end
ensure_dir!(dir) click to toggle source

Creates the dir if it does not already exist

# File lib/schema-evolution-manager/library.rb, line 12
def Library.ensure_dir!(dir)
  Preconditions.assert_class(dir, String)

  if !File.directory?(dir)
    Library.system_or_error("mkdir -p #{dir}")
  end
  Library.assert_dir_exists(dir)
end
format_time(timestamp=Time.now) click to toggle source
# File lib/schema-evolution-manager/library.rb, line 29
def Library.format_time(timestamp=Time.now)
  timestamp.strftime('%Y-%m-%d %H:%M:%S %Z')
end
git_assert_tag_exists(tag) click to toggle source
# File lib/schema-evolution-manager/library.rb, line 33
def Library.git_assert_tag_exists(tag)
  command = "git tag -l"
  results = Library.system_or_error(command)
  if results.nil?
    raise "No git tags found"
  end

  if !Library.tag_exists?(tag)
    raise "Tag[#{tag}] not found. Check #{command}"
  end
end
git_changes(opts={}) click to toggle source

Returns a formatted string of git commits made since the specified tag.

# File lib/schema-evolution-manager/library.rb, line 154
def Library.git_changes(opts={})
  tag = opts.delete(:tag)
  number_changes = opts.delete(:number_changes) || 10
  Preconditions.check_state(number_changes > 0)
  Preconditions.assert_empty_opts(opts)

  git_log_command = "git log --pretty=format:\"%h %ad | %s%d [%an]\" --date=short -#{number_changes}"
  git_log = Library.system_or_error(git_log_command)
  out = ""
  out << "Created: %s\n" % Library.format_time
  if tag
    out << "Git Tag: %s\n" % tag
  end
  out << "\n"
  out << "%s:\n" % git_log_command
  out << "  " << git_log.split("\n").join("\n  ") << "\n"
  out
end
git_create_tag(tag) click to toggle source

Ex: Library.git_create_tag(“0.0.1”)

# File lib/schema-evolution-manager/library.rb, line 65
def Library.git_create_tag(tag)
  Library.assert_valid_tag(tag)
  has_remote = Library.git_has_remote?
  if has_remote
    Library.system_or_error("git fetch --tags origin")
  end
  Library.system_or_error("git tag -a -m #{tag} #{tag}")
  if has_remote
    Library.system_or_error("git push --tags origin")
  end
end
git_has_remote?() click to toggle source
# File lib/schema-evolution-manager/library.rb, line 49
def Library.git_has_remote?
  system("git config --get remote.origin.url")
end
is_verbose?() click to toggle source
# File lib/schema-evolution-manager/library.rb, line 145
def Library.is_verbose?
  @@verbose
end
latest_tag() click to toggle source

Fetches the latest tag from the git repo. Returns nil if there are no tags, otherwise returns an instance of Version. Only searches for tags matching x.x.x (e.g. 1.0.2)

# File lib/schema-evolution-manager/library.rb, line 56
def Library.latest_tag
  `git tag -l`.strip.split.select { |tag| Version.is_valid?(tag) }.map { |tag| Version.parse(tag) }.sort.last
end
normalize_path(path) click to toggle source
# File lib/schema-evolution-manager/library.rb, line 141
def Library.normalize_path(path)
  Pathname.new(path).cleanpath.to_s
end
set_base_dir(value) click to toggle source
# File lib/schema-evolution-manager/library.rb, line 116
def Library.set_base_dir(value)
  Preconditions.check_state(File.directory?(value), "Dir[%s] not found" % value)
  @@base_dir = Library.normalize_path(value)
end
set_verbose(value) click to toggle source
# File lib/schema-evolution-manager/library.rb, line 149
def Library.set_verbose(value)
  @@verbose = value ? true : false
end
system_or_error(command) click to toggle source

Runs the specified command, raising an error if there is a problem (based on status code of the process executed). Otherwise returns all the output from the script invoked.

# File lib/schema-evolution-manager/library.rb, line 124
def Library.system_or_error(command)
  if Library.is_verbose?
    puts command
  end

  begin
    result = `#{command}`.strip
    status = $?
    if status.to_i > 0
      raise "Non zero exit code[%s] running command[%s]" % [status, command]
    end
  rescue Exception => e
    raise "Error running command[%s]: %s" % [command, e.to_s]
  end
  result
end
tag_exists?(tag) click to toggle source
# File lib/schema-evolution-manager/library.rb, line 60
def Library.tag_exists?(tag)
  `git tag -l`.strip.include?(tag)
end
with_temp_file(opts={}) { |path| ... } click to toggle source

Generates a temp file name, yield the full path to the file. Cleans up automatically on exit.

# File lib/schema-evolution-manager/library.rb, line 79
def Library.with_temp_file(opts={})
  prefix = opts.delete(:prefix)
  Preconditions.assert_empty_opts(opts)

  if prefix.to_s == ""
    prefix = TMPFILE_PREFIX
  end
  path = File.join(TMPFILE_DIR, "%s.%s" % [prefix, @@tmpfile_count])
  @@tmpfile_count += 1
  yield path
ensure
  Library.delete_file_if_exists(path)
end
write_to_temp_file(string) { |path| ... } click to toggle source

Writes the string to a temp file, yielding the path. Cleans up on exit.

# File lib/schema-evolution-manager/library.rb, line 101
def Library.write_to_temp_file(string)
  Library.with_temp_file do |path|
    File.open(path, "w") do |out|
      out << string
    end
    yield path
  end
end