Skip to content

Exporting Model Diagrams and CSV Data

Mark Morga edited this page Jan 23, 2019 · 2 revisions

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"

MODEL_HASH = {}
DIAGRAM_DIR_HASH = {}
ENTITIES_DIR_HASH = {}

def output_dir(basedir, subdir)
  out_dir = File.join(basedir, subdir)
  directory out_dir
  task build: out_dir
  CLOBBER.include out_dir
  out_dir
end

# 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
    task build: basedir
    CLOBBER.include(basedir)
  end
  basedir
end

desc "Extracts the CSV and image data from the ArchiMate model"
task :build => [:readmodel, :export_csv, :export_diagrams]

FileList['**/*.archimate'].each do |src|
  basedir = output_basedir(src)
  DIAGRAM_DIR_HASH[src] = output_dir(basedir, DIAGRAMS_FOLDER)
  ENTITIES_DIR_HASH[src] = output_dir(basedir, ENTITIES_FOLDER)
  task readmodel: src
  task export_csv: src
  task export_csv: ENTITIES_DIR_HASH[src]
  task export_diagrams: src
  task export_diagrams: DIAGRAM_DIR_HASH[src]
end

task :readmodel do |t|
  t.prerequisites.each do |src|
    puts "Reading ArchiMate model #{src}"
    MODEL_HASH[src] = Archimate.read(src)
  end
end

task :export_csv do |t|
  t.prerequisites.each do |src|
    if MODEL_HASH.include?(src)
      puts "Exporting entities for #{src}"
      Archimate::Export::CSVExport.new(MODEL_HASH[src]).to_csv(output_dir: ENTITIES_DIR_HASH[src])
    end
  end
end

task :export_diagrams do |t|
  t.prerequisites.each do |src|
    if MODEL_HASH.include?(src)
      puts "Exporting diagrams for #{src}"
      Archimate::Cli::Svger.new(MODEL_HASH[src].diagrams, DIAGRAM_DIR_HASH[src], :name).export_svgs
    end
  end
end

task default: :build

To install dependencies run: bundle

To run the build: rake

Clone this wiki locally