Exporting Model Diagrams and CSV Data - mmorga/archi-tools-rb GitHub Wiki

If you need to often export Diagrams and CSV data from your model, you can use rake and this Rakefile to do so.

You need to have a Ruby version 2.5 or newer and the two files: Gemfile and Rakefile.

Gemfile

gem "archimate", "~> 2.0.3"
gem "rake", "~> 12.3"

Rakefile

require "archimate"
require "rake/clean"

# The following folders are created (if necessary) when the build task is run
# to store diagrams and entities, respectively, from the ArchiMate model.
DIAGRAMS_FOLDER = "diagrams"
ENTITIES_FOLDER = "entities"

# Returns the base directory into which the diagrams and entities directories
# are created for this model. If there is more than one ArchiMate model in
# a directory, then a directory is created with the name of the model file
# (minus the .archimate extension) to keep the output from colliding.
def output_basedir(src)
  basedir = File.dirname(src)
  if Dir.glob(File.join(File.dirname(src), "*.archimate")).size > 1
    basedir = File.join(basedir, File.basename(src, ".archimate"))
    directory basedir
    file src => basedir
    CLOBBER.include basedir
  end
  basedir
end

# Creates a subdir (for entities or diagrams) under a basedir
def output_dir(basedir, subdir)
  out_dir = File.join(basedir, subdir)
  directory out_dir => basedir
  CLOBBER.include out_dir
  out_dir
end

ModelPaths = Struct.new(:diagrams_path, :entities_path)

MODEL_HASH = Hash.new do |hash, key|
  basedir = output_basedir(key)
  hash[key] = ModelPaths.new(
    output_dir(basedir, DIAGRAMS_FOLDER),
    output_dir(basedir, ENTITIES_FOLDER)
  )
end

FileList['**/*.archimate'].each do |src|
  task build: src
  model_paths = MODEL_HASH[src]
  file src => [model_paths.entities_path, model_paths.diagrams_path]
end

desc "Extracts the CSV and image data from the ArchiMate model"
task :build do |t|
  t.prerequisites.each do |src|
    puts "Reading ArchiMate model #{src}"
    model = Archimate.read(src)

    puts "Exporting entities for #{src}"
    Archimate::Export::CSVExport.new(model).to_csv(output_dir: MODEL_HASH[src].entities_path)

    puts "Exporting diagrams for #{src}"
    Archimate::Cli::Svger.new(model.diagrams, MODEL_HASH[src].diagrams_path, :name).export_svgs
  end
end

task default: :build

To install dependencies run: bundle

To run the build: rake