-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathYamlSystemModelRepository.cs
More file actions
94 lines (83 loc) · 4.22 KB
/
Copy pathYamlSystemModelRepository.cs
File metadata and controls
94 lines (83 loc) · 4.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.Extensions.Logging;
using OctopusProjectBuilder.Model;
using OctopusProjectBuilder.YamlReader.Helpers;
using OctopusProjectBuilder.YamlReader.Model;
namespace OctopusProjectBuilder.YamlReader
{
public class YamlSystemModelRepository : ISystemModelRepository
{
private readonly ILogger<YamlSystemModelRepository> _logger;
private readonly YamlSystemModelReader _reader = new YamlSystemModelReader();
private readonly YamlSystemModelWriter _writer = new YamlSystemModelWriter();
public YamlSystemModelRepository(ILoggerFactory loggerFactory)
{
_logger = loggerFactory.CreateLogger<YamlSystemModelRepository>();
}
public SystemModel Load(string modelDirectory)
{
var model = new YamlOctopusModel();
var files = FindFiles(modelDirectory);
foreach (var subModel in files.SelectMany(LoadModels))
model.MergeIn(subModel);
return model.ApplyTemplates().BuildWith(new SystemModelBuilder()).Build();
}
private YamlOctopusModel[] LoadModels(string path)
{
_logger.LogInformation($"Loading: {Path.GetFileName(path)}");
return ReadFile(path);
}
public void CleanupConfig(string modelDirectory)
{
foreach (var path in FindFiles(modelDirectory))
{
_logger.LogInformation($"Cleaning up: {Path.GetFileName(path)}");
WriteFile(path + ".new", ReadFile(path).ToArray());
File.Move(path, path + ".old");
File.Move(path + ".new", path);
}
}
private YamlOctopusModel[] ReadFile(string file)
{
using (var stream = new FileStream(file, FileMode.Open))
return _reader.Read(stream);
}
public void Save(SystemModel model, string modelDirectory)
{
Directory.CreateDirectory(modelDirectory);
foreach (var splitModel in model.SplitModel().Select(YamlOctopusModel.FromModel))
SaveModel(splitModel, GetModelPath(splitModel, modelDirectory));
}
private void SaveModel(YamlOctopusModel model, string path)
{
_logger.LogInformation($"Saving: {Path.GetFileName(path)}");
WriteFile(path, model);
}
private void WriteFile(string file, params YamlOctopusModel[] models)
{
using (var stream = new FileStream(file, FileMode.Create))
_writer.Write(stream, models);
}
private string GetModelPath(YamlOctopusModel splitModel, string modelDirectory)
{
var name = splitModel.MachinePolicies.EnsureNotNull().Select(x => $"MachinePolicy_{x.Name.SanitiseNameIfNeeded()}.yml")
.Concat(splitModel.Environments.EnsureNotNull().Select(x => $"Environment_{x.Name.SanitiseNameIfNeeded()}.yml"))
.Concat(splitModel.ProjectGroups.EnsureNotNull().Select(x => $"ProjectGroup_{x.Name.SanitiseNameIfNeeded()}.yml"))
.Concat(splitModel.Projects.EnsureNotNull().Select(x => $"Project_{x.Name.SanitiseNameIfNeeded()}.yml"))
.Concat(splitModel.Lifecycles.EnsureNotNull().Select(x => $"Lifecycle_{x.Name.SanitiseNameIfNeeded()}.yml"))
.Concat(splitModel.LibraryVariableSets.EnsureNotNull().Select(x => $"LibraryVariableSet_{x.Name.SanitiseNameIfNeeded()}.yml"))
.Concat(splitModel.UserRoles.EnsureNotNull().Select(x => $"UserRole_{x.Name.SanitiseNameIfNeeded()}.yml"))
.Concat(splitModel.Teams.EnsureNotNull().Select(x => $"Team_{x.Name.SanitiseNameIfNeeded()}.yml"))
.Concat(splitModel.Tenants.EnsureNotNull().Select(x => $"Tenant_{x.Name.SanitiseNameIfNeeded()}.yml"))
.Concat(splitModel.TagSets.EnsureNotNull().Select(x => $"TagSet_{x.Name.SanitiseNameIfNeeded()}.yml"))
.Single();
return Path.Combine(modelDirectory, name);
}
private static IEnumerable<string> FindFiles(string modelDirectory)
{
return Directory.EnumerateFiles(modelDirectory, "*.yml", SearchOption.AllDirectories);
}
}
}