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

#2419. Try parse SemVerInfo from .nupkg file name for localFolder nuget source #2422

Merged
merged 1 commit into from
Jun 16, 2017
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
25 changes: 19 additions & 6 deletions src/Paket.Core/Dependencies/NuGetLocal.fs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,18 @@ let getAllVersionsFromLocalPath (isCache, localNugetPath, package:PackageName, a
return Some(versions)
}


/// Reads packageName and version from .nupkg file name
let parsePackageInfoFromFileName fileName : (PackageName * SemVerInfo) option =
let regex = Regex ("^(?<name>.*?)\.(?<version>\d.*)\.nupkg$", RegexOptions.IgnoreCase)
match regex.Match fileName with
| matchResult when matchResult.Success && matchResult.Groups.Count = 3 ->
try
let semVer = SemVer.Parse matchResult.Groups.["version"].Value
let packageName = PackageName matchResult.Groups.["name"].Value
Some (packageName, semVer)
with
| _ -> None
| _ -> None

let findLocalPackage directory (packageName:PackageName) (version:SemVerInfo) =
if not <| Directory.Exists directory then
Expand All @@ -41,16 +52,18 @@ let findLocalPackage directory (packageName:PackageName) (version:SemVerInfo) =
let v2 = FileInfo(Path.Combine(directory, sprintf "%O.%s.nupkg" packageName normalizedVersion))
if v2.Exists then v2 else

let condition x =
match parsePackageInfoFromFileName x with
| Some (name, ver) -> packageName = name && version = ver
| None -> false

let v3 =
Directory.EnumerateFiles(directory,"*.nupkg",SearchOption.AllDirectories)
|> Seq.map (fun x -> FileInfo(x))
|> Seq.filter (fun fi -> String.containsIgnoreCase (packageName.CompareString) fi.Name)
|> Seq.filter (fun fi -> fi.Name.Contains(normalizedVersion) || fi.Name.Contains(version.ToString()))
|> Seq.tryHead
|> Seq.tryFind (Path.GetFileName >> condition)

match v3 with
| None -> failwithf "The package %O %O can't be found in %s.%sPlease check the feed definition in your paket.dependencies file." packageName version directory Environment.NewLine
| Some x -> x
| Some x -> FileInfo x

/// Reads package name from a nupkg file
let getPackageNameFromLocalFile fileName =
Expand Down
40 changes: 40 additions & 0 deletions tests/Paket.Tests/NuGetLocal/NuGetLocalSpecs.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
module Paket.NuGetLocalSpecs

open Paket.Domain
open NUnit.Framework
open FsUnit
open Paket.NuGetLocal
open System.IO


[<Test>]
let ``package file name split to id and version correctly``() =
[
"package.1.2.3.nupkg", Some (PackageName "package", SemVer.Parse "1.2.3")
"package.1.2.3-alpha2.nupkg", Some (PackageName "package", SemVer.Parse "1.2.3-alpha2")
"package.name.with.dots.1.2.3-alpha2.nupkg", Some (PackageName "package.name.with.dots", SemVer.Parse "1.2.3-alpha2")
"package.1.2.3.nupkg.back", None
"package.name.without.version.nupkg", None
]
|> List.iter
(fun (input, expectedResult) ->
let actualResult = parsePackageInfoFromFileName input
shouldEqual expectedResult actualResult)

[<Test>]
let ``package file picked up correctly``() =
[ // folder * inputPackageId * inputPackageVersion * expectedFileName
"./NuGetLocal/case1", PackageName "package.name", SemVer.Parse "0.1.0-alpha2", "package.name.0.1.0-alpha2.nupkg"
"./NuGetLocal/case2", PackageName "package.name", SemVer.Parse "0.1.0-alpha2", "package.name.0.1.0.0-alpha2.nupkg"

"./NuGetLocal/case1", PackageName "package.name", SemVer.Parse "1.2", "package.name.1.2.0.nupkg"
"./NuGetLocal/case1", PackageName "package.name", SemVer.Parse "0.1.2", "package.name.0.1.2.nupkg"

"./NuGetLocal/case2", PackageName "package.name", SemVer.Parse "1.2", "package.name.1.2.0.0.nupkg"

"./NuGetLocal", PackageName "package.in.nested.folder", SemVer.Parse "1.0", "package.in.nested.folder.1.0.0.nupkg"
]
|> List.iter
(fun (folder, packageId, packageVersion, expectedFileName) ->
let actualResult = findLocalPackage folder packageId packageVersion
shouldEqual expectedFileName actualResult.Name)
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
19 changes: 19 additions & 0 deletions tests/Paket.Tests/Paket.Tests.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
<Link>FsUnit.fs</Link>
</Compile>
<Compile Include="AssemblyInfo.fs" />
<Compile Include="NuGetLocal\NuGetLocalSpecs.fs" />
<Compile Include="TestHelpers.fs" />
<Compile Include="UtilsSpecs.fs" />
<Compile Include="Versioning\SemVerSpecs.fs" />
Expand Down Expand Up @@ -94,6 +95,24 @@
<None Include="Nuspec\WindowsAzure.Storage.nuspec" />
<None Include="Nuspec\EasyNetQ.nuspec" />
<Compile Include="Nuspec\NuspecSpecs.fs" />
<None Include="NuGetLocal\case1\package.name.0.1.0-alpha2.nupkg">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="NuGetLocal\case1\package.name.0.1.2.nupkg">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="NuGetLocal\case1\package.name.1.2.0.nupkg">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="NuGetLocal\case2\package.name.0.1.0.0-alpha2.nupkg">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="NuGetLocal\case2\package.name.1.2.0.0.nupkg">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="NuGetLocal\case3\netstedFolder\package.in.nested.folder.1.0.0.nupkg">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<Content Include="PackagesConfig\xunit.visualstudio.packages.config">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
Expand Down