Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow nuget packages to put version in the path #928

Merged
merged 3 commits into from
Jul 22, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion paket.dependencies
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ nuget Newtonsoft.Json
nuget UnionArgParser
nuget NUnit.Runners
nuget NUnit
nuget FAKE
nuget FAKE prerelease
nuget FSharp.Formatting
nuget FSharp.Core
nuget ILRepack
Expand Down
6 changes: 3 additions & 3 deletions paket.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
NUGET
remote: https://nuget.org/api/v2
specs:
FAKE (3.36.0)
FAKE (4.0.0-alpha004)
FSharp.Compiler.Service (1.3.1.0)
FSharp.Core (3.1.2.5)
FSharp.Formatting (2.9.10)
Expand All @@ -28,9 +28,9 @@ GITHUB
FsUnit.fs (81d27fd09575a32c4ed52eadb2eeac5f365b8348)
remote: fsharp/FAKE
specs:
modules/Octokit/Octokit.fsx (3bde7566d4514ce83e88d376141ee848847ae6e9)
modules/Octokit/Octokit.fsx (8b027106c29c4e05f30fe0dc3dbf6332293e05fa)
Octokit
src/app/FakeLib/Globbing/Globbing.fs (3bde7566d4514ce83e88d376141ee848847ae6e9)
src/app/FakeLib/Globbing/Globbing.fs (8b027106c29c4e05f30fe0dc3dbf6332293e05fa)
remote: fsprojects/Chessie
specs:
src/Chessie/ErrorHandling.fs (2e4d76714d6d01ac64913317870f05d7f7ae8e37)
3 changes: 3 additions & 0 deletions src/Paket.Core/Constants.fs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ let PaketVersionFileName = "paket.version"
[<Literal>]
let TemplateFile = "paket.template"

[<Literal>]
let PackagesConfigFile = "packages.config"

[<Literal>]
let FullProjectSourceFileName = "FULLPROJECT"

Expand Down
42 changes: 33 additions & 9 deletions src/Paket.Core/InstallProcess.fs
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,25 @@ open System.Collections.Generic
open FSharp.Polyfill
open System.Reflection
open System.Diagnostics
open Paket.PackagesConfigFile
open Paket.Requirements
open System.Security.AccessControl

let findPackageFolder root (PackageName name) =
let lowerName = name.ToLower()
let findPackageFolder root (PackageName name) (settings:InstallSettings,version:SemVerInfo) =
let includeVersionInPath = defaultArg settings.IncludeVersionInPath false
let lowerName = (name + if includeVersionInPath then "." + version.ToString() else "").ToLower()
let di = DirectoryInfo(Path.Combine(root, Constants.PackagesFolderName))
let direct = DirectoryInfo(Path.Combine(di.FullName, name))
let targetFolder = getTargetFolder root name version includeVersionInPath
let direct = DirectoryInfo(targetFolder)
if direct.Exists then direct else
match di.GetDirectories() |> Seq.tryFind (fun subDir -> subDir.FullName.ToLower().EndsWith(lowerName)) with
| Some x -> x
| None -> failwithf "Package directory for package %s was not found." name

let private findPackagesWithContent (root,usedPackages:Map<PackageName,InstallSettings>) =
let private findPackagesWithContent (root,usedPackages:Map<PackageName,InstallSettings*SemVerInfo>) =
usedPackages
|> Seq.filter (fun kv -> defaultArg kv.Value.OmitContent false |> not)
|> Seq.map (fun kv -> findPackageFolder root kv.Key)
|> Seq.filter (fun kv -> defaultArg (fst kv.Value).OmitContent false |> not)
|> Seq.map (fun kv -> findPackageFolder root kv.Key kv.Value)
|> Seq.choose (fun packageDir ->
packageDir.GetDirectories("Content")
|> Array.append (packageDir.GetDirectories("content"))
Expand Down Expand Up @@ -190,8 +193,7 @@ let InstallIntoProjects(sources, options : InstallerOptions, lockFile : LockFile
| None -> failwithf "%s uses NuGet package %O, but it was not found in the paket.lock file." referenceFile.FileName ps.Name

let resolvedSettings = [lockFile.Options.Settings; package.Settings] |> List.fold (+) ps.Settings
ps.Name, resolvedSettings
)
ps.Name, resolvedSettings)
|> Map.ofSeq


Expand Down Expand Up @@ -221,8 +223,30 @@ let InstallIntoProjects(sources, options : InstallerOptions, lockFile : LockFile
|> Seq.map (fun u -> NormalizedPackageName u.Key,u.Value)
|> Map.ofSeq


let usedPackageVersions =
usedPackages
|> Seq.map (fun u ->
let name = NormalizedPackageName u.Key
match packages |> Map.tryFind name with
| Some p -> u.Key,(u.Value,p.Version)
| None -> failwithf "%s uses NuGet package %O, but it was not found in the paket.lock file." referenceFile.FileName u.Key)
|> Map.ofSeq

project.UpdateReferences(model, usedPackageSettings, options.Hard)

let packagesConfigFile = Path.Combine(FileInfo(project.FileName).Directory.FullName, Constants.PackagesConfigFile)

usedPackageVersions
|> Seq.filter (fun kv -> defaultArg (fst kv.Value).IncludeVersionInPath false)
|> Seq.map (fun kv ->
let settings,version = kv.Value
{ Id = kv.Key.ToString()
Version = version
TargetFramework = None })
|> PackagesConfigFile.Save packagesConfigFile


removeCopiedFiles project

let gitRemoteItems =
Expand Down Expand Up @@ -256,7 +280,7 @@ let InstallIntoProjects(sources, options : InstallerOptions, lockFile : LockFile
Link = None })

let nuGetFileItems =
copyContentFiles(project, findPackagesWithContent(root,usedPackages))
copyContentFiles(project, findPackagesWithContent(root,usedPackageVersions))
|> List.map (fun file ->
{ BuildAction = project.DetermineBuildAction file.Name
Include = createRelativePath project.FileName file.FullName
Expand Down
15 changes: 7 additions & 8 deletions src/Paket.Core/NuGetV2.fs
Original file line number Diff line number Diff line change
Expand Up @@ -422,14 +422,13 @@ let ExtractPackage(fileName:string, targetFolder, name, version:SemVerInfo) =
return targetFolder
}

let CopyLicenseFromCache(root, cacheFileName, name, version:SemVerInfo, force) =
let CopyLicenseFromCache(root, cacheFileName, name, version:SemVerInfo, includeVersionInPath, force) =
async {
try
if String.IsNullOrWhiteSpace cacheFileName then return () else
let cacheFile = FileInfo cacheFileName
if cacheFile.Exists then
let targetFolder = DirectoryInfo(Path.Combine(root, Constants.PackagesFolderName, name)).FullName
let targetFile = FileInfo(Path.Combine(targetFolder, "license.html"))
let targetFile = FileInfo(Path.Combine(getTargetFolder root name version includeVersionInPath, "license.html"))
if not force && targetFile.Exists then
verbosefn "License %s %A already copied" name version
else
Expand All @@ -439,9 +438,9 @@ let CopyLicenseFromCache(root, cacheFileName, name, version:SemVerInfo, force) =
}

/// Extracts the given package to the ./packages folder
let CopyFromCache(root, cacheFileName, licenseCacheFile, name, version:SemVerInfo, force) =
let CopyFromCache(root, cacheFileName, licenseCacheFile, name, version:SemVerInfo, includeVersionInPath, force) =
async {
let targetFolder = DirectoryInfo(Path.Combine(root, Constants.PackagesFolderName, name)).FullName
let targetFolder = DirectoryInfo(getTargetFolder root name version includeVersionInPath).FullName
let fi = FileInfo(cacheFileName)
let targetFile = FileInfo(Path.Combine(targetFolder, fi.Name))
if not force && targetFile.Exists then
Expand All @@ -451,7 +450,7 @@ let CopyFromCache(root, cacheFileName, licenseCacheFile, name, version:SemVerInf
File.Copy(cacheFileName, targetFile.FullName)
try
let! extracted = ExtractPackage(targetFile.FullName,targetFolder,name,version)
do! CopyLicenseFromCache(root, licenseCacheFile, name, version, force)
do! CopyLicenseFromCache(root, licenseCacheFile, name, version, includeVersionInPath, force)
return extracted
with
| exn ->
Expand Down Expand Up @@ -499,7 +498,7 @@ let DownloadLicense(root,force,name,version:SemVerInfo,licenseUrl,targetFileName
}

/// Downloads the given package to the NuGet Cache folder
let DownloadPackage(root, auth, url, name, version:SemVerInfo, force) =
let DownloadPackage(root, auth, url, name, version:SemVerInfo, includeVersionInPath, force) =
async {
let targetFileName = Path.Combine(CacheFolder, name + "." + version.Normalize() + ".nupkg")
let targetFile = FileInfo targetFileName
Expand Down Expand Up @@ -556,7 +555,7 @@ let DownloadPackage(root, auth, url, name, version:SemVerInfo, force) =
with
| exn -> failwithf "Could not download %s %A.%s %s" name version Environment.NewLine exn.Message

return! CopyFromCache(root, targetFile.FullName, licenseFileName, name, version, force)
return! CopyFromCache(root, targetFile.FullName, licenseFileName, name, version, includeVersionInPath, force)
}

/// Finds all libraries in a nuget package.
Expand Down
20 changes: 4 additions & 16 deletions src/Paket.Core/NugetConvert.fs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ open Paket.NuGetV2
open Paket.PackageSources
open Paket.Requirements
open Chessie.ErrorHandling
open Paket.PackagesConfigFile

type CredsMigrationMode =
| Encrypt
Expand Down Expand Up @@ -44,12 +45,6 @@ type CredsMigrationMode =
/// Represents type of NuGet packages.config file
type NugetPackagesConfigType = ProjectLevel | SolutionLevel

type NugetPackage = {
Id : string
Version : SemVerInfo
TargetFramework : string option
}

/// Represents NuGet packages.config file
type NugetPackagesConfig = {
File: FileInfo
Expand Down Expand Up @@ -179,23 +174,16 @@ module NugetEnv =

let readNugetPackages(rootDirectory : DirectoryInfo) =
let readSingle(file : FileInfo) =
try
let doc = XmlDocument()
doc.Load file.FullName

try
{ File = file
Type = if file.Directory.Name = ".nuget" then SolutionLevel else ProjectLevel
Packages = [for node in doc.SelectNodes("//package") ->
{ Id = node.Attributes.["id"].Value
Version = node.Attributes.["version"].Value |> SemVer.Parse
TargetFramework =
node |> getAttribute "targetFramework" |> Option.map (fun t -> ">= " + t) } ]}
Packages = PackagesConfigFile.Read file.FullName }
|> ok
with _ -> fail (NugetPackagesConfigParseError file)

ProjectFile.FindAllProjects rootDirectory.FullName
|> List.ofArray
|> List.map (fun p -> p, Path.Combine(Path.GetDirectoryName(p.FileName), "packages.config"))
|> List.map (fun p -> p, Path.Combine(Path.GetDirectoryName(p.FileName), Constants.PackagesConfigFile))
|> List.filter (fun (p,packages) -> File.Exists packages)
|> List.map (fun (p,packages) -> readSingle(FileInfo(packages)) |> lift (fun packages -> (p,packages)))
|> collect
Expand Down
60 changes: 60 additions & 0 deletions src/Paket.Core/PackagesConfigFile.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
module Paket.PackagesConfigFile

open Paket
open System
open System.IO
open System.Xml
open Paket.Xml
open Paket.Domain
open Chessie.ErrorHandling

type NugetPackage = {
Id : string
Version : SemVerInfo
TargetFramework : string option
}

let Read fileName =
let file = FileInfo fileName
if file.Exists |> not then [] else
let doc = XmlDocument()
doc.Load file.FullName

[for node in doc.SelectNodes("//package") ->
{ Id = node.Attributes.["id"].Value
Version = SemVer.Parse node.Attributes.["version"].Value
TargetFramework =
node
|> getAttribute "targetFramework"
|> Option.map (fun t -> ">= " + t) } ]

let Serialize (packages: NugetPackage seq) =
if Seq.isEmpty packages then "" else
let packages =
packages
|> Seq.map (fun p ->
let framework =
match p.TargetFramework with
| Some tf -> sprintf "targetFramework=\"%s\" " (tf.Replace(">= ",""))
| _ -> ""

sprintf """ <package id="%s" version="%O" %s/>""" p.Id p.Version framework)

sprintf """<?xml version="1.0" encoding="utf-8"?>
<packages>
%s
</packages>""" (String.Join(Environment.NewLine,packages))

let Save fileName packages =
let original =
if File.Exists fileName then
File.ReadAllText fileName |> normalizeLineEndings
else ""

let newFile = Serialize packages |> normalizeLineEndings

if newFile = "" then
if File.Exists fileName then File.Delete fileName
else
if newFile <> original then
File.WriteAllText(fileName,newFile)
1 change: 1 addition & 0 deletions src/Paket.Core/Paket.Core.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@
<Compile Include="TemplateFile.fs" />
<Compile Include="NupkgWriter.fs" />
<Compile Include="ProcessOptions.fs" />
<Compile Include="PackagesConfigFile.fs" />
<Compile Include="InstallProcess.fs" />
<Compile Include="UpdateProcess.fs" />
<Compile Include="RemoveProcess.fs" />
Expand Down
2 changes: 1 addition & 1 deletion src/Paket.Core/ProjectFile.fs
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ type ProjectFile =
(this.Document |> getDescendants "None") @
(this.Document |> getDescendants "Content")

match noneAndContentNodes |> List.tryFind (withAttributeValue "Include" "packages.config") with
match noneAndContentNodes |> List.tryFind (withAttributeValue "Include" Constants.PackagesConfigFile) with
| None -> ()
| Some nugetNode ->
match noneAndContentNodes |> List.filter (withAttributeValue "Include" Constants.ReferencesFile) with
Expand Down
5 changes: 3 additions & 2 deletions src/Paket.Core/ReferencesFile.fs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ type ReferencesFile =
let lines = File.ReadAllLines(fileName)
{ ReferencesFile.FromLines lines with FileName = fileName }

member this.AddNuGetReference(packageName : PackageName, copyLocal: bool, importTargets: bool, frameworkRestrictions, omitContent : bool) =
member this.AddNuGetReference(packageName : PackageName, copyLocal: bool, importTargets: bool, frameworkRestrictions, includeVersionInPath, omitContent : bool) =
let (PackageName referenceName) = packageName
let normalized = NormalizedPackageName packageName
if this.NugetPackages |> Seq.exists (fun p -> NormalizedPackageName p.Name = normalized) then
Expand All @@ -84,11 +84,12 @@ type ReferencesFile =
{ CopyLocal = if not copyLocal then Some copyLocal else None
ImportTargets = if not importTargets then Some importTargets else None
FrameworkRestrictions = frameworkRestrictions
IncludeVersionInPath = if includeVersionInPath then Some includeVersionInPath else None
OmitContent = if omitContent then Some omitContent else None } }

{ this with NugetPackages = this.NugetPackages @ [ package ] }

member this.AddNuGetReference(packageName : PackageName) = this.AddNuGetReference(packageName, true, true, [], false)
member this.AddNuGetReference(packageName : PackageName) = this.AddNuGetReference(packageName, true, true, [], false, false)

member this.RemoveNuGetReference(packageName : PackageName) =
let (PackageName referenceName) = packageName
Expand Down
11 changes: 11 additions & 0 deletions src/Paket.Core/Requirements.fs
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,14 @@ type InstallSettings =
{ ImportTargets : bool option
FrameworkRestrictions: FrameworkRestrictions
OmitContent : bool option
IncludeVersionInPath: bool option
CopyLocal : bool option }

static member Default =
{ CopyLocal = None
ImportTargets = None
FrameworkRestrictions = []
IncludeVersionInPath = None
OmitContent = None }

member this.ToString(asLines) =
Expand All @@ -148,6 +150,9 @@ type InstallSettings =
| Some true -> yield "content: none"
| Some false -> yield "content: true"
| None -> ()
match this.IncludeVersionInPath with
| Some x -> yield "version_in_path: " + x.ToString().ToLower()
| None -> ()
match this.FrameworkRestrictions with
| [] -> ()
| _ -> yield "framework: " + (String.Join(", ",this.FrameworkRestrictions))]
Expand All @@ -164,6 +169,7 @@ type InstallSettings =
FrameworkRestrictions = (self.FrameworkRestrictions @ other.FrameworkRestrictions) |> Seq.ofList |> Seq.distinct |> List.ofSeq
OmitContent = self.OmitContent ++ other.OmitContent
CopyLocal = self.CopyLocal ++ other.CopyLocal
IncludeVersionInPath = self.IncludeVersionInPath ++ other.IncludeVersionInPath
}

static member Parse(text:string) : InstallSettings =
Expand All @@ -183,6 +189,11 @@ type InstallSettings =
| true, "none" -> Some true
| true, "true" -> Some false
| _ -> None
IncludeVersionInPath =
match kvPairs.TryGetValue "version_in_path" with
| true, "false" -> Some false
| true, "true" -> Some true
| _ -> None
CopyLocal =
match kvPairs.TryGetValue "copy_local" with
| true, "false" -> Some false
Expand Down
Loading