Class: Locca::OneskySyncAction

Inherits:
Object
  • Object
show all
Defined in:
lib/locca/actions/onesky_sync_action.rb

Instance Method Summary collapse

Constructor Details

#initialize(project, onesky, collection_builder, collection_writer, collections_generator, collection_merger) ⇒ OneskySyncAction

Returns a new instance of OneskySyncAction.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# 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

Instance Method Details

#fetchObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/locca/actions/onesky_sync_action.rb', line 64

def fetch()
    # 1
    @generated_collections.each do |generated_collection|
        @langs.each do |lang|
            print "[*] #{@project.name}: fetch: #{lang}/#{generated_collection.name}\n"
            data = @onesky.fetch_translations(lang, @project.full_collection_name(generated_collection.name))
            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) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/locca/actions/onesky_sync_action.rb', line 83

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|
        print "[*] #{@project.name}: upload: #{@project.base_lang}/#{generated_collection.name}\n"
        collection_path = @project.path_for_collection(generated_collection.name, @project.base_lang())
        @onesky.upload_file(collection_path, @project.one_sky_file_format, prune_missing_strings)
    end
end