Class: Fastlane::Helper::ConfigureHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/fastlane/plugin/wpmreleasetoolkit/helper/configure_helper.rb

Class Method Summary collapse

Class Method Details

.add_file(params) ⇒ Object

Adds a file to the ‘.configure` file’s ‘files_to_copy` hash. The hash for this method must contain the `source` and `destination` keys



222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/configure_helper.rb', line 222

def self.add_file(params)
  UI.user_error!('You must pass a `source` to `add_file`') unless params[:source]

  UI.user_error!('You must pass a `destination` to `add_file`') unless params[:destination]

  unless Fastlane::Helper::GitHelper.is_ignored?(path: params[:destination])
    UI.user_error! "Attempted to add a file to a location which is not ignored under Git (#{params[:destination]}). Please either edit your `.configure` file to use an already-ignored destination, or add that destination to the `.gitignore` manually to fix this."
  end

  new_config = configuration
  new_config.add_file_to_copy(params[:source], params[:destination], encrypt: params[:encrypt])
  update_configuration(new_config)
end

.configurationObject

Returns the contents of the project’s ‘.configure` file. If the file doesn’t exist, it’ll return an empty Configuration that can later be saved to ‘.configure`.



15
16
17
18
19
20
21
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/configure_helper.rb', line 15

def self.configuration
  if configuration_path_exists
    Configuration.from_file(FilesystemHelper.configure_file)
  else
    Configuration.new
  end
end

.configuration_path_existsObject

Returns whether or not the ‘.configure` file exists in the project.



24
25
26
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/configure_helper.rb', line 24

def self.configuration_path_exists
  File.file?(FilesystemHelper.configure_file)
end

.configure_file_branch_nameObject

Returns the ‘branch` field of the project’s ‘.configure` file.



45
46
47
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/configure_helper.rb', line 45

def self.configure_file_branch_name
  configuration.branch
end

.configure_file_commit_hashObject

Returns the ‘pinned_hash` field of the project’s ‘.configure` file.



57
58
59
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/configure_helper.rb', line 57

def self.configure_file_commit_hash
  configuration.pinned_hash
end

.configure_file_commits_behind_repoObject



102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/configure_helper.rb', line 102

def self.configure_file_commits_behind_repo
  # Get a sily number of revisions to ensure we don't miss any
  result = `cd #{repository_path} && git --no-pager log -10000 --pretty=format:"%H" && echo`
  hashes = result.each_line.map(&:strip).reverse

  index_of_configure_hash = hashes.find_index(configure_file_commit_hash)
  index_of_repo_commit_hash = hashes.find_index(repo_commit_hash)

  return 0 if index_of_configure_hash >= index_of_repo_commit_hash

  index_of_repo_commit_hash - index_of_configure_hash
end

.configure_file_is_behind_localObject

Returns whether or not the ‘.configure` file has a pinned hash that’s older than the most recent ~/.mobile-secrets` commit hash.



98
99
100
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/configure_helper.rb', line 98

def self.configure_file_is_behind_local
  configure_file_commits_behind_repo.positive?
end

.encryption_keyObject

Gets the key to be used for encrypting/decrypting files for the project Uses the project encryption key or the CONFIGURE_ENCRYPTION_KEY env variable, if present



250
251
252
253
254
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/configure_helper.rb', line 250

def self.encryption_key
  return Base64.decode64(ENV['CONFIGURE_ENCRYPTION_KEY']) if ENV.key?('CONFIGURE_ENCRYPTION_KEY')

  project_encryption_key
end

.file_dependenciesObject

Returns the list of files that this project uses from ‘.configure`.



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/configure_helper.rb', line 181

def self.file_dependencies
  file_dependencies = configuration.file_dependencies
  file_dependencies ||= []

  # Allows support for specifying directories – they'll be expanded recursively
  expanded_file_dependencies = file_dependencies.map do |path|
    abs_path = mobile_secrets_path(path)

    return path unless File.directory?(abs_path)

    Dir.glob("#{abs_path}**/*").map do |sub_path|
      sub_path.gsub("#{repository_path}/", '')
    end
  end

  files_to_copy.map(&:file) + expanded_file_dependencies
end

.files_changed_between(commit_hash_1, commit_hash_2) ⇒ Object

Get a list of files changed in the secrets repo between to commits



116
117
118
119
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/configure_helper.rb', line 116

def self.files_changed_between(commit_hash_1, commit_hash_2)
  result = `cd #{repository_path} && git diff --name-only #{commit_hash_1}...#{commit_hash_2}`
  result.each_line.map(&:strip)
end

.files_to_copyObject

Returns the list of files to copy from ‘.configure`.



176
177
178
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/configure_helper.rb', line 176

def self.files_to_copy
  configuration.files_to_copy
end

.has_filesObject

Returns whether or not the ‘files_to_copy` hash in `.configure` is empty.



171
172
173
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/configure_helper.rb', line 171

def self.has_files
  !files_to_copy.empty?
end

.mobile_secrets_keys_jsonObject

Contents of ~/.mobile-secrets/keys.json as a hash



242
243
244
245
246
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/configure_helper.rb', line 242

def self.mobile_secrets_keys_json
  return {} unless File.file?(Fastlane::Helper::FilesystemHelper.secret_store_keys_path)

  JSON.parse(File.read(Fastlane::Helper::FilesystemHelper.secret_store_keys_path))
end

.mobile_secrets_path(path) ⇒ Object

Turns a relative mobile secrets path into an absolute path



237
238
239
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/configure_helper.rb', line 237

def self.mobile_secrets_path(path)
  "#{repository_path}/#{path}"
end

.new_files_in(files) ⇒ Object

If we specify a directory in ‘file_dependencies` instead of listing each file individually, there may be new files that we don’t know about. This method finds those.



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/configure_helper.rb', line 201

def self.new_files_in(files)
  file_dependencies = configuration.file_dependencies
  file_dependencies ||= []

  directory_dependencies = file_dependencies.select do |path|
    File.directory?(mobile_secrets_path(path))
  end

  new_files = []

  files.each do |path|
    directory_dependencies.each do |directory_name|
      new_files << path if path.start_with?(directory_name)
    end
  end

  new_files
end

.parse_distance(match) ⇒ Object

A helper function to extract the distance from the provided string. (ie – this function will recieve “behind 2” or “ahead 6” and return 2 or 6, respectively.



153
154
155
156
157
158
159
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/configure_helper.rb', line 153

def self.parse_distance(match)
  distance = match.to_s.scan(/\d+/).first

  return 0 if distance.nil?

  distance.to_i
end

.project_encryption_keyObject

Gets the project encryption key defined in ~/.mobile-secrets/keys.json



257
258
259
260
261
262
263
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/configure_helper.rb', line 257

def self.project_encryption_key
  keys_json = mobile_secrets_keys_json
  return nil unless keys_json.key?(configuration.project_name)

  base64_key = keys_json[configuration.project_name]
  Base64.decode64(base64_key)
end

.repo_branch_nameObject

Returns the currently checked out branch for the ‘~/.mobile-secrets` repository. NB: Returns nil if the repo is in a detached HEAD state.



74
75
76
77
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/configure_helper.rb', line 74

def self.repo_branch_name
  result = `cd #{repository_path} && git rev-parse --abbrev-ref HEAD`.strip
  result == 'HEAD' ? nil : result
end

.repo_commit_hashObject

Returns the most recent commit hash in the ‘~/.mobile-secrets` repository.



80
81
82
83
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/configure_helper.rb', line 80

def self.repo_commit_hash
  hash = `cd #{repository_path} && git rev-parse --verify HEAD`
  hash.strip
end

.repo_commits_ahead_of_remoteObject

Determine how far ahead of the remote repo the ~/.mobile-secrets` repository is.



143
144
145
146
147
148
149
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/configure_helper.rb', line 143

def self.repo_commits_ahead_of_remote
  matches = repo_status.match(/ahead \d+/)

  return 0 if matches.nil?

  parse_distance(matches[0])
end

.repo_commits_behind_remoteObject

Determine how far behind the remote repo the ~/.mobile-secrets` repository is.



128
129
130
131
132
133
134
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/configure_helper.rb', line 128

def self.repo_commits_behind_remote
  matches = repo_status.match(/behind \d+/)

  return 0 if matches.nil?

  parse_distance(matches[0])
end

.repo_has_changesObject

Returns whether the ~/.mobile-secrets` repository is clean or dirty.



91
92
93
94
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/configure_helper.rb', line 91

def self.repo_has_changes
  result = `cd #{repository_path} && git status --porcelain`
  result.empty?
end

.repo_is_ahead_of_remoteObject

Determine whether ~/.mobile-secrets` repository is ahead of its remote counterpart. (ie – the local repo has changes that the remote repo doesn’t)



138
139
140
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/configure_helper.rb', line 138

def self.repo_is_ahead_of_remote
  repo_commits_ahead_of_remote.positive?
end

.repo_is_behind_remoteObject

Determine whether ~/.mobile-secrets` repository is behind its remote counterpart. (ie – the remote repo has changes that the local repo doesn’t)



123
124
125
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/configure_helper.rb', line 123

def self.repo_is_behind_remote
  repo_commits_behind_remote.positive?
end

.repo_statusObject

A helper function to determine how far apart the local and remote repos are.



162
163
164
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/configure_helper.rb', line 162

def self.repo_status
  `cd #{repository_path} && git fetch && git status --porcelain -b`
end

.repository_pathObject

Returns an absolute path to the ‘~/.mobile-secrets` repository.



86
87
88
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/configure_helper.rb', line 86

def self.repository_path
  FilesystemHelper.secret_store_dir
end

.update_configuration(config) ⇒ Object

A global helper to save the current configuration to ‘.configure`.



29
30
31
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/configure_helper.rb', line 29

def self.update_configuration(config)
  config.save_to_file(FilesystemHelper.configure_file)
end

.update_configure_file_branch_name(new_branch_name) ⇒ Object

Writes the provided new branch name to the ‘branch` field of the project’s ‘.configure` file.



50
51
52
53
54
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/configure_helper.rb', line 50

def self.update_configure_file_branch_name(new_branch_name)
  new_configuration = configuration
  new_configuration.branch = new_branch_name
  update_configuration(new_configuration)
end

.update_configure_file_commit_hash(new_hash) ⇒ Object

Writes the provided new commit hash to the ‘pinned_hash` field of the project’s ‘.configure` file.



62
63
64
65
66
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/configure_helper.rb', line 62

def self.update_configure_file_commit_hash(new_hash)
  new_configuration = configuration
  new_configuration.pinned_hash = new_hash
  update_configuration(new_configuration)
end

.update_configure_file_from_repositoryObject

Reads current branch name and commit hash ‘~/.mobile-secrets` and writes them to the project’s ‘.configure` file.



39
40
41
42
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/configure_helper.rb', line 39

def self.update_configure_file_from_repository
  update_configure_file_branch_name(repo_branch_name)
  update_configure_file_commit_hash(repo_commit_hash)
end

.update_project_encryption_keyObject

Updates the project encryption key defined in ~/.mobile-secrets/keys.json The updated file is commited and push to the repo



267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/configure_helper.rb', line 267

def self.update_project_encryption_key
  # Update keys.json with the new key
  keys_json = mobile_secrets_keys_json
  keys_json[configuration.project_name] = Base64.encode64(Fastlane::Helper::EncryptionHelper.generate_key)
  File.write(Fastlane::Helper::FilesystemHelper.secret_store_keys_path, JSON.pretty_generate(keys_json))

  # Commit and push the result to the repo
  `cd #{repository_path} && git add keys.json && git commit -m "Update keys.json for #{configuration.project_name}" && git push origin #{repo_branch_name}`

  # Check command success
  UI.user_error!("Failed to update encryption key for #{configuration.project_name}") unless $CHILD_STATUS.success?
end