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



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

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`.



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

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.



26
27
28
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/configure_helper.rb', line 26

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.



47
48
49
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/configure_helper.rb', line 47

def self.configure_file_branch_name
  configuration.branch
end

.configure_file_commit_hashObject

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



59
60
61
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/configure_helper.rb', line 59

def self.configure_file_commit_hash
  configuration.pinned_hash
end

.configure_file_commits_behind_repoObject



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

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.



100
101
102
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/configure_helper.rb', line 100

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



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

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`.



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

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



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

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`.



178
179
180
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/configure_helper.rb', line 178

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.



173
174
175
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/configure_helper.rb', line 173

def self.has_files
  !files_to_copy.empty?
end

.mobile_secrets_keys_jsonObject

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



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

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



239
240
241
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/configure_helper.rb', line 239

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.



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

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.



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

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



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

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.



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

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.



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

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.



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

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.



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

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.



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

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)



140
141
142
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/configure_helper.rb', line 140

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)



125
126
127
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/configure_helper.rb', line 125

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.



164
165
166
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/configure_helper.rb', line 164

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.



88
89
90
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/configure_helper.rb', line 88

def self.repository_path
  FilesystemHelper.secret_store_dir
end

.update_configuration(config) ⇒ Object

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



31
32
33
# File 'lib/fastlane/plugin/wpmreleasetoolkit/helper/configure_helper.rb', line 31

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.



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

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.



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

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.



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

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



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

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