Class: Fastlane::Actions::ConfigureApplyAction

Inherits:
Action
  • Object
show all
Defined in:
lib/fastlane/plugin/wpmreleasetoolkit/actions/configure/configure_apply_action.rb

Class Method Summary collapse

Class Method Details

.absolute_project_path(relative_path) ⇒ Object



102
103
104
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/configure/configure_apply_action.rb', line 102

def self.absolute_project_path(relative_path)
  Fastlane::Helper::FilesystemHelper.absolute_project_path(relative_path)
end

.absolute_secret_store_path(relative_path) ⇒ Object



106
107
108
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/configure/configure_apply_action.rb', line 106

def self.absolute_secret_store_path(relative_path)
  Fastlane::Helper::FilesystemHelper.absolute_secret_store_path(relative_path)
end

.apply_file(file_reference, force) ⇒ Object

Check with the user whether we should overwrite the file, if it exists



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/configure/configure_apply_action.rb', line 55

def self.apply_file(file_reference, force)
  # If the file doesn't exist or force is true, we don't need to confirm
  if !File.file?(file_reference.destination) || force
    file_reference.apply
    return # Don't continue if we were able to copy the file without conflict
  end

  unless file_reference.needs_apply?
    return # Nothing to do if the files are identical
  end

  puts Diffy::Diff.new(file_reference.destination_contents, file_reference.source_contents) if UI.confirm("#{file_reference.destination} has changes that need to be merged. Would you like to see a diff?")

  if UI.confirm("Would you like to make a backup of #{file_reference.destination}?")
    extension = File.extname(file_reference.destination)
    base = File.basename(Pathname.new(file_reference.destination), extension)

    date_string = Time.now.strftime('%m-%d-%Y--%H-%M-%S')

    backup_path = base
                  .concat('-') # Handy-dandy separator
                  .concat(date_string) # date string to allow multiple backups
                  .concat(extension) # and the original file extension
                  .concat('.bak') # add the .bak file extension - easier to .gitignore

    # Create the destination directory if it doesn't exist
    FileUtils.mkdir_p(Pathname.new(file_reference.destination).dirname)
    FileUtils.cp(file_reference.destination, backup_path)
  end

  if UI.confirm("Would you like to overwrite #{file_reference.destination}?")
    file_reference.apply
  else
    UI.message "Skipping #{file_reference.destination}"
  end
rescue StandardError => e
  UI.user_error!(e)
end

.authorsObject



114
115
116
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/configure/configure_apply_action.rb', line 114

def self.authors
  ['Automattic']
end

.available_optionsObject



122
123
124
125
126
127
128
129
130
131
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/configure/configure_apply_action.rb', line 122

def self.available_options
  [
    FastlaneCore::ConfigItem.new(key: :force,
                                 env_name: 'FORCE_OVERWRITE',
                                 description: 'Overwrite copied files without confirmation',
                                 optional: true,
                                 default_value: false,
                                 type: Boolean),
  ]
end

.descriptionObject



110
111
112
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/configure/configure_apply_action.rb', line 110

def self.description
  'Copy files specified in `.config` from the secrets repository to the project. Specify force:true to avoid confirmation'
end

.detailsObject



118
119
120
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/configure/configure_apply_action.rb', line 118

def self.details
  'Copy files specified in `.config` from the secrets repository to the project. Specify force:true to avoid confirmation'
end

.files_to_copyObject



98
99
100
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/configure/configure_apply_action.rb', line 98

def self.files_to_copy
  Fastlane::Helper::ConfigureHelper.files_to_copy
end

.is_supported?(platform) ⇒ Boolean

Returns:

  • (Boolean)


133
134
135
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/configure/configure_apply_action.rb', line 133

def self.is_supported?(platform)
  true
end

.prepare_repositoryObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/configure/configure_apply_action.rb', line 25

def self.prepare_repository
  secrets_respository_exists = File.exist?(repository_path)

  # If the secrets repo doesn't exist, just run the block
  unless secrets_respository_exists
    # Run the provided block, and return
    yield
    return
  end

  ### Make sure secrets repo is at the proper hash as specified in .configure.
  repo_hash = Fastlane::Helper::ConfigureHelper.repo_commit_hash
  file_hash = Fastlane::Helper::ConfigureHelper.configure_file_commit_hash

  ### Get the ref to restore the repo to
  original_repo_ref = Fastlane::Helper::ConfigureHelper.repo_branch_name
  original_repo_ref = repo_hash if original_repo_ref.nil?

  other_action.sh(command: "cd #{repository_path} && git fetch && git checkout #{file_hash}", log: false) unless repo_hash == file_hash

  # Run the provided block
  yield

  ### Restore secrets repo to original branch.  If it was originally in a
  ### detached HEAD state, we need to use the hash since there's no branch name.
  other_action.sh(command: "cd #{repository_path} && git checkout #{original_repo_ref}", log: false)
end

.repository_pathObject



94
95
96
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/configure/configure_apply_action.rb', line 94

def self.repository_path
  Fastlane::Helper::FilesystemHelper.secret_store_dir
end

.run(params = {}) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/configure/configure_apply_action.rb', line 12

def self.run(params = {})
  # Checkout the right commit hash etc. before applying the configuration
  prepare_repository do
    UI.user_error!('Decryption key could not be found') if Fastlane::Helper::ConfigureHelper.encryption_key.nil?

    # Copy/decrypt the files
    files_to_copy.each do |file_reference|
      apply_file(file_reference, params[:force])
    end
  end
  UI.success 'Applied configuration'
end