-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScaffold_Script_Example.ps1
163 lines (134 loc) · 7.2 KB
/
Scaffold_Script_Example.ps1
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# Define the connection string for Azure SQL with AAD authentication
# Replace this with your actual connection string
$connectionString = "{Your_Connection_String}" # Use a safe string like AD auth
# Define user-specified variables
$projectFileName = "{csproj_file_name}.csproj" # Name of your project file (assumes .\ by default)
$namespace = "{Project_Namespace}" # Project namespace
$dbContextFile = "{Your_DbContext_Name}" # Name of your DbContext Unique Class Name
# Define default directories and paths (users are advised not to change these)
$modelsDirectory = ".\DbModels" # Models directory (default: .\DbModels)
$dbHelpersDirectory = ".\DbHelpers" # Directory for DB helpers (default: .\DbHelpers)
$extensionsDirectory = ".\Extensions" # Directory for extensions (default: .\Extensions)
$metaDataDirectory = ".\MetaDataClasses" # Directory for metadata classes (default: .\MetaDataClasses)
$interfacesDirectory = ".\Interfaces" # Directory for interfaces (default: .\Interfaces)
$concreteDirectory = ".\Concrete" # Directory for concrete classes (default: .\Concrete)
$separateOutputVirtualDirectory = "" # (optional) Can specify separated virtual file directory. eg, ".\SeparatedVdFiles"
# ---------------------------------------------------------------
# Share Library Variables (Optional)
# These variables define the paths for the designated Share library locations,
# meant to be used with 'initialShareSetupHandler' and 'shareScaffoldProtocolHandler'.
# By default, they are empty, but when correctly set, they enable the share protocol.
# ---------------------------------------------------------------
# Define Share namespace (default set to ":" to indicate disabled state)
$shareNamespace = ":"
# Determine the base directory for Share library (assumes Share project is in the same solution)
$shareBasePath = (Join-Path (Get-Item $modelsDirectory).Parent.Parent.FullName $shareNamespace)
# Define Share library paths (assuming standard protocol unless manually changed)
$shareProjectFilePath = Join-Path $shareBasePath "$shareNamespace.csproj"
$shareReadOnlyInterfacesPath = Join-Path $shareBasePath "ReadOnlyInterfaces"
$shareInterfaceExtensionsPath = Join-Path $shareBasePath "InterfaceExtensions"
$shareReadOnlyModelsPath = Join-Path $shareBasePath "ReadOnlyModels"
$shareMetadataClassesPath = Join-Path $shareBasePath "MetaDataClasses"
$shareViewDtoModelsPath = Join-Path $shareBasePath "ViewDtoModels"
$shareSharedExtensionsPath = Join-Path $shareBasePath "SharedExtensions"
$shareSharedMetadataPath = Join-Path $shareBasePath "SharedMetaData"
# Define the Flatten library variables (requires share protocol to be enabled and required variables to be set)
$flattenNamespace = ""
# Define Flatten library paths (assuming standard protocol unless manually changed)
$flattenViewDtoDirectoryPath = (Join-Path (Get-Item $modelsDirectory).Parent.Parent.FullName $flattenNamespace)
if (-not [string]::IsNullOrWhiteSpace($shareNamespace)) {
# Navigate to the project directory
Push-Location $shareBasePath
try {
# Install or update the NuGet package
Write-Host "Installing or updating Magic.Truth.Toolkit in $shareBasePath..."
dotnet add package Magic.Share.Toolkit --force
Write-Host "Successfully installed or updated Magic.Truth.Toolkit."
}
catch {
Write-Host "Error occurred while installing Magic.Truth.Toolkit: $_" -ForegroundColor Red
}
finally {
# Return to the original directory
Pop-Location
}
} else {
Write-Host "Skipping installation: flattenNamespace is null, empty, or whitespace."
}
# ---------------------------------------------------------------
# Install and update MagicEf tool
dotnet tool install --global MagicEf
dotnet tool update --global MagicEf
MagicEf --initialSetup --projectFilePath ".\$projectFileName" --namespace "$namespace" --dbContext "$dbContextFile"
# Scaffold DbContext and models
dotnet ef dbcontext scaffold $connectionString Microsoft.EntityFrameworkCore.SqlServer `
--project ".\$projectFileName" `
--context ReadOnlyDbContext `
--output-dir $modelsDirectory `
--context-dir "." `
--namespace $namespace `
--force `
--data-annotations --verbose
# Use MagicEf for scaffolding and file organization
MagicEF --scaffoldProtocol `
--concretePath $concreteDirectory `
--modelPath $modelsDirectory `
--extensionPath $extensionsDirectory `
--metadataPath $metaDataDirectory `
--interfacesPath $interfacesDirectory `
--projectFilePath ".\$projectFileName" `
--verbose
MagicEF --ambiguousIndex --directoryPath $modelsDirectory
MagicEF --removeOnConfiguring --filePath ".\ReadOnlyDbContext.cs"
MagicEF --separateVirtualProperties --directoryPath $modelsDirectory --outputPath $separateOutputVirtualDirectory
MagicEF --dbHelpers $dbHelpersDirectory --customContextFilePath ".\$dbContextFile.cs"
# ================= SHARE LIBRARY SETUP =================
# The Share Scaffold setup runs only if the shareNamespace is not the default ":"
if ($shareNamespace -ne ":") {
Write-Host "Share Scaffold is enabled. Verifying paths and initializing..."
# Check if required Share paths exist
if (!(Test-Path $shareBasePath)) {
Write-Host "ERROR: Share base path not found: $shareBasePath"
exit 1
}
if (!(Test-Path $shareProjectFilePath)) {
Write-Host "ERROR: Share project file not found: $shareProjectFilePath"
exit 1
}
# Run the initial setup handler (this will create default Share directories if missing)
MagicEF --initialShareSetupHandler --shareProjectFilePath "$shareProjectFilePath" --dbProjectFilePath ".\$projectFileName"
# Revalidate the Share directories after potential creation
$shareDirectories = @(
$shareReadOnlyInterfacesPath,
$shareInterfaceExtensionsPath,
$shareReadOnlyModelsPath,
$shareMetadataClassesPath,
$shareViewDtoModelsPath,
$shareSharedExtensionsPath,
$shareSharedMetadataPath
)
$missingDirs = $shareDirectories | Where-Object { !(Test-Path $_) }
if ($missingDirs.Count -gt 0) {
Write-Host "ERROR: The following required Share directories are missing after setup:"
$missingDirs | ForEach-Object { Write-Host $_ }
exit 1
}
# Run Share Scaffold Protocol Handler
MagicEF --shareScaffoldProtocolHandler `
--shareNamespace "$shareNamespace" `
--shareReadOnlyInterfacesPath "$shareReadOnlyInterfacesPath" `
--shareInterfaceExtensionsPath "$shareInterfaceExtensionsPath" `
--shareReadOnlyModelsPath "$shareReadOnlyModelsPath" `
--shareMetadataClassesPath "$shareMetadataClassesPath" `
--shareViewDtoModelsPath "$shareViewDtoModelsPath" `
--shareSharedExtensionsPath "$shareSharedExtensionsPath" `
--shareSharedMetadataPath "$shareSharedMetadataPath" `
--dbNamespace "$namespace" `
--dbModelsPath "$modelsDirectory" `
--dbExtensionsPath "$extensionsDirectory" `
--dbMetadataPath "$metaDataDirectory"
Write-Host "Share Scaffold completed successfully."
} else {
Write-Host "Share Scaffold is disabled. Set a valid shareNamespace to enable."
}
Write-Output "Script execution completed."