Class: Fastlane::Configuration::FileReference

Inherits:
Object
  • Object
show all
Defined in:
lib/fastlane/plugin/wpmreleasetoolkit/models/file_reference.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ FileReference

Returns a new instance of FileReference.



6
7
8
9
10
# File 'lib/fastlane/plugin/wpmreleasetoolkit/models/file_reference.rb', line 6

def initialize(params = {})
  self.file = params[:file] || ''
  self.destination = params[:destination] || ''
  self.encrypt = params[:encrypt] || false
end

Instance Attribute Details

#destinationObject

Returns the value of attribute destination.



4
5
6
# File 'lib/fastlane/plugin/wpmreleasetoolkit/models/file_reference.rb', line 4

def destination
  @destination
end

#encryptObject

Returns the value of attribute encrypt.



4
5
6
# File 'lib/fastlane/plugin/wpmreleasetoolkit/models/file_reference.rb', line 4

def encrypt
  @encrypt
end

#fileObject

Returns the value of attribute file.



4
5
6
# File 'lib/fastlane/plugin/wpmreleasetoolkit/models/file_reference.rb', line 4

def file
  @file
end

Instance Method Details

#applyObject

“Applies” the instruction described in the instance to the file system. That is, copies the content of the source ‘file` to the `destination` path.

Raises:

  • (StandardError)

    For security reasons, it will raise if ‘destination` is not ignored under Git.



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/fastlane/plugin/wpmreleasetoolkit/models/file_reference.rb', line 46

def apply
  # Only decrypt the file if the destination is ignored in Git
  unless Fastlane::Helper::GitHelper.is_ignored?(path: destination_file_path)
    raise "Attempted to decrypt #{file} to #{destination_file_path} which is not ignored under Git. Please either edit your `.configure` file to use an already-ignored destination, or add that destination to the `.gitignore` manually to fix this."
  end

  # Create the destination directory if it doesn't exist
  FileUtils.mkdir_p(Pathname.new(destination_file_path).dirname)
  # Copy/decrypt the file
  File.write(destination_file_path, source_contents)
end

#destination_contentsObject



20
21
22
23
24
# File 'lib/fastlane/plugin/wpmreleasetoolkit/models/file_reference.rb', line 20

def destination_contents
  return nil unless File.file?(destination_file_path)

  File.read(destination_file_path)
end

#destination_file_pathObject



66
67
68
69
70
# File 'lib/fastlane/plugin/wpmreleasetoolkit/models/file_reference.rb', line 66

def destination_file_path
  return File.expand_path(destination) if destination.start_with?('~')

  File.join(Fastlane::Helper::FilesystemHelper.project_path, destination)
end

#encrypted_file_pathObject



62
63
64
# File 'lib/fastlane/plugin/wpmreleasetoolkit/models/file_reference.rb', line 62

def encrypted_file_path
  Fastlane::Helper::FilesystemHelper.encrypted_file_path(file)
end

#encryption_keyObject



72
73
74
# File 'lib/fastlane/plugin/wpmreleasetoolkit/models/file_reference.rb', line 72

def encryption_key
  Fastlane::Helper::ConfigureHelper.encryption_key
end

#needs_apply?Boolean

Returns:

  • (Boolean)


26
27
28
29
# File 'lib/fastlane/plugin/wpmreleasetoolkit/models/file_reference.rb', line 26

def needs_apply?
  destination = destination_contents
  destination.nil? || source_contents != destination
end

#secrets_repository_file_pathObject



58
59
60
# File 'lib/fastlane/plugin/wpmreleasetoolkit/models/file_reference.rb', line 58

def secrets_repository_file_path
  File.join(Fastlane::Helper::FilesystemHelper.secret_store_dir, file)
end

#source_contentsObject



12
13
14
15
16
17
18
# File 'lib/fastlane/plugin/wpmreleasetoolkit/models/file_reference.rb', line 12

def source_contents
  return File.read(secrets_repository_file_path) unless encrypt || FastlaneCore::Helper.is_ci?
  return nil unless File.file?(encrypted_file_path)

  encrypted = File.read(encrypted_file_path)
  Fastlane::Helper::EncryptionHelper.decrypt(encrypted, encryption_key)
end

#to_hashObject



76
77
78
# File 'lib/fastlane/plugin/wpmreleasetoolkit/models/file_reference.rb', line 76

def to_hash
  { file: file, destination: destination, encrypt: encrypt }
end

#updateObject



31
32
33
34
35
36
37
38
39
40
# File 'lib/fastlane/plugin/wpmreleasetoolkit/models/file_reference.rb', line 31

def update
  return unless encrypt

  # Create the destination directory if it doesn't exist
  FileUtils.mkdir_p(Pathname.new(encrypted_file_path).dirname)
  # Encrypt the file
  file_contents = File.read(secrets_repository_file_path)
  encrypted = Fastlane::Helper::EncryptionHelper.encrypt(file_contents, encryption_key)
  File.write(encrypted_file_path, encrypted)
end