Module: EbmSharedLib
- Defined in:
- lib/ebmsharedlib/options.rb,
lib/ebmsharedlib/utilities.rb
Overview
Greg Seitz
Copyright 2013, eBay Inc.
All rights reserved.
http://www.ebay.com
Defined Under Namespace
Constant Summary collapse
- ROOT_PATH =
File.("~/.ebm")
- CONFIG_DIR =
"build_configs"- CONFIG_SUFFIX =
".config"- SETTINGS_FILE =
".ebm-settings.json"- CONFIG_REPOS =
the key is the shortcut used for the config repos
{ "stash" => "https://mobiebay.com/git/scm/ebmconfigs/build_configs.git", "corp" => "[email protected]:eBayMobile/build_configs.git", "mobi" => "[email protected]:eBayMobile/build_configs.git", }
Class Method Summary collapse
-
.base_config_path(repo_url) ⇒ Object
return the base config path minus the git dir for the given url.
-
.config_name_from_dir ⇒ Object
expects us to be in the top level dir that has the same name as the config.
-
.full_config_path(repo_url) ⇒ Object
the full config path.
-
.get_config_from_top_dir(err_msg = nil) ⇒ Object
read the repo_config file for the prepared directory by first fetching the settings from this dir.
- .get_config_repo_url(config_repo) ⇒ Object
-
.get_current_branch(repo, repo_path) ⇒ Object
get the current banch.
- .get_repo_name(git_path) ⇒ Object
-
.prepare_config_repo(config_repo_url) ⇒ Object
takes the repo name (shortcut or full url) and fetches config into appropriate location returns the full repo_url.
- .printer ⇒ Object
-
.read_json_file(file_path, err_msg = nil) ⇒ Object
read and parse a json file.
-
.read_repo_config(repo_url, config_name, err_msg = nil) ⇒ Object
read and return the config info for this repo.
-
.read_settings(err_msg = nil) ⇒ Object
read top level settings within a prepared dir lets us get to the appropriate config file.
-
.repo_url_hash(repo_url) ⇒ Object
compute a unique hash for this repo so we can store it in a subdir of configs.
-
.write_json_file(map, file_path, err_msg = nil) ⇒ Object
write the map as json into the specified file.
-
.write_settings(map, dir, err_msg = nil) ⇒ Object
write the prepared settings, expects us to pass dir to write into.
Class Method Details
.base_config_path(repo_url) ⇒ Object
return the base config path minus the git dir for the given url
89 90 91 92 93 |
# File 'lib/ebmsharedlib/utilities.rb', line 89 def self.base_config_path(repo_url) config_hash = repo_url_hash(repo_url) "#{ROOT_PATH}/repo_configs/#{config_hash}" end |
.config_name_from_dir ⇒ Object
expects us to be in the top level dir that has the same name as the config. Such as /Users/gseitz/Develop/ebay/iphone_3.1 will extract the config_name of iphone_3.1
178 179 180 |
# File 'lib/ebmsharedlib/utilities.rb', line 178 def self.config_name_from_dir config_name = "#{Dir.pwd}".split("/").last end |
.full_config_path(repo_url) ⇒ Object
the full config path
96 97 98 |
# File 'lib/ebmsharedlib/utilities.rb', line 96 def self.full_config_path(repo_url) "#{base_config_path(repo_url)}/#{CONFIG_DIR}" end |
.get_config_from_top_dir(err_msg = nil) ⇒ Object
read the repo_config file for the prepared directory by first fetching the settings from this dir
204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/ebmsharedlib/utilities.rb', line 204 def self.get_config_from_top_dir(err_msg = nil) begin # for now we still operate without settings by using defaults # this should be removed once everyone is on the new tool settings = read_settings(".ebm-settings not found, make sure you are in the top level directory.") repo_url = settings[:config_repo_url] config_name = settings[:config_name] rescue # use defaults since failed to load settings config_name = config_name_from_dir() repo_url = get_config_repo_url(nil) end read_repo_config(repo_url, config_name, "Config not found, make sure you are in the top level directory.") end |
.get_config_repo_url(config_repo) ⇒ Object
71 72 73 74 75 76 77 78 79 |
# File 'lib/ebmsharedlib/utilities.rb', line 71 def self.get_config_repo_url(config_repo) # convert to lowercase for comparison below if non nil repo_alias = config_repo.nil? ? "default" : config_repo.downcase # loop up matching repo found_repo = CONFIG_REPOS[repo_alias] # if no match use the passed in repo config_repo = found_repo.nil? ? config_repo : found_repo end |
.get_current_branch(repo, repo_path) ⇒ Object
get the current banch
191 192 193 194 195 196 197 198 199 200 |
# File 'lib/ebmsharedlib/utilities.rb', line 191 def self.get_current_branch(repo, repo_path) repo_name = EbmSharedLib.get_repo_name(repo[:git_path]) cmd = "git symbolic-ref HEAD" result = EbmSharedLib::CL.do_cmd_output(cmd, repo_path) if $?.exitstatus != 0 raise "Unable to get the current branch for #{repo_name}, you may be on a detached HEAD." end branch = result.rstrip.split("/").last end |
.get_repo_name(git_path) ⇒ Object
182 183 184 |
# File 'lib/ebmsharedlib/utilities.rb', line 182 def self.get_repo_name(git_path) repo_name = git_path.split("/").last.split(".git")[0] end |
.prepare_config_repo(config_repo_url) ⇒ Object
takes the repo name (shortcut or full url) and fetches config into appropriate location returns the full repo_url
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/ebmsharedlib/utilities.rb', line 103 def self.prepare_config_repo(config_repo_url) # get the full url repo_url = EbmSharedLib.get_config_repo_url(config_repo_url) # get the local config dir base_config_path = base_config_path(repo_url) # make the root if missing FileUtils.mkpath(base_config_path) # and the full config path config_path = full_config_path(repo_url) # try to pull, if it fails could be due to repo not cloned cmd = "git pull" if EbmSharedLib::CL.do_cmd_result(cmd, config_path) != 0 # pull failed, try to clone cmd = "git clone #{repo_url} #{CONFIG_DIR}" if EbmSharedLib::CL.do_cmd_result(cmd, base_config_path) != 0 raise "Unable to clone #{CONFIG_DIR} repo into #{base_config_path}" end end repo_url end |
.printer ⇒ Object
186 187 188 |
# File 'lib/ebmsharedlib/utilities.rb', line 186 def self.printer @printer ||= Printer.new end |
.read_json_file(file_path, err_msg = nil) ⇒ Object
read and parse a json file
130 131 132 133 134 135 136 137 138 139 |
# File 'lib/ebmsharedlib/utilities.rb', line 130 def self.read_json_file(file_path, err_msg = nil) begin json = File.open(file_path, 'r') {|f| f.read } info = JSON.parse(json) info.recursively_symbolize_keys! rescue msg = err_msg.nil? ? "Error opening config file JSON: #{config_path}" : err_msg raise msg end end |
.read_repo_config(repo_url, config_name, err_msg = nil) ⇒ Object
read and return the config info for this repo
162 163 164 165 166 |
# File 'lib/ebmsharedlib/utilities.rb', line 162 def self.read_repo_config(repo_url, config_name, err_msg = nil) config_file_path = "#{full_config_path(repo_url)}/configs/#{config_name}#{EbmSharedLib::CONFIG_SUFFIX}" read_json_file(config_file_path, err_msg) end |
.read_settings(err_msg = nil) ⇒ Object
read top level settings within a prepared dir lets us get to the appropriate config file
170 171 172 173 |
# File 'lib/ebmsharedlib/utilities.rb', line 170 def self.read_settings(err_msg = nil) settings_path = "#{Dir.pwd}/#{SETTINGS_FILE}" settings = read_json_file(settings_path, err_msg) end |
.repo_url_hash(repo_url) ⇒ Object
compute a unique hash for this repo so we can store it in a subdir of configs
83 84 85 |
# File 'lib/ebmsharedlib/utilities.rb', line 83 def self.repo_url_hash(repo_url) Digest::SHA1.hexdigest(repo_url) end |
.write_json_file(map, file_path, err_msg = nil) ⇒ Object
write the map as json into the specified file
142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/ebmsharedlib/utilities.rb', line 142 def self.write_json_file(map, file_path, err_msg = nil) begin json = JSON.pretty_generate(map) File.open(file_path, 'w') { |file| file.write(json) } rescue msg = err_msg.nil? ? "Error creating JSON file: #{file_path}" : err_msg raise msg end end |
.write_settings(map, dir, err_msg = nil) ⇒ Object
write the prepared settings, expects us to pass dir to write into
156 157 158 159 |
# File 'lib/ebmsharedlib/utilities.rb', line 156 def self.write_settings(map, dir, err_msg = nil) settings_path = "#{dir}/#{SETTINGS_FILE}" write_json_file(map, settings_path, err_msg) end |