Skip to content

Commit

Permalink
Add an output directory option for migrations
Browse files Browse the repository at this point in the history
closes #24
  • Loading branch information
mrahhal committed Aug 10, 2016
1 parent 3a2b6a4 commit 29d091f
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 11 deletions.
4 changes: 3 additions & 1 deletion build/Build.proj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
<Version>$(Major).$(Minor).$(Patch)</Version>
<QualityWithDash Condition="'$(Quality)' != ''">-$(Quality)</QualityWithDash>
<InformationalVersion>$(Version)$(QualityWithDash)</InformationalVersion>
<DevPre>dev-</DevPre>
<DevPre Condition="'$(CI)' == 'true'">ci-</DevPre>

<BuildType Condition="'$(BuildType)' == ''">Dev</BuildType>
<BuildType Condition="'$(APPVEYOR_REPO_TAG)' == 'true'">Release</BuildType>
Expand All @@ -28,7 +30,7 @@
</CreateStamp>
<PropertyGroup>
<InfoVersion Condition="'$(BuildType)' == 'Release'">$(InformationalVersion)</InfoVersion>
<InfoVersion Condition="'$(BuildType)' != 'Release'">$(InformationalVersion)-$(Stamp)</InfoVersion>
<InfoVersion Condition="'$(BuildType)' != 'Release'">$(InformationalVersion)-$(DevPre)$(Stamp)</InfoVersion>
<Find>{
"version": ".+"</Find>
<Replace>{
Expand Down
18 changes: 11 additions & 7 deletions src/Migrator.EF6.Tools/Executor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,18 @@ public Executor()
_startupAssembly = Assembly.Load(new AssemblyName(_targetName));
_rootNamespace = project.Name;
_types = _startupAssembly.GetTypes();
Directory.CreateDirectory(MigrationsDir);
}

private string MigrationsDir => Path.Combine(_projectDir, "Migrations");
private string GetMigrationsDir(string outputDir)
=> Path.Combine(_projectDir, outputDir ?? "Migrations");

private string Combine(params string[] paths) => Path.Combine(paths);

public void EnableMigrations()
public void EnableMigrations(string outputDir)
{
var path = Combine(MigrationsDir, "Configuration.cs");
var migrationsDir = GetMigrationsDir(outputDir);
Directory.CreateDirectory(migrationsDir);
var path = Combine(migrationsDir, "Configuration.cs");

var assembly = Assembly.GetExecutingAssembly();
var fileContent = default(string);
Expand All @@ -54,16 +56,18 @@ public void EnableMigrations()
File.WriteAllText(path, fileContent);
}

public void AddMigration(string name, bool ignoreChanges)
public void AddMigration(string name, string outputDir, bool ignoreChanges)
{
var migrationsDir = GetMigrationsDir(outputDir);
Directory.CreateDirectory(migrationsDir);
var config = FindDbMigrationsConfiguration();

// Scaffold migration.
var scaffolder = new MigrationScaffolder(config);
var migration = scaffolder.Scaffold(name, ignoreChanges);

// Write the user code file.
File.WriteAllText(Combine(MigrationsDir, migration.MigrationId + ".cs"), migration.UserCode);
File.WriteAllText(Combine(migrationsDir, migration.MigrationId + ".cs"), migration.UserCode);

// Write needed resource values directly inside the designer code file.
// Apparently, aspnet and resource files don't play well (or more specifically,
Expand All @@ -74,7 +78,7 @@ public void AddMigration(string name, bool ignoreChanges)
.Replace("private readonly ResourceManager Resources = new ResourceManager(typeof(InitialCreate));", "");

// Write the designer code file.
File.WriteAllText(Path.Combine(MigrationsDir, migration.MigrationId + ".Designer.cs"), designerCode);
File.WriteAllText(Path.Combine(migrationsDir, migration.MigrationId + ".Designer.cs"), designerCode);
}

public void ScriptMigration(string from, string to, string output)
Expand Down
12 changes: 10 additions & 2 deletions src/Migrator.EF6.Tools/MigrationsCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@ public static void Configure(CommandLineApplication command)
enable.Description = "Enable migrations";
enable.HelpOption();

var outputDir = enable.Option(
"-o|--output-dir <path>",
"The directory (and sub-namespace) to use. If omitted, \"Migrations\" is used. Relative paths are relative the directory in which the command is executed.");

enable.OnExecute(() =>
{
new Executor().EnableMigrations();
new Executor().EnableMigrations(outputDir.Value());
});
});

Expand All @@ -42,14 +46,18 @@ public static void Configure(CommandLineApplication command)
"Ignore changes and start with an empty migration",
CommandOptionType.NoValue);

var outputDir = add.Option(
"-o|--output-dir <path>",
"The directory (and sub-namespace) to use. If omitted, \"Migrations\" is used. Relative paths are relative the directory in which the command is executed.");

add.OnExecute(() =>
{
if (string.IsNullOrEmpty(name.Value))
{
return 1;
}

new Executor().AddMigration(name.Value, ignoreChanges.HasValue());
new Executor().AddMigration(name.Value, outputDir.Value(), ignoreChanges.HasValue());
return 0;
});
});
Expand Down
2 changes: 1 addition & 1 deletion src/Migrator.EF6.Tools/project.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
{
"version": "1.0.0-*",
"description": "A .NetCore CLI tool to enable EF6 migrations in an Asp.Net Core app",
"authors": [ "Mohammad Rahhal" ],
Expand Down

0 comments on commit 29d091f

Please sign in to comment.