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
165
166
167
168
169
170
171
|
# File 'lib/maglove/commands/theme.rb', line 42
def push
info("▸ Pushing theme '#{theme.identifier}' to MagLoft")
error!("Theme '#{theme.identifier}' not found") unless theme.identifier
begin
typeloft_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 typeloft_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: theme.identifier })
invoke(Assets, :compress, [], { theme: theme.identifier })
info("▸ Synchronizing Metadata")
typeloft_theme.base_version = theme.base_version
typeloft_theme.name = theme.name
typeloft_theme.description = theme.description
typeloft_theme.widgets = theme.widgets
typeloft_theme.fonts = theme.fonts
typeloft_theme.save
info("▸ Synchronizing Blocks")
theme_blocks = typeloft_theme.typeloft_blocks.all
theme_blocks_map = Hash[theme_blocks.map { |block| [block.identifier, block] }]
if theme.src_dir.dir("blocks").exists?
block_files = theme.src_dir.dir("blocks").children("**/*.haml")
block_files.each do |block_file|
block_identifier = block_file.basename
if (block = theme_blocks_map[block_identifier])
block.name = block_file.basename.titlecase
block.contents = block_file.read
block.contents.length
if block.changed?
info "▸ Updating Block '#{block_identifier}'"
block.save
end
else
info "▸ Creating Block '#{block_identifier}'"
typeloft_theme.typeloft_blocks.create(identifier: block_identifier, name: block_file.basename.titlecase, contents: block_file.read)
end
end
end
info("▸ Synchronizing Images")
theme_images = typeloft_theme.typeloft_images.all
theme_images_map = Hash[theme_images.map { |image| [image.remote_file, image] }]
hydra = Typhoeus::Hydra.new
theme.dist_dir.children("images/**/*.{jpg,png,gif,svg}").each do |image_file|
remote_file = "themes/#{theme.identifier}/#{image_file.relative_path}"
image_file_md5 = Digest::MD5.hexdigest(image_file.read)
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 = typeloft_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")
typeloft_theme.upload_javascript(theme.dist_dir.file("theme.js").to_s)
info("▸ Synchronizing Stylesheet")
typeloft_theme.upload_stylesheet(theme.dist_dir.file("theme.css").to_s)
info("▸ Synchronizing Templates")
theme_templates = typeloft_theme.typeloft_templates.all
theme_templates_map = Hash[theme_templates.map { |template| [template.identifier, template] }]
templates = theme.templates
templates.each_with_index do |template_identifier, position|
template_file = theme.src_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}'"
typeloft_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: theme.identifier })
hydra = Typhoeus::Hydra.new
info("▸ Synchronizing Template Thumbnails")
typeloft_theme.typeloft_templates.all.each do |template|
thumbnail_file = theme.dist_dir.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")
typeloft_theme.typeloft_blocks.all.each do |block|
thumbnail_file = theme.dist_dir.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
|