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

Classes: CL, Options

Constant Summary collapse

ROOT_PATH =
File.expand_path("~/.ebm")
CONFIG_DIR =
"build_configs"
CONFIG_SUFFIX =
".config"
SETTINGS_FILE =
".ebm-settings.json"
REPO_COMMAND_DETAILS =
"Remote git repository for initial configs file download [mobi,corp,stash,<git url>]"
CONFIG_REPOS =

the key is the shortcut used for the config repos

{
    "stash" => "https://mobiebay.com/git/scm/ebmconfigs/build_configs.git",
    "mobi" => "[email protected]:eBayMobile/build_configs.git",
}

Class Method Summary collapse

Class Method Details

.base_config_path(repo_url) ⇒ Object

return the base config path minus the git dir for the given url



105
106
107
108
109
# File 'lib/ebmsharedlib/utilities.rb', line 105

def self.base_config_path(repo_url)
  config_hash = repo_url_hash(repo_url)

  "#{ROOT_PATH}/repo_configs/#{config_hash}"
end

.config_name_from_dirObject

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



194
195
196
# File 'lib/ebmsharedlib/utilities.rb', line 194

def self.config_name_from_dir
  config_name = "#{Dir.pwd}".split("/").last
end

.full_config_path(repo_url) ⇒ Object

the full config path



112
113
114
# File 'lib/ebmsharedlib/utilities.rb', line 112

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



220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/ebmsharedlib/utilities.rb', line 220

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



87
88
89
90
91
92
93
94
95
# File 'lib/ebmsharedlib/utilities.rb', line 87

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



207
208
209
210
211
212
213
214
215
216
# File 'lib/ebmsharedlib/utilities.rb', line 207

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



198
199
200
# File 'lib/ebmsharedlib/utilities.rb', line 198

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



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/ebmsharedlib/utilities.rb', line 119

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

.printerObject



202
203
204
# File 'lib/ebmsharedlib/utilities.rb', line 202

def self.printer
  @printer ||= Printer.new
end

.read_json_file(file_path, err_msg = nil) ⇒ Object

read and parse a json file



146
147
148
149
150
151
152
153
154
155
# File 'lib/ebmsharedlib/utilities.rb', line 146

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: #{file_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



178
179
180
181
182
# File 'lib/ebmsharedlib/utilities.rb', line 178

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



186
187
188
189
# File 'lib/ebmsharedlib/utilities.rb', line 186

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



99
100
101
# File 'lib/ebmsharedlib/utilities.rb', line 99

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



158
159
160
161
162
163
164
165
166
167
168
# File 'lib/ebmsharedlib/utilities.rb', line 158

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



172
173
174
175
# File 'lib/ebmsharedlib/utilities.rb', line 172

def self.write_settings(map, dir, err_msg = nil)
  settings_path = "#{dir}/#{SETTINGS_FILE}"
  write_json_file(map, settings_path, err_msg)
end