locomotivecms_mounter 1.0.0.alpha1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/locomotive/mounter/config.rb +21 -0
- data/lib/locomotive/mounter/engine_api.rb +40 -0
- data/lib/locomotive/mounter/exceptions.rb +38 -0
- data/lib/locomotive/mounter/extensions/compass.rb +36 -0
- data/lib/locomotive/mounter/extensions/httmultiparty.rb +22 -0
- data/lib/locomotive/mounter/extensions/tilt/css.rb +31 -0
- data/lib/locomotive/mounter/extensions/tilt/haml.rb +27 -0
- data/lib/locomotive/mounter/extensions/tilt/liquid.rb +23 -0
- data/lib/locomotive/mounter/extensions/tilt/template.rb +11 -0
- data/lib/locomotive/mounter/fields.rb +250 -0
- data/lib/locomotive/mounter/models/base.rb +41 -0
- data/lib/locomotive/mounter/models/content_asset.rb +84 -0
- data/lib/locomotive/mounter/models/content_entry.rb +290 -0
- data/lib/locomotive/mounter/models/content_field.rb +128 -0
- data/lib/locomotive/mounter/models/content_select_option.rb +29 -0
- data/lib/locomotive/mounter/models/content_type.rb +217 -0
- data/lib/locomotive/mounter/models/editable_element.rb +27 -0
- data/lib/locomotive/mounter/models/page.rb +377 -0
- data/lib/locomotive/mounter/models/site.rb +27 -0
- data/lib/locomotive/mounter/models/snippet.rb +55 -0
- data/lib/locomotive/mounter/models/theme_asset.rb +135 -0
- data/lib/locomotive/mounter/models/translation.rb +28 -0
- data/lib/locomotive/mounter/mounting_point.rb +49 -0
- data/lib/locomotive/mounter/reader/api/base.rb +49 -0
- data/lib/locomotive/mounter/reader/api/content_assets_reader.rb +40 -0
- data/lib/locomotive/mounter/reader/api/content_entries_reader.rb +141 -0
- data/lib/locomotive/mounter/reader/api/content_types_reader.rb +74 -0
- data/lib/locomotive/mounter/reader/api/pages_reader.rb +174 -0
- data/lib/locomotive/mounter/reader/api/site_reader.rb +37 -0
- data/lib/locomotive/mounter/reader/api/snippets_reader.rb +59 -0
- data/lib/locomotive/mounter/reader/api/theme_assets_reader.rb +42 -0
- data/lib/locomotive/mounter/reader/api/translations_reader.rb +28 -0
- data/lib/locomotive/mounter/reader/api.rb +49 -0
- data/lib/locomotive/mounter/reader/file_system/base.rb +65 -0
- data/lib/locomotive/mounter/reader/file_system/content_assets_reader.rb +88 -0
- data/lib/locomotive/mounter/reader/file_system/content_entries_reader.rb +101 -0
- data/lib/locomotive/mounter/reader/file_system/content_types_reader.rb +88 -0
- data/lib/locomotive/mounter/reader/file_system/pages_reader.rb +206 -0
- data/lib/locomotive/mounter/reader/file_system/site_reader.rb +24 -0
- data/lib/locomotive/mounter/reader/file_system/snippets_reader.rb +78 -0
- data/lib/locomotive/mounter/reader/file_system/theme_assets_reader.rb +78 -0
- data/lib/locomotive/mounter/reader/file_system/translations_reader.rb +36 -0
- data/lib/locomotive/mounter/reader/file_system.rb +42 -0
- data/lib/locomotive/mounter/reader/runner.rb +89 -0
- data/lib/locomotive/mounter/utils/hash.rb +31 -0
- data/lib/locomotive/mounter/utils/string.rb +17 -0
- data/lib/locomotive/mounter/utils/yaml.rb +125 -0
- data/lib/locomotive/mounter/version.rb +8 -0
- data/lib/locomotive/mounter/writer/api/base.rb +323 -0
- data/lib/locomotive/mounter/writer/api/content_assets_writer.rb +74 -0
- data/lib/locomotive/mounter/writer/api/content_entries_writer.rb +223 -0
- data/lib/locomotive/mounter/writer/api/content_types_writer.rb +151 -0
- data/lib/locomotive/mounter/writer/api/pages_writer.rb +225 -0
- data/lib/locomotive/mounter/writer/api/site_writer.rb +164 -0
- data/lib/locomotive/mounter/writer/api/snippets_writer.rb +111 -0
- data/lib/locomotive/mounter/writer/api/theme_assets_writer.rb +152 -0
- data/lib/locomotive/mounter/writer/api/translations_writer.rb +83 -0
- data/lib/locomotive/mounter/writer/api.rb +62 -0
- data/lib/locomotive/mounter/writer/file_system/base.rb +61 -0
- data/lib/locomotive/mounter/writer/file_system/content_assets_writer.rb +33 -0
- data/lib/locomotive/mounter/writer/file_system/content_entries_writer.rb +29 -0
- data/lib/locomotive/mounter/writer/file_system/content_types_writer.rb +27 -0
- data/lib/locomotive/mounter/writer/file_system/pages_writer.rb +73 -0
- data/lib/locomotive/mounter/writer/file_system/site_writer.rb +25 -0
- data/lib/locomotive/mounter/writer/file_system/snippets_writer.rb +54 -0
- data/lib/locomotive/mounter/writer/file_system/theme_assets_writer.rb +35 -0
- data/lib/locomotive/mounter/writer/file_system/translations_writer.rb +22 -0
- data/lib/locomotive/mounter/writer/file_system.rb +69 -0
- data/lib/locomotive/mounter/writer/runner.rb +68 -0
- data/lib/locomotive/mounter.rb +97 -0
- metadata +487 -0
@@ -0,0 +1,152 @@
|
|
1
|
+
module Locomotive
|
2
|
+
module Mounter
|
3
|
+
module Writer
|
4
|
+
module Api
|
5
|
+
|
6
|
+
# Push theme assets to a remote LocomotiveCMS engine.
|
7
|
+
#
|
8
|
+
# New assets are automatically pushed.
|
9
|
+
# Existing ones are not pushed unless the :force option is
|
10
|
+
# passed OR if the size of the asset (if not a javascript or stylesheet) has changed.
|
11
|
+
#
|
12
|
+
class ThemeAssetsWriter < Base
|
13
|
+
|
14
|
+
# Other local attributes
|
15
|
+
attr_accessor :tmp_folder
|
16
|
+
|
17
|
+
def prepare
|
18
|
+
super
|
19
|
+
|
20
|
+
# prepare the place where the assets will be stored temporarily.
|
21
|
+
self.create_tmp_folder
|
22
|
+
|
23
|
+
# assign an _id to a local content type if possible
|
24
|
+
self.get(:theme_assets, nil, true).each do |attributes|
|
25
|
+
remote_path = File.join('/', attributes['folder'], File.basename(attributes['local_path']))
|
26
|
+
|
27
|
+
if theme_asset = self.theme_assets[remote_path]
|
28
|
+
theme_asset._id = attributes['id']
|
29
|
+
theme_asset.size = attributes['raw_size'].to_i
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def write
|
35
|
+
self.theme_assets_by_priority.each do |theme_asset|
|
36
|
+
# track it in the logs
|
37
|
+
self.output_resource_op theme_asset
|
38
|
+
|
39
|
+
status = :skipped
|
40
|
+
file = self.build_temp_file(theme_asset)
|
41
|
+
params = theme_asset.to_params.merge(source: file, performing_plain_text: false)
|
42
|
+
|
43
|
+
if theme_asset.persisted?
|
44
|
+
# we only update it if the size has changed or if the force option has been set.
|
45
|
+
if self.force? || self.theme_asset_changed?(theme_asset, file)
|
46
|
+
response = self.put :theme_assets, theme_asset._id, params
|
47
|
+
|
48
|
+
status = self.response_to_status(response)
|
49
|
+
end
|
50
|
+
else
|
51
|
+
response = self.post :theme_assets, params, nil, true
|
52
|
+
|
53
|
+
status = self.response_to_status(response)
|
54
|
+
end
|
55
|
+
|
56
|
+
# very important. we do not want a huge number of non-closed file descriptor.
|
57
|
+
file.close
|
58
|
+
|
59
|
+
# track the status
|
60
|
+
self.output_resource_op_status theme_asset, status
|
61
|
+
end
|
62
|
+
|
63
|
+
# make the stuff like they were before
|
64
|
+
self.remove_tmp_folder
|
65
|
+
end
|
66
|
+
|
67
|
+
protected
|
68
|
+
|
69
|
+
# Create the folder to store temporarily the files.
|
70
|
+
#
|
71
|
+
def create_tmp_folder
|
72
|
+
self.tmp_folder = self.runner.parameters[:tmp_dir] || File.join(Dir.getwd, '.push-tmp')
|
73
|
+
|
74
|
+
FileUtils.mkdir_p(self.tmp_folder)
|
75
|
+
end
|
76
|
+
|
77
|
+
# Clean the folder which had stored temporarily the files.
|
78
|
+
#
|
79
|
+
def remove_tmp_folder
|
80
|
+
FileUtils.rm_rf(self.tmp_folder) if self.tmp_folder
|
81
|
+
end
|
82
|
+
|
83
|
+
# Build a temp file from a theme asset.
|
84
|
+
#
|
85
|
+
# @param [ Object ] theme_asset The theme asset
|
86
|
+
#
|
87
|
+
# @return [ File ] The file descriptor
|
88
|
+
#
|
89
|
+
def build_temp_file(theme_asset)
|
90
|
+
path = File.join(self.tmp_folder, theme_asset.path)
|
91
|
+
|
92
|
+
FileUtils.mkdir_p(File.dirname(path))
|
93
|
+
|
94
|
+
File.open(path, 'w') do |file|
|
95
|
+
file.write(theme_asset.content)
|
96
|
+
end
|
97
|
+
|
98
|
+
File.new(path)
|
99
|
+
end
|
100
|
+
|
101
|
+
# Shortcut to get all the local snippets.
|
102
|
+
#
|
103
|
+
# @return [ Hash ] The hash whose key is the slug and the value is the snippet itself
|
104
|
+
#
|
105
|
+
def theme_assets
|
106
|
+
return @theme_assets if @theme_assets
|
107
|
+
|
108
|
+
@theme_assets = {}.tap do |hash|
|
109
|
+
self.mounting_point.theme_assets.each do |theme_asset|
|
110
|
+
hash[theme_asset.path] = theme_asset
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
# List of theme assets sorted by their priority.
|
116
|
+
#
|
117
|
+
# @return [ Array ] Sorted list of the theme assets
|
118
|
+
#
|
119
|
+
def theme_assets_by_priority
|
120
|
+
self.theme_assets.values.sort { |a, b| a.priority <=> b.priority }
|
121
|
+
end
|
122
|
+
|
123
|
+
# Tell if the theme_asset has changed in order to update it
|
124
|
+
# if so or simply skip it.
|
125
|
+
#
|
126
|
+
# @param [ Object ] theme_asset The theme asset
|
127
|
+
# @param [ Object ] tmp_file The size of the file (after precompilation if required)
|
128
|
+
#
|
129
|
+
# @return [ Boolean ] True if the source of the 2 assets (local and remote) is different.
|
130
|
+
#
|
131
|
+
def theme_asset_changed?(theme_asset, tmp_file)
|
132
|
+
if theme_asset.stylesheet_or_javascript?
|
133
|
+
if theme_asset.precompiled?
|
134
|
+
# puts "[#{theme_asset.filename} PRECOMPILED] local size #{File.size(tmp_file)} / remote size #{theme_asset.size}"
|
135
|
+
File.size(tmp_file) != theme_asset.size
|
136
|
+
else
|
137
|
+
# puts "[#{theme_asset.filename}] local size #{File.size(tmp_file)} / remote size #{theme_asset.size}"
|
138
|
+
File.size(tmp_file) != theme_asset.size
|
139
|
+
end
|
140
|
+
else
|
141
|
+
File.size(tmp_file) != theme_asset.size
|
142
|
+
end
|
143
|
+
|
144
|
+
# (!theme_asset.stylesheet_or_javascript? && File.size(file) != theme_asset.size) ||
|
145
|
+
# (theme_asset.stylesheet_or_javascript? && theme_asset.content.size != theme_asset.size)
|
146
|
+
end
|
147
|
+
|
148
|
+
end
|
149
|
+
end
|
150
|
+
end
|
151
|
+
end
|
152
|
+
end
|
@@ -0,0 +1,83 @@
|
|
1
|
+
module Locomotive
|
2
|
+
module Mounter
|
3
|
+
module Writer
|
4
|
+
module Api
|
5
|
+
|
6
|
+
# Push translations to a remote LocomotiveCMS engine.
|
7
|
+
#
|
8
|
+
# The force option is not used.
|
9
|
+
#
|
10
|
+
class TranslationsWriter < Base
|
11
|
+
|
12
|
+
def prepare
|
13
|
+
super
|
14
|
+
|
15
|
+
# set the unique identifier to each local translation
|
16
|
+
self.get(:translations, nil, true).each do |attributes|
|
17
|
+
translation = self.translations[attributes['key']]
|
18
|
+
|
19
|
+
translation._id = attributes['id'] if translation
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
# Write all the translations to the remote destination
|
24
|
+
def write
|
25
|
+
self.translations.each do |key, translation|
|
26
|
+
self.output_resource_op translation
|
27
|
+
|
28
|
+
success = translation.persisted? ? self.update_translation(translation) : self.create_translation(translation)
|
29
|
+
|
30
|
+
self.output_resource_op_status translation, success ? :success : :error
|
31
|
+
self.flush_log_buffer
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
protected
|
36
|
+
|
37
|
+
# Persist a translation by calling the API. The returned id
|
38
|
+
# is then set to the translation itself.
|
39
|
+
#
|
40
|
+
# @param [ Object ] translation The translation to create
|
41
|
+
#
|
42
|
+
# @return [ Boolean ] True if the call to the API succeeded
|
43
|
+
#
|
44
|
+
def create_translation(translation)
|
45
|
+
params = self.buffer_log { translation.to_params }
|
46
|
+
|
47
|
+
# make a call to the API to create the translation, no need to set
|
48
|
+
# the locale since it first happens for the default locale.
|
49
|
+
response = self.post :translations, params, nil, true
|
50
|
+
|
51
|
+
translation._id = response['id'] if response
|
52
|
+
|
53
|
+
!response.nil?
|
54
|
+
end
|
55
|
+
|
56
|
+
# Update a translation by calling the API.
|
57
|
+
#
|
58
|
+
# @param [ Object ] translation The translation to persist
|
59
|
+
#
|
60
|
+
# @return [ Boolean ] True if the call to the API succeeded
|
61
|
+
#
|
62
|
+
def update_translation(translation)
|
63
|
+
params = self.buffer_log { translation.to_params }
|
64
|
+
|
65
|
+
# make a call to the API for the update
|
66
|
+
response = self.put :translations, translation._id, params
|
67
|
+
|
68
|
+
!response.nil?
|
69
|
+
end
|
70
|
+
|
71
|
+
# Shortcut to get all the local translations.
|
72
|
+
#
|
73
|
+
# @return [ Hash ] The hash whose key is the tr key and the value is translation itself
|
74
|
+
#
|
75
|
+
def translations
|
76
|
+
self.mounting_point.translations
|
77
|
+
end
|
78
|
+
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
module Locomotive
|
2
|
+
module Mounter
|
3
|
+
module Writer
|
4
|
+
module Api
|
5
|
+
|
6
|
+
# Build a singleton instance of the Runner class.
|
7
|
+
#
|
8
|
+
# @return [ Object ] A singleton instance of the Runner class
|
9
|
+
#
|
10
|
+
def self.instance
|
11
|
+
@@instance ||= Runner.new(:api)
|
12
|
+
end
|
13
|
+
|
14
|
+
class Runner < Locomotive::Mounter::Writer::Runner
|
15
|
+
|
16
|
+
attr_accessor :uri
|
17
|
+
|
18
|
+
# Call the LocomotiveCMS engine to get a token for
|
19
|
+
# the next API calls
|
20
|
+
def prepare
|
21
|
+
# by default, do not push data (content entries and editable elements)
|
22
|
+
self.parameters[:data] ||= false
|
23
|
+
|
24
|
+
self.uri = self.parameters.delete(:uri)
|
25
|
+
email = self.parameters.delete(:email)
|
26
|
+
password = self.parameters.delete(:password)
|
27
|
+
|
28
|
+
if uri.blank? || email.blank? || password.blank?
|
29
|
+
raise Locomotive::Mounter::WriterException.new("one or many API credentials (uri, email, password) are missing")
|
30
|
+
end
|
31
|
+
|
32
|
+
begin
|
33
|
+
Locomotive::Mounter::EngineApi.set_token(uri, email, password)
|
34
|
+
rescue Exception => e
|
35
|
+
raise Locomotive::Mounter::WriterException.new("unable to get an API token: #{e.message}")
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
# Ordered list of atomic writers
|
40
|
+
#
|
41
|
+
# @return [ Array ] List of classes
|
42
|
+
#
|
43
|
+
def writers
|
44
|
+
[SiteWriter, SnippetsWriter, ContentTypesWriter, ContentEntriesWriter, PagesWriter, ThemeAssetsWriter, TranslationsWriter]
|
45
|
+
end
|
46
|
+
|
47
|
+
# Get the writer to push content assets
|
48
|
+
#
|
49
|
+
# @return [ Object ] A memoized instance of the content assets writer
|
50
|
+
#
|
51
|
+
def content_assets_writer
|
52
|
+
@content_assets_writer ||= ContentAssetsWriter.new(self.mounting_point, self).tap do |writer|
|
53
|
+
writer.prepare
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
module Locomotive
|
2
|
+
module Mounter
|
3
|
+
module Writer
|
4
|
+
module FileSystem
|
5
|
+
|
6
|
+
class Base
|
7
|
+
|
8
|
+
attr_accessor :mounting_point, :runner
|
9
|
+
|
10
|
+
def initialize(mounting_point, runner)
|
11
|
+
self.mounting_point = mounting_point
|
12
|
+
self.runner = runner
|
13
|
+
end
|
14
|
+
|
15
|
+
# It should always be called before executing the write method.
|
16
|
+
# Writers inheriting from this class can overide it
|
17
|
+
#
|
18
|
+
def prepare
|
19
|
+
end
|
20
|
+
|
21
|
+
# Writers inheriting from this class *must* overide it
|
22
|
+
def write
|
23
|
+
raise 'The write method has to be overridden'
|
24
|
+
end
|
25
|
+
|
26
|
+
# Helper method to create a folder from a relative path
|
27
|
+
#
|
28
|
+
# @param [ String ] path The relative path
|
29
|
+
#
|
30
|
+
def create_folder(path)
|
31
|
+
fullpath = File.join(self.target_path, path)
|
32
|
+
unless File.exists?(fullpath)
|
33
|
+
FileUtils.mkdir_p(fullpath)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
# Open a file described by the relative path. The file will be closed after the execution of the block.
|
38
|
+
#
|
39
|
+
# @param [ String ] path The relative path
|
40
|
+
# @param [ String ] mode The file mode ('w' by default)
|
41
|
+
# @param [ Lambda ] &block The block passed to the File.open method
|
42
|
+
#
|
43
|
+
def open_file(path, mode = 'w', &block)
|
44
|
+
# make sure the target folder exists
|
45
|
+
self.create_folder(File.dirname(path))
|
46
|
+
|
47
|
+
fullpath = File.join(self.target_path, path)
|
48
|
+
|
49
|
+
File.open(fullpath, mode, &block)
|
50
|
+
end
|
51
|
+
|
52
|
+
def target_path
|
53
|
+
self.runner.target_path
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module Locomotive
|
2
|
+
module Mounter
|
3
|
+
module Writer
|
4
|
+
module FileSystem
|
5
|
+
|
6
|
+
class ContentAssetsWriter < Base
|
7
|
+
|
8
|
+
# It creates the theme assets folders
|
9
|
+
def prepare
|
10
|
+
self.create_folder 'public'
|
11
|
+
end
|
12
|
+
|
13
|
+
# It writes all the snippets into files
|
14
|
+
def write
|
15
|
+
self.mounting_point.content_assets.each do |asset|
|
16
|
+
self.open_file(self.target_asset_path(asset)) do |file|
|
17
|
+
file.write(asset.content)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
protected
|
23
|
+
|
24
|
+
def target_asset_path(asset)
|
25
|
+
File.join('public', asset.folder, asset.filename)
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module Locomotive
|
2
|
+
module Mounter
|
3
|
+
module Writer
|
4
|
+
module FileSystem
|
5
|
+
|
6
|
+
class ContentEntriesWriter < Base
|
7
|
+
|
8
|
+
# It creates the data folder
|
9
|
+
def prepare
|
10
|
+
self.create_folder 'data'
|
11
|
+
end
|
12
|
+
|
13
|
+
# It writes all the content types into files
|
14
|
+
def write
|
15
|
+
self.mounting_point.content_types.each do |filename, content_type|
|
16
|
+
entries = (content_type.entries || []).map(&:to_hash)
|
17
|
+
|
18
|
+
self.open_file("data/#{filename}.yml") do |file|
|
19
|
+
file.write(entries.to_yaml)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module Locomotive
|
2
|
+
module Mounter
|
3
|
+
module Writer
|
4
|
+
module FileSystem
|
5
|
+
|
6
|
+
class ContentTypesWriter < Base
|
7
|
+
|
8
|
+
# It creates the content types folder
|
9
|
+
def prepare
|
10
|
+
self.create_folder 'app/content_types'
|
11
|
+
end
|
12
|
+
|
13
|
+
# It writes all the content types into files
|
14
|
+
def write
|
15
|
+
self.mounting_point.content_types.each do |filename, content_type|
|
16
|
+
self.open_file("app/content_types/#{filename}.yml") do |file|
|
17
|
+
file.write(content_type.to_yaml)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
module Locomotive
|
2
|
+
module Mounter
|
3
|
+
module Writer
|
4
|
+
module FileSystem
|
5
|
+
|
6
|
+
class PagesWriter < Base
|
7
|
+
|
8
|
+
# It creates the pages folder
|
9
|
+
def prepare
|
10
|
+
self.create_folder 'app/views/pages'
|
11
|
+
end
|
12
|
+
|
13
|
+
# It writes all the pages into files
|
14
|
+
def write
|
15
|
+
self.write_page(self.mounting_point.pages['index'])
|
16
|
+
|
17
|
+
self.write_page(self.mounting_point.pages['404'])
|
18
|
+
end
|
19
|
+
|
20
|
+
protected
|
21
|
+
|
22
|
+
# Write the information about a page into the filesystem.
|
23
|
+
# Called itself recursively. Called at first by the write method
|
24
|
+
#
|
25
|
+
# @param [ Object ] page The page
|
26
|
+
# @param [ String ] path The parent path
|
27
|
+
#
|
28
|
+
def write_page(page, path = '')
|
29
|
+
# Note: we assume the current locale is the default one
|
30
|
+
page.translated_in.each do |locale|
|
31
|
+
default_locale = locale.to_sym == self.mounting_point.default_locale.to_sym
|
32
|
+
|
33
|
+
filepath = (path.blank? ? page.slug : File.join(path, page.slug)).underscore
|
34
|
+
|
35
|
+
Locomotive::Mounter.with_locale(locale) do
|
36
|
+
self.write_page_to_fs(page, filepath, default_locale ? nil : locale)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
# also write the nested pages
|
41
|
+
(page.children || []).each do |child|
|
42
|
+
self.write_page(child, page.depth == 0 ? '' : page.slug)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
# Write into the filesystem the file about the page which will store
|
47
|
+
# information about this page + template.
|
48
|
+
# The file is localized meaning a same page could generate a file for each translation.
|
49
|
+
#
|
50
|
+
# @param [ Object ] page The page
|
51
|
+
# @param [ String ] filepath The path to the file describing the page (not localized)
|
52
|
+
# @param [ Locale ] locale The locale, nil if default locale
|
53
|
+
#
|
54
|
+
#
|
55
|
+
def write_page_to_fs(page, filepath, locale)
|
56
|
+
_filepath = "#{filepath}.liquid"
|
57
|
+
_filepath.gsub!(/.liquid$/, ".#{locale}.liquid") if locale
|
58
|
+
|
59
|
+
# unless page.template.nil?
|
60
|
+
_filepath = File.join('app', 'views', 'pages', _filepath)
|
61
|
+
|
62
|
+
self.open_file(_filepath) do |file|
|
63
|
+
file.write(page.to_yaml)
|
64
|
+
end
|
65
|
+
# end
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Locomotive
|
2
|
+
module Mounter
|
3
|
+
module Writer
|
4
|
+
module FileSystem
|
5
|
+
|
6
|
+
class SiteWriter < Base
|
7
|
+
|
8
|
+
# It creates the config folder
|
9
|
+
def prepare
|
10
|
+
self.create_folder 'config'
|
11
|
+
end
|
12
|
+
|
13
|
+
# It fills the config/site.yml file
|
14
|
+
def write
|
15
|
+
self.open_file('config/site.yml') do |file|
|
16
|
+
file.write(self.mounting_point.site.to_yaml)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
module Locomotive
|
2
|
+
module Mounter
|
3
|
+
module Writer
|
4
|
+
module FileSystem
|
5
|
+
|
6
|
+
class SnippetsWriter < Base
|
7
|
+
|
8
|
+
# It creates the snippets folder
|
9
|
+
def prepare
|
10
|
+
self.create_folder 'app/views/snippets'
|
11
|
+
end
|
12
|
+
|
13
|
+
# It writes all the snippets into files
|
14
|
+
def write
|
15
|
+
self.mounting_point.snippets.each do |filepath, snippet|
|
16
|
+
# Note: we assume the current locale is the default one
|
17
|
+
snippet.translated_in.each do |locale|
|
18
|
+
default_locale = locale.to_sym == self.mounting_point.default_locale.to_sym
|
19
|
+
|
20
|
+
Locomotive::Mounter.with_locale(locale) do
|
21
|
+
self.write_snippet_to_fs(snippet, filepath, default_locale ? nil : locale)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
protected
|
28
|
+
|
29
|
+
# Write into the filesystem the file which stores the snippet template
|
30
|
+
# The file is localized meaning a same snippet could generate a file for each translation.
|
31
|
+
#
|
32
|
+
# @param [ Object ] snippet The snippet
|
33
|
+
# @param [ String ] filepath The path to the file
|
34
|
+
# @param [ Locale ] locale The locale, nil if default locale
|
35
|
+
#
|
36
|
+
def write_snippet_to_fs(snippet, filepath, locale)
|
37
|
+
_filepath = "#{filepath}.liquid"
|
38
|
+
_filepath.gsub!(/.liquid$/, ".#{locale}.liquid") if locale
|
39
|
+
|
40
|
+
unless snippet.template.blank?
|
41
|
+
_filepath = File.join('app', 'views', 'snippets', _filepath)
|
42
|
+
|
43
|
+
self.open_file(_filepath) do |file|
|
44
|
+
file.write(snippet.source)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module Locomotive
|
2
|
+
module Mounter
|
3
|
+
module Writer
|
4
|
+
module FileSystem
|
5
|
+
|
6
|
+
class ThemeAssetsWriter < Base
|
7
|
+
|
8
|
+
# Create the theme assets folders
|
9
|
+
#
|
10
|
+
def prepare
|
11
|
+
self.create_folder 'public'
|
12
|
+
end
|
13
|
+
|
14
|
+
# Write all the snippets into files
|
15
|
+
#
|
16
|
+
def write
|
17
|
+
self.mounting_point.theme_assets.each do |asset|
|
18
|
+
self.open_file(self.target_asset_path(asset)) do |file|
|
19
|
+
file.write(asset.content)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
protected
|
25
|
+
|
26
|
+
def target_asset_path(asset)
|
27
|
+
File.join('public', asset.folder, asset.filename)
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module Locomotive
|
2
|
+
module Mounter
|
3
|
+
module Writer
|
4
|
+
module FileSystem
|
5
|
+
|
6
|
+
class TranslationsWriter < Base
|
7
|
+
|
8
|
+
def prepare
|
9
|
+
self.create_folder 'config'
|
10
|
+
end
|
11
|
+
|
12
|
+
def write
|
13
|
+
self.open_file('config/translations.yml') do |file|
|
14
|
+
file.write self.mounting_point.translations.map(&:to_params).to_yaml
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|