Advertisement
Guest User

Untitled

a guest
Apr 4th, 2020
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. [CmdletBinding()]
  2. param(
  3.   [Parameter(Mandatory = $true)]
  4.   [string]
  5.   $SolutionDir,
  6.   [Parameter(Mandatory = $true)]
  7.   [string]
  8.   $Configuration
  9. )
  10.  
  11. . "$PSScriptRoot\models.ps1"
  12. $stopwatch = [System.Diagnostics.Stopwatch]::StartNew()
  13.  
  14. $loadOrder = @(
  15.   'System.Collections.Immutable.dll',
  16.   'System.Threading.Tasks.Dataflow.dll',
  17.   'Microsoft.VisualStudio.Setup.Configuration.Interop.dll',
  18.   'Microsoft.Build.Framework.dll',
  19.   'Microsoft.Build.dll',
  20.   'Microsoft.Build.Utilities.Core.dll'
  21. )
  22.  
  23. $loadOrder | ForEach-Object { Add-Type -Path "$PSScriptRoot\bin\$_" }
  24.  
  25. $solutionFilePath = Join-Path $SolutionDir "HRNet.sln"
  26.  
  27. if ( -not ( Test-Path $solutionFilePath ) ) {
  28.   throw "Solution file 'HRNet.sln' cannot be found in path '$SolutionDir'"
  29. }
  30.  
  31. $PackageRegExpr = New-Object System.Text.RegularExpressions.Regex("(?:[\.\.\\]+)(?:\\packages\\)(.+?)\.((?:\.?[0-9]+)+)")
  32. $VersionRegExpr = New-Object System.Text.RegularExpressions.Regex("([0-9]+)(\.[0-9]+){1,3}")
  33. $DllRegExpr = New-Object System.Text.RegularExpressions.Regex("[^\\]+.dll")
  34. $TestRegExpr = New-Object System.Text.RegularExpressions.Regex("Test")
  35.  
  36. $MsBuildSolution = [Microsoft.Build.Construction.SolutionFile]::Parse( $solutionFilePath )
  37. $realProjects = $MsBuildSolution.ProjectsInOrder | Where-Object {
  38.   $_.AbsolutePath.EndsWith(".csproj") -or $_.AbsolutePath.EndsWith(".fsproj")
  39. }
  40.  
  41. $ProjectInfos = @{ }
  42. $ProjectLookupByName = @{ }
  43.  
  44. $TestProjects = @( )
  45.  
  46. foreach ( $proj in $realProjects ) {
  47.   $MsBuildProject = New-Object Microsoft.Build.Evaluation.Project( $proj.AbsolutePath )
  48.  
  49.   $ProjectId = [System.Guid]::Parse( $proj.ProjectGuid )
  50.   $ProjectName = $proj.ProjectName
  51.   $AssemblyName = $MsBuildProject.GetPropertyValue( 'AssemblyName' )
  52.   $OutputDirectory = $MsBuildProject.GetPropertyValue( 'TargetDir' ).Replace( 'Debug', $Configuration )
  53.   $TargetPath = $MsBuildProject.GetPropertyValue( 'TargetPath' )
  54.   $FileName = $MsBuildProject.GetPropertyValue( 'TargetFileName' )
  55.  
  56.   if ( $TestRegExpr.IsMatch( $ProjectName ) ) {
  57.     $TestProjects += $AssemblyName
  58.   }
  59.  
  60.   $ProjectLookupByName.Add( $AssemblyName, $ProjectId )
  61.  
  62.   $projectDependencies = New-Object System.Collections.Generic.HashSet[System.Guid]
  63.   $referencedAssemblies = New-Object System.Collections.Generic.HashSet[PackageInfo]
  64.  
  65.   foreach ( $projItem in $MsBuildProject.Items ) {
  66.     if ( $projItem.ItemType -eq 'Reference' ) {
  67.       $projItem.Metadata | Where-Object { $_.Name -eq 'HintPath' } | ForEach-Object {
  68.         $relativeDir = $_.EvaluatedValue
  69.         $absolutePath = [System.IO.Path]::GetFullPath( [System.IO.Path]::Combine( $MsBuildProject.DirectoryPath, $relativeDir ) )
  70.  
  71.         $match = $PackageRegExpr.Match( $_.EvaluatedValue )
  72.         if ( $match.Success ) {
  73.           $name = $DllRegExpr.Match( $_.EvaluatedValue ).Value
  74.           $versionMatch = $VersionRegExpr.Match( $match.Groups[2].Value )
  75.           $version = [System.Version]::Parse( $versionMatch.Value )
  76.         }
  77.         else {
  78.           $name = ''
  79.           $version = $null
  80.         }
  81.  
  82.         $referencedAssemblies.Add( [PackageInfo]::new( $name, $absolutePath, $version ) ) | Out-Null
  83.       }
  84.     }
  85.     elseif ( $projItem.ItemType -eq 'ProjectReference' ) {
  86.       $projItem.Metadata | Where-Object { $_.Name -eq 'Project' } | ForEach-Object {
  87.         $projectDependencies.Add( [System.Guid]::Parse( $_.EvaluatedValue ) ) | Out-Null
  88.       }
  89.     }
  90.   }
  91.  
  92.   $projectInfos.Add(
  93.     $ProjectId,
  94.     [ProjectInfo]::new( $ProjectId, $ProjectName, $AssemblyName, $OutputDirectory, $TargetPath, $FileName, $referencedAssemblies, $projectDependencies )
  95.   ) | Out-Null
  96. }
  97.  
  98. $DependenciesByProject = @{ }
  99.  
  100. foreach ( $projGuid in $ProjectInfos.Keys ) {
  101.   $dependenciesToVisit = New-Object System.Collections.Generic.Stack[System.Guid]
  102.   $seenProjects = New-Object System.Collections.Generic.HashSet[System.Guid]
  103.   $assemblies = New-Object System.Collections.Generic.HashSet[PackageInfo]
  104.   $allProjectDependencies = New-Object System.Collections.Generic.HashSet[System.Guid]
  105.    
  106.   $dependenciesToVisit.Push( $projGuid )
  107.     $seenProjects.Add( $projGuid ) | Out-Null
  108.  
  109.     while ( $dependenciesToVisit.Count -ne 0 ) {
  110.         $currentProjectId = $dependenciesToVisit.Pop()
  111.         $project = $ProjectInfos[ $currentProjectId ]
  112.  
  113.     $allProjectDependencies.Add( $currentProjectId ) | Out-Null
  114.     $assemblies.UnionWith( $project.ReferencedAssemblies )
  115.  
  116.     foreach ( $dep in $project.ProjectDependencies ) {
  117.       if ( -not $seenProjects.Contains( $dep ) ) {
  118.         $dependenciesToVisit.Push( $dep )
  119.         $seenProjects.Add( $dep ) | Out-Null
  120.       }
  121.     }
  122.   }
  123.    
  124.     $lookup = @{ }
  125.     $notPackages = New-Object System.Collections.Generic.HashSet[PackageInfo]
  126.  
  127.     foreach ($reference in $assemblies) {
  128.         if ($reference.Name -eq '') {
  129.       $notPackages.Add( $reference ) | Out-Null
  130.       continue
  131.     }
  132.  
  133.     if ( $lookup.ContainsKey( $reference.Name ) ) {
  134.       $seenVersion = $lookup[ $reference.Name ].PackageVersion
  135.       $currentVersion = $reference.PackageVersion
  136.  
  137.       if ( $currentVersion.CompareTo( $seenVersion ) -eq 1 ) {
  138.         $lookup[ $reference.Name ] = $reference
  139.       }
  140.     }
  141.     else {
  142.       $lookup.Add( $reference.Name, $reference ) | Out-Null
  143.     }
  144.   }
  145.  
  146.   $assemblies = New-Object System.Collections.Generic.HashSet[PackageInfo]
  147.   $lookup.Keys | ForEach-Object { $assemblies.Add( $lookup[ $_ ] ) | Out-Null }
  148.   $assemblies.UnionWith( $notPackages )
  149.   $allProjectDependencies.Remove( $projGuid ) | Out-Null
  150.  
  151.   $DependenciesByProject.Add(
  152.     $projGuid,
  153.     [ProjectDependencies]::new( $assemblies, $allProjectDependencies )
  154.   ) | Out-Null
  155. }
  156.  
  157. $ExecutableProjects = @(
  158.   'erecruit.Web',
  159.   'erecruit.Mvc',
  160.   'erecruit.Mvc.Search',
  161.   'erecruit.WebApi',
  162.   'erecruit.Mobile',
  163.   'RESTServices2',
  164.   'SearchService',
  165.   'erecruit.TaskService2',
  166.   'erecruitTaskService',
  167.   'erecruit.ServiceBus.ListenerService',
  168.   'erecruit.ChangeBrokerService',
  169.   'erecruit.Indexing.Rehydrator',
  170.   'erecruit.SqlGenerator',
  171.   'csman',
  172.   'erecruit.Localization.ImportExport',
  173.   'OnboardingRefactoringMigration',
  174.   'PopulateIndex',
  175.   'ResumeImport',
  176.   'SearchServiceTest',
  177.   'sbman',
  178.   'vcs',
  179.   'IndexingDefinitions',
  180.   'erecruit.IndexingManager',
  181.   'erecruit.IntegrationManager',
  182.   'ClientSecretHasher',
  183.   'EncryptPhoneSystemPassword',
  184.   'SsnHasher',
  185.   'erecruit.Stamp',
  186.   'erecruitDispatchService'
  187. )
  188.  
  189. foreach ( $executableProjectName in ( $ExecutableProjects + $TestProjects ) ) {
  190.   $projId = $ProjectLookupByName[ $executableProjectName ]
  191.   [ProjectInfo] $projectInfo = $ProjectInfos[ $projId ]
  192.   [ProjectDependencies] $projDependencies = $DependenciesByProject[ $projId ]
  193.  
  194.   foreach ( $assembly in $projDependencies.ReferencedAssemblies ) {
  195.     $fileName = [System.IO.Path]::GetFileName( $assembly.AbsolutePath )
  196.    
  197.     New-Item -ItemType HardLink `
  198.       -Path ( Join-Path $projectInfo.OutputDirectory $fileName ) `
  199.       -Target $assembly.AbsolutePath `
  200.       -Force | Out-Null
  201.   }
  202.  
  203.   foreach ( $projectId in $projDependencies.Projects ) {
  204.     [ProjectInfo] $info = $ProjectInfos[ $projectId ]
  205.  
  206.     $path = Join-Path $projectInfo.OutputDirectory $info.FileName
  207.    
  208.     New-Item -ItemType HardLink `
  209.         -Path $path `
  210.         -Target $info.TargetPath `
  211.         -Force | Out-Null
  212.   }
  213. }
  214.  
  215. $stopwatch.Stop()
  216. Write-Host "Hard links created in $($stopwatch.Elapsed)"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement