class Locca::OneskySyncAction

Public Class Methods

new(project, onesky, collection_builder, collection_writer, collections_generator, collection_merger) click to toggle source
# File lib/locca/actions/onesky_sync_action.rb, line 34
def initialize(project, onesky, collection_builder, collection_writer, collections_generator, collection_merger)
    @project = project
    @collections_generator = collections_generator
    @collection_merger = collection_merger
    @collection_builder = collection_builder
    @collection_writer = collection_writer
    @onesky = onesky

    @langs = @project.langs()
    @generated_collections = @collections_generator.generate()

    @project.collection_names.each do |collection_name|
        existing_collection = nil

        @generated_collections.each do |generated_collection|
            if generated_collection.name == collection_name
                existing_collection = generated_collection
                break
            end
        end

        if existing_collection == nil 
            collection_path = @project.path_for_collection(collection_name, @project.base_lang)
            collection = @collection_builder.collection_at_path(collection_path)
            @generated_collections.push(collection)
        end
    end

end

Public Instance Methods

fetch() click to toggle source
# File lib/locca/actions/onesky_sync_action.rb, line 64
def fetch()
    # 1
    @generated_collections.each do |generated_collection|
        @langs.each do |lang|
            max_attempts = 3
            for attempt in 1..max_attempts do
                print "[*] #{@project.name}: fetch: #{lang}/#{generated_collection.name}\n"
                begin
                    data = @onesky.fetch_translations(lang, @project.full_collection_name(generated_collection.name))
                rescue Exception => ex
                    if attempt == max_attempts
                        raise ex
                    end
                    puts "[!] #{ex}"
                    sleep 2
                else 
                    break
                end
            end
            
            fetched_collection = @collection_builder.collection_from_datastring(data)

            local_collection_path = @project.path_for_collection(generated_collection.name, lang)
            local_collection = @collection_builder.collection_at_path(local_collection_path)

            # 2
            print "[*] #{@project.name}: merge: onesky -> #{lang}/#{generated_collection.name}\n"
            @collection_merger.merge(fetched_collection, local_collection, CollectionMerger::ACTION_ADD | CollectionMerger::ACTION_UPDATE)
            @collection_writer.write_to_path(local_collection, local_collection_path)
        end
    end
end
sync(prune_missing_strings = false) click to toggle source
# File lib/locca/actions/onesky_sync_action.rb, line 97
def sync(prune_missing_strings = false)
    fetch()

    # 3
    @generated_collections.each do |generated_collection|
        @langs.each do |lang|
            print "[*] #{@project.name}: merge: code -> #{lang}/#{generated_collection.name}\n"

            local_collection_path = @project.path_for_collection(generated_collection.name, lang)
            local_collection = @collection_builder.collection_at_path(local_collection_path)
            @collection_merger.merge(generated_collection, local_collection, (CollectionMerger::ACTION_ADD | CollectionMerger::ACTION_DELETE | CollectionMerger::ACTION_UPDATE_COMMENTS))

            @collection_writer.write_to_path(local_collection, local_collection_path)
        end
    end

    if @project.prevent_sync_without_comments?                    
        lang = @project.base_lang
        @generated_collections.each do |generated_collection|
            print "[*] #{@project.name}: check: #{lang}/#{generated_collection.name}\n"

            local_collection_path = @project.path_for_collection(generated_collection.name, lang)
            local_collection = @collection_builder.collection_at_path(local_collection_path)                

            keys = local_collection.keys_without_comments
            if keys.length > 0
                raise "Keys without comments:\n" + keys.join("\n")
            end
        end
    end

    # 4
    @generated_collections.each do |generated_collection|
        collection_path = @project.path_for_collection(generated_collection.name, @project.base_lang())

        max_attempts = 3
        for attempt in 1..max_attempts do
            print "[*] #{@project.name}: upload: #{@project.base_lang}/#{generated_collection.name}\n"
            begin    
                @onesky.upload_file(collection_path, @project.one_sky_file_format, prune_missing_strings)
            rescue Exception => ex
                if attempt == max_attempts
                    raise ex
                end
                puts "[!] #{ex}"
                sleep 2
            else 
                break
            end
        end
        
    end
end