Skip to content

Commit

Permalink
use System.IO.Abstraction; implement InitConfig and AddPatch
Browse files Browse the repository at this point in the history
  • Loading branch information
ormico committed Feb 29, 2016
1 parent fe8abed commit 1344dff
Showing 1 changed file with 39 additions and 2 deletions.
41 changes: 39 additions & 2 deletions src/Ormico.DbPatchManager/PatchManager.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Abstractions;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
Expand All @@ -16,14 +16,51 @@ public PatchManager(string ConfigFile)

readonly string _configFileName;

/// <summary>
/// Use System.IO.Abstraction to make testing easier.
/// </summary>
FileSystem _io = new FileSystem();

public void InitConfig(InitOptions Options)
{

var cfgWriter = new BuildConfigurationWriter(_configFileName);
cfgWriter.Write(new DatabaseBuildConfiguration()
{
DatabaseType = "Ormico.DbPatchManager.TestDatabase",
ConnectionString = "File=testDatabase.json;",
PatchFolder = "Patches",
CodeFolder = "Code"
});
}

public void AddPatch(string PatchID, PatchOptions Options = null)
{
var cfgWriter = new BuildConfigurationWriter(_configFileName);
var cfg = cfgWriter.Read();

//create unique id prefix
string prefix = string.Format("{0:yyyyMMddHHmm}-{1:0000}",
DateTime.Now,
(new Random()).Next(0, 9999));

string finalId = string.Format("{0}-{1}", prefix, PatchID);

if(!_io.Directory.Exists(finalId))
{
_io.Directory.CreateDirectory(finalId);

// when adding a patch, make it dependent on all patches
// that don't already have a dependency.
// need to guard against circular dependencies
var openPatches = cfg.GetOpenPatches();
cfg.patches.Add(new Patch(finalId, openPatches));
cfgWriter.Write(cfg);
}
else
{
// create custom exception
throw new ApplicationException(string.Format("a folder named {0} already exists", finalId));
}
}

public void Build()
Expand Down

0 comments on commit 1344dff

Please sign in to comment.