-
Notifications
You must be signed in to change notification settings - Fork 19
/
FluentMigrator.psm1
61 lines (52 loc) · 1.78 KB
/
FluentMigrator.psm1
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
function Add-FluentMigration
{
[CmdletBinding(DefaultParameterSetName = 'Name')]
param (
[parameter(Position = 0,
Mandatory = $true)]
[string] $Name,
[string] $ProjectName,
[switch] $AddTimeStampToClassName)
$timestamp = (Get-Date -Format yyyyMMddHHmmss)
$class_name_timestamp = (Get-Date -Format yyyyMMdd_HHmmss)
if ($ProjectName) {
$project = Get-Project $ProjectName
if ($project -is [array])
{
throw "More than one project '$ProjectName' was found. Please specify the full name of the one to use."
}
}
else {
$project = Get-Project
}
# if the $name parameter contains a string '{T}', then replace it with the timestamp but with a underscore between the
# yyyyMMdd part and the HHmmss part
if ($AddTimeStampToClassName) {
$name = [System.String]::Replace($name, "{T}", $class_name_timestamp)
}
$namespace = $project.Properties.Item("DefaultNamespace").Value.ToString() + ".Migrations"
$projectPath = [System.IO.Path]::GetDirectoryName($project.FullName)
$migrationsPath = [System.IO.Path]::Combine($projectPath, "Migrations")
$outputPath = [System.IO.Path]::Combine($migrationsPath, "$timestamp" + "_$name.cs")
if (-not (Test-Path $migrationsPath))
{
[System.IO.Directory]::CreateDirectory($migrationsPath)
}
"using FluentMigrator;
namespace $namespace
{
[Migration($timestamp)]
public class $name : Migration
{
public override void Up()
{
}
public override void Down()
{
}
}
}" | Out-File -Encoding "UTF8" -Force $outputPath
$project.ProjectItems.AddFromFile($outputPath)
$project.Save($null)
}
Export-ModuleMember @( 'Add-FluentMigration' )