Class: Fastlane::Actions::AnUpdateMetadataSourceAction

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

Documentation collapse

Class Method Summary collapse

Class Method Details

.authorsObject



164
165
166
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/android/an_update_metadata_source_action.rb', line 164

def self.authors
  ['Automattic']
end

.available_optionsObject



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/android/an_update_metadata_source_action.rb', line 128

def self.available_options
  # Define all options your action supports.

  # Below a few examples
  [
    FastlaneCore::ConfigItem.new(key: :po_file_path,
                                 env_name: 'FL_UPDATE_METADATA_SOURCE_PO_FILE_PATH',
                                 description: 'The path of the .po file to update',
                                 type: String,
                                 verify_block: proc do |value|
                                   UI.user_error!("No .po file path for UpdateMetadataSourceAction given, pass using `po_file_path: 'file path'`") unless value && !value.empty?
                                   UI.user_error!("Couldn't find file at path '#{value}'") unless File.exist?(value)
                                 end),
    FastlaneCore::ConfigItem.new(key: :release_version,
                                 env_name: 'FL_UPDATE_METADATA_SOURCE_RELEASE_VERSION',
                                 description: 'The release version of the app (to use to mark the release notes)',
                                 verify_block: proc do |value|
                                   UI.user_error!("No relase version for UpdateMetadataSourceAction given, pass using `release_version: 'version'`") unless value && !value.empty?
                                 end),
    FastlaneCore::ConfigItem.new(key: :source_files,
                                 env_name: 'FL_UPDATE_METADATA_SOURCE_SOURCE_FILES',
                                 description: 'The hash with the path to the source files and the key to use to include their content',
                                 type: Hash,
                                 verify_block: proc do |value|
                                   UI.user_error!("No source file hash for UpdateMetadataSourceAction given, pass using `source_files: 'source file hash'`") unless value && !value.empty?
                                 end),
  ]
end

.check_source_files(source_files) ⇒ Object

Verifies that all the source files are available to this action



27
28
29
30
31
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/android/an_update_metadata_source_action.rb', line 27

def self.check_source_files(source_files)
  source_files.each_value do |file_path|
    UI.user_error!("Couldn't find file at path '#{file_path}'") unless File.exist?(file_path)
  end
end

.create_block_parsers(release_version, block_files) ⇒ Object

Creates the block instances



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/android/an_update_metadata_source_action.rb', line 72

def self.create_block_parsers(release_version, block_files)
  @blocks = []

  # Inits default handler
  @blocks.push Fastlane::Helper::UnknownMetadataBlock.new

  # Init special handlers
  block_files.each do |key, file_path|
    case key
    when :release_note
      @blocks.push Fastlane::Helper::ReleaseNoteMetadataBlock.new(key, file_path, release_version)
    when :release_note_short
      @blocks.push Fastlane::Helper::ReleaseNoteShortMetadataBlock.new(key, file_path, release_version)
    else
      @blocks.push Fastlane::Helper::StandardMetadataBlock.new(key, file_path)
    end
  end

  # Sets the default
  @current_block = @blocks[0]
end

.create_target_file_path(orig_file_path) ⇒ Object

Generates the temp file path



67
68
69
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/android/an_update_metadata_source_action.rb', line 67

def self.create_target_file_path(orig_file_path)
  "#{File.dirname(orig_file_path)}/#{File.basename(orig_file_path, '.*')}.tmp"
end

.create_temp_po(params) ⇒ Object

Creates a temp po file merging new data for known tags and the data already in the original .po fo the others.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/android/an_update_metadata_source_action.rb', line 37

def self.create_temp_po(params)
  orig = params[:po_file_path]
  target = create_target_file_path(orig)

  # Clear if older exists
  FileUtils.rm_f(target)

  # Create the new one
  begin
    File.open(target, 'a') do |fw|
      File.open(orig, 'r').each do |fr|
        write_target_block(fw, fr)
      end
    end
  rescue StandardError
    FileUtils.rm_f(target)
    raise
  end

  target
end

.descriptionObject



120
121
122
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/android/an_update_metadata_source_action.rb', line 120

def self.description
  'Updates a .po file with new data from .txt files'
end

.detailsObject



124
125
126
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/android/an_update_metadata_source_action.rb', line 124

def self.details
  'You can use this action to update the .po file that contains the string to load to GlotPress for localization.'
end

.is_block_id(line) ⇒ Object



108
109
110
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/android/an_update_metadata_source_action.rb', line 108

def self.is_block_id(line)
  line.start_with?('msgctxt')
end

.is_comment(line) ⇒ Object



112
113
114
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/android/an_update_metadata_source_action.rb', line 112

def self.is_comment(line)
  line.start_with?('#')
end

.is_supported?(platform) ⇒ Boolean

Returns:

  • (Boolean)


168
169
170
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/android/an_update_metadata_source_action.rb', line 168

def self.is_supported?(platform)
  [:android].include?(platform)
end

.outputObject



157
158
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/android/an_update_metadata_source_action.rb', line 157

def self.output
end

.return_valueObject



160
161
162
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/android/an_update_metadata_source_action.rb', line 160

def self.return_value
  # If your method provides a return value, you can describe here what it does
end

.run(params) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/android/an_update_metadata_source_action.rb', line 9

def self.run(params)
  # fastlane will take care of reading in the parameter and fetching the environment variable:
  UI.message "Parameter .po file path: #{params[:po_file_path]}"
  UI.message "Release version: #{params[:release_version]}"

  # Init
  create_block_parsers(params[:release_version], params[:source_files])

  # Do
  check_source_files(params[:source_files])
  temp_po_name = create_temp_po(params)
  swap_po(params[:po_file_path], temp_po_name)

  UI.message "File #{params[:po_file_path]} updated!"
end

.swap_po(orig_file_path, temp_file_path) ⇒ Object

Deletes the old po and moves the temp one to the final location



61
62
63
64
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/android/an_update_metadata_source_action.rb', line 61

def self.swap_po(orig_file_path, temp_file_path)
  FileUtils.rm_f(orig_file_path)
  File.rename(temp_file_path, orig_file_path)
end

.write_target_block(fw, line) ⇒ Object

Manages tags depending on the type



95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/android/an_update_metadata_source_action.rb', line 95

def self.write_target_block(fw, line)
  if is_block_id(line)
    key = line.split[1].tr('\"', '')
    @blocks.each do |block|
      @current_block = block if block.is_handler_for(key)
    end
  end

  @current_block = @blocks.first if is_comment(line)

  @current_block.handle_line(fw, line)
end