Skip to content

Commit

Permalink
Merge pull request #45 from nick5454/develop
Browse files Browse the repository at this point in the history
Added List migrations functionality
  • Loading branch information
mvput committed Aug 11, 2020
2 parents 33912b7 + cdd0c79 commit 76d2118
Show file tree
Hide file tree
Showing 7 changed files with 323 additions and 42 deletions.
2 changes: 1 addition & 1 deletion recipe.cake
@@ -1,4 +1,4 @@
#load nuget:?package=Cake.Recipe&version=1.0.0
#load nuget:?package=Cake.Recipe&version=1.1.2

Environment.SetVariableNames();

Expand Down
@@ -0,0 +1,17 @@
using Cake.DotNetCoreEf.Migration;

namespace Cake.DotNetCoreEf.Tests.Fixtures.Migration
{
internal sealed class DotNetCoreEfMigrationListerFixture : DotNetCoreEfFixture<DotNetCoreEfMigrationListerSettings>
{
public string Project { get; set; }

public string Arguments { get; set; }

protected override void RunTool()
{
var tool = new DotNetCoreEfMigrationLister(FileSystem, Environment, ProcessRunner, Tools);
tool.Script(Project, Arguments, Settings);
}
}
}
@@ -0,0 +1,102 @@
using Cake.DotNetCoreEf.Tests.Fixtures.Migration;
using Cake.Testing;
using Xunit;

namespace Cake.DotNetCoreEf.Tests.Unit.Migration
{
public sealed class DotNetCoreEfMigrationScriptListerTests
{

[Fact]
public void Should_Throw_If_Settings_Are_Null()
{
// Given
var fixture = new DotNetCoreEfMigrationListerFixture();
fixture.Project = "./src/";
fixture.Settings = null;
fixture.GivenDefaultToolDoNotExist();

// When
var result = Record.Exception(() => fixture.Run());

// Then
AssertExtensions.IsArgumentNullException(result, "settings");
}

[Fact]
public void Should_Throw_If_Process_Was_Not_Started()
{
// Given
var fixture = new DotNetCoreEfMigrationListerFixture();
fixture.Project = "./src/";
fixture.GivenProcessCannotStart();

// When
var result = Record.Exception(() => fixture.Run());

// Then
AssertExtensions.IsCakeException(result, ".NET Core CLI: Process was not started.");
}

// Removed. Process returns json
//[Fact]
//public void Should_Throw_If_Process_Has_A_Non_Zero_Exit_Code()
//{
// // Given
// var fixture = new DotNetCoreEfMigrationScriptListerFixture();
// fixture.Project = "./src/";
// fixture.GivenProcessExitsWithCode(1);

// // When
// var result = Record.Exception(() => fixture.Run());

// // Then
// AssertExtensions.IsCakeException(result, ".NET Core CLI: Process returned an error (exit code 1).");
//}

[Fact]
public void Should_Add_Mandatory_Arguments()
{
// Given
var fixture = new DotNetCoreEfMigrationListerFixture();

// When
var result = fixture.Run();

// Then
Assert.Equal("ef migrations list --no-build --json", result.Args);
}

[Fact]
public void Should_Add_Path_Arguments()
{
// Given
var fixture = new DotNetCoreEfMigrationListerFixture();
fixture.Project = "./tools/tool/";
fixture.Arguments = "--args=\"value\"";
// When
var result = fixture.Run();

// Then
Assert.Equal("ef migrations list --no-build --json --args=\"value\"", result.Args);
Assert.Equal("/Working/tools/tool", result.Process.WorkingDirectory.FullPath);
}

[Fact]
public void Should_Add_Additional_Settings()
{
// Given
var fixture = new DotNetCoreEfMigrationListerFixture();
fixture.Settings.StartupProject = "./src/MyMvcProject";
fixture.Settings.Project = "./src/MyDataProject";
fixture.Settings.Configuration = "release";
fixture.Settings.PrefixOutput = true;
fixture.Settings.NoBuild = true;
// When
var result = fixture.Run();

// Then
Assert.Equal("ef migrations list --project \"./src/MyDataProject\" --startup-project \"./src/MyMvcProject\" --prefix-output --no-build --json", result.Args);
}
}
}
82 changes: 41 additions & 41 deletions src/Cake.DotNetCoreEf.sln
@@ -1,41 +1,41 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.28010.2041
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Cake.DotNetCoreEf", "Cake.DotNetCoreEf\Cake.DotNetCoreEf.csproj", "{EF4D86EB-7401-4E38-A02B-AB4884DCB168}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "meta", "meta", "{77CAC347-B714-4AD6-9932-328C59DC9B2B}"
ProjectSection(SolutionItems) = preProject
..\.appveyor.yml = ..\.appveyor.yml
..\build.ps1 = ..\build.ps1
..\nuspec\Cake.DotNetCoreEf.nuspec = ..\nuspec\Cake.DotNetCoreEf.nuspec
..\LICENSE = ..\LICENSE
..\README.md = ..\README.md
..\recipe.cake = ..\recipe.cake
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Cake.DotNetCoreEf.UnitTests", "Cake.DotNetCoreEf.UnitTests\Cake.DotNetCoreEf.UnitTests.csproj", "{8C502FAA-A13C-4051-9716-1C41A4596BED}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{EF4D86EB-7401-4E38-A02B-AB4884DCB168}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EF4D86EB-7401-4E38-A02B-AB4884DCB168}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EF4D86EB-7401-4E38-A02B-AB4884DCB168}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EF4D86EB-7401-4E38-A02B-AB4884DCB168}.Release|Any CPU.Build.0 = Release|Any CPU
{8C502FAA-A13C-4051-9716-1C41A4596BED}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8C502FAA-A13C-4051-9716-1C41A4596BED}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8C502FAA-A13C-4051-9716-1C41A4596BED}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8C502FAA-A13C-4051-9716-1C41A4596BED}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {732C4874-8EF7-42F1-82E0-DFF23CD78A46}
EndGlobalSection
EndGlobal

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.28010.2041
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Cake.DotNetCoreEf", "Cake.DotNetCoreEf\Cake.DotNetCoreEf.csproj", "{EF4D86EB-7401-4E38-A02B-AB4884DCB168}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "meta", "meta", "{77CAC347-B714-4AD6-9932-328C59DC9B2B}"
ProjectSection(SolutionItems) = preProject
..\.appveyor.yml = ..\.appveyor.yml
..\build.ps1 = ..\build.ps1
..\nuspec\Cake.DotNetCoreEf.nuspec = ..\nuspec\Cake.DotNetCoreEf.nuspec
..\LICENSE = ..\LICENSE
..\README.md = ..\README.md
..\recipe.cake = ..\recipe.cake
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Cake.DotNetCoreEf.UnitTests", "Cake.DotNetCoreEf.UnitTests\Cake.DotNetCoreEf.UnitTests.csproj", "{8C502FAA-A13C-4051-9716-1C41A4596BED}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{EF4D86EB-7401-4E38-A02B-AB4884DCB168}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EF4D86EB-7401-4E38-A02B-AB4884DCB168}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EF4D86EB-7401-4E38-A02B-AB4884DCB168}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EF4D86EB-7401-4E38-A02B-AB4884DCB168}.Release|Any CPU.Build.0 = Release|Any CPU
{8C502FAA-A13C-4051-9716-1C41A4596BED}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8C502FAA-A13C-4051-9716-1C41A4596BED}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8C502FAA-A13C-4051-9716-1C41A4596BED}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8C502FAA-A13C-4051-9716-1C41A4596BED}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {732C4874-8EF7-42F1-82E0-DFF23CD78A46}
EndGlobalSection
EndGlobal
38 changes: 38 additions & 0 deletions src/Cake.DotNetCoreEf/DotNetCoreEfAliases.cs
Expand Up @@ -403,5 +403,43 @@ public static void DotNetCoreEfMigrationScript(this ICakeContext context, string
var runner = new DotNetCoreEfMigrationScripter(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools);
runner.Script(project, arguments, settings);
}

/// <summary>
/// List all migrations
/// </summary>
/// <param name="context">The context.</param>
/// <param name="project">The project path.</param>
/// <param name="arguments">The arguments.</param>
/// <param name="settings">The settings.</param>
/// <example>
/// <code>
/// var settings = new DotNetCoreEfMigrationScriptListerSettings
/// {
/// Context = SchoolContext,
/// StartupProject = "./src/MvcProject",
/// NoBuild = true
/// };
///
/// DotNetCoreEfMigrationScript("./src/Project", "--args", settings);
/// </code>
/// </example>
[CakeMethodAlias]
[CakeAliasCategory("MigrationScript")]
[CakeNamespaceImport("Cake.DotNetCoreEf.Migration")]
public static string DotNetCoreEfMigrationList(this ICakeContext context, string project, ProcessArgumentBuilder arguments, DotNetCoreEfMigrationListerSettings settings)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}

if (settings == null)
{
settings = new DotNetCoreEfMigrationListerSettings();
}

var runner = new DotNetCoreEfMigrationLister(context.FileSystem, context.Environment, context.ProcessRunner, context.Tools);
return runner.Script(project, arguments, settings);
}
}
}
99 changes: 99 additions & 0 deletions src/Cake.DotNetCoreEf/Migration/DotNetCoreEfMigrationLister.cs
@@ -0,0 +1,99 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Cake.Core;
using Cake.Core.IO;
using Cake.Core.Tooling;

namespace Cake.DotNetCoreEf.Migration
{
/// <summary>
/// Support for script migrations using .NET Core cli tooling
/// </summary>
public class DotNetCoreEfMigrationLister : DotNetCoreEfTool<DotNetCoreEfMigrationListerSettings>
{
private readonly ICakeEnvironment _environment;

/// <summary>
/// Initializes a new instance of the <see cref="DotNetCoreEfMigrationScriptLister" />.
/// </summary>
/// <param name="fileSystem">The file system.</param>
/// <param name="environment">The environment.</param>
/// <param name="processRunner">The process runner.</param>
/// <param name="tools">The tool locator.</param>
public DotNetCoreEfMigrationLister(
IFileSystem fileSystem,
ICakeEnvironment environment,
IProcessRunner processRunner,
IToolLocator tools)
: base(fileSystem, environment, processRunner, tools)
{
this._environment = environment;
}

/// <summary>
/// Script migrations for the project using the specified path with arguments and settings.
/// </summary>
/// <param name="project">The target project path.</param>
/// <param name="arguments">The arguments.</param>
/// <param name="settings">The settings.</param>
public string Script(string project, ProcessArgumentBuilder arguments, DotNetCoreEfMigrationListerSettings settings)
{
if (settings == null)
{
throw new ArgumentNullException(nameof(settings));
}

var result = RunProcess(settings, GetArguments(project, arguments, settings)).GetStandardOutput();

return result?.FirstOrDefault();
}

private ProcessArgumentBuilder GetArguments(string project, ProcessArgumentBuilder arguments, DotNetCoreEfMigrationListerSettings settings)
{
ProcessArgumentBuilder builder = new ProcessArgumentBuilder();
ProcessArgumentBuilder builderArguments = CreateArgumentBuilder(settings);

builder.Append("ef");
builder.Append("migrations");
builder.Append("list");

settings.SetProject(project);

if (!string.IsNullOrWhiteSpace(settings.Project))
{
builder.Append("--project");
builder.AppendQuoted(settings.Project);
}

if (!string.IsNullOrWhiteSpace(settings.StartupProject))
{
builder.Append("--startup-project");
builder.AppendQuoted(settings.StartupProject);
}

if (settings.PrefixOutput)
{
builder.Append("--prefix-output");
}

if (settings.NoBuild)
{
builder.Append("--no-build");
}

// return json output
builder.Append("--json");

// Arguments
if (!arguments.IsNullOrEmpty())
{
arguments.CopyTo(builder);
}

return builder;
}
}
}
@@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Cake.DotNetCoreEf.Migration
{
/// <summary>
/// Contains settings used by <see cref="DotNetCoreEfMigrationScriptLister"/>.
/// </summary>
public class DotNetCoreEfMigrationListerSettings : DotNetCoreEfSettings
{
/// <summary>
/// Prefix characters in output.
/// </summary>
public bool PrefixOutput { get; set; } = false;

/// <summary>
/// Gets or sets a value indicating whether to not to build the project before publishing.
/// This makes build faster, but requires build to be done before publish is executed.
/// </summary>
public bool NoBuild { get; set; } = true;
}
}

0 comments on commit 76d2118

Please sign in to comment.