Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@dfch
Last active August 28, 2021 22:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dfch/caa11ec02d03595974427955ab0ff2fb to your computer and use it in GitHub Desktop.
Save dfch/caa11ec02d03595974427955ab0ff2fb to your computer and use it in GitHub Desktop.
Simplifying Sparx Enterprise Architect Development with C# Interactive (CSI)
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.Contracts;
using System.IO;
using System.Threading;
namespace biz.dfch.CS.EA.ProductModeler.Tests.Repository
{
public class RepositorySingleton : IDisposable
{
private static readonly object _syncRoot = new object();
private static readonly List<Action> _cleanupActions = new List<Action>();
private static int _instances;
private static string _repoPathAndFileName;
private static readonly Lazy<global::EA.Repository> _repository = new Lazy<global::EA.Repository>(() =>
{
var repository = new global::EA.Repository();
Contract.Assert(null != repository);
_cleanupActions.Add(() => repository.Exit());
var isOpen = repository.OpenFile(_repoPathAndFileName);
Contract.Assert(isOpen);
return repository;
});
// ReSharper disable once InconsistentlySynchronizedField
public global::EA.Repository Repository => _repository.Value;
public RepositorySingleton(string path)
{
Contract.Requires(File.Exists(path));
Interlocked.Increment(ref _instances);
if (_repository.IsValueCreated) return;
lock (_syncRoot)
{
if (_repository.IsValueCreated) return;
_repoPathAndFileName = path;
var result = _repository.Value;
}
}
private static void ReleaseUnmanagedResources()
{
if (0 != Interlocked.Decrement(ref _instances)) return;
_cleanupActions.Reverse();
foreach (var action in _cleanupActions)
{
try
{
action.Invoke();
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
}
}
public void Dispose()
{
ReleaseUnmanagedResources();
GC.SuppressFinalize(this);
}
~RepositorySingleton()
{
ReleaseUnmanagedResources();
}
}
}
/**
* Copyright 2018 d-fens GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#r "C:\src\biz.dfch.CS.EA.ProductModeler\src\biz.dfch.CS.EA.ProductModeler.Tests\bin\Debug\biz.dfch.CS.EA.ProductModeler.Tests.dll"
#r "C:\Program Files (x86)\Sparx Systems\EA\Interop.EA.dll"
using System;
using System.Diagnostics.Contracts;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using biz.dfch.CS.EA.ProductModeler.Tests.AddIn;
var path = @"C:\src\biz.dfch.CS.EA.ProductModeler\src\biz.dfch.PS.EA.ProductModeler.Scripts\Resources\Test.eapx";
var i = new biz.dfch.CS.EA.ProductModeler.Tests.Repository.RepositorySingleton(path);
// show EA UI, so you can interact with it manually
i.Repository.ShowWindow(1);
// get number of models in current EA repository
i.Repository.Models.OfType<EA.Package>().Count();
// get all top level models
i.Repository.Models.OfType<EA.Package>().Select(e => e.Name);
// clean up
i.Dispose();
/**
* Copyright 2018 d-fens GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment