Skip to content

Commit

Permalink
Merge pull request #19 from oudirichi/fix-extractor
Browse files Browse the repository at this point in the history
fix extraction of assets
  • Loading branch information
oudirichi authored Oct 31, 2019
2 parents 06bc9a5 + e43e201 commit 0fbc46b
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 89 deletions.
184 changes: 100 additions & 84 deletions lib/cbx_loco/extractor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,112 +5,128 @@
require 'colorize'
require 'get_pomo'
require 'fileutils'
require 'cbx_loco/utils'

class CbxLoco::Extractor
def run
puts "\n" + "Extract i18n assets".colorize(:green).bold
def run
puts "\n" + "Extract i18n assets".colorize(:green).bold

print "Removing old files... "
CbxLoco.configuration.i18n_files.each do |i18n_file|
fmt = CbxLoco.configuration.file_formats[i18n_file[:format]]
puts "Removing old files... "
CbxLoco.configuration.i18n_files.each do |i18n_file|
fmt = CbxLoco.configuration.file_formats[i18n_file[:format]]

next unless fmt[:delete]
next unless fmt[:delete]

path = fmt[:path]
src_ext = fmt[:src_ext]
i18n_file_path = CbxLoco.file_path path, [i18n_file[:name], src_ext].join(".")
File.unlink i18n_file_path if File.file?(i18n_file_path)
end
puts "Done!".colorize(:green)
path = fmt[:path]
src_ext = fmt[:src_ext]

CbxLoco.configuration.emit :before_extract

@assets = {}
CbxLoco.configuration.i18n_files.each do |i18n_file|
fmt = CbxLoco.configuration.file_formats[i18n_file[:format]]
path = fmt[:path]
src_ext = fmt[:src_ext]

case i18n_file[:format]
when :gettext
file_path = CbxLoco.file_path path, [i18n_file[:name], src_ext].join(".")
translations = GetPomo::PoFile.parse File.read(file_path)
msgids = translations.reject { |t| t.msgid.blank? }.map(&:msgid)
when :yaml
language = CbxLoco.configuration.languages.first
file_path = CbxLoco.file_path path, [i18n_file[:name], language, src_ext].join(".")
translations = YAML.load_file file_path
msgids = CbxLoco.flatten_hash(translations[language])
end
if path.blank? || src_ext.blank?
CbxLoco::Utils.print_error "path or src_ext is not provided for name: '#{i18n_file[:name]}', id: '#{i18n_file[:id]}', format: '#{i18n_file[:format]}'"
exit(1)
end

msgids.each do |msgid|
if msgid.is_a? Array
# we have a plural (get text only)
singular = msgid[0]
plural = msgid[1]
i18n_file_path = CbxLoco.file_path path, [i18n_file[:name], src_ext].join(".")

# add the singular
@assets[singular] = { tags: [] } if @assets[singular].nil?
@assets[singular][:tags] << CbxLoco.asset_tag(i18n_file[:id], i18n_file[:name])
tag = CbxLoco.asset_tag i18n_file[:id], i18n_file[:name]
puts "Removing old assets #{tag}"
File.unlink i18n_file_path if File.file?(i18n_file_path)
end
puts "Done!".colorize(:green).bold

CbxLoco.configuration.emit :before_extract

@assets = {}
CbxLoco.configuration.i18n_files.each do |i18n_file|
fmt = CbxLoco.configuration.file_formats[i18n_file[:format]]

next unless fmt[:extractable]

path = fmt[:path]
src_ext = fmt[:src_ext]

case i18n_file[:format]
when :gettext
file_path = CbxLoco.file_path path, [i18n_file[:name], src_ext].join(".")
translations = GetPomo::PoFile.parse File.read(file_path)
msgids = translations.reject { |t| t.msgid.blank? }.map(&:msgid)
when :yaml
language = CbxLoco.configuration.languages.first
file_path = CbxLoco.file_path path, [i18n_file[:name], language, src_ext].join(".")
translations = YAML.load_file file_path
msgids = CbxLoco.flatten_hash(translations[language])
else
CbxLoco::Utils.print_error "#{i18n_file[:format]} IS NOT SUPPORTED FOR EXTRACTION"
next
end

# add the plural
@assets[plural] = { tags: [] } if @assets[plural].nil?
@assets[plural][:singular_id] = singular
@assets[plural][:tags] << CbxLoco.asset_tag(i18n_file[:id], i18n_file[:name])
else
@assets[msgid] = { tags: [] } if @assets[msgid].nil?
@assets[msgid][:id] = msgid if i18n_file[:format] == :yaml
@assets[msgid][:tags] << CbxLoco.asset_tag(i18n_file[:id], i18n_file[:name])
end
msgids.each do |msgid|
if msgid.is_a? Array
# we have a plural (get text only)
singular = msgid[0]
plural = msgid[1]

# add the singular
@assets[singular] = { tags: [] } if @assets[singular].nil?
@assets[singular][:tags] << CbxLoco.asset_tag(i18n_file[:id], i18n_file[:name])

# add the plural
@assets[plural] = { tags: [] } if @assets[plural].nil?
@assets[plural][:singular_id] = singular
@assets[plural][:tags] << CbxLoco.asset_tag(i18n_file[:id], i18n_file[:name])
else
@assets[msgid] = { tags: [] } if @assets[msgid].nil?
@assets[msgid][:id] = msgid if i18n_file[:format] == :yaml
@assets[msgid][:tags] << CbxLoco.asset_tag(i18n_file[:id], i18n_file[:name])
end
end
end

puts "\n" + "Upload i18n assets to Loco".colorize(:green).bold
begin
print "Grabbing the list of existing assets... "
res = CbxLoco::Adapter.get "assets.json"

existing_assets = CbxLoco::ExtractAdapter.new(res).grab_existing_assets
res = nil
puts "\n" + "Upload i18n assets to Loco".colorize(:green).bold
begin
print "Grabbing the list of existing assets... "
res = CbxLoco::Adapter.get "assets.json"

puts "Done!".colorize(:green)
existing_assets = CbxLoco::ExtractAdapter.new(res).grab_existing_assets
res = nil

@assets.each do |asset_name, asset|
trimmed_asset_name = asset_name.length > 100 ? asset_name[0..96] + "..." : asset_name
existing_asset = existing_assets[trimmed_asset_name] || existing_assets[asset_name]
puts "Done!".colorize(:green)

if existing_asset.nil?
print_asset_name = asset_name.length > 50 ? asset_name[0..46] + "[...]" : asset_name
print "Uploading asset: \"#{print_asset_name}\"... "
@assets.each do |asset_name, asset|
trimmed_asset_name = asset_name.length > 100 ? asset_name[0..96] + "..." : asset_name
existing_asset = existing_assets[trimmed_asset_name] || existing_assets[asset_name]

asset_hash = { name: asset_name, type: "text" }
if existing_asset.nil?
print_asset_name = asset_name.length > 50 ? asset_name[0..46] + "[...]" : asset_name
print "Uploading asset: \"#{print_asset_name}\"... "

if !asset[:singular_id].blank?
singular_id = existing_assets[asset[:singular_id]][:id]
res = CbxLoco::Adapter.post "assets/#{singular_id}/plurals.json", asset_hash
else
asset_hash[:id] = asset_name if asset[:id]
res = CbxLoco::Adapter.post "assets.json", asset_hash
end
asset_hash = { name: asset_name, type: "text" }

existing_asset = { id: res["id"], tags: res["tags"] }
existing_assets[trimmed_asset_name] = existing_asset
puts "Done!".colorize(:green)
if !asset[:singular_id].blank?
singular_id = existing_assets[asset[:singular_id]][:id]
res = CbxLoco::Adapter.post "assets/#{singular_id}/plurals.json", asset_hash
else
asset_hash[:id] = asset_name if asset[:id]
res = CbxLoco::Adapter.post "assets.json", asset_hash
end

new_tags = asset[:tags] - existing_asset[:tags]
new_tags.each do |tag|
print_asset_id = existing_asset[:id].length > 30 ? existing_asset[:id][0..26] + "[...]" : existing_asset[:id]
print "Uploading tag \"#{tag}\" for asset: \"#{print_asset_id}\"... "
CbxLoco::Adapter.post "assets/#{URI.escape(existing_asset[:id])}/tags.json", name: tag
puts "Done!".colorize(:green)
end
existing_asset = { id: res["id"], tags: res["tags"] }
existing_assets[trimmed_asset_name] = existing_asset
puts "Done!".colorize(:green)
end

puts "\n" + "All done!".colorize(:green).bold
rescue => e
res = JSON.parse e.response
print_error "Upload to Loco failed: #{e.message}: #{res["error"]}"
new_tags = asset[:tags] - existing_asset[:tags]
new_tags.each do |tag|
print_asset_id = existing_asset[:id].length > 30 ? existing_asset[:id][0..26] + "[...]" : existing_asset[:id]
print "Uploading tag \"#{tag}\" for asset: \"#{print_asset_id}\"... "
CbxLoco::Adapter.post "assets/#{URI.escape(existing_asset[:id])}/tags.json", name: tag
puts "Done!".colorize(:green)
end
end

puts "\n" + "All done!".colorize(:green).bold
rescue => e
res = JSON.parse e.response
CbxLoco::Utils.print_error "Upload to Loco failed: #{e.message}: #{res["error"]}"
end
end
end
4 changes: 3 additions & 1 deletion lib/cbx_loco/importer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ def run

begin
CbxLoco.configuration.i18n_files.each do |i18n_file|
fmt = CbxLoco.configuration.file_formats[i18n_file[:format]]
next unless fmt[:importable]

extension_class = "CbxLoco::Extension::#{i18n_file[:format].to_s.camelize}".constantize
extension_instance = extension_class.new

fmt = CbxLoco.configuration.file_formats[i18n_file[:format]]
tag = CbxLoco.asset_tag i18n_file[:id], i18n_file[:name]
api_params = { filter: tag, order: :id, format: fmt[:format] }

Expand Down
2 changes: 2 additions & 0 deletions spec/cbx_loco/extractor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,15 @@ def delete_files
before(:all) do
@fake_file_formats = {
gettext: {
extractable: true,
api_ext: "po",
delete: true,
dst_ext: "po",
src_ext: "pot",
path: "locale"
},
yaml: {
extractable: true,
api_ext: "yml",
delete: false,
dst_ext: "yml",
Expand Down
28 changes: 24 additions & 4 deletions spec/cbx_loco/importer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,28 @@
before(:all) do
@fake_file_formats = {
gettext: {
importable: true,
api_ext: "po",
delete: true,
dst_ext: "po",
src_ext: "pot",
path: "locale"
},
yaml: {
importable: true,
api_ext: "yml",
delete: false,
dst_ext: "yml",
src_ext: "yml",
path: "locale"
},
json: {
importable: false,
api_ext: "json",
delete: false,
dst_ext: "json",
src_ext: "json",
path: "locale"
}
}

Expand All @@ -29,6 +39,11 @@
format: :gettext,
id: "test_client",
name: "test_front_end"
},
{
format: :json,
id: "test_client_not_importable",
name: "test_front_end"
}
]

Expand All @@ -38,7 +53,7 @@
@str_json = "{\"test\": \"#{@str_response}\"}"
end

before(:each) do
before do
@after_import_call = false

CbxLoco.configure do |c|
Expand All @@ -57,7 +72,7 @@
end

describe "#run" do
before(:each) do
before do
allow(CbxLoco::Adapter).to receive(:get).and_return(@str_response)
@fake_i18n_files.each do |i18n_file|
extension_class = "CbxLoco::Extension::#{i18n_file[:format].to_s.camelize}".constantize
Expand All @@ -72,8 +87,13 @@
@fake_i18n_files.each do |i18n_file|
extension_class = "CbxLoco::Extension::#{i18n_file[:format].to_s.camelize}".constantize

expect(extension_class).to have_received(:new)
expect(extension_class).to have_received(:download)
if @fake_file_formats[i18n_file[:format]][:importable]
expect(extension_class).to have_received(:new)
expect(extension_class).to have_received(:download)
else
expect(extension_class).to_not have_received(:new)
expect(extension_class).to_not have_received(:download)
end
end
end

Expand Down

0 comments on commit 0fbc46b

Please sign in to comment.