locomotivecms_mounter_pull_19 1.5.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (84) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +12 -0
  3. data/.rspec +3 -0
  4. data/.travis.yml +4 -0
  5. data/Gemfile +7 -0
  6. data/Gemfile.lock +131 -0
  7. data/MIT-LICENSE +20 -0
  8. data/NOTES +4 -0
  9. data/README.md +26 -0
  10. data/Rakefile +37 -0
  11. data/TODO +82 -0
  12. data/init.rb +2 -0
  13. data/lib/locomotive/mounter.rb +106 -0
  14. data/lib/locomotive/mounter/config.rb +21 -0
  15. data/lib/locomotive/mounter/engine_api.rb +205 -0
  16. data/lib/locomotive/mounter/exceptions.rb +44 -0
  17. data/lib/locomotive/mounter/extensions/compass.rb +38 -0
  18. data/lib/locomotive/mounter/extensions/httmultiparty.rb +22 -0
  19. data/lib/locomotive/mounter/extensions/sprockets.rb +46 -0
  20. data/lib/locomotive/mounter/extensions/tilt/css.rb +31 -0
  21. data/lib/locomotive/mounter/fields.rb +254 -0
  22. data/lib/locomotive/mounter/models/base.rb +42 -0
  23. data/lib/locomotive/mounter/models/content_asset.rb +84 -0
  24. data/lib/locomotive/mounter/models/content_entry.rb +372 -0
  25. data/lib/locomotive/mounter/models/content_field.rb +190 -0
  26. data/lib/locomotive/mounter/models/content_select_option.rb +29 -0
  27. data/lib/locomotive/mounter/models/content_type.rb +274 -0
  28. data/lib/locomotive/mounter/models/editable_element.rb +27 -0
  29. data/lib/locomotive/mounter/models/page.rb +442 -0
  30. data/lib/locomotive/mounter/models/site.rb +28 -0
  31. data/lib/locomotive/mounter/models/snippet.rb +55 -0
  32. data/lib/locomotive/mounter/models/theme_asset.rb +148 -0
  33. data/lib/locomotive/mounter/models/translation.rb +28 -0
  34. data/lib/locomotive/mounter/mounting_point.rb +65 -0
  35. data/lib/locomotive/mounter/reader/api.rb +64 -0
  36. data/lib/locomotive/mounter/reader/api/base.rb +67 -0
  37. data/lib/locomotive/mounter/reader/api/content_assets_reader.rb +39 -0
  38. data/lib/locomotive/mounter/reader/api/content_entries_reader.rb +142 -0
  39. data/lib/locomotive/mounter/reader/api/content_types_reader.rb +76 -0
  40. data/lib/locomotive/mounter/reader/api/pages_reader.rb +192 -0
  41. data/lib/locomotive/mounter/reader/api/site_reader.rb +42 -0
  42. data/lib/locomotive/mounter/reader/api/snippets_reader.rb +61 -0
  43. data/lib/locomotive/mounter/reader/api/theme_assets_reader.rb +42 -0
  44. data/lib/locomotive/mounter/reader/api/translations_reader.rb +30 -0
  45. data/lib/locomotive/mounter/reader/file_system.rb +43 -0
  46. data/lib/locomotive/mounter/reader/file_system/base.rb +65 -0
  47. data/lib/locomotive/mounter/reader/file_system/content_assets_reader.rb +90 -0
  48. data/lib/locomotive/mounter/reader/file_system/content_entries_reader.rb +97 -0
  49. data/lib/locomotive/mounter/reader/file_system/content_types_reader.rb +88 -0
  50. data/lib/locomotive/mounter/reader/file_system/pages_reader.rb +211 -0
  51. data/lib/locomotive/mounter/reader/file_system/site_reader.rb +27 -0
  52. data/lib/locomotive/mounter/reader/file_system/snippets_reader.rb +115 -0
  53. data/lib/locomotive/mounter/reader/file_system/theme_assets_reader.rb +83 -0
  54. data/lib/locomotive/mounter/reader/file_system/translations_reader.rb +36 -0
  55. data/lib/locomotive/mounter/reader/runner.rb +89 -0
  56. data/lib/locomotive/mounter/utils/hash.rb +31 -0
  57. data/lib/locomotive/mounter/utils/output.rb +124 -0
  58. data/lib/locomotive/mounter/utils/string.rb +40 -0
  59. data/lib/locomotive/mounter/utils/yaml.rb +125 -0
  60. data/lib/locomotive/mounter/utils/yaml_front_matters_template.rb +45 -0
  61. data/lib/locomotive/mounter/version.rb +8 -0
  62. data/lib/locomotive/mounter/writer/api.rb +74 -0
  63. data/lib/locomotive/mounter/writer/api/base.rb +172 -0
  64. data/lib/locomotive/mounter/writer/api/content_assets_writer.rb +74 -0
  65. data/lib/locomotive/mounter/writer/api/content_entries_writer.rb +227 -0
  66. data/lib/locomotive/mounter/writer/api/content_types_writer.rb +151 -0
  67. data/lib/locomotive/mounter/writer/api/pages_writer.rb +250 -0
  68. data/lib/locomotive/mounter/writer/api/site_writer.rb +125 -0
  69. data/lib/locomotive/mounter/writer/api/snippets_writer.rb +111 -0
  70. data/lib/locomotive/mounter/writer/api/theme_assets_writer.rb +201 -0
  71. data/lib/locomotive/mounter/writer/api/translations_writer.rb +85 -0
  72. data/lib/locomotive/mounter/writer/file_system.rb +44 -0
  73. data/lib/locomotive/mounter/writer/file_system/base.rb +70 -0
  74. data/lib/locomotive/mounter/writer/file_system/content_assets_writer.rb +38 -0
  75. data/lib/locomotive/mounter/writer/file_system/content_entries_writer.rb +33 -0
  76. data/lib/locomotive/mounter/writer/file_system/content_types_writer.rb +32 -0
  77. data/lib/locomotive/mounter/writer/file_system/pages_writer.rb +93 -0
  78. data/lib/locomotive/mounter/writer/file_system/site_writer.rb +30 -0
  79. data/lib/locomotive/mounter/writer/file_system/snippets_writer.rb +59 -0
  80. data/lib/locomotive/mounter/writer/file_system/theme_assets_writer.rb +72 -0
  81. data/lib/locomotive/mounter/writer/file_system/translations_writer.rb +29 -0
  82. data/lib/locomotive/mounter/writer/runner.rb +73 -0
  83. data/locomotivecms_mounter.gemspec +64 -0
  84. metadata +539 -0
@@ -0,0 +1,151 @@
1
+ module Locomotive
2
+ module Mounter
3
+ module Writer
4
+ module Api
5
+
6
+ # Push content types to a remote LocomotiveCMS engine.
7
+ #
8
+ # In a first time, create the content types without any relationships fields.
9
+ # Then, add the relationships one by one.
10
+ #
11
+ # If the :force option is passed, the remote fields not defined in the mounter version
12
+ # of the content type will be destroyed when pushed. The options of
13
+ # a select field will be pushed as well, otherwise they won't unless if
14
+ # it is a brand new content type.
15
+ #
16
+ class ContentTypesWriter < Base
17
+
18
+ def prepare
19
+ super
20
+
21
+ # assign an _id to a local content type if possible
22
+ self.get(:content_types, nil, true).each do |attributes|
23
+ content_type = self.content_types[attributes['slug']]
24
+
25
+ self.apply_response(content_type, attributes)
26
+ end
27
+ end
28
+
29
+ def write
30
+ done = {}
31
+
32
+ # first new content types
33
+ self.not_persisted.each do |content_type|
34
+ self.create_content_type(content_type)
35
+
36
+ done[content_type.slug] = content_type.with_relationships? ? :todo : :done
37
+ end
38
+
39
+ # then update the others
40
+ self.content_types.values.each do |content_type|
41
+ next unless done[content_type.slug].nil?
42
+
43
+ self.update_content_type(content_type)
44
+ end
45
+
46
+ # finally, update the newly created embedding a relationship field
47
+ done.each do |slug, status|
48
+ next if status == :done
49
+
50
+ content_type = self.content_types[slug]
51
+
52
+ self.update_content_type(content_type)
53
+ end
54
+ end
55
+
56
+ protected
57
+
58
+ # Persist a content type by calling the API. It is enhanced then
59
+ # by the response if no errors occured.
60
+ #
61
+ # @param [ Object ] content_type The content type to create
62
+ #
63
+ def create_content_type(content_type)
64
+ self.output_resource_op content_type
65
+
66
+ response = self.post :content_types, content_type.to_params, nil, true
67
+
68
+ self.apply_response(content_type, response)
69
+
70
+ # status = self.response_to_status(response)
71
+
72
+ self.output_resource_op_status content_type, :success
73
+ rescue Exception => e
74
+ self.output_resource_op_status content_type, :error, e.message
75
+ end
76
+
77
+ # Update a content type by calling the API.
78
+ #
79
+ # @param [ Object ] content_type The content type to update
80
+ #
81
+ def update_content_type(content_type)
82
+ self.output_resource_op content_type
83
+
84
+ params = self.content_type_to_params(content_type)
85
+
86
+ # make a call to the API for the update
87
+ self.put :content_types, content_type._id, params
88
+
89
+ self.output_resource_op_status content_type, :success
90
+ rescue Exception => e
91
+ self.output_resource_op_status content_type, :error, e.message
92
+ end
93
+
94
+ def content_types
95
+ self.mounting_point.content_types
96
+ end
97
+
98
+ # Return the content types not persisted yet.
99
+ #
100
+ # @return [ Array ] The list of non persisted content types.
101
+ #
102
+ def not_persisted
103
+ self.content_types.values.find_all { |content_type| !content_type.persisted? }
104
+ end
105
+
106
+ # Enhance the content type with the information returned by an API call.
107
+ #
108
+ # @param [ Object ] content_type The content type instance
109
+ # @param [ Hash ] response The API response
110
+ #
111
+ def apply_response(content_type, response)
112
+ return if content_type.nil? || response.nil?
113
+
114
+ content_type._id = response['id']
115
+ content_type.klass_name = response['klass_name']
116
+
117
+ response['entries_custom_fields'].each do |remote_field|
118
+ field = content_type.find_field(remote_field['name'])
119
+ _id = remote_field['id']
120
+
121
+ if field.nil?
122
+ if self.force?
123
+ content_type.fields << Locomotive::Mounter::Models::ContentField.new(_id: _id, _destroy: true)
124
+ end
125
+ else
126
+ field._id = _id
127
+ end
128
+ end
129
+ end
130
+
131
+ # Get the params of a content type for an update.
132
+ # Delete the select_options unless the force flag is true.
133
+ #
134
+ # @param [ Object ] content_type The ContentType
135
+ #
136
+ # @return [ Hash ] The params of the ContentType ready to be used in the API
137
+ #
138
+ def content_type_to_params(content_type)
139
+ content_type.to_params(all_fields: true).tap do |params|
140
+ params[:entries_custom_fields].each do |attributes|
141
+ attributes.delete(:select_options) unless self.force?
142
+ end
143
+ end
144
+ end
145
+
146
+ end
147
+
148
+ end
149
+ end
150
+ end
151
+ end
@@ -0,0 +1,250 @@
1
+ module Locomotive
2
+ module Mounter
3
+ module Writer
4
+ module Api
5
+
6
+ # Push pages to a remote LocomotiveCMS engine.
7
+ #
8
+ # New pages are created and existing ones are partially updated.
9
+ #
10
+ # If the :force option is passed, the existing pages are fully updated (title, ...etc).
11
+ # But in any cases, the content of the page will be destroyed, unless the layout of the page
12
+ # changes.
13
+ #
14
+ class PagesWriter < Base
15
+
16
+ MAX_ATTEMPTS = 5
17
+
18
+ attr_accessor :new_pages
19
+ attr_accessor :remote_translations
20
+
21
+ def prepare
22
+ super
23
+
24
+ self.new_pages, self.remote_translations = [], {}
25
+
26
+ # set the unique identifier to each local page
27
+ self.get(:pages, nil, true).each do |attributes|
28
+ page = self.pages.values.find do |_page|
29
+ _page.safe_fullpath(false) == attributes['fullpath'].dasherize
30
+ end
31
+
32
+ self.remote_translations[attributes['fullpath']] = attributes['translated_in']
33
+
34
+ page._id = attributes['id'] if page
35
+ end
36
+
37
+ # assign the parent_id and the content_type_id to all the pages
38
+ self.pages.values.each do |page|
39
+ next if page.index_or_404?
40
+
41
+ page.parent_id = page.parent._id
42
+ end
43
+ end
44
+
45
+ # Write all the pages to the remote destination
46
+ def write
47
+ self.each_locale do |locale|
48
+ self.output_locale
49
+
50
+ done, attempts = {}, 0
51
+
52
+ # begin by the layouts (pages inside the layouts folder)
53
+ _write_layouts(pages, done)
54
+
55
+ while done.size < pages.length - 1 && attempts < MAX_ATTEMPTS
56
+ _write(pages['index'], done, attempts > 0)
57
+
58
+ # keep track of the attempts because we don't want to get an infinite loop.
59
+ attempts += 1
60
+ end
61
+
62
+ write_page(pages['404'])
63
+
64
+ if done.size < pages.length - 1
65
+ self.log %{Warning: NOT all the pages were pushed.\n\tCheck that the pages inheritance was done right OR that you translated all your pages.\n}.colorize(color: :red)
66
+ end
67
+ end
68
+ end
69
+
70
+ protected
71
+
72
+ def _write_layouts(pages, done)
73
+ if pages['layouts']
74
+ _write(pages['layouts'], done, false, false)
75
+
76
+ layouts = pages.values.find_all { |page| page.fullpath =~ /^layouts\// }
77
+
78
+ # make sure the templates without the extends tag are first in the list
79
+ layouts.sort! do |a, b|
80
+ (a.depth_and_position + (a.extends_template? ? 1000 : 0)) <=> (b.depth_and_position + (b.extends_template? ? 1000 : 0))
81
+ end
82
+ layouts.each do |page|
83
+ _write(page, done, false, false)
84
+ end
85
+ end
86
+ end
87
+
88
+ def _write(page, done, already_done = false, with_children = true)
89
+ if self.safely_translated?(page)
90
+ write_page(page) unless already_done
91
+ else
92
+ self.output_resource_op page
93
+ self.output_resource_op_status page, :not_translated
94
+ end
95
+
96
+ # mark it as done
97
+ done[page.fullpath] = true
98
+
99
+ # loop over its children
100
+ if with_children
101
+ (page.children || []).sort_by(&:depth_and_position).each do |child|
102
+ template_fullpath = child.template_fullpath
103
+ template_fullpath = page.fullpath if template_fullpath && template_fullpath == 'parent'
104
+
105
+ if done[child.fullpath].nil? && (!template_fullpath || done[template_fullpath])
106
+ _write(child, done)
107
+ end
108
+ end
109
+ end
110
+ end
111
+
112
+ def write_page(page)
113
+ locale = Locomotive::Mounter.locale
114
+
115
+ return unless page.translated_in?(locale)
116
+
117
+ self.output_resource_op page
118
+
119
+ success = page.persisted? ? self.update_page(page) : self.create_page(page)
120
+
121
+ self.output_resource_op_status page, success ? :success : :error
122
+
123
+ self.flush_log_buffer
124
+ end
125
+
126
+ # Persist a page by calling the API. The returned _id
127
+ # is then set to the page itself.
128
+ #
129
+ # @param [ Object ] page The page to create
130
+ #
131
+ # @return [ Boolean ] True if the call to the API succeeded
132
+ #
133
+ def create_page(page)
134
+ if !page.index_or_404? && page.parent_id.nil?
135
+ raise Mounter::WriterException.new("We are unable to find the parent page for #{page.fullpath}")
136
+ end
137
+
138
+ params = self.buffer_log { page_to_params(page) }
139
+
140
+ # make a call to the API to create the page, no need to set
141
+ # the locale since it first happens for the default locale.
142
+ response = self.post :pages, params, nil, true
143
+
144
+ if response
145
+ page._id = response['id']
146
+ self.new_pages << page._id
147
+ end
148
+
149
+ !response.nil?
150
+ end
151
+
152
+ # Update a page by calling the API.
153
+ #
154
+ # @param [ Object ] page The page to persist
155
+ #
156
+ # @return [ Boolean ] True if the call to the API succeeded
157
+ #
158
+ def update_page(page)
159
+ locale = Locomotive::Mounter.locale
160
+
161
+ # All the attributes of the page or just some of them
162
+ params = self.buffer_log do
163
+ self.page_to_params(page, self.data? || !self.already_translated?(page))
164
+ end
165
+
166
+ # make a call to the API for the update
167
+ response = self.put :pages, page._id, params, locale
168
+
169
+ !response.nil?
170
+ end
171
+
172
+ # Shortcut to get pages.
173
+ #
174
+ # @return [ Hash ] The hash whose key is the fullpath and the value is the page itself
175
+ #
176
+ def pages
177
+ self.mounting_point.pages
178
+ end
179
+
180
+ # Tell if the page passed in parameter has already been
181
+ # translated on the remote engine for the locale passed
182
+ # as the second parameter.
183
+ #
184
+ # @param [ Object ] page The page
185
+ # @param [ String / Symbol ] locale The locale. Use the current locale by default
186
+ #
187
+ # @return [ Boolean] True if already translated.
188
+ #
189
+ def already_translated?(page, locale = nil)
190
+ locale ||= Locomotive::Mounter.locale
191
+
192
+ (@remote_translations[page.fullpath] || []).include?(locale.to_s)
193
+ end
194
+
195
+ # Tell if the page is correctly localized, meaning it is localized itself
196
+ # as well as its parent.
197
+ #
198
+ # @param [ Object ] page The page
199
+ #
200
+ # @return [ Boolean] True if safely translated.
201
+ #
202
+ def safely_translated?(page)
203
+ if page.parent.nil?
204
+ page.translated_in?(Locomotive::Mounter.locale)
205
+ else
206
+ page.parent.translated_in?(Locomotive::Mounter.locale) &&
207
+ page.translated_in?(Locomotive::Mounter.locale)
208
+ end
209
+ end
210
+
211
+ # Return the parameters of a page sent by the API.
212
+ # It includes the editable_elements if the data option is enabled or
213
+ # if the page is a new one.
214
+ #
215
+ # @param [ Object ] page The page
216
+ # @param [ Boolean ] safe If true the to_safe_params is called, otherwise to_params is applied.
217
+ #
218
+ # @return [ Hash ] The parameters of the page
219
+ #
220
+ def page_to_params(page, safe = false)
221
+ (safe ? page.to_safe_params : page.to_params).tap do |params|
222
+ # raw template
223
+ params[:raw_template] = self.replace_content_assets!(params[:raw_template])
224
+
225
+ if self.data? || self.new_pages.include?(page._id)
226
+ params[:editable_elements] = (page.editable_elements || []).map(&:to_params)
227
+ else
228
+ params.delete(:editable_elements)
229
+ end
230
+
231
+ # editable elements
232
+ (params[:editable_elements] || []).each do |element|
233
+ if element[:content] =~ /^\/samples\//
234
+ element[:source] = self.path_to_file(element.delete(:content))
235
+ elsif element[:content] =~ %r(^http://)
236
+ element[:source_url] = element.delete(:content)
237
+ else
238
+ # string / text elements
239
+ element[:content] = self.replace_content_assets!(element[:content])
240
+ end
241
+ end
242
+ end
243
+ end
244
+
245
+ end
246
+
247
+ end
248
+ end
249
+ end
250
+ end
@@ -0,0 +1,125 @@
1
+ module Locomotive
2
+ module Mounter
3
+ module Writer
4
+ module Api
5
+
6
+ class SiteWriter < Base
7
+
8
+ attr_accessor :remote_site
9
+
10
+ # Check if the site has to be created before.
11
+ def prepare
12
+ super
13
+
14
+ self.fetch_site
15
+ end
16
+
17
+ # Create the site if it does not exist
18
+ def write
19
+ if self.site.persisted?
20
+ self.check_locales! unless self.force? # requirements
21
+
22
+ if self.force?
23
+ self.update_site
24
+ end
25
+ else
26
+ self.create_site
27
+ end
28
+ end
29
+
30
+ protected
31
+
32
+ # Create the current site in all the locales
33
+ #
34
+ def create_site
35
+ # create it in the default locale
36
+ Mounter.with_locale(self.default_locale) do
37
+ self.output_locale
38
+
39
+ self.output_resource_op self.site
40
+
41
+ if (site = self.post(:sites, self.site.to_hash(false), Mounter.locale)).nil?
42
+ raise Mounter::WriterException.new('Sorry, we are unable to create the site.')
43
+ else
44
+ self.site._id = site['id']
45
+ self.output_resource_op_status self.site
46
+ end
47
+ end
48
+
49
+ # update it in other locales
50
+ self.update_site(true)
51
+ end
52
+
53
+ # Update the current site in all the locales
54
+ #
55
+ # @param [ Boolean ] exclude_current_locale Update the site for all the locales other than the default one.
56
+ #
57
+ def update_site(exclude_current_locale = false)
58
+ self.each_locale do |locale|
59
+ next if exclude_current_locale && locale.to_s == self.default_locale.to_s
60
+
61
+ self.output_locale
62
+
63
+ begin
64
+ self.output_resource_op self.site
65
+
66
+ self.put(:sites, self.site._id, self.site.to_hash(false), locale)
67
+
68
+ self.output_resource_op_status self.site
69
+ rescue Exception => e
70
+ self.output_resource_op_status self.site, :error, e.message
71
+ end
72
+ end
73
+ end
74
+
75
+ def safe_attributes
76
+ %w(id name locales timezone)
77
+ end
78
+
79
+ def fetch_site
80
+ begin
81
+ self.get(:current_site).tap do |_site|
82
+ if _site
83
+ self.remote_site = _site
84
+ self.site._id = _site['id']
85
+ end
86
+ end
87
+ rescue WriterException, ApiReadException => e
88
+ nil
89
+ end
90
+ end
91
+
92
+ # To push all the other resources, the big requirement is to
93
+ # have the same locales between the local site and the remote one.
94
+ #
95
+ def check_locales!
96
+ default_locale = self.mounting_point.default_locale.to_s
97
+ locales = self.site.locales.map(&:to_s)
98
+ remote_locales = self.remote_site['locales']
99
+ message = nil
100
+
101
+ unless locales.all? { |l| remote_locales.include?(l) }
102
+ message = "Your site locales (#{locales.join(', ')}) do not match exactly the ones of your target (#{remote_locales.join(', ')})"
103
+ end
104
+
105
+ if default_locale != remote_locales.first
106
+ message = "Your default site locale (#{default_locale.inspect}) is not the same as the one of your target (#{remote_locales.first.inspect})"
107
+ end
108
+
109
+ if message
110
+ self.output_resource_op self.site
111
+ self.output_resource_op_status self.site, :error, message
112
+ raise Mounter::WriterException.new('Use the force option in order to force your locale settings.')
113
+ end
114
+ end
115
+
116
+ def has_admin_rights?
117
+ self.get(:my_account, nil, true)['admin']
118
+ end
119
+
120
+ end
121
+
122
+ end
123
+ end
124
+ end
125
+ end