|
#I __SOURCE_DIRECTORY__ |
|
|
|
#r "libs/NuGet.Core.dll" |
|
#r "System.Xml.Linq" |
|
|
|
open NuGet |
|
open System |
|
open System.IO |
|
|
|
module NuGet = |
|
let useSideBySidePaths = false |
|
let packageSourceUrl = "https://go.microsoft.com/fwlink/?LinkID=206669" |
|
// offline source |
|
//let packageSourceUrl = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "NuGet", "Cache") |
|
let packageSource = PackageSource(packageSourceUrl) |
|
let repositoryFactory = PackageRepositoryFactory() |
|
let repository = repositoryFactory.CreateRepository packageSource.Source |
|
|
|
let targetFramework = |
|
let version = NuGet.VersionUtility.DefaultTargetFramework |
|
if version.Version = Version("4.0.0.0") && Type.GetType("System.Reflection.ReflectionContext", false) <> null then |
|
System.Runtime.Versioning.FrameworkName(version.Identifier, Version("4.5"), version.Profile) |
|
else version |
|
|
|
let mgr (packagesPath: string) = |
|
PackageManager(repository, |
|
DefaultPackagePathResolver(packagesPath, useSideBySidePaths = useSideBySidePaths), |
|
PhysicalFileSystem(packagesPath), |
|
Logger = { new ILogger with |
|
member self.Log(level, message, ps) = System.Console.WriteLine(message, ps) |
|
member self.ResolveFileConflict(message) = |
|
printfn "File conflict: %s" message |
|
FileConflictResolution.Ignore }) |
|
|
|
let enumPackages path = |
|
Directory.EnumerateDirectories(path) |
|
|> Seq.map (fun path -> Path.GetFileName path) |
|
|> Seq.filter (fun packageId -> packageId <> "packages") |
|
|
|
let rec findPackageAssemblies packagesPath packageId = |
|
seq { |
|
let repo = LocalPackageRepository(packagesPath) |
|
let package = repo.FindPackage(packageId) |
|
|
|
let getAssemblyReferences (package: IPackage) = |
|
query { |
|
for ref in package.AssemblyReferences do |
|
where (VersionUtility.IsCompatible(targetFramework, ref.SupportedFrameworks)) // check framework version |
|
// where (query { for fw in ref.SupportedFrameworks do |
|
// where (VersionUtility.DefaultTargetFramework.Identifier = fw.Identifier) |
|
// count } <> 0) |
|
where (ref.Name <> "_._") // some refs have such a name e.g. ExcelTypeProvider |
|
groupBy (ref.Name) into g |
|
select (package, query { |
|
for ref in g do |
|
where (ref.TargetFramework <> null) |
|
sortByDescending ref.TargetFramework.Version |
|
select (Some ref) |
|
headOrDefault |
|
}) |
|
} |> Seq.choose (fun (p, r) -> match r with Some r -> Some (p, r) | None -> None) |
|
|
|
yield! |
|
package.DependencySets |
|
|> Seq.collect (fun ds -> ds.Dependencies) |
|
|> Seq.collect (fun p -> findPackageAssemblies packagesPath p.Id) |
|
yield! |
|
match getAssemblyReferences package with |
|
| refs when refs.IsEmpty() -> package.AssemblyReferences |> Seq.map (fun ref -> package, ref) |
|
| refs -> refs |
|
} |
|
|> Seq.distinctBy snd |
|
|
|
let packageFolderName (package: IPackage) = |
|
if useSideBySidePaths then |
|
sprintf "%s.%O" package.Id package.Version |
|
else package.Id |
|
|
|
open NuGet |
|
|
|
let installPackage packagesPath (packageId: string) = |
|
let mgr = NuGet.mgr packagesPath |
|
try |
|
mgr.InstallPackage(packageId, null, false, false) |
|
with e -> |
|
printfn "Error installing package %s: %s" packageId e.Message |
|
|
|
let updatePackage packagesPath (packageId: string) = |
|
let mgr = NuGet.mgr packagesPath |
|
try |
|
mgr.UpdatePackage(packageId, true, false) |
|
with e -> |
|
printfn "Error updating package %s: %s" packageId e.Message |
|
|
|
let updatePackages packagesPath = |
|
let mgr = NuGet.mgr packagesPath |
|
enumPackages packagesPath |
|
|> Seq.iter (updatePackage packagesPath) |
|
|
|
let uninstallPackage packagesPath (packageId: string) = |
|
let mgr = NuGet.mgr packagesPath |
|
try |
|
mgr.UninstallPackage(packageId) |
|
with e -> |
|
printfn " Error uninstalling package %s: %s" packageId e.Message |
|
|
|
#r "System.Windows.Forms" |
|
let copyPackageReferences packagesPath packageId = |
|
let rs = |
|
NuGet.findPackageAssemblies packagesPath packageId |
|
|> Seq.map (fun (package, file) -> |
|
sprintf "#r @\"%s\"" (Path.Combine(packagesPath, Path.Combine(packageFolderName package, file.Path)))) |
|
|> String.concat "\n" |
|
System.Windows.Forms.Clipboard.SetData("Text", rs) |
|
printfn "%s" rs |
|
|
|
let installPackageAndCopyReferences packagesPath packageId = |
|
installPackage packagesPath packageId |
|
copyPackageReferences packagesPath packageId |
|
|
|
let nugetLibDir = |
|
let fallbackNugetLibDir = "D:\NugetLibs" |
|
match System.Environment.GetEnvironmentVariable("NugetLibDir") with |
|
| null -> fallbackNugetLibDir |
|
| dir -> dir |
|
|
|
let ipr packageId = |
|
installPackageAndCopyReferences nugetLibDir packageId |
|
|