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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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
|
# File 'lib/maglove/commands/theme.rb', line 39
def push
info("▸ Pushing theme '#{options.theme}' to MagLoft")
theme_identifier = theme_config(:identifier)
error!("Theme '#{theme_identifier}' not found") unless theme_identifier
begin
theme = magloft_api.typeloft_themes.find_by_identifier(theme_identifier)
rescue MagLoft::ApiCaller::UnauthorizedError
error!("▸ You are not allowed to access the MagLoft API.")
end
if theme.nil?
info("▸ To create a new theme, run: maglove theme:create --theme '#{theme_identifier}'")
error!("Theme '#{theme_identifier}' was not yet created.")
end
invoke(Fonts, :compile, [], {})
invoke(Assets, :compile, [], { theme: options.theme })
info("▸ Synchronizing Metadata")
theme.base_version = theme_config(:base_version)
theme.name = theme_config(:name)
theme.description = theme_config(:description)
theme.widgets = theme_config(:widgets)
theme.fonts = theme_config(:fonts)
theme.save
theme_blocks = theme.typeloft_blocks.all
theme_blocks_map = Hash[theme_blocks.map { |block| [block.identifier, block] }]
if theme_dir.chdir("blocks").exists?
block_files = theme_dir.chdir("blocks").files("**/*.haml")
block_files.each do |block_file|
block_identifier = block_file.slug
if (block = theme_blocks_map[block_identifier])
block.name = block_file.basename.titlecase
block.contents = block_file.read
if block.changed?
info "▸ Updating Block '#{block_identifier}'"
block.save
end
else
info "▸ Creating Block '#{block_identifier}'"
theme.typeloft_blocks.create(identifier: block_identifier, name: block_file.basename.titlecase, contents: block_file.read)
end
end
end
info("▸ Synchronizing Images")
theme_images = theme.typeloft_images.all
theme_images_map = Hash[theme_images.map { |image| [image.remote_file, image] }]
hydra = Typhoeus::Hydra.new
theme_dir(root: "dist").files("images/**/*.{jpg,png,gif,svg}").each do |image_file|
remote_file = "themes/#{options.theme}/#{image_file.relative_path}"
if (existing_image = theme_images_map[remote_file])
if image_file.md5 != existing_image.md5
info("▸ Updating Image '#{remote_file}'")
existing_image.md5 = image_file.md5
hydra.queue(existing_image.queue_upload(image_file.to_s) { debug("▸ Finished updating Image '#{remote_file}'") })
existing_image.save
end
else
info("▸ Creating Image '#{remote_file}'")
new_image = theme.typeloft_images.create(remote_file: remote_file, title: image_file.basename.titlecase, md5: image_file.md5)
hydra.queue(new_image.queue_upload(image_file.to_s) { info("▸ Finished creating Image '#{remote_file}'") })
end
end
hydra.run
info("▸ Synchronizing JavaScript and Stylesheet")
theme.upload_stylesheet(theme_dir(root: "dist").file("theme.css").to_s)
theme.upload_javascript(theme_dir(root: "dist").file("theme.js").to_s)
info("▸ Synchronizing Templates")
theme_templates = theme.typeloft_templates.all
theme_templates_map = Hash[theme_templates.map { |template| [template.identifier, template] }]
templates = theme_config(:templates)
templates.each_with_index do |template_identifier, position|
template_file = theme_dir.file("templates/#{template_identifier}.haml")
next unless !template_file.nil? and template_file.exists?
if (template = theme_templates_map[template_identifier])
template.title = template_identifier.titlecase
template.contents = template_file.read
template.position = position
if template.changed?
info "▸ Updating Template '#{template_identifier}'"
template.save
end
else
info "▸ Creating Template '#{template_identifier}'"
theme.typeloft_templates.create(identifier: template_identifier, title: template_identifier.titlecase, contents: template_file.read, position: position)
end
end
if options.thumbnails
invoke(:thumbnails, [], { theme: options.theme })
hydra = Typhoeus::Hydra.new
info("▸ Synchronizing Template Thumbnails")
theme.typeloft_templates.all.each do |template|
thumbnail_file = theme_dir(root: "dist").dir("templates").file("#{template.identifier}.png")
if thumbnail_file.exists?
info("~> Uploading Thumbnail for '#{template.identifier}'")
hydra.queue(template.queue_upload_thumbnail(thumbnail_file.to_s) { info("▸ Finished uploading Thumbnail for '#{template.identifier}'") })
end
end
info("▸ Synchronizing Block Thumbnails")
theme.typeloft_blocks.all.each do |block|
thumbnail_file = theme_dir(root: "dist").dir("blocks").file("#{block.identifier}.png")
if thumbnail_file.exists?
info("~> Uploading Thumbnail for '#{block.identifier}'")
hydra.queue(block.queue_upload_thumbnail(thumbnail_file.to_s) { info("▸ Finished uploading Thumbnail for '#{block.identifier}'") })
end
end
hydra.run
end
info("▸ Successfully Pushed Theme")
end
|