Class: SchemaEvolutionManager::Library
- Inherits:
-
Object
- Object
- SchemaEvolutionManager::Library
- Defined in:
- lib/schema-evolution-manager/library.rb
Constant Summary collapse
- TMPFILE_DIR =
"/tmp"
- TMPFILE_PREFIX =
"schema-evolution-manager-#{Process.pid}.tmp"
- @@tmpfile_count =
0
- @@verbose =
false
- @@base_dir =
Library.normalize_path(File.join(File.dirname(__FILE__), ".."))
Class Method Summary collapse
- .assert_dir_exists(dir) ⇒ Object
- .assert_valid_tag(tag) ⇒ Object
-
.base_dir ⇒ Object
Returns the relative path to the base directory (root of this git repo).
- .delete_file_if_exists(path) ⇒ Object
-
.ensure_dir!(dir) ⇒ Object
Creates the dir if it does not already exist.
- .format_time(timestamp = Time.now) ⇒ Object
- .git_assert_tag_exists(tag) ⇒ Object
-
.git_changes(opts = {}) ⇒ Object
Returns a formatted string of git commits made since the specified tag.
-
.git_create_tag(tag) ⇒ Object
Ex: Library.git_create_tag(“0.0.1”).
- .git_has_remote? ⇒ Boolean
- .is_verbose? ⇒ Boolean
-
.latest_tag ⇒ Object
Fetches the latest tag from the git repo.
- .normalize_path(path) ⇒ Object
- .set_base_dir(value) ⇒ Object
- .set_verbose(value) ⇒ Object
-
.system_or_error(command) ⇒ Object
Runs the specified command, raising an error if there is a problem (based on status code of the process executed).
- .tag_exists?(tag) ⇒ Boolean
-
.with_temp_file(opts = {}) ⇒ Object
Generates a temp file name, yield the full path to the file.
-
.write_to_temp_file(string) ⇒ Object
Writes the string to a temp file, yielding the path.
Class Method Details
.assert_dir_exists(dir) ⇒ Object
21 22 23 24 25 26 27 |
# 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) ⇒ Object
45 46 47 |
# 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 ⇒ Object
Returns the relative path to the base directory (root of this git repo)
112 113 114 |
# File 'lib/schema-evolution-manager/library.rb', line 112 def Library.base_dir @@base_dir end |
.delete_file_if_exists(path) ⇒ Object
93 94 95 96 97 |
# 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) ⇒ Object
Creates the dir if it does not already exist
12 13 14 15 16 17 18 19 |
# 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) ⇒ Object
29 30 31 |
# File 'lib/schema-evolution-manager/library.rb', line 29 def Library.format_time(=Time.now) .strftime('%Y-%m-%d %H:%M:%S %Z') end |
.git_assert_tag_exists(tag) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 |
# 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 = {}) ⇒ Object
Returns a formatted string of git commits made since the specified tag.
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# 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) ⇒ Object
Ex: Library.git_create_tag(“0.0.1”)
65 66 67 68 69 70 71 72 73 74 75 |
# 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? ⇒ Boolean
49 50 51 |
# File 'lib/schema-evolution-manager/library.rb', line 49 def Library.git_has_remote? system("git config --get remote.origin.url") end |
.is_verbose? ⇒ Boolean
145 146 147 |
# File 'lib/schema-evolution-manager/library.rb', line 145 def Library.is_verbose? @@verbose end |
.latest_tag ⇒ Object
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)
56 57 58 |
# 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) ⇒ Object
141 142 143 |
# 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) ⇒ Object
116 117 118 119 |
# 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) ⇒ Object
149 150 151 |
# File 'lib/schema-evolution-manager/library.rb', line 149 def Library.set_verbose(value) @@verbose = value ? true : false end |
.system_or_error(command) ⇒ Object
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.
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# 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) ⇒ Boolean
60 61 62 |
# 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 = {}) ⇒ Object
Generates a temp file name, yield the full path to the file. Cleans up automatically on exit.
79 80 81 82 83 84 85 86 87 88 89 90 91 |
# 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) ⇒ Object
Writes the string to a temp file, yielding the path. Cleans up on exit.
101 102 103 104 105 106 107 108 |
# 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 |