Class: Fastlane::Actions::GpUpdateMetadataSourceAction

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

Documentation collapse

Class Method Summary collapse

Class Method Details

.authorsObject



163
164
165
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/common/gp_update_metadata_source.rb', line 163

def self.authors
  ['Automattic']
end

.available_optionsObject



127
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
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/common/gp_update_metadata_source.rb', line 127

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



26
27
28
29
30
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/common/gp_update_metadata_source.rb', line 26

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



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/common/gp_update_metadata_source.rb', line 71

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 :whats_new
      @blocks.push Fastlane::Helper::WhatsNewMetadataBlock.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



66
67
68
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/common/gp_update_metadata_source.rb', line 66

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.



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

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



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

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

.detailsObject



123
124
125
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/common/gp_update_metadata_source.rb', line 123

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



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

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

.is_comment(line) ⇒ Object



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

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

.is_supported?(platform) ⇒ Boolean

Returns:

  • (Boolean)


167
168
169
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/common/gp_update_metadata_source.rb', line 167

def self.is_supported?(platform)
  true
end

.outputObject



156
157
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/common/gp_update_metadata_source.rb', line 156

def self.output
end

.return_valueObject



159
160
161
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/common/gp_update_metadata_source.rb', line 159

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

.run(params) ⇒ Object



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

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



60
61
62
63
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/common/gp_update_metadata_source.rb', line 60

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



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

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