Class: Jekyll::Archimate::ArchimateHook

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll/archimate/archimate_hook.rb

Overview

Configuration variables: clean: clean destination directories before rendering layout: layout to use for the archimate navigator

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site, archimate_file) ⇒ ArchimateHook

Returns a new instance of ArchimateHook.



182
183
184
185
186
187
188
# File 'lib/jekyll/archimate/archimate_hook.rb', line 182

def initialize(site, archimate_file)
  @site = site
  @archimate_file = archimate_file
  @clean_generated_dirs = @site.config.fetch('clean', false)
  @model = ArchimateCache.load_model(archimate_file.sub(site.source, ""))
  @site.data["archimate_model"] = @model
end

Instance Attribute Details

#archimate_fileObject (readonly)

Returns the value of attribute archimate_file.



180
181
182
# File 'lib/jekyll/archimate/archimate_hook.rb', line 180

def archimate_file
  @archimate_file
end

#clean_generated_dirsObject (readonly)

Returns the value of attribute clean_generated_dirs.



177
178
179
# File 'lib/jekyll/archimate/archimate_hook.rb', line 177

def clean_generated_dirs
  @clean_generated_dirs
end

#modelObject (readonly)

Returns the value of attribute model.



179
180
181
# File 'lib/jekyll/archimate/archimate_hook.rb', line 179

def model
  @model
end

#siteObject (readonly)

Returns the value of attribute site.



178
179
180
# File 'lib/jekyll/archimate/archimate_hook.rb', line 178

def site
  @site
end

Instance Method Details

#catalog_element(el) ⇒ Object



239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/jekyll/archimate/archimate_hook.rb', line 239

def catalog_element(el)
  [:id, :name, :documentation, :type, :properties].each_with_object({}) do |sym, hash|
    hash[sym] = el.send(sym)
  end
  {
    "id" => el.id,
    "name" => el.name.to_s,
    "documentation" => el.documentation&.to_s,
    "type" => el.type,
    "properties" => el.properties.each_with_object({}) do |property, hash|
      hash[property.key.to_s] = property.value.to_s
    end
  }
end

#export_catalogsObject



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/jekyll/archimate/archimate_hook.rb', line 220

def export_catalogs
  data = File.join(site.source, "_data")
  Dir.mkdir(data) unless Dir.exist?(data)
  archimate = File.join(data, "archimate")
  Dir.mkdir(archimate) unless Dir.exist?(archimate)
  catalog = File.join(archimate, "catalog.yml")
  File.open(catalog, "wb") do |file|
    file.write(
      model
        .elements
        .chunk { |el| el.type }
        .each_with_object({}) { |(type, els), hash|
          hash[type] = els.map { |el| catalog_element(el) }
        }
        .to_yaml
    )
  end
end

#export_svgsObject



213
214
215
216
217
218
# File 'lib/jekyll/archimate/archimate_hook.rb', line 213

def export_svgs
  return unless svgs_need_export?
  model.diagrams.each do |diagram|
    SvgFile.new(File.join(svg_dest_dir, "#{diagram.id}.svg")).write(diagram)
  end
end

#export_unified_jsonObject



196
197
198
199
200
201
# File 'lib/jekyll/archimate/archimate_hook.rb', line 196

def export_unified_json
  dest_file = File.join(File.dirname(archimate_file), 'index.json')
  dest_mtime = File.exist?(dest_file) && File.mtime(dest_file)
  return unless !dest_mtime || File.mtime(archimate_file) > dest_mtime
  JsonFile.new(dest_file).write(UnifiedModel.new(model))
end

#generateObject



190
191
192
193
194
# File 'lib/jekyll/archimate/archimate_hook.rb', line 190

def generate
  export_svgs
  export_unified_json
  export_catalogs
end

#svg_dest_dirObject



203
204
205
# File 'lib/jekyll/archimate/archimate_hook.rb', line 203

def svg_dest_dir
  @svg_dest_dir ||= File.join(File.dirname(archimate_file), 'svg')
end

#svgs_need_export?Boolean

Returns:

  • (Boolean)


207
208
209
210
211
# File 'lib/jekyll/archimate/archimate_hook.rb', line 207

def svgs_need_export?
  Dir.mkdir(svg_dest_dir) unless Dir.exist?(svg_dest_dir)
  last_svg_mtime = Dir.glob(File.join(svg_dest_dir, "*.svg")).map { |svg_file| File.mtime(svg_file) }.max
  !last_svg_mtime || File.mtime(archimate_file) > last_svg_mtime
end