Class: Fastlane::Actions::LokaliseAction

Inherits:
Action
  • Object
show all
Defined in:
lib/fastlane/plugin/lokalise/actions/lokalise_action.rb

Documentation collapse

Class Method Summary collapse

Class Method Details

.authorsObject



171
172
173
# File 'lib/fastlane/plugin/lokalise/actions/lokalise_action.rb', line 171

def self.authors
  "Fedya-L"
end

.available_optionsObject



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/fastlane/plugin/lokalise/actions/lokalise_action.rb', line 108

def self.available_options
  [
    FastlaneCore::ConfigItem.new(key: :api_token,
                                 env_name: "LOKALISE_API_TOKEN",
                                 description: "API Token for Lokalise",
                                 verify_block: proc do |value|
                                    UI.user_error! "No API token for Lokalise given, pass using `api_token: 'token'`" unless (value and not value.empty?)
                                 end),
    FastlaneCore::ConfigItem.new(key: :project_identifier,
                                 env_name: "LOKALISE_PROJECT_ID",
                                 description: "Lokalise Project ID",
                                 verify_block: proc do |value|
                                    UI.user_error! "No Project Identifier for Lokalise given, pass using `project_identifier: 'identifier'`" unless (value and not value.empty?)
                                 end),
    FastlaneCore::ConfigItem.new(key: :destination,
                                 description: "Localization destination",
                                 verify_block: proc do |value|
                                    UI.user_error! "Things are pretty bad" unless (value and not value.empty?)
                                    UI.user_error! "Directory you passed is in your imagination" unless File.directory?(value)
                                 end),
    FastlaneCore::ConfigItem.new(key: :clean_destination,
                                 description: "Clean destination folder",
                                 optional: true,
                                 is_string: false,
                                 default_value: false,
                                 verify_block: proc do |value|
                                    UI.user_error! "Clean destination should be true or false" unless [true, false].include? value
                                 end),
    FastlaneCore::ConfigItem.new(key: :languages,
                                 description: "Languages to download",
                                 optional: true,
                                 is_string: false,
                                 verify_block: proc do |value|
                                    UI.user_error! "Language codes should be passed as array" unless value.kind_of? Array
                                 end),
      FastlaneCore::ConfigItem.new(key: :include_comments,
                                 description: "Include comments in exported files",
                                 optional: true,
                                 is_string: false,
                                 default_value: false,
                                 verify_block: proc do |value|
                                   UI.user_error! "Include comments should be true or false" unless [true, false].include? value
                                 end),
      FastlaneCore::ConfigItem.new(key: :use_original,
                                 description: "Use original filenames/formats (bundle_structure parameter is ignored then)",
                                 optional: true,
                                 is_string: false,
                                 default_value: false,
                                 verify_block: proc do |value|
                                   UI.user_error! "Use original should be true of false." unless [true, false].include?(value)
                                  end),
      FastlaneCore::ConfigItem.new(key: :tags,
                                  description: "Include only the keys tagged with a given set of tags",
                                  optional: true,
                                  is_string: false,
                                  verify_block: proc do |value|
                                    UI.user_error! "Tags should be passed as array" unless value.kind_of? Array
                                  end),

  ]
end

.descriptionObject



103
104
105
# File 'lib/fastlane/plugin/lokalise/actions/lokalise_action.rb', line 103

def self.description
  "Download Lokalise localization"
end

.is_supported?(platform) ⇒ Boolean

Returns:

  • (Boolean)


176
177
178
# File 'lib/fastlane/plugin/lokalise/actions/lokalise_action.rb', line 176

def self.is_supported?(platform)
  [:ios, :android, :mac].include? platform 
end

.run(params) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/fastlane/plugin/lokalise/actions/lokalise_action.rb', line 8

def self.run(params)

  token = params[:api_token]
  project_identifier = params[:project_identifier]
  destination = params[:destination]
  clean_destination = params[:clean_destination]
  include_comments = params[:include_comments] ? 1 : 0
  use_original = params[:use_original] ? 1 : 0

  request_data = {
    api_token: token,
    id: project_identifier,
    type: "strings",
    use_original: use_original,
    bundle_filename: "Localization.zip",
    bundle_structure: "%LANG_ISO%.lproj/Localizable.%FORMAT%",
    ota_plugin_bundle: 0,
    export_empty: "base",
    include_comments: include_comments
  }

  languages = params[:languages]
  if languages.kind_of? Array then
    request_data["langs"] = languages.to_json
  end

  tags = params[:tags]
  if tags.kind_of? Array then
    request_data["include_tags"] = tags.to_json
  end

  uri = URI("https://api.lokalise.co/api/project/export")
  request = Net::HTTP::Post.new(uri)
  request.set_form_data(request_data)

  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  response = http.request(request)


  jsonResponse = JSON.parse(response.body)
  UI.error "Bad response 🉐\n#{response.body}" unless jsonResponse.kind_of? Hash
  if jsonResponse["response"]["status"] == "success" && jsonResponse["bundle"]["file"].kind_of?(String)  then
    UI.message "Downloading localizations archive 📦"
    FileUtils.mkdir_p("lokalisetmp")
    fileURL = jsonResponse["bundle"]["full_file"]
    uri = URI(fileURL)
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = true
    zipRequest = Net::HTTP::Get.new(uri)
    response = http.request(zipRequest)
    if response.content_type == "application/zip" or response.content_type == "application/octet-stream" then
      FileUtils.mkdir_p("lokalisetmp")
      open("lokalisetmp/a.zip", "wb") { |file| 
        file.write(response.body)
      }
      unzip_file("lokalisetmp/a.zip", destination, clean_destination)
      FileUtils.remove_dir("lokalisetmp")
      UI.success "Localizations extracted to #{destination} 📗 📕 📘"
    else
      UI.error "Response did not include ZIP"
    end
  elsif jsonResponse["response"]["status"] == "error"
    code = jsonResponse["response"]["code"]
    message = jsonResponse["response"]["message"]
    UI.error "Response error code #{code} (#{message}) 📟"
  else
    UI.error "Bad response 🉐\n#{jsonResponse}"
  end
end

.unzip_file(file, destination, clean_destination) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/fastlane/plugin/lokalise/actions/lokalise_action.rb', line 80

def self.unzip_file(file, destination, clean_destination)
  Zip::File.open(file) { |zip_file|
    if clean_destination then
      UI.message "Cleaning destination folder ♻️"
      FileUtils.remove_dir(destination)
      FileUtils.mkdir_p(destination)
    end
    UI.message "Unarchiving localizations to destination 📚"
     zip_file.each { |f|
       f_path= File.join(destination, f.name)
       FileUtils.mkdir_p(File.dirname(f_path))
       FileUtils.rm(f_path) if File.file? f_path
       zip_file.extract(f, f_path)
     }
  }
end