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



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

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

.absolute_secret_store_path(relative_path) ⇒ Object



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

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



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
93
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/configure/configure_apply_action.rb', line 56

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



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

def self.authors
  ['Automattic']
end

.available_optionsObject



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

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



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

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

.detailsObject



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

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



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

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

.is_supported?(platform) ⇒ Boolean

Returns:

  • (Boolean)


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

def self.is_supported?(platform)
  true
end

.prepare_repositoryObject



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
52
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/configure/configure_apply_action.rb', line 26

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



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

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
24
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/configure/configure_apply_action.rb', line 12

def self.run(params = {})
  # Preflight
  UI.user_error!('Decryption key could not be found') if Fastlane::Helper::ConfigureHelper.encryption_key.nil?

  # Checkout the right commit hash etc. before applying the configuration
  prepare_repository do
    # Copy/decrypt the files
    files_to_copy.each do |file_reference|
      apply_file(file_reference, params[:force])
    end
  end
  UI.success 'Applied configuration'
end