tml 4.4.1 → 4.4.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 219ec288ae06a6e1f41055d5a43911f912a9a60b
4
- data.tar.gz: af5bb4eb638a821d605611fe3dfdb0eb3bfd1ef1
3
+ metadata.gz: c5b559306971c39fdd4df874f01174dd53af720f
4
+ data.tar.gz: 64df8e8b2fabc6b1bdab21461b72f0ae0f373687
5
5
  SHA512:
6
- metadata.gz: 284ba13aed6ef6aff3a8b016edf77d8e9e844e587fd80e4d8029e8fe2904552f7c5d75b91e31ea7173ab0bdcae24f8ffc6f3e19396d632322f566e120b5e3b85
7
- data.tar.gz: a53fe934ba88ef001c8ab23c8f8ac14344397f5240d0ffb330bf5ac8d6967a02943c97505bea742ca0ea3d7b6a8bd023863eb29a40639b8e44d41e4f9d2c2656
6
+ metadata.gz: 43033def0b0187721cb8c0020c5ca60958c62228c992ad11e23092c2872f5ad9118a6a8d159854475e5c3636183043b0f9181d021076423ed60a3859e973ae43
7
+ data.tar.gz: acbcb86d03f2abf4c5d16a6af803228af8df3dfdb671f0453ca84a9d085d3ad7526c15d2e4d1b1b043b2d949cbc0aaa3ddbf31f05c79ba3297dd2465e66919b0
@@ -68,6 +68,10 @@ class Tml::Api::Client < Tml::Base
68
68
  application.host || API_HOST
69
69
  end
70
70
 
71
+ def cdn_host
72
+ CDN_HOST
73
+ end
74
+
71
75
  def connection
72
76
  @connection ||= Faraday.new(:url => host) do |faraday|
73
77
  faraday.request(:url_encoded) # form-encode POST params
@@ -76,12 +80,16 @@ class Tml::Api::Client < Tml::Base
76
80
  end
77
81
  end
78
82
 
83
+ def get_cache_version
84
+ execute_request('applications/current/version', {}, {:raw => true})
85
+ end
86
+
79
87
  def verify_cache_version
80
88
  return if Tml.cache.version and Tml.cache.version != 'undefined'
81
89
 
82
90
  current_version = Tml.cache.fetch_version
83
91
  if current_version == 'undefined'
84
- Tml.cache.store_version(execute_request('applications/current/version', {}, {:raw => true}))
92
+ Tml.cache.store_version(get_cache_version)
85
93
  else
86
94
  Tml.cache.version = current_version
87
95
  end
@@ -89,7 +97,7 @@ class Tml::Api::Client < Tml::Base
89
97
  end
90
98
 
91
99
  def cdn_connection
92
- @cdn_connection ||= Faraday.new(:url => CDN_HOST) do |faraday|
100
+ @cdn_connection ||= Faraday.new(:url => cdn_host) do |faraday|
93
101
  faraday.request(:url_encoded) # form-encode POST params
94
102
  faraday.adapter(Faraday.default_adapter) # make requests with Net::HTTP
95
103
  end
@@ -40,11 +40,21 @@ class Tml::Generators::Base
40
40
  end
41
41
 
42
42
  def cache_path
43
- raise Tml::Exception.new('Must be implemented by the subclass')
43
+ @cache_path ||= begin
44
+ path = Tml.config.cache[:path]
45
+ path ||= 'config/tml'
46
+ FileUtils.mkdir_p(path)
47
+ FileUtils.chmod(0777, path)
48
+ path
49
+ end
50
+ end
51
+
52
+ def current_path
53
+ "#{cache_path}/current"
44
54
  end
45
55
 
46
56
  def cache_version
47
- @started_at.strftime('%Y%m%d%H%M%S')
57
+ @cache_version ||= api_client.get_cache_version
48
58
  end
49
59
 
50
60
  def cache(key, data)
@@ -64,7 +74,6 @@ class Tml::Generators::Base
64
74
  def prepare
65
75
  @started_at = Time.now
66
76
  Tml.session.init
67
- cache_path
68
77
  end
69
78
 
70
79
  def api_client
@@ -81,37 +90,33 @@ class Tml::Generators::Base
81
90
 
82
91
  def finalize
83
92
  @finished_at = Time.now
84
- log("Cache has been stored in #{cache_path}")
85
93
  log("Cache generation took #{@finished_at - @started_at} mls.")
86
94
  log('Done.')
87
95
  end
88
96
 
89
- def cache_application
90
- log('Downloading application...')
91
- cache(Tml::Application.cache_key, application)
92
- log('Application has been cached.')
97
+ def ungzip(tarfile)
98
+ z = Zlib::GzipReader.new(tarfile)
99
+ unzipped = StringIO.new(z.read)
100
+ z.close
101
+ unzipped
93
102
  end
94
103
 
95
- def cache_languages
96
- log('Downloading languages...')
97
- unless application['languages']
98
- log('No languages are available...')
99
- return
104
+ def untar(io, destination)
105
+ Gem::Package::TarReader.new io do |tar|
106
+ tar.each do |tarfile|
107
+ destination_file = File.join destination, tarfile.full_name
108
+
109
+ if tarfile.directory?
110
+ FileUtils.mkdir_p destination_file
111
+ else
112
+ destination_directory = File.dirname(destination_file)
113
+ FileUtils.mkdir_p destination_directory unless File.directory?(destination_directory)
114
+ File.open destination_file, "wb" do |f|
115
+ f.print tarfile.read
116
+ end
117
+ end
118
+ end
100
119
  end
101
- languages.each do |lang|
102
- language = api_client.get("languages/#{lang['locale']}", :definition => true)
103
- cache(Tml::Language.cache_key(language['locale']), language)
104
- end
105
- log("#{application['languages'].count} languages have been cached.")
106
- end
107
-
108
- def symlink_path
109
- "#{Tml.config.cache[:path]}/current"
110
120
  end
111
121
 
112
- def generate_symlink
113
- FileUtils.rm(symlink_path) if File.exist?(symlink_path)
114
- FileUtils.ln_s(cache_version, symlink_path)
115
- log('Symlink has been updated.')
116
- end
117
122
  end
@@ -29,131 +29,43 @@
29
29
  # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30
30
  #++
31
31
 
32
- class Tml::Generators::File < Tml::Generators::Base
33
-
34
- def cache_path
35
- @cache_path ||= begin
36
- path = "#{Tml.config.cache[:path]}/#{cache_version}"
37
- log("Cache will be stored in #{path}")
38
- FileUtils.mkdir_p(path)
39
- FileUtils.chmod(0777, path)
40
- path
41
- end
42
- end
32
+ require 'rubygems'
33
+ require 'rubygems/package'
34
+ require 'open-uri'
35
+ require 'zlib'
36
+ require 'fileutils'
43
37
 
44
- def file_path(key)
45
- path = key.split('/')
46
- if path.count > 1
47
- filename = path.pop
48
- path = File.join(cache_path, path)
49
- FileUtils.mkdir_p(path)
50
- File.join(path, "#{filename}.json")
51
- else
52
- File.join(cache_path, "#{path.first}.json")
53
- end
54
- end
55
-
56
- def cache(key, data)
57
- File.open(file_path(key), 'w') { |file| file.write(JSON.pretty_generate(data)) }
58
- end
38
+ class Tml::Generators::File < Tml::Generators::Base
59
39
 
60
40
  def execute
61
- cache_application
62
- cache_languages
63
- cache_translations
64
- generate_symlink
65
- end
66
-
67
- def cache_translations
68
- log('Downloading translations...')
69
-
70
- languages.each do |language|
71
- log("Downloading #{language['english_name']} language...")
72
-
73
- if Tml.config.cache[:segmented]
74
- api_client.paginate('applications/current/sources') do |source|
75
- next unless source['source']
76
-
77
- cache_path = Tml::Source.cache_key(language['locale'], source['source'])
78
- log("Downloading #{source['source']} in #{language['locale']} to #{cache_path}...")
79
-
80
- data = api_client.get("sources/#{source['key']}/translations", {:locale => language['locale'], :original => true, :per_page => 1000})
81
- cache(cache_path, data)
82
- end
83
- else
84
- cache_path = Tml::Application.translations_cache_key(language['locale'])
85
- log("Downloading translations in #{language['locale']} to #{cache_path}...")
86
- data = {}
87
- api_client.paginate('applications/current/translations', {:locale => language['locale'], :original => true, :per_page => 1000}) do |translations|
88
- data.merge!(translations)
89
- end
90
- cache(cache_path, data)
91
- end
92
- end
93
- end
94
-
95
- def rollback
96
- folders = Dir["#{Tml.config.cache[:path]}/*"]
97
- folders.delete_if{|e| e.index('current')}.sort!
98
-
99
- if File.exist?(symlink_path)
100
- current_dest = File.readlink("#{Tml.config.cache[:path]}/current")
101
- current_dest = "#{Tml.config.cache[:path]}/#{current_dest}"
41
+ if cache_version == '0'
42
+ log("No releases have been generated yet. Please visit your Dashboard and publish translations.")
102
43
  else
103
- current_dest = 'undefined'
104
- end
105
-
106
- index = folders.index(current_dest)
107
-
108
- if index == 0
109
- log('There are no earlier cache versions')
110
- return
111
- end
112
-
113
- if index.nil?
114
- new_version_path = folders[folders.size-1]
115
- else
116
- new_version_path = folders[index-1]
117
- end
118
-
119
- new_version_path = new_version_path.split('/').last
120
-
121
- FileUtils.rm(symlink_path) if File.exist?(symlink_path)
122
- FileUtils.ln_s(new_version_path, symlink_path)
123
-
124
- log("Cache has been rolled back to version #{new_version_path}.")
125
- end
126
-
127
- def rollup
128
- folders = Dir["#{Tml.config.cache[:path]}/*"]
129
- folders.delete_if{|e| e.index('current')}.sort!
130
-
131
- if File.exist?(symlink_path)
132
- current_dest = File.readlink("#{Tml.config.cache[:path]}/current")
133
- current_dest = "#{Tml.config.cache[:path]}/#{current_dest}"
134
- else
135
- current_dest = 'undefined'
136
- end
44
+ log("Current cache version: #{cache_version}")
45
+
46
+ archive_name = "#{cache_version}.tar.gz"
47
+ path = "#{cache_path}/#{archive_name}"
48
+ url = "#{api_client.cdn_host}/#{Tml.config.access_token}/#{archive_name}"
49
+ log("Downloading cache file: #{url}")
50
+ open(path, 'wb') do |file|
51
+ file << open(url).read
52
+ end
53
+ log('Extracting cache file...')
137
54
 
138
- index = folders.index(current_dest)
55
+ version_path = "#{cache_path}/#{cache_version}"
56
+ untar(ungzip(File.new(path)), version_path)
57
+ log("Cache has been stored in #{version_path}")
139
58
 
140
- if index == (folders.size - 1)
141
- log('You are on the latest version of the cache already. No further versions are available')
142
- return
143
- end
59
+ File.unlink(path)
144
60
 
145
- if index.nil?
146
- new_version_path = folders[0]
147
- else
148
- new_version_path = folders[index+1]
61
+ begin
62
+ FileUtils.rm(current_path) if File.exist?(current_path)
63
+ FileUtils.ln_s(cache_version, current_path)
64
+ log('The new cache path has been marked as current')
65
+ rescue
66
+ log('Could not generate current symlink to the cach path. Please indicate the version manually in the Tml initializer.')
67
+ end
149
68
  end
150
-
151
- new_version_path = new_version_path.split('/').last
152
-
153
- FileUtils.rm(symlink_path) if File.exist?(symlink_path)
154
- FileUtils.ln_s(new_version_path, symlink_path)
155
-
156
- log("Cache has been upgraded to version #{new_version_path}.")
157
69
  end
158
70
 
159
71
  end
data/lib/tml/version.rb CHANGED
@@ -30,5 +30,5 @@
30
30
  #++
31
31
 
32
32
  module Tml
33
- VERSION = '4.4.1'
33
+ VERSION = '4.4.2'
34
34
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tml
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.4.1
4
+ version: 4.4.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Berkovich