Browse Source

Integrate TxClient into rakefile

Antony Male 10 years ago
parent
commit
8f1448bdfb
2 changed files with 45 additions and 20 deletions
  1. 27 1
      Rakefile
  2. 18 19
      build/TxClient.rb

+ 27 - 1
Rakefile

@@ -2,6 +2,8 @@ require 'tmpdir'
 require 'open-uri'
 require 'openssl'
 
+require_relative 'build/TxClient'
+
 ISCC = ENV['ISCC'] || 'C:\Program Files (x86)\Inno Setup 5\ISCC.exe'
 SZIP = ENV['SZIP'] || 'C:\Program Files\7-Zip\7z.exe'
 SIGNTOOL = ENV['SIGNTOOL'] || 'C:\Program Files (x86)\Microsoft SDKs\Windows\v7.1A\Bin\signtool.exe'
@@ -307,4 +309,28 @@ namespace :"download-syncthing" do
 end
 
 desc 'Download syncthing for all architectures'
-task :"download-syncthing", [:version] => ARCH_CONFIG.map{ |x| :"download-syncthing:#{x.arch}" }
+task :"download-syncthing", [:version] => ARCH_CONFIG.map{ |x| :"download-syncthing:#{x.arch}" }
+
+def create_tx_client(require_password = false)
+  raise "TX_PASSWORD not specified" if require_password && (ENV['TX_PASSWORD'].nil? || ENV['TX_PASSWORD'].empty?)
+  tx_client = TxClient.new('synctrayzor', 'canton7', ENV['TX_PASSWORD'], 'src/SyncTrayzor/SyncTrayzor.csproj', 'Properties/Strings')
+  tx_client.language_exceptions['es_ES'] = 'es'
+  tx_client
+end
+
+namespace :tx do
+  desc "Remove all translations from csproj"
+  task :clean do
+    create_tx_client(false).clean_translations
+  end
+
+  desc "Fetch all translatinos"
+  task :pull do
+    tx_client = create_tx_client(true)
+    tx_client.list_translations.each do |language|
+      next if language == 'en'
+      puts "Fetching #{language}..."
+      tx_client.add_translation(language)
+    end
+  end
+end

+ 18 - 19
build/TxClient.rb

@@ -4,40 +4,41 @@ require 'openssl'
 require 'rexml/document'
 
 class TxClient
-  TX_BASE = 'https://www.transifex.com/api/2/project/synctrayzor'
+  TX_BASE = 'https://www.transifex.com/api/2/project/%s'
   STATS_URL = TX_BASE + '/resource/strings/stats'
   TRANSLATION_URL = TX_BASE + '/resource/strings/translation/%s'
 
   attr_reader :language_exceptions
 
-  def initialize(user, password, csproj_path, relative_resx_path)
-    @user, @password, @csproj_path, @relative_resx_path = user, password, csproj_path, relative_resx_path
+  def initialize(project, user, password, csproj_path, relative_resx_path)
+    @project, @user, @password, @csproj_path, @relative_resx_path = project, user, password, csproj_path, relative_resx_path
 
     @language_exceptions = {
       'ca@valencia' => 'ca-ES-valencia',
     }
   end
 
-  def request(uri)
-    open(uri, ssl_verify_mode: OpenSSL::SSL::VERIFY_NONE, http_basic_authentication: [@user, @password]) do |f|
-      JSON.parse(f.read)
-    end
-  end
-
   def list_translations(completion_percent = 75)
-    request(STATS_URL).select do |lang, stats|
+    request(sprintf(STATS_URL, @project)).select do |lang, stats|
       stats['translated_entities'].fdiv(stats['translated_entities'] + stats['untranslated_entities']) * 100 > completion_percent
     end.keys
   end
 
   def add_translation(language)
-    # resx_path = resx_path_for_language(language)
+    download_and_write_resx(language)
+    add_resx_to_csproj(language)
+  end
 
-    # download_and_write_resx(language)
+  def clean_translations
+    remove_all_resx_from_csproj
+  end
 
-    add_resx_to_csproj(language)
-    # remove_all_resx_from_csproj
-    
+  private
+
+  def request(uri)
+    open(uri, ssl_verify_mode: OpenSSL::SSL::VERIFY_NONE, http_basic_authentication: [@user, @password]) do |f|
+      JSON.parse(f.read)
+    end
   end
 
   def relative_resx_path_for_language(language)
@@ -46,11 +47,11 @@ class TxClient
   end
 
   def absolute_resx_path_for_language(language)
-    File.join(File.dirname(@csproj_path), relative_resx_path_for_language(langauge))
+    File.join(File.dirname(@csproj_path), relative_resx_path_for_language(language))
   end
 
   def download_and_write_resx(language)
-    content = request(sprintf(TRANSLATION_URL, language))["content"]
+    content = request(sprintf(TRANSLATION_URL, @project, language))["content"]
     File.open(absolute_resx_path_for_language(language), 'w') do |f|
       f.write(content)
     end
@@ -111,5 +112,3 @@ class TxClient
     end
   end
 end
-
-TxClient.new('canton7', '', '../src/SyncTrayzor/SyncTrayzor.csproj', 'Properties/Strings').add_translation("de")