From aef42a773666470cca577671d24a5eb09ffb2248 Mon Sep 17 00:00:00 2001
From: Benedikt Heinrichs <heinrichs@itc.rwth-aachen.de>
Date: Tue, 7 Jul 2020 10:29:59 +0200
Subject: [PATCH] New: Implement a general graph deployer

---
 README.md                                     | 32 ++-------
 ...nizationDeployer.sln => GraphDeployer.sln} |  2 +-
 .../App.config                                |  0
 .../GraphDeployer.csproj}                     |  6 +-
 .../Program.cs                                | 69 ++++++++++++-------
 src/GraphDeployer/Properties/AssemblyInfo.cs  | 16 +++++
 .../getRepository.ps1}                        | 28 +++++---
 .../packages.config                           |  0
 .../Properties/AssemblyInfo.cs                | 36 ----------
 9 files changed, 87 insertions(+), 102 deletions(-)
 rename src/{OrganizationDeployer.sln => GraphDeployer.sln} (85%)
 rename src/{OrganizationDeployer => GraphDeployer}/App.config (100%)
 rename src/{OrganizationDeployer/OrganizationDeployer.csproj => GraphDeployer/GraphDeployer.csproj} (97%)
 rename src/{OrganizationDeployer => GraphDeployer}/Program.cs (60%)
 create mode 100644 src/GraphDeployer/Properties/AssemblyInfo.cs
 rename src/{OrganizationDeployer/getOrganizations.ps1 => GraphDeployer/getRepository.ps1} (52%)
 rename src/{OrganizationDeployer => GraphDeployer}/packages.config (100%)
 delete mode 100644 src/OrganizationDeployer/Properties/AssemblyInfo.cs

diff --git a/README.md b/README.md
index 549e949..e9fa630 100644
--- a/README.md
+++ b/README.md
@@ -1,31 +1,9 @@
-## C# Template
+# GraphDeployer
 
-This template includes:
+This repository handles the deployment of every static graph which gets included into the CoScInE environment.
 
-* Automatic building using cake
-* Automatic testing with NUnit
-* Automatic linting with Resharper
-* Automatic documentation publishing using Gitlab CI / CD and a self written script which puts the docs in the docs folder to the wiki
-* Automatic releases using semantic-release ([ESLint Code Convention](docs/ESLintConvention)), cake and Gitlab CI / CD
+## Usage
 
-## What you need to do
+For every deployable graph repository, an entry is expected in the configuration path: `coscine/local/graphs/`.
 
-Place you C# project solution file in .src/.
-Make sure Create directory for solution is unticked.
-
-![alt text](docs/images/create_project.png "Create a new Project")
-
-Delete unused docs and update this README.
-
-Add [NUnit](docs/nunit.md) tests to your solution.
-
-## Building
-
-Build this project by running either the build.ps1 or the build<span></span>.sh script.
-The project will be build and tested.
-
-### Links 
-
-*  [Commit convention](docs/ESLintConvention.md)
-*  [Everything possible with markup](docs/testdoc.md)
-*  [Adding NUnit tests](docs/nunit.md)
\ No newline at end of file
+Every repository MUST include the key `coscine/local/graphs/{graphname}/repositoryurl` for specifying the url (e.g. `git.rwth-aachen.de/coscine/applicationprofiles.git`) and CAN include the key `coscine/local/graphs/{graphname}/branch` which specifies the state of the repository being imported (e.g. `master`).
diff --git a/src/OrganizationDeployer.sln b/src/GraphDeployer.sln
similarity index 85%
rename from src/OrganizationDeployer.sln
rename to src/GraphDeployer.sln
index e1461fc..ecebfcc 100644
--- a/src/OrganizationDeployer.sln
+++ b/src/GraphDeployer.sln
@@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00
 # Visual Studio Version 16
 VisualStudioVersion = 16.0.29102.190
 MinimumVisualStudioVersion = 10.0.40219.1
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OrganizationDeployer", "OrganizationDeployer\OrganizationDeployer.csproj", "{341EB0C4-6882-4281-9310-4213AEAFB65A}"
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GraphDeployer", "GraphDeployer\GraphDeployer.csproj", "{341EB0C4-6882-4281-9310-4213AEAFB65A}"
 EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
diff --git a/src/OrganizationDeployer/App.config b/src/GraphDeployer/App.config
similarity index 100%
rename from src/OrganizationDeployer/App.config
rename to src/GraphDeployer/App.config
diff --git a/src/OrganizationDeployer/OrganizationDeployer.csproj b/src/GraphDeployer/GraphDeployer.csproj
similarity index 97%
rename from src/OrganizationDeployer/OrganizationDeployer.csproj
rename to src/GraphDeployer/GraphDeployer.csproj
index 2bd95aa..c244b5d 100644
--- a/src/OrganizationDeployer/OrganizationDeployer.csproj
+++ b/src/GraphDeployer/GraphDeployer.csproj
@@ -8,8 +8,8 @@
     <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
     <ProjectGuid>{341EB0C4-6882-4281-9310-4213AEAFB65A}</ProjectGuid>
     <OutputType>Exe</OutputType>
-    <RootNamespace>Coscine.OrganizationDeployer</RootNamespace>
-    <AssemblyName>Coscine.OrganizationDeployer</AssemblyName>
+    <RootNamespace>Coscine.GraphDeployer</RootNamespace>
+    <AssemblyName>Coscine.GraphDeployer</AssemblyName>
     <TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
     <FileAlignment>512</FileAlignment>
     <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
@@ -99,7 +99,7 @@
   </ItemGroup>
   <ItemGroup>
     <None Include="App.config" />
-    <None Include="getOrganizations.ps1">
+    <None Include="getRepository.ps1">
       <CopyToOutputDirectory>Always</CopyToOutputDirectory>
     </None>
     <None Include="packages.config" />
diff --git a/src/OrganizationDeployer/Program.cs b/src/GraphDeployer/Program.cs
similarity index 60%
rename from src/OrganizationDeployer/Program.cs
rename to src/GraphDeployer/Program.cs
index c31ef28..22bd49e 100644
--- a/src/OrganizationDeployer/Program.cs
+++ b/src/GraphDeployer/Program.cs
@@ -8,17 +8,12 @@ using System.Linq;
 using VDS.RDF;
 using VDS.RDF.Storage;
 
-namespace Coscine.OrganizationDeployer
+namespace Coscine.GraphDeployer
 {
     public class Program
     {
         public static void Main(string[] args)
         {
-            ExecuteCommand(
-                "powershell.exe",
-                $@"& '{ Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "getOrganizations.ps1") }'"
-            );
-
             var configuration = new ConsulConfiguration();
 
             var virtuosoServer = configuration.GetString("coscine/local/virtuoso/additional/url");
@@ -34,31 +29,57 @@ namespace Coscine.OrganizationDeployer
                 "coscine/local/virtuoso/isql",
                 "C:/Programs/Virtuoso/bin/isql.exe"
             );
-            var queries = new List<string>();
-            foreach (var file in GetFiles(folder).Where((file) => file.Contains(".ttl")))
-            {
-                var fileInfo = new FileInfo(file);
-                var graph = new Graph();
-                graph.LoadFromFile(file);
-                var graphName = graph.BaseUri.ToString();
 
-                if (_util.HasGraph(graphName))
-                {
-                    Console.WriteLine($"Clearing {graphName}");
-                    _util.ClearGraph(graphName);
-                }
+            var graphKeys = configuration.Keys("coscine/local/graphs/");
 
-                queries.Add($"ld_dir('{fileInfo.DirectoryName.Substring(2).Replace("\\", "/")}', '{fileInfo.Name}', '{graphName}');");
-            }
-            queries.Add($"rdf_loader_run ();");
-            queries.Add($"DELETE from DB.DBA.load_list where 1=1;");
+            graphKeys = graphKeys.Where((entry) => entry.Contains("repositoryurl"))
+                                    .Select((entry) => entry.Substring(0, entry.LastIndexOf("/") + 1))
+                                    .ToArray();
 
-            foreach (var query in queries)
+            foreach (var graphKey in graphKeys)
             {
+                // Example: {gitHost}/{groupName}/{repository}.git
+                var repositoryUrl = configuration.GetString(graphKey + "repositoryurl");
+
+                Console.WriteLine("Working with: " + repositoryUrl);
+
+                var branch = configuration.GetString(graphKey + "branch", "master");
+
+                var repositoryFolder = repositoryUrl.Substring(0, repositoryUrl.LastIndexOf("."));
+                repositoryFolder = repositoryFolder.Substring(repositoryFolder.LastIndexOf("/") + 1);
+
                 ExecuteCommand(
                     "powershell.exe",
-                    $"\"\\\"{query}\\\" | {virtuosoISQLLocation}\""
+                    $@"& '{ Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "getRepository.ps1") }' "
+                    + $"'{repositoryUrl}' '{branch}' '{repositoryFolder}'"
                 );
+
+                var queries = new List<string>();
+                foreach (var file in GetFiles(folder).Where((file) => file.Contains(".ttl")))
+                {
+                    var fileInfo = new FileInfo(file);
+                    var graph = new Graph();
+                    graph.LoadFromFile(file);
+                    var graphName = graph.BaseUri.ToString();
+
+                    if (_util.HasGraph(graphName))
+                    {
+                        Console.WriteLine($"Clearing {graphName}");
+                        _util.ClearGraph(graphName);
+                    }
+
+                    queries.Add($"ld_dir('{fileInfo.DirectoryName.Substring(2).Replace("\\", "/")}', '{fileInfo.Name}', '{graphName}');");
+                }
+                queries.Add($"rdf_loader_run ();");
+                queries.Add($"DELETE from DB.DBA.load_list where 1=1;");
+
+                foreach (var query in queries)
+                {
+                    ExecuteCommand(
+                        "powershell.exe",
+                        $"\"\\\"{query}\\\" | {virtuosoISQLLocation}\""
+                    );
+                }
             }
 
             Console.WriteLine("Done");
diff --git a/src/GraphDeployer/Properties/AssemblyInfo.cs b/src/GraphDeployer/Properties/AssemblyInfo.cs
new file mode 100644
index 0000000..2ef56ed
--- /dev/null
+++ b/src/GraphDeployer/Properties/AssemblyInfo.cs
@@ -0,0 +1,16 @@
+//------------------------------------------------------------------------------
+// <auto-generated>
+//     This code was generated by Cake.
+// </auto-generated>
+//------------------------------------------------------------------------------
+using System.Reflection;
+
+[assembly: AssemblyTitle("GraphDeployer")]
+[assembly: AssemblyDescription("GraphDeployer is a part of the CoScInE group.")]
+[assembly: AssemblyCompany("IT Center, RWTH Aachen University")]
+[assembly: AssemblyProduct("GraphDeployer")]
+[assembly: AssemblyVersion("1.1.1")]
+[assembly: AssemblyFileVersion("1.1.1")]
+[assembly: AssemblyInformationalVersion("1.1.1")]
+[assembly: AssemblyCopyright("2020 IT Center, RWTH Aachen University")]
+
diff --git a/src/OrganizationDeployer/getOrganizations.ps1 b/src/GraphDeployer/getRepository.ps1
similarity index 52%
rename from src/OrganizationDeployer/getOrganizations.ps1
rename to src/GraphDeployer/getRepository.ps1
index 34e0498..e022d59 100644
--- a/src/OrganizationDeployer/getOrganizations.ps1
+++ b/src/GraphDeployer/getRepository.ps1
@@ -1,4 +1,13 @@
-$extractPath = "C:\Programs\Consul\"
+param (
+	[Parameter(Mandatory=$true, Position=0)]
+	[string] $repositoryUrl,
+	[Parameter(Mandatory=$true, Position=1)]
+	[string] $branch,
+	[Parameter(Mandatory=$true, Position=2)]
+	[string] $repositoryFolder
+)
+
+$extractPath = "C:\Programs\Consul\"
 $fileName = "Consul.exe"
 $fullPath = ($extractPath + $fileName)
 
@@ -6,19 +15,16 @@ $gitlab_token = & $fullPath kv get "coscine/global/gitlabtoken"
 
 Push-Location $PSScriptRoot
 
-$organizationsPath = 'organizations'
 # Twice remove, because of: https://stackoverflow.com/questions/7909167/how-to-quietly-remove-a-directory-with-content-in-powershell#comment10316056_7909195
-If(Test-Path $organizationsPath) { Remove-Item -LiteralPath $organizationsPath -Force -Recurse }
-If(Test-Path $organizationsPath) { Remove-Item -LiteralPath $organizationsPath -Force -Recurse }
+If(Test-Path $repositoryFolder) { Remove-Item -LiteralPath $repositoryFolder -Force -Recurse }
+If(Test-Path $repositoryFolder) { Remove-Item -LiteralPath $repositoryFolder -Force -Recurse }
 
-git clone https://gitlab-ci-token:$gitlab_token@git.rwth-aachen.de/coscine/organizations.git
+git clone https://gitlab-ci-token:$gitlab_token@$repositoryUrl
 
-cd organizations
+cd $repositoryFolder
 
-$pagebranch = & $fullPath kv get 'coscine/local/organizationdeployer/branch'
-
-if ($pagebranch -And $pagebranch -ne 'master' ) {
-	git checkout $pagebranch
+if ($branch -And $branch -ne 'master' ) {
+	git checkout $branch
 	git pull
 }
 
@@ -32,6 +38,6 @@ If(Test-Path $path) { Remove-Item -LiteralPath $path -Force -Recurse }
 
 New-Item -ItemType Directory -Force -Path $path
 
-Copy-Item './organizations/*' $path -Recurse;
+Copy-Item "./$repositoryFolder/*" $path -Recurse;
 
 Pop-Location
\ No newline at end of file
diff --git a/src/OrganizationDeployer/packages.config b/src/GraphDeployer/packages.config
similarity index 100%
rename from src/OrganizationDeployer/packages.config
rename to src/GraphDeployer/packages.config
diff --git a/src/OrganizationDeployer/Properties/AssemblyInfo.cs b/src/OrganizationDeployer/Properties/AssemblyInfo.cs
deleted file mode 100644
index 2c08776..0000000
--- a/src/OrganizationDeployer/Properties/AssemblyInfo.cs
+++ /dev/null
@@ -1,36 +0,0 @@
-using System.Reflection;
-using System.Runtime.CompilerServices;
-using System.Runtime.InteropServices;
-
-// General Information about an assembly is controlled through the following
-// set of attributes. Change these attribute values to modify the information
-// associated with an assembly.
-[assembly: AssemblyTitle("OrganizationDeployer")]
-[assembly: AssemblyDescription("OrganizationDeployer is a part of the CoScInE group.")]
-[assembly: AssemblyConfiguration("")]
-[assembly: AssemblyCompany("IT Center, RWTH Aachen University")]
-[assembly: AssemblyProduct("OrganizationDeployer")]
-[assembly: AssemblyCopyright("2020 IT Center, RWTH Aachen University")]
-[assembly: AssemblyTrademark("")]
-[assembly: AssemblyCulture("")]
-
-// Setting ComVisible to false makes the types in this assembly not visible
-// to COM components.  If you need to access a type in this assembly from
-// COM, set the ComVisible attribute to true on that type.
-[assembly: ComVisible(false)]
-
-// The following GUID is for the ID of the typelib if this project is exposed to COM
-[assembly: Guid("341eb0c4-6882-4281-9310-4213aeafb65a")]
-
-// Version information for an assembly consists of the following four values:
-//
-//      Major Version
-//      Minor Version
-//      Build Number
-//      Revision
-//
-// You can specify all the values or you can default the Build and Revision Numbers
-// by using the '*' as shown below:
-// [assembly: AssemblyVersion("1.0.*")]
-[assembly: AssemblyVersion("1.0.0.0")]
-[assembly: AssemblyFileVersion("1.0.0.0")]
-- 
GitLab