Class: MagLove::Commands::Theme

Inherits:
Base
  • Object
show all
Defined in:
lib/maglove/commands/theme.rb

Instance Method Summary collapse

Methods inherited from Base

#initialize

Methods included from Workspace

#gem_dir, #theme_base_dir, #theme_base_file, #theme_config, #theme_dir, #theme_file, #workspace_dir, #workspace_file

Methods included from Helper::LogHelper

#debug, #error, #error!, #info, #logger

Constructor Details

This class inherits a constructor from MagLove::Commands::Base

Instance Method Details

#createObject



169
170
171
172
173
174
175
176
177
178
# File 'lib/maglove/commands/theme.rb', line 169

def create
  info("▸ Creating new theme '#{options.theme}'")

  error!("Missing yaml config 'name'") unless theme_config(:name)
  error!("Missing yaml config 'base_version'") unless theme_config(:base_version)
  theme = magloft_api.typeloft_themes.find_by_identifier(options.theme)
  error!("This theme already exists") unless theme.nil?
  magloft_api.typeloft_themes.create(identifier: options.theme, name: theme_config(:name), description: theme_config(:description), base_version: theme_config(:base_version))
  info("▸ Theme successfully created!")
end

#deleteObject



183
184
185
186
187
188
189
# File 'lib/maglove/commands/theme.rb', line 183

def delete
  info("▸ Deleting theme '#{options.theme}'")
  theme = magloft_api.typeloft_themes.find_by_identifier(options.theme)
  error!("Could not find a theme with identifier '#{options.theme}'") if theme.nil?
  theme.destroy
  info("▸ Theme successfully deleted!")
end

#listObject



193
194
195
196
197
198
# File 'lib/maglove/commands/theme.rb', line 193

def list
  info("▸ Listing themes")
  magloft_api.typeloft_themes.all.each do |theme|
    info("~> #{theme.identifier} (NAME: #{theme.name}, ACTIVE: #{theme.active ? 'YES' : 'NO'})")
  end
end

#pushObject



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")

  # validate theme
  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 asset compilation
  invoke(Fonts, :compile, [], {})
  invoke(Assets, :compile, [], { theme: options.theme })

  # update 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

  # upload blocks
  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

  # upload images
  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

  # upload css/js
  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)

  # upload templates
  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

  # update thumbnails
  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

#serverObject



7
8
9
10
11
12
13
# File 'lib/maglove/commands/theme.rb', line 7

def server
  invoke(Assets, :compile, [], { theme: options.theme })
  invoke(Fonts, :compile, [], {})
  info("▸ starting server for theme '#{options.theme}' on '127.0.0.1:#{options.port}'")
  require 'maglove/server'
  MagLove::Server.start(options.port, options.theme, theme_config(:templates))
end

#thumbnailsObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/maglove/commands/theme.rb', line 17

def thumbnails
  require "powersnap"
  invoke(Fonts, :compile, [], {})
  invoke(Assets, :compile, [], theme: options.theme)

  info("▸ Generating Template Thumbnails")
  output_dir = theme_dir(root: "dist").chdir("templates")
  powersnap = Powersnap.new(output_dir.files("*.html").map(&:url))
  powersnap.generate(dir: output_dir.to_s, width: 768, height: 1024, pattern: "{basename}.png", zoom: 1.0, page: false)

  info("▸ Generating Block Thumbnails")
  output_dir = theme_dir(root: "dist").chdir("blocks")
  if output_dir.exists?
    powersnap = Powersnap.new(output_dir.files("*.html").map(&:url))
    powersnap.generate(dir: output_dir.to_s, width: 512, height: 200, pattern: "{basename}.png", zoom: 1.0, page: true)
  end
end