123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407 |
- $ErrorActionPreference = 'Stop'
- $InitialDatabase = '0'
- function Add-EFProvider
- {
- [CmdletBinding(PositionalBinding = $false)]
- param(
- [parameter(Position = 0, Mandatory = $true)]
- $Project,
- [parameter(Position = 1, Mandatory = $true)]
- [string] $InvariantName,
- [parameter(Position = 2, Mandatory = $true)]
- [string] $TypeName)
- $configPath = GetConfigPath $Project
- if (!$configPath)
- {
- return
- }
- [xml] $configXml = Get-Content $configPath
- $providers = $configXml.configuration.entityFramework.providers
- $providers.provider |
- where invariantName -eq $InvariantName |
- %{ $providers.RemoveChild($_) | Out-Null }
- $provider = $providers.AppendChild($configXml.CreateElement('provider'))
- $provider.SetAttribute('invariantName', $InvariantName)
- $provider.SetAttribute('type', $TypeName)
- $configXml.Save($configPath)
- }
- function Add-EFDefaultConnectionFactory
- {
- [CmdletBinding(PositionalBinding = $false)]
- param(
- [parameter(Position = 0, Mandatory = $true)]
- $Project,
- [parameter(Position = 1, Mandatory = $true)]
- [string] $TypeName,
- [string[]] $ConstructorArguments)
- $configPath = GetConfigPath $Project
- if (!$configPath)
- {
- return
- }
- [xml] $configXml = Get-Content $configPath
- $entityFramework = $configXml.configuration.entityFramework
- $defaultConnectionFactory = $entityFramework.defaultConnectionFactory
- if ($defaultConnectionFactory)
- {
- $entityFramework.RemoveChild($defaultConnectionFactory) | Out-Null
- }
- $defaultConnectionFactory = $entityFramework.AppendChild($configXml.CreateElement('defaultConnectionFactory'))
- $defaultConnectionFactory.SetAttribute('type', $TypeName)
- if ($ConstructorArguments)
- {
- $parameters = $defaultConnectionFactory.AppendChild($configXml.CreateElement('parameters'))
- foreach ($constructorArgument in $ConstructorArguments)
- {
- $parameter = $parameters.AppendChild($configXml.CreateElement('parameter'))
- $parameter.SetAttribute('value', $constructorArgument)
- }
- }
- $configXml.Save($configPath)
- }
- function Enable-Migrations
- {
- [CmdletBinding(DefaultParameterSetName = 'ConnectionStringName', PositionalBinding = $false)]
- param(
- [string] $ContextTypeName,
- [alias('Auto')]
- [switch] $EnableAutomaticMigrations,
- [string] $MigrationsDirectory,
- [string] $ProjectName,
- [string] $StartUpProjectName,
- [string] $ContextProjectName,
- [parameter(ParameterSetName = 'ConnectionStringName')]
- [string] $ConnectionStringName,
- [parameter(ParameterSetName = 'ConnectionStringAndProviderName', Mandatory = $true)]
- [string] $ConnectionString,
- [parameter(ParameterSetName = 'ConnectionStringAndProviderName', Mandatory = $true)]
- [string] $ConnectionProviderName,
- [switch] $Force,
- [string] $ContextAssemblyName,
- [string] $AppDomainBaseDirectory)
- WarnIfOtherEFs 'Enable-Migrations'
- $project = GetProject $ProjectName
- $startupProject = GetStartupProject $StartUpProjectName $project
- if (!$ContextAssemblyName -and $ContextProjectName)
- {
- $contextProject = Get-Project $ContextProjectName
- $ContextAssemblyName = GetProperty $contextProject.Properties 'AssemblyName'
- }
- $params = 'migrations', 'enable', '--json'
- if ($ContextTypeName)
- {
- $params += '--context', $ContextTypeName
- }
- if ($ContextAssemblyName)
- {
- $params += '--context-assembly', $ContextAssemblyName
- }
- if ($EnableAutomaticMigrations)
- {
- $params += '--auto'
- }
- if ($MigrationsDirectory)
- {
- $params += '--migrations-dir', $MigrationsDirectory
- }
- $params += GetParams $ConnectionStringName $ConnectionString $ConnectionProviderName
- if ($Force)
- {
- $params += '--force'
- }
-
- $result = (EF6 $project $startupProject $AppDomainBaseDirectory $params) -join "`n" | ConvertFrom-Json
- $project.ProjectItems.AddFromFile($result.migrationsConfiguration) | Out-Null
- $DTE.ItemOperations.OpenFile($result.migrationsConfiguration) | Out-Null
- ShowConsole
- if ($result.migration)
- {
- $project.ProjectItems.AddFromFile($result.migration) | Out-Null
- $resourcesProperties = $project.ProjectItems.AddFromFile($result.migrationResources).Properties
- $project.ProjectItems.AddFromFile($result.migrationDesigner) | Out-Null
- }
- }
- function Add-Migration
- {
- [CmdletBinding(DefaultParameterSetName = 'ConnectionStringName', PositionalBinding = $false)]
- param(
- [parameter(Position = 0, Mandatory = $true)]
- [string] $Name,
- [switch] $Force,
- [string] $ProjectName,
- [string] $StartUpProjectName,
- [string] $ConfigurationTypeName,
- [parameter(ParameterSetName = 'ConnectionStringName')]
- [string] $ConnectionStringName,
- [parameter(ParameterSetName = 'ConnectionStringAndProviderName', Mandatory = $true)]
- [string] $ConnectionString,
- [parameter(ParameterSetName = 'ConnectionStringAndProviderName', Mandatory = $true)]
- [string] $ConnectionProviderName,
- [switch] $IgnoreChanges,
- [string] $AppDomainBaseDirectory)
- WarnIfOtherEFs 'Add-Migration'
- $project = GetProject $ProjectName
- $startupProject = GetStartupProject $StartUpProjectName $project
- $params = 'migrations', 'add', $Name, '--json'
- if ($Force)
- {
- $params += '--force'
- }
- if ($ConfigurationTypeName)
- {
- $params += '--migrations-config', $ConfigurationTypeName
- }
- if ($IgnoreChanges)
- {
- $params += '--ignore-changes'
- }
- $params += GetParams $ConnectionStringName $ConnectionString $ConnectionProviderName
-
- $result = (EF6 $project $startupProject $AppDomainBaseDirectory $params) -join "`n" | ConvertFrom-Json
- $project.ProjectItems.AddFromFile($result.migration) | Out-Null
- $DTE.ItemOperations.OpenFile($result.migration) | Out-Null
- $resourcesProperties = $project.ProjectItems.AddFromFile($result.migrationResources).Properties
- $project.ProjectItems.AddFromFile($result.migrationDesigner) | Out-Null
- }
- function Update-Database
- {
- [CmdletBinding(DefaultParameterSetName = 'ConnectionStringName', PositionalBinding = $false)]
- param(
- [string] $SourceMigration,
- [string] $TargetMigration,
- [switch] $Script,
- [switch] $Force,
- [string] $ProjectName,
- [string] $StartUpProjectName,
- [string] $ConfigurationTypeName,
- [parameter(ParameterSetName = 'ConnectionStringName')]
- [string] $ConnectionStringName,
- [parameter(ParameterSetName = 'ConnectionStringAndProviderName', Mandatory = $true)]
- [string] $ConnectionString,
- [parameter(ParameterSetName = 'ConnectionStringAndProviderName', Mandatory = $true)]
- [string] $ConnectionProviderName,
- [string] $AppDomainBaseDirectory)
- WarnIfOtherEFs 'Update-Database'
- $project = GetProject $ProjectName
- $startupProject = GetStartupProject $StartUpProjectName $project
- $params = 'database', 'update'
- if ($SourceMigration)
- {
- $params += '--source', $SourceMigration
- }
- if ($TargetMigration)
- {
- $params += '--target', $TargetMigration
- }
- if ($Script)
- {
- $params += '--script'
- }
- if ($Force)
- {
- $params += '--force'
- }
- if ($ConfigurationTypeName)
- {
- $params += '--migrations-config', $ConfigurationTypeName
- }
- $params += GetParams $ConnectionStringName $ConnectionString $ConnectionProviderName
- $result = (EF6 $project $startupProject $AppDomainBaseDirectory $params) -join "`n"
- if ($result)
- {
- try
- {
- $window = $DTE.ItemOperations.NewFile('General\Sql File')
- $textDocument = $window.Document.Object('TextDocument')
- $editPoint = $textDocument.StartPoint.CreateEditPoint()
- $editPoint.Insert($result)
- }
- catch
- {
- $intermediatePath = GetIntermediatePath $project
- if (![IO.Path]::IsPathRooted($intermediatePath))
- {
- $projectDir = GetProperty $project.Properties 'FullPath'
- $intermediatePath = Join-Path $projectDir $intermediatePath -Resolve | Convert-Path
- }
- $fileName = [IO.Path]::ChangeExtension([IO.Path]::GetRandomFileName(), '.sql')
- $sqlFile = Join-Path $intermediatePath $fileName
- [IO.File]::WriteAllText($sqlFile, $result)
- $DTE.ItemOperations.OpenFile($sqlFile) | Out-Null
- }
- ShowConsole
- }
- }
- function Get-Migrations
- {
- [CmdletBinding(DefaultParameterSetName = 'ConnectionStringName', PositionalBinding = $false)]
- param(
- [string] $ProjectName,
- [string] $StartUpProjectName,
- [string] $ConfigurationTypeName,
- [parameter(ParameterSetName = 'ConnectionStringName')]
- [string] $ConnectionStringName,
- [parameter(ParameterSetName = 'ConnectionStringAndProviderName', Mandatory = $true)]
- [string] $ConnectionString,
- [parameter(ParameterSetName = 'ConnectionStringAndProviderName', Mandatory = $true)]
- [string] $ConnectionProviderName,
- [string] $AppDomainBaseDirectory)
- WarnIfOtherEFs 'Get-Migrations'
- $project = GetProject $ProjectName
- $startupProject = GetStartupProject $StartUpProjectName $project
- $params = 'migrations', 'list'
- if ($ConfigurationTypeName)
- {
- $params += '--migrations-config', $ConfigurationTypeName
- }
- $params += GetParams $ConnectionStringName $ConnectionString $ConnectionProviderName
- return EF6 $project $startupProject $AppDomainBaseDirectory $params
- }
- function WarnIfOtherEFs($cmdlet)
- {
- if (Get-Module 'EntityFrameworkCore')
- {
- Write-Warning "Both Entity Framework 6 and Entity Framework Core are installed. The Entity Framework 6 tools are running. Use 'EntityFrameworkCore\$cmdlet' for Entity Framework Core."
- }
- if (Get-Module 'EntityFramework')
- {
- Write-Warning "A version of Entity Framework older than 6.3 is also installed. The newer tools are running. Use 'EntityFramework\$cmdlet' for the older version."
- }
- }
- function GetProject($projectName)
- {
- if (!$projectName)
- {
- return Get-Project
- }
- return Get-Project $projectName
- }
- function GetStartupProject($name, $fallbackProject)
- {
- if ($name)
- {
- return Get-Project $name
- }
- $startupProjectPaths = $DTE.Solution.SolutionBuild.StartupProjects
- if ($startupProjectPaths)
- {
- if ($startupProjectPaths.Length -eq 1)
- {
- $startupProjectPath = $startupProjectPaths[0]
- if (![IO.Path]::IsPathRooted($startupProjectPath))
- {
- $solutionPath = Split-Path (GetProperty $DTE.Solution.Properties 'Path')
- $startupProjectPath = Join-Path $solutionPath $startupProjectPath -Resolve | Convert-Path
- }
- $startupProject = GetSolutionProjects |
- ?{
- try
- {
- $fullName = $_.FullName
- }
- catch [NotImplementedException]
- {
- return $false
- }
- if ($fullName -and $fullName.EndsWith('\'))
- {
- $fullName = $fullName.Substring(0, $fullName.Length - 1)
- }
- return $fullName -eq $startupProjectPath
- }
- if ($startupProject)
- {
- return $startupProject
- }
- Write-Warning "Unable to resolve startup project '$startupProjectPath'."
- }
- else
- {
- Write-Warning 'Multiple startup projects set.'
- }
- }
- else
- {
- Write-Warning 'No startup project set.'
- }
- Write-Warning "Using project '$($fallbackProject.ProjectName)' as the startup project."
- return $fallbackProject
- }
- function GetSolutionProjects()
- {
- $projects = New-Object 'System.Collections.Stack'
- $DTE.Solution.Projects |
- %{ $projects.Push($_) }
- while ($projects.Count)
- {
- $project = $projects.Pop();
- $project
- if ($project.ProjectItems)
- {
- $project.ProjectItems |
- ?{ $_.SubProject } |
- %{ $projects.Push($_.SubProject) }
- }
- }
- }
- function GetParams($connectionStringName, $connectionString, $connectionProviderName)
- {
- $params = @()
- if ($connectionStringName)
- {
- $params += '--connection-string-name', $connectionStringName
- }
- if ($connectionString)
- {
- $params += '--connection-string', $connectionString,
- '--connection-provider', $connectionProviderName
- }
- return $params
- }
- function ShowConsole
- {
- $componentModel = Get-VSComponentModel
- $powerConsoleWindow = $componentModel.GetService([NuGetConsole.IPowerConsoleWindow])
- $powerConsoleWindow.Show()
- }
- function WriteErrorLine($message)
- {
- try
- {
-
- $componentModel = Get-VSComponentModel
- $powerConsoleWindow = $componentModel.GetService([NuGetConsole.IPowerConsoleWindow])
- $bindingFlags = [Reflection.BindingFlags]::Instance -bor [Reflection.BindingFlags]::NonPublic
- $activeHostInfo = $powerConsoleWindow.GetType().GetProperty('ActiveHostInfo', $bindingFlags).GetValue($powerConsoleWindow)
- $internalHost = $activeHostInfo.WpfConsole.Host
- $reportErrorMethod = $internalHost.GetType().GetMethod('ReportError', $bindingFlags, $null, [Exception], $null)
- $exception = New-Object Exception $message
- $reportErrorMethod.Invoke($internalHost, $exception)
- }
- catch
- {
- Write-Host $message -ForegroundColor DarkRed
- }
- }
- function EF6($project, $startupProject, $workingDir, $params)
- {
- $solutionBuild = $DTE.Solution.SolutionBuild
- $solutionBuild.BuildProject(
- $solutionBuild.ActiveConfiguration.Name,
- $project.UniqueName,
- $true)
- if ($solutionBuild.LastBuildInfo)
- {
- throw "The project '$($project.ProjectName)' failed to build."
- }
- $projectDir = GetProperty $project.Properties 'FullPath'
- $outputPath = GetProperty $project.ConfigurationManager.ActiveConfiguration.Properties 'OutputPath'
- $targetDir = [IO.Path]::GetFullPath([IO.Path]::Combine($projectDir, $outputPath))
- $targetFrameworkMoniker = GetProperty $project.Properties 'TargetFrameworkMoniker'
- $frameworkName = New-Object 'System.Runtime.Versioning.FrameworkName' $targetFrameworkMoniker
- $targetFrameworkIdentifier = $frameworkName.Identifier
- $targetFrameworkVersion = $frameworkName.Version
- if ($targetFrameworkIdentifier -in '.NETFramework')
- {
- if ($targetFrameworkVersion -lt '4.5')
- {
- $frameworkDir = 'net40'
- }
- else
- {
- $frameworkDir = 'net45'
- }
- $platformTarget = GetPlatformTarget $project
- if ($platformTarget -eq 'x86')
- {
- $runtimeDir = 'win-x86'
- }
- elseif ($platformTarget -in 'AnyCPU', 'x64')
- {
- $runtimeDir = 'any'
- }
- else
- {
- throw "Project '$($project.ProjectName)' has an active platform of '$platformTarget'. Select a different " +
- 'platform and try again.'
- }
- $exePath = Join-Path $PSScriptRoot "$frameworkDir\$runtimeDir\ef6.exe"
- }
- elseif ($targetFrameworkIdentifier -eq '.NETCoreApp')
- {
- $exePath = (Get-Command 'dotnet').Path
- $targetName = GetProperty $project.Properties 'AssemblyName'
- $depsFile = Join-Path $targetDir ($targetName + '.deps.json')
- $projectAssetsFile = GetCpsProperty $project 'ProjectAssetsFile'
- $runtimeConfig = Join-Path $targetDir ($targetName + '.runtimeconfig.json')
- $runtimeFrameworkVersion = GetCpsProperty $project 'RuntimeFrameworkVersion'
- $efPath = Join-Path $PSScriptRoot 'netcoreapp3.0\any\ef6.dll'
- $dotnetParams = 'exec', '--depsfile', $depsFile
- if ($projectAssetsFile)
- {
-
-
- $projectAssets = [IO.File]::ReadAllText($projectAssetsFile) | ConvertFrom-Json
- $projectAssets.packageFolders.psobject.Properties.Name |
- %{ $dotnetParams += '--additionalprobingpath', $_.TrimEnd('\') }
- }
- if (Test-Path $runtimeConfig)
- {
- $dotnetParams += '--runtimeconfig', $runtimeConfig
- }
- elseif ($runtimeFrameworkVersion)
- {
- $dotnetParams += '--fx-version', $runtimeFrameworkVersion
- }
- $dotnetParams += $efPath
- $params = $dotnetParams + $params
- }
- else
- {
- throw "Project '$($startupProject.ProjectName)' targets framework '$targetFrameworkIdentifier'. The Entity Framework " +
- 'Package Manager Console Tools don''t support this framework.'
- }
- $targetFileName = GetProperty $project.Properties 'OutputFileName'
- $targetPath = Join-Path $targetDir $targetFileName
- $rootNamespace = GetProperty $project.Properties 'RootNamespace'
- $language = GetLanguage $project
- $params += '--verbose',
- '--no-color',
- '--prefix-output',
- '--assembly', $targetPath,
- '--project-dir', $projectDir,
- '--language', $language
- if (IsWeb $startupProject)
- {
- $startupProjectDir = GetProperty $startupProject.Properties 'FullPath'
- $params += '--data-dir', (Join-Path $startupProjectDir 'App_Data')
- }
- if ($rootNamespace)
- {
- $params += '--root-namespace', $rootNamespace
- }
- $configFile = GetConfigPath $startupProject
- if ($configFile)
- {
- $params += '--config', $configFile
- }
- if (!$workingDir)
- {
- $workingDir = $targetDir
- }
- $arguments = ToArguments $params
- $startInfo = New-Object 'System.Diagnostics.ProcessStartInfo' -Property @{
- FileName = $exePath;
- Arguments = $arguments;
- UseShellExecute = $false;
- CreateNoWindow = $true;
- RedirectStandardOutput = $true;
- StandardOutputEncoding = [Text.Encoding]::UTF8;
- RedirectStandardError = $true;
- WorkingDirectory = $workingDir;
- }
- Write-Verbose "$exePath $arguments"
- $process = [Diagnostics.Process]::Start($startInfo)
- while (($line = $process.StandardOutput.ReadLine()) -ne $null)
- {
- $level = $null
- $text = $null
- $parts = $line.Split(':', 2)
- if ($parts.Length -eq 2)
- {
- $level = $parts[0]
- $i = 0
- $count = 8 - $level.Length
- while ($i -lt $count -and $parts[1][$i] -eq ' ')
- {
- $i++
- }
- $text = $parts[1].Substring($i)
- }
- switch ($level)
- {
- 'error' { WriteErrorLine $text }
- 'warn' { Write-Warning $text }
- 'info' { Write-Host $text }
- 'data' { Write-Output $text }
- 'verbose' { Write-Verbose $text }
- default { Write-Host $line }
- }
- }
- $process.WaitForExit()
- if ($process.ExitCode)
- {
- while (($line = $process.StandardError.ReadLine()) -ne $null)
- {
- WriteErrorLine $line
- }
- exit
- }
- }
- function IsCpsProject($project)
- {
- $hierarchy = GetVsHierarchy $project
- $isCapabilityMatch = [Microsoft.VisualStudio.Shell.PackageUtilities].GetMethod(
- 'IsCapabilityMatch',
- [type[]]([Microsoft.VisualStudio.Shell.Interop.IVsHierarchy], [string]))
- return $isCapabilityMatch.Invoke($null, ($hierarchy, 'CPS'))
- }
- function IsWeb($project)
- {
- $hierarchy = GetVsHierarchy $project
- $aggregatableProject = Get-Interface $hierarchy 'Microsoft.VisualStudio.Shell.Interop.IVsAggregatableProject'
- if (!$aggregatableProject)
- {
- $projectTypes = $project.Kind
- }
- else
- {
- $projectTypeGuids = $null
- $hr = $aggregatableProject.GetAggregateProjectTypeGuids([ref] $projectTypeGuids)
- [Runtime.InteropServices.Marshal]::ThrowExceptionForHR($hr)
- $projectTypes = $projectTypeGuids.Split(';')
- }
- foreach ($projectType in $projectTypes)
- {
- if ($projectType -in '{349C5851-65DF-11DA-9384-00065B846F21}', '{E24C65DC-7377-472B-9ABA-BC803B73C61A}')
- {
- return $true
- }
- }
- return $false;
- }
- function GetIntermediatePath($project)
- {
- $intermediatePath = GetProperty $project.ConfigurationManager.ActiveConfiguration.Properties 'IntermediatePath'
- if ($intermediatePath)
- {
- return $intermediatePath
- }
- return GetMSBuildProperty $project 'IntermediateOutputPath'
- }
- function GetPlatformTarget($project)
- {
- if (IsCpsProject $project)
- {
- $platformTarget = GetCpsProperty $project 'PlatformTarget'
- if ($platformTarget)
- {
- return $platformTarget
- }
- return GetCpsProperty $project 'Platform'
- }
- $platformTarget = GetProperty $project.ConfigurationManager.ActiveConfiguration.Properties 'PlatformTarget'
- if ($platformTarget)
- {
- return $platformTarget
- }
-
- $platformTarget = GetMSBuildProperty $project 'PlatformTarget'
- if ($platformTarget)
- {
- return $platformTarget
- }
- return 'AnyCPU'
- }
- function GetLanguage($project)
- {
- if (IsCpsProject $project)
- {
- return GetCpsProperty $project 'Language'
- }
- return GetMSBuildProperty $project 'Language'
- }
- function GetVsHierarchy($project)
- {
- $solution = Get-VSService 'Microsoft.VisualStudio.Shell.Interop.SVsSolution' 'Microsoft.VisualStudio.Shell.Interop.IVsSolution'
- $hierarchy = $null
- $hr = $solution.GetProjectOfUniqueName($project.UniqueName, [ref] $hierarchy)
- [Runtime.InteropServices.Marshal]::ThrowExceptionForHR($hr)
- return $hierarchy
- }
- function GetProperty($properties, $propertyName)
- {
- try
- {
- return $properties.Item($propertyName).Value
- }
- catch
- {
- return $null
- }
- }
- function GetCpsProperty($project, $propertyName)
- {
- $browseObjectContext = Get-Interface $project 'Microsoft.VisualStudio.ProjectSystem.Properties.IVsBrowseObjectContext'
- $unconfiguredProject = $browseObjectContext.UnconfiguredProject
- $configuredProject = $unconfiguredProject.GetSuggestedConfiguredProjectAsync().Result
- $properties = $configuredProject.Services.ProjectPropertiesProvider.GetCommonProperties()
- return $properties.GetEvaluatedPropertyValueAsync($propertyName).Result
- }
- function GetMSBuildProperty($project, $propertyName)
- {
- $msbuildProject = [Microsoft.Build.Evaluation.ProjectCollection]::GlobalProjectCollection.LoadedProjects |
- where FullPath -eq $project.FullName
- return $msbuildProject.GetProperty($propertyName).EvaluatedValue
- }
- function ToArguments($params)
- {
- $arguments = ''
- for ($i = 0; $i -lt $params.Length; $i++)
- {
- if ($i)
- {
- $arguments += ' '
- }
- if (!$params[$i].Contains(' '))
- {
- $arguments += $params[$i]
- continue
- }
- $arguments += '"'
- $pendingBackslashs = 0
- for ($j = 0; $j -lt $params[$i].Length; $j++)
- {
- switch ($params[$i][$j])
- {
- '"'
- {
- if ($pendingBackslashs)
- {
- $arguments += '\' * $pendingBackslashs * 2
- $pendingBackslashs = 0
- }
- $arguments += '\"'
- }
- '\'
- {
- $pendingBackslashs++
- }
- default
- {
- if ($pendingBackslashs)
- {
- if ($pendingBackslashs -eq 1)
- {
- $arguments += '\'
- }
- else
- {
- $arguments += '\' * $pendingBackslashs * 2
- }
- $pendingBackslashs = 0
- }
- $arguments += $params[$i][$j]
- }
- }
- }
- if ($pendingBackslashs)
- {
- $arguments += '\' * $pendingBackslashs * 2
- }
- $arguments += '"'
- }
- return $arguments
- }
- function GetConfigPath($project)
- {
- if (IsWeb $project)
- {
- $configFileName = 'web.config'
- }
- else
- {
- $configFileName = 'app.config'
- }
- $item = $project.ProjectItems |
- where Name -eq $configFileName |
- select -First 1
- return GetProperty $item.Properties 'FullPath'
- }
- Export-ModuleMember 'Add-EFDefaultConnectionFactory', 'Add-EFProvider', 'Add-Migration', 'Enable-Migrations', 'Get-Migrations', 'Update-Database' -Variable 'InitialDatabase'
|