|
| 1 | +// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information. |
| 2 | +namespace FSharp.DependencyManager |
| 3 | + |
| 4 | +open System |
| 5 | +open System.Collections |
| 6 | +open System.Collections.Generic |
| 7 | +open System.Diagnostics |
| 8 | +open System.IO |
| 9 | +open System.Reflection |
| 10 | +open System.Runtime.CompilerServices |
| 11 | +open System.Runtime.Versioning |
| 12 | + |
| 13 | +open Internal.Utilities.FSharpEnvironment |
| 14 | + |
| 15 | +// Package reference information |
| 16 | +type PackageReference = { |
| 17 | + Include:string |
| 18 | + Version:string |
| 19 | + RestoreSources:string |
| 20 | + Script:string } |
| 21 | + |
| 22 | + |
| 23 | +// Resolved assembly information |
| 24 | +type Resolution = { |
| 25 | + NugetPackageId : string |
| 26 | + NugetPackageVersion : string |
| 27 | + PackageRoot : string |
| 28 | + FullPath : string |
| 29 | + IsNotImplementationReference: string |
| 30 | + NativePath : string |
| 31 | + AppHostRuntimeIdentifier : string |
| 32 | + InitializeSourcePath : string } |
| 33 | + |
| 34 | + |
| 35 | +module ProjectFile = |
| 36 | + |
| 37 | + let findLoadsFromResolutions (resolutions:Resolution array) = |
| 38 | + resolutions |
| 39 | + |> Array.filter(fun r -> not(String.IsNullOrEmpty(r.NugetPackageId) || |
| 40 | + String.IsNullOrEmpty(r.InitializeSourcePath)) && |
| 41 | + File.Exists(r.InitializeSourcePath)) |
| 42 | + |> Array.map(fun r -> r.InitializeSourcePath) |
| 43 | + |> Array.distinct |
| 44 | + |
| 45 | + let findIncludesFromResolutions (resolutions:Resolution array) = |
| 46 | + let managedRoots = |
| 47 | + resolutions |
| 48 | + |> Array.filter(fun r -> not(String.IsNullOrEmpty(r.NugetPackageId) || |
| 49 | + String.IsNullOrEmpty(r.PackageRoot)) && |
| 50 | + Directory.Exists(r.PackageRoot)) |
| 51 | + |> Array.map(fun r -> r.PackageRoot) |
| 52 | + |> Array.distinct |
| 53 | + |
| 54 | + let nativeRoots = |
| 55 | + resolutions |
| 56 | + |> Array.filter(fun r -> not(String.IsNullOrEmpty(r.NugetPackageId) || |
| 57 | + String.IsNullOrEmpty(r.NativePath)) && |
| 58 | + Directory.Exists(r.NativePath)) |
| 59 | + |> Array.map(fun r -> r.NativePath) |
| 60 | + |> Array.distinct |
| 61 | + |
| 62 | + Array.concat [|managedRoots; nativeRoots|] |
| 63 | + |
| 64 | + let getResolutionsFromFile resolutionsFile = |
| 65 | + |
| 66 | + let lines = |
| 67 | + try |
| 68 | + File.ReadAllText(resolutionsFile).Split([| '\r'; '\n'|], StringSplitOptions.None) |
| 69 | + |> Array.filter(fun line -> not(String.IsNullOrEmpty(line))) |
| 70 | + with |
| 71 | + | _ -> [||] |
| 72 | + |
| 73 | + [| for line in lines do |
| 74 | + let fields = line.Split(',') |
| 75 | + if fields.Length < 8 then raise (new System.InvalidOperationException(sprintf "Internal error - Invalid resolutions file format '%s'" line)) |
| 76 | + else { |
| 77 | + NugetPackageId = fields.[0] |
| 78 | + NugetPackageVersion = fields.[1] |
| 79 | + PackageRoot = fields.[2] |
| 80 | + FullPath = fields.[3] |
| 81 | + IsNotImplementationReference = fields.[4] |
| 82 | + InitializeSourcePath = fields.[5] |
| 83 | + NativePath = fields.[6] |
| 84 | + AppHostRuntimeIdentifier = fields.[7] |
| 85 | + } |
| 86 | + |] |
| 87 | + |
| 88 | + let makeScriptFromResolutions (resolutions:Resolution array) poundRprefix = |
| 89 | + let expandReferences = |
| 90 | + resolutions |
| 91 | + |> Array.filter(fun r -> not(String.IsNullOrEmpty(r.NugetPackageId) || |
| 92 | + String.IsNullOrEmpty(r.FullPath)) && |
| 93 | + String.Compare(r.IsNotImplementationReference, "true", StringComparison.InvariantCultureIgnoreCase) <> 0 && |
| 94 | + File.Exists(r.FullPath)) |
| 95 | + |> Array.fold(fun acc r -> acc + poundRprefix + r.FullPath + "\"" + Environment.NewLine) "" |
| 96 | + |
| 97 | + let projectTemplate =""" |
| 98 | +// Generated from #r "nuget:Package References" |
| 99 | +// ============================================ |
| 100 | +// |
| 101 | +// DOTNET_HOST_PATH:(C:\Program Files\dotnet\dotnet.exe) |
| 102 | +// MSBuildSDKsPath:(C:\Program Files\dotnet\sdk\3.1.200-preview-014883\Sdks) |
| 103 | +// MSBuildExtensionsPath:(C:\Program Files\dotnet\sdk\3.1.200-preview-014883\) |
| 104 | +// |
| 105 | +// References |
| 106 | +// |
| 107 | +$(POUND_R) |
| 108 | +
|
| 109 | +""" |
| 110 | + projectTemplate.Replace("$(POUND_R)", expandReferences) |
| 111 | + |
| 112 | + let generateProjectBody = """ |
| 113 | +<Project Sdk='Microsoft.NET.Sdk'> |
| 114 | +
|
| 115 | + <PropertyGroup> |
| 116 | + <TargetFramework>$(TARGETFRAMEWORK)</TargetFramework> |
| 117 | + <IsPackable>false</IsPackable> |
| 118 | + <DisableImplicitFSharpCoreReference>true</DisableImplicitFSharpCoreReference> |
| 119 | + <DisableImplicitSystemValueTupleReference>true</DisableImplicitSystemValueTupleReference> |
| 120 | +
|
| 121 | + <!-- Temporary fix some sdks, shipped internally with broken parameterization --> |
| 122 | + <FSharpCoreImplicitPackageVersion Condition="'$(FSharpCoreImplicitPackageVersion)' == '{{FSharpCoreShippedPackageVersion}}'">4.7.0</FSharpCoreImplicitPackageVersion> |
| 123 | + <FSharpCoreImplicitPackageVersion Condition="'$(FSharpCoreImplicitPackageVersion)' == '{{FSharpCorePreviewPackageVersion}}'">4.7.1-*</FSharpCoreImplicitPackageVersion> |
| 124 | + </PropertyGroup> |
| 125 | +
|
| 126 | +$(PACKAGEREFERENCES) |
| 127 | +
|
| 128 | + <Target Name="ComputePackageRootsForInteractivePackageManagement" |
| 129 | + DependsOnTargets="ResolveReferences;ResolveSdkReferences;ResolveTargetingPackAssets;ResolveSDKReferences;GenerateBuildDependencyFile"> |
| 130 | +
|
| 131 | + <ItemGroup> |
| 132 | + <__InteractiveReferencedAssemblies Include = "@(ReferencePath)" /> |
| 133 | + <__InteractiveReferencedAssembliesCopyLocal Include = "@(RuntimeCopyLocalItems)" Condition="'$(TargetFrameworkIdentifier)'!='.NETFramework'" /> |
| 134 | + <__InteractiveReferencedAssembliesCopyLocal Include = "@(ReferenceCopyLocalPaths)" Condition="'$(TargetFrameworkIdentifier)'=='.NETFramework'" /> |
| 135 | + <__ConflictsList Include="%(_ConflictPackageFiles.ConflictItemType)=%(_ConflictPackageFiles.Filename)%(_ConflictPackageFiles.Extension)" /> |
| 136 | + </ItemGroup> |
| 137 | +
|
| 138 | + <PropertyGroup> |
| 139 | + <__Conflicts>@(__ConflictsList, ';');</__Conflicts> |
| 140 | + </PropertyGroup> |
| 141 | +
|
| 142 | + <ItemGroup> |
| 143 | + <InteractiveResolvedFile Include="@(__InteractiveReferencedAssemblies)" |
| 144 | + Condition="$([System.String]::new($(__Conflicts)).Contains($([System.String]::new('Reference=%(__InteractiveReferencedAssemblies.Filename)%(__InteractiveReferencedAssemblies.Extension);'))))" |
| 145 | + KeepDuplicates="false"> |
| 146 | + <NormalizedIdentity Condition="'%(Identity)'!=''">$([System.String]::Copy('%(Identity)').Replace('\', '/'))</NormalizedIdentity> |
| 147 | + <NormalizedPathInPackage Condition="'%(__InteractiveReferencedAssemblies.PathInPackage)'!=''">$([System.String]::Copy('%(__InteractiveReferencedAssemblies.PathInPackage)').Replace('\', '/'))</NormalizedPathInPackage> |
| 148 | + <PositionPathInPackage Condition="'%(InteractiveResolvedFile.NormalizedPathInPackage)'!=''">$([System.String]::Copy('%(InteractiveResolvedFile.NormalizedIdentity)').IndexOf('%(InteractiveResolvedFile.NormalizedPathInPackage)'))</PositionPathInPackage> |
| 149 | + <PackageRoot Condition="'%(InteractiveResolvedFile.NormalizedPathInPackage)'!='' and '%(InteractiveResolvedFile.PositionPathInPackage)'!='-1'">$([System.String]::Copy('%(InteractiveResolvedFile.NormalizedIdentity)').Substring(0, %(InteractiveResolvedFile.PositionPathInPackage)))</PackageRoot> |
| 150 | + <InitializeSourcePath>%(InteractiveResolvedFile.PackageRoot)content\%(__InteractiveReferencedAssemblies.FileName)%(__InteractiveReferencedAssemblies.Extension)$(SCRIPTEXTENSION)</InitializeSourcePath> |
| 151 | + <IsNotImplementationReference>$([System.String]::Copy('%(__InteractiveReferencedAssemblies.PathInPackage)').StartsWith('ref/'))</IsNotImplementationReference> |
| 152 | + <NuGetPackageId>%(__InteractiveReferencedAssemblies.NuGetPackageId)</NuGetPackageId> |
| 153 | + <NuGetPackageVersion>%(__InteractiveReferencedAssemblies.NuGetPackageVersion)</NuGetPackageVersion> |
| 154 | + </InteractiveResolvedFile> |
| 155 | +
|
| 156 | + <InteractiveResolvedFile Include="@(__InteractiveReferencedAssembliesCopyLocal)" KeepDuplicates="false"> |
| 157 | + <NormalizedIdentity Condition="'%(Identity)'!=''">$([System.String]::Copy('%(Identity)').Replace('\', '/'))</NormalizedIdentity> |
| 158 | + <NormalizedPathInPackage Condition="'%(__InteractiveReferencedAssembliesCopyLocal.PathInPackage)'!=''">$([System.String]::Copy('%(__InteractiveReferencedAssembliesCopyLocal.PathInPackage)').Replace('\', '/'))</NormalizedPathInPackage> |
| 159 | + <PositionPathInPackage Condition="'%(InteractiveResolvedFile.NormalizedPathInPackage)'!=''">$([System.String]::Copy('%(InteractiveResolvedFile.NormalizedIdentity)').IndexOf('%(InteractiveResolvedFile.NormalizedPathInPackage)'))</PositionPathInPackage> |
| 160 | + <PackageRoot Condition="'%(InteractiveResolvedFile.NormalizedPathInPackage)'!='' and '%(InteractiveResolvedFile.PositionPathInPackage)'!='-1'">$([System.String]::Copy('%(InteractiveResolvedFile.NormalizedIdentity)').Substring(0, %(InteractiveResolvedFile.PositionPathInPackage)))</PackageRoot> |
| 161 | + <InitializeSourcePath>%(InteractiveResolvedFile.PackageRoot)content\%(__InteractiveReferencedAssembliesCopyLocal.FileName)%(__InteractiveReferencedAssembliesCopyLocal.Extension)$(SCRIPTEXTENSION)</InitializeSourcePath> |
| 162 | + <IsNotImplementationReference>$([System.String]::Copy('%(__InteractiveReferencedAssembliesCopyLocal.PathInPackage)').StartsWith('ref/'))</IsNotImplementationReference> |
| 163 | + <NuGetPackageId>%(__InteractiveReferencedAssembliesCopyLocal.NuGetPackageId)</NuGetPackageId> |
| 164 | + <NuGetPackageVersion>%(__InteractiveReferencedAssembliesCopyLocal.NuGetPackageVersion)</NuGetPackageVersion> |
| 165 | + </InteractiveResolvedFile> |
| 166 | +
|
| 167 | + <NativeIncludeRoots |
| 168 | + Include="@(RuntimeTargetsCopyLocalItems)" |
| 169 | + Condition="'%(RuntimeTargetsCopyLocalItems.AssetType)' == 'native'"> |
| 170 | + <Path>$([MSBuild]::EnsureTrailingSlash('$([System.String]::Copy('%(FullPath)').Substring(0, $([System.String]::Copy('%(FullPath)').LastIndexOf('runtimes'))))'))</Path> |
| 171 | + </NativeIncludeRoots> |
| 172 | + </ItemGroup> |
| 173 | + </Target> |
| 174 | +
|
| 175 | + <Target Name="InteractivePackageManagement" |
| 176 | + DependsOnTargets="ComputePackageRootsForInteractivePackageManagement" |
| 177 | + BeforeTargets="CoreCompile" |
| 178 | + AfterTargets="PrepareForBuild"> |
| 179 | +
|
| 180 | + <ItemGroup> |
| 181 | + <ResolvedReferenceLines Remove='*' /> |
| 182 | + <ResolvedReferenceLines Include='%(InteractiveResolvedFile.NugetPackageId),%(InteractiveResolvedFile.NugetPackageVersion),%(InteractiveResolvedFile.PackageRoot),%(InteractiveResolvedFile.FullPath),%(InteractiveResolvedFile.IsNotImplementationReference),%(InteractiveResolvedFile.InitializeSourcePath),%(NativeIncludeRoots.Path),$(AppHostRuntimeIdentifier)' KeepDuplicates="false" /> |
| 183 | + </ItemGroup> |
| 184 | +
|
| 185 | + <WriteLinesToFile Lines='@(ResolvedReferenceLines)' |
| 186 | + File='$(MSBuildProjectFullPath).resolvedReferences.paths' |
| 187 | + Overwrite='True' WriteOnlyWhenDifferent='True' /> |
| 188 | + </Target> |
| 189 | +
|
| 190 | +</Project>""" |
0 commit comments