Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create new build configuration writer #1

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
src/.idea/
src/.vs/
temp/
src2/
Expand All @@ -13,3 +14,4 @@ bin
obj

src/Ormico.DbPatchManager.CLI/deb/
src/Ormico.DbPatchManager.CLI/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
Expand All @@ -17,6 +17,7 @@
<Version>2.1.2</Version>
<PackageIcon>dbpatch-manager-profile.png</PackageIcon>
<Description>Command Line Interface for Database Change managment designed for multi-dev/multi-branch.</Description>
<LangVersion>10</LangVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
23 changes: 22 additions & 1 deletion src/Ormico.DbPatchManager.CLI/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using CommandLine;
using Newtonsoft.Json;
using Ormico.DbPatchManager.Logic;
using Ormico.DbPatchManager.CLI.CommandLineOptions;

Expand All @@ -12,14 +13,24 @@ static int Main(string[] args)
int rc = 0;
try
{
rc = CommandLine.Parser.Default.ParseArguments(args, typeof(InitCmdLineOptions), typeof(AddPatchCmdLineOptions), typeof(BuildCmdLineOptions))
rc = CommandLine.Parser.Default.ParseArguments(args, typeof(InitCmdLineOptions),
typeof(AddPatchCmdLineOptions), typeof(BuildCmdLineOptions))
.MapResult(
(InitCmdLineOptions o) => InitBuildSettings(o),
(AddPatchCmdLineOptions o) => AddPatch(o),
(BuildCmdLineOptions o) => Build(o),
err => 1
);
}
catch (JsonException jsonException)
{
Console.WriteLine("{0}", jsonException.Message);
Console.WriteLine("{0}", jsonException.StackTrace);
if (jsonException.InnerException != null)
{
Console.WriteLine("{0}", jsonException.InnerException.Message);
}
}
catch (Exception ex)
{
Console.WriteLine($"{ex.Message}");
Expand Down Expand Up @@ -54,19 +65,29 @@ static int InitBuildSettings(InitCmdLineOptions options)
static int AddPatch(AddPatchCmdLineOptions options)
{
int rc = 0;
var startTime = DateTimeOffset.Now;
Console.WriteLine("{0:O} - Database Add Patch Started", startTime);
PatchManager manager = new PatchManager(_patchFileName, _patchLocalFileName);
//todo: pass all settings
manager.AddPatch(options.Name, new PatchOptions()
{
});
var endTime = DateTimeOffset.Now;
Console.WriteLine("{0:O} - Database Add Patch Completed", endTime);
Console.WriteLine("{0:g} - Add Patch Time", endTime.Subtract(startTime));
return rc;
}

static int Build(BuildCmdLineOptions options)
{
int rc = 0;
var startTime = DateTimeOffset.Now;
Console.WriteLine("{0:O} - Database Build Started", startTime);
PatchManager manager = new PatchManager(_patchFileName, _patchLocalFileName);
manager.Build();
var endTime = DateTimeOffset.Now;
Console.WriteLine("{0:O} - Database Build Completed", endTime);
Console.WriteLine("{0:g} - Build Time", endTime.Subtract(startTime));
return rc;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<RepositoryUrl>https://github.com/ormico/dbpatchmanager</RepositoryUrl>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<Authors>Zack Moore</Authors>
Expand All @@ -14,6 +13,8 @@
<Copyright>Copyright (c) 2020 Zack Moore</Copyright>
<PackageProjectUrl>https://dbpatch.dev/</PackageProjectUrl>
<Version>2.1.2</Version>
<LangVersion>10</LangVersion>
<TargetFrameworks>net6.0;netstandard2.1</TargetFrameworks>
</PropertyGroup>

<ItemGroup>
Expand All @@ -27,4 +28,8 @@
</None>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
</ItemGroup>

</Project>
70 changes: 70 additions & 0 deletions src/Ormico.DbPatchManager.Common/PatchFileModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace Ormico.DbPatchManager.Common
{
public partial class PatchFile
{
[JsonProperty("DatabaseType")] public string DatabaseType { get; set; }

[JsonProperty("ConnectionString")] public string ConnectionString { get; set; }

[JsonProperty("CodeFolder")] public string CodeFolder { get; set; }

[JsonProperty("CodeFiles")] public List<string> CodeFiles { get; set; }

[JsonProperty("PatchFolder")] public string PatchFolder { get; set; }

[JsonProperty("Options")] public Dictionary<string, string> Options { get; set; }

[JsonProperty("patches")] public List<PatchFromFile> Patches { get; set; }

public PatchFile()
{
Patches = new List<PatchFromFile>();
Options = new Dictionary<string, string>();
CodeFiles = new List<string>();
}
}

public partial class PatchFromFile
{
[JsonProperty("id")] public string Id { get; set; }

[JsonProperty("dependsOn")] public List<string> DependsOn { get; set; }

public PatchFromFile()
{
DependsOn = new List<string>();
}
}

public partial class PatchFile
{
public static PatchFile FromJson(string json) =>
JsonConvert.DeserializeObject<PatchFile>(json, PatchFileConverter.Settings);
}

public static class PatchFileSerializer
{
public static string ToJson(this PatchFile self) =>
JsonConvert.SerializeObject(self, PatchFileConverter.Settings);
}

internal static class PatchFileConverter
{
public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
{
MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
DateParseHandling = DateParseHandling.None,
Converters =
{
new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.AssumeUniversal }
},
Formatting = Formatting.Indented
};
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net6.0</TargetFramework>

<IsPackable>false</IsPackable>

<LangVersion>10</LangVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
96 changes: 57 additions & 39 deletions src/Ormico.DbPatchManager.Logic/BuildConfigurationWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@

namespace Ormico.DbPatchManager.Logic
{
/// <summary>
/// Readn and Write DatabaseBuildConfiguration to storage.
/// </summary>
public class BuildConfigurationWriter
public class BuildConfigurationWriter : IBuildConfigurationWriter
{
public BuildConfigurationWriter(string filePath, string localFilePath)
{
Expand Down Expand Up @@ -46,14 +43,17 @@ public BuildConfigurationWriter(string filePath, string localFilePath)
public DatabaseBuildConfiguration Read()
{
DatabaseBuildConfiguration rc = null;
if(_io.File.Exists(_filePath))
if (_io.File.Exists(_filePath))
{
//rc = JsonConvert.DeserializeObject<DatabaseBuildConfiguration>(_io.File.ReadAllText(_filePath), _jsonSettings);
var o = (Newtonsoft.Json.Linq.JObject)Newtonsoft.Json.Linq.JToken.Parse(_io.File.ReadAllText(_filePath));
var o = (Newtonsoft.Json.Linq.JObject)Newtonsoft.Json.Linq.JToken.Parse(
_io.File.ReadAllText(_filePath));

if (_io.File.Exists(_localFilePath))
{
var localO = (Newtonsoft.Json.Linq.JObject)Newtonsoft.Json.Linq.JToken.Parse(_io.File.ReadAllText(_localFilePath));
var localO =
(Newtonsoft.Json.Linq.JObject)Newtonsoft.Json.Linq.JToken.Parse(
_io.File.ReadAllText(_localFilePath));
o.Merge(localO, new JsonMergeSettings()
{
MergeArrayHandling = MergeArrayHandling.Union
Expand All @@ -76,37 +76,39 @@ public DatabaseBuildConfiguration Read()
rc.patches = new List<Patch>();
}
else
{
{
var patches = (from p in o["patches"]
select new Patch()
{
Id = (string)p["id"]
}).ToList();
select new Patch()
{
Id = (string)p["id"]
}).ToList();
// populate DependsOn
foreach(var p in patches)
foreach (var p in patches)
{
var cur = from x in o["patches"]
from d in x["dependsOn"]
from a in patches
where (string)x["id"] == p.Id &&
a.Id == (string)d
select a;
from d in x["dependsOn"]
from a in patches
where (string)x["id"] == p.Id &&
a.Id == (string)d
select a;
p.DependsOn = cur.Distinct(new PatchComparer()).ToList();
//todo: double check this query
var children = from x in o["patches"]
from d in x["dependsOn"]
from a in patches
where (string)d == p.Id && (string)x["id"] == a.Id
select a;
from d in x["dependsOn"]
from a in patches
where (string)d == p.Id && (string)x["id"] == a.Id
select a;
p.Children = children.Distinct(new PatchComparer()).ToList();
}

rc.patches = patches.ToList();
}
}
else
{
throw new ApplicationException("Configuration file does not exist. Call init first.");
}

return rc;
}

Expand All @@ -117,7 +119,9 @@ public void Write(DatabaseBuildConfiguration buildConfiguration)
//todo: if local file exists, don't write values to patches.json if value exists in patches.local.json
if (_io.File.Exists(_localFilePath))
{
var localO = (Newtonsoft.Json.Linq.JObject)Newtonsoft.Json.Linq.JToken.Parse(_io.File.ReadAllText(_localFilePath));
var localO =
(Newtonsoft.Json.Linq.JObject)Newtonsoft.Json.Linq.JToken.Parse(
_io.File.ReadAllText(_localFilePath));
data = new JObject();

//todo: find a way to do this that isn't manual. can you loop over all values in buildConfiguration?
Expand All @@ -136,8 +140,9 @@ public void Write(DatabaseBuildConfiguration buildConfiguration)
data["CodeFolder"] = buildConfiguration.CodeFolder;
}

if (localO["CodeFiles"] == null && buildConfiguration.CodeFiles != null)
if (localO["CodeFiles"] == null || localO["CodeFiles"].HasValues == false)
{
buildConfiguration.CodeFiles = buildConfiguration.CodeFiles ?? new List<string>();
data["CodeFiles"] = JArray.FromObject(buildConfiguration.CodeFiles);
}

Expand All @@ -146,20 +151,31 @@ public void Write(DatabaseBuildConfiguration buildConfiguration)
data["PatchFolder"] = buildConfiguration.PatchFolder;
}

if (localO["Options"] == null && buildConfiguration.Options != null)
try
{
if (localO["Options"] == null || localO["Options"].HasValues == false)
{
buildConfiguration.Options = buildConfiguration.Options ?? new Dictionary<string, string>();
data["Options"] = JObject.FromObject(buildConfiguration.Options);
}
}
catch (JsonException e)
{
data["Options"] = JArray.FromObject(buildConfiguration.Options);
Console.WriteLine(e.Message);
data["Options"] = JObject.FromObject(new Dictionary<string, string>());
}

if (localO["patches"] == null && buildConfiguration.patches != null)
{
data["patches"] = JArray.FromObject(from p in buildConfiguration.patches
select new
{
id = p.Id,
dependsOn = p.DependsOn != null ? (from d in p.DependsOn
select d.Id) : null
});
select new
{
id = p.Id,
dependsOn = p.DependsOn != null
? (from d in p.DependsOn
select d.Id)
: null
});
}
}
else
Expand All @@ -174,16 +190,18 @@ public void Write(DatabaseBuildConfiguration buildConfiguration)
PatchFolder = buildConfiguration.PatchFolder,
Options = buildConfiguration.Options,
patches = from p in buildConfiguration.patches
select new
{
id = p.Id,
dependsOn = p.DependsOn != null?(from d in p.DependsOn.Distinct(new PatchComparer())
select d.Id):null
}
select new
{
id = p.Id,
dependsOn = p.DependsOn != null
? (from d in p.DependsOn.Distinct(new PatchComparer())
select d.Id)
: null
}
});
}

_io.File.WriteAllText(_filePath, data.ToString());
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public DatabaseBuildConfiguration()
{
//defaults
PatchFolder = "Patches";
Options = new Dictionary<string, string>();
CodeFolder = "Code";
patches = new List<Patch>();
CodeFiles = new List<string>()
Expand Down
Loading