Skip to content

Commit

Permalink
Merge pull request #19 from iknow/normalize-command
Browse files Browse the repository at this point in the history
Add `normalize` command
  • Loading branch information
chrisandreae authored Jun 19, 2023
2 parents e0796ae + 09eaf28 commit 2a4bfe4
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
23 changes: 21 additions & 2 deletions bin/phraseapp_updater
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class PhraseAppUpdaterCLI < Thor
method_option :phraseapp_api_key, type: :string, required: true, desc: 'PhraseApp API key.'
method_option :phraseapp_project_name, type: :string, required: true, desc: 'Name for new PhraseApp project.'
method_option :parent_commit, type: :string, required: true, desc: 'git commit hash of initial locales'
method_option :remove_orphans, type: :boolean, default: true, desc: 'Remove keys not in the uploaded default locale'

def setup(locales_path)
validate_readable_path!('locales', locales_path)
Expand All @@ -26,7 +27,7 @@ class PhraseAppUpdaterCLI < Thor
options[:parent_commit],
verbose: options[:verbose])

updater.upload_directory(locales_path)
updater.upload_directory(locales_path, remove_orphans: options[:remove_orphans])

puts project_id
end
Expand Down Expand Up @@ -104,6 +105,7 @@ class PhraseAppUpdaterCLI < Thor
method_option :phraseapp_api_key, type: :string, required: true, desc: 'PhraseApp API key.'
method_option :phraseapp_project_id, type: :string, required: true, desc: 'PhraseApp project ID.'
method_option :parent_commit, type: :string, required: true, desc: 'git commit hash of locales being uploaded'
method_option :remove_orphans, type: :boolean, default: true, desc: 'Remove keys not in the uploaded default locale'

def upload(source_path)
validate_readable_path!('source path', source_path)
Expand All @@ -115,7 +117,7 @@ class PhraseAppUpdaterCLI < Thor
options[:file_format],
verbose: options[:verbose])

updater.upload_directory(source_path)
updater.upload_directory(source_path, remove_orphans: options[:remove_orphans])
updater.update_parent_commit(options[:parent_commit])
end
end
Expand Down Expand Up @@ -162,6 +164,23 @@ class PhraseAppUpdaterCLI < Thor
end
end

desc 'normalize <source_path> <destination_path>',
'Normalize the locale directory at <source_path> into <destination_path>.'

long_desc <<-LONGDESC
Read, normalize, then write out the locales in <source_path> into <destination_path>.
LONGDESC

def normalize(source_path, destination_path)
validate_readable_path!('source_path', source_path)
validate_writable_path!('destination_path', destination_path)

handle_errors do
updater = PhraseAppUpdater.new(nil, nil, options[:file_format], verbose: options[:verbose])
updater.normalize_directory(source_path, destination_path)
end
end

desc 'merge <ancestor_path> <our_path> <their_path>',
'3-way merge locale file directories <ancestor_path>, <our_path>, <their_path> into TO.'

Expand Down
17 changes: 12 additions & 5 deletions lib/phraseapp_updater.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,21 @@ def merge_files(ours, theirs, ancestor, result)
write_locale_file(result, result_file)
end

def upload_directory(path)
def upload_directory(path, remove_orphans: true)
locales = load_locale_directory(path)
upload_locale_files(locales)
upload_locale_files(locales, remove_orphans: remove_orphans)
end

def download_to_directory(path)
locale_files = download_locale_files
write_locale_directory(path, locale_files)
end

def normalize_directory(source, destination)
locales = load_locale_directory(source)
write_locale_directory(destination, locales)
end

def update_parent_commit(parent_commit)
@phraseapp_api.update_parent_commit(parent_commit)
end
Expand Down Expand Up @@ -98,7 +103,7 @@ def merge_locale_files(our_locales, their_locales, ancestor_locales)
end
end

def upload_locale_files(locale_files)
def upload_locale_files(locale_files, remove_orphans: true)
# We assert that the default locale contains all legitimate strings, and so
# we clean up orphaned content on PhraseApp post-upload by removing keys not
# in the default locale.
Expand All @@ -109,8 +114,10 @@ def upload_locale_files(locale_files)
upload_ids = @phraseapp_api.upload_files(locale_files, default_locale: @default_locale)
default_upload_id = upload_ids.fetch(@default_locale)

STDERR.puts "Removing keys not in default locale '#{@default_locale}' upload '#{default_upload_id}'"
@phraseapp_api.remove_keys_not_in_upload(default_upload_id)
if remove_orphans
STDERR.puts "Removing keys not in default locale '#{@default_locale}' upload '#{default_upload_id}'"
@phraseapp_api.remove_keys_not_in_upload(default_upload_id)
end
end

def download_locale_files
Expand Down
2 changes: 1 addition & 1 deletion lib/phraseapp_updater/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

class PhraseAppUpdater
VERSION = '3.0.1'
VERSION = '3.1.0'
end

0 comments on commit 2a4bfe4

Please sign in to comment.