Skip to content

Commit

Permalink
Cache path extraction - references #487
Browse files Browse the repository at this point in the history
  • Loading branch information
forki committed Jan 2, 2015
1 parent 85eebe3 commit 94cc943
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 83 deletions.
3 changes: 3 additions & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#### 0.20.17 - 02.01.2015
* PERFORMANCE: Cache path extraction - https://github.com/fsprojects/Paket/issues/487

#### 0.20.16 - 02.01.2015
* Trace stacktrace on all NullReferenceExceptions - https://github.com/fsprojects/Paket/issues/500

Expand Down
102 changes: 57 additions & 45 deletions src/Paket.Core/FrameworkHandling.fs
Original file line number Diff line number Diff line change
Expand Up @@ -67,38 +67,6 @@ type FrameworkIdentifier =
| WindowsPhoneSilverlight of string
| Silverlight of string

static member Extract(path:string) =

let path =
let sb = new Text.StringBuilder(path.ToLower())
for pattern,replacement in KnownAliases.Data do
sb.Replace(pattern,replacement) |> ignore
sb.ToString()

match path with
| "net10" | "net1" | "10" -> Some (DotNetFramework FrameworkVersion.V1)
| "net11" | "11" -> Some (DotNetFramework FrameworkVersion.V1_1)
| "net20" | "net2" | "net" | "net20-full" | "20" -> Some (DotNetFramework FrameworkVersion.V2)
| "net35" | "net35-full" | "35" -> Some (DotNetFramework FrameworkVersion.V3_5)
| "net40" | "net4" | "40" | "net40-client" | "net4-client" -> Some (DotNetFramework FrameworkVersion.V4_Client)
| "net40-full" | "net403" -> Some (DotNetFramework FrameworkVersion.V4)
| "net45" | "net45-full" | "45" -> Some (DotNetFramework FrameworkVersion.V4_5)
| "net451" -> Some (DotNetFramework FrameworkVersion.V4_5_1)
| "net452" -> Some (DotNetFramework FrameworkVersion.V4_5_2)
| "net453" -> Some (DotNetFramework FrameworkVersion.V4_5_3)
| "monotouch" | "monotouch10" -> Some MonoTouch
| "monoandroid" | "monoandroid10" -> Some MonoAndroid
| "monomac" | "monomac10" -> Some MonoMac
| "sl3" | "sl30" -> Some (Silverlight "v3.0")
| "sl4" | "sl40" -> Some (Silverlight "v4.0")
| "sl5" | "sl50" -> Some (Silverlight "v5.0")
| "win8" | "win80" | "netcore45" | "win" -> Some (Windows "v4.5")
| "win81" | "netcore46" -> Some (Windows "v4.5.1")
| "wp7" | "wp70" | "sl4-wp7"| "sl4-wp70" -> Some (WindowsPhoneSilverlight "v7.0")
| "wp71" | "sl4-wp71" | "sl4-wp" -> Some (WindowsPhoneSilverlight "v7.1")
| "wp8" | "wp80" -> Some (WindowsPhoneSilverlight "v8.0")
| "wpa00" | "wpa81" -> Some (WindowsPhoneApp "v8.1")
| _ -> None

override x.ToString() =
match x with
Expand All @@ -124,19 +92,6 @@ type FrameworkIdentifier =
| WindowsPhoneSilverlight v -> "wp" + v
| Silverlight v -> "sl" + v

static member DetectFromPath(path : string) : FrameworkIdentifier option =

let path = path.Replace("\\", "/").ToLower()
let fi = new FileInfo(path)

if path.Contains("lib/" + fi.Name.ToLower()) then Some(DotNetFramework(FrameworkVersion.V1))
else
let startPos = path.LastIndexOf("lib/")
let endPos = path.LastIndexOf(fi.Name.ToLower())
if startPos < 0 || endPos < 0 then None
else
path.Substring(startPos + 4, endPos - startPos - 5)
|> FrameworkIdentifier.Extract

// returns a list of compatible platforms that this platform also supports
member x.SupportedPlatforms =
Expand Down Expand Up @@ -172,6 +127,63 @@ type FrameworkIdentifier =
| WindowsPhoneApp _ -> [ WindowsPhoneApp "v8.1" ]
| WindowsPhoneSilverlight _ -> [ WindowsPhoneSilverlight "v8.1" ]


module FrameworkDetection =
let private cache = System.Collections.Generic.Dictionary<_,_>()

let Extract(path:string) =
match cache.TryGetValue path with
| true,x -> x
| _ ->
let path =
let sb = new Text.StringBuilder(path.ToLower())
for pattern,replacement in KnownAliases.Data do
sb.Replace(pattern,replacement) |> ignore
sb.ToString()

let result =
match path with
| "net10" | "net1" | "10" -> Some (DotNetFramework FrameworkVersion.V1)
| "net11" | "11" -> Some (DotNetFramework FrameworkVersion.V1_1)
| "net20" | "net2" | "net" | "net20-full" | "20" -> Some (DotNetFramework FrameworkVersion.V2)
| "net35" | "net35-full" | "35" -> Some (DotNetFramework FrameworkVersion.V3_5)
| "net40" | "net4" | "40" | "net40-client" | "net4-client" -> Some (DotNetFramework FrameworkVersion.V4_Client)
| "net40-full" | "net403" -> Some (DotNetFramework FrameworkVersion.V4)
| "net45" | "net45-full" | "45" -> Some (DotNetFramework FrameworkVersion.V4_5)
| "net451" -> Some (DotNetFramework FrameworkVersion.V4_5_1)
| "net452" -> Some (DotNetFramework FrameworkVersion.V4_5_2)
| "net453" -> Some (DotNetFramework FrameworkVersion.V4_5_3)
| "monotouch" | "monotouch10" -> Some MonoTouch
| "monoandroid" | "monoandroid10" -> Some MonoAndroid
| "monomac" | "monomac10" -> Some MonoMac
| "sl3" | "sl30" -> Some (Silverlight "v3.0")
| "sl4" | "sl40" -> Some (Silverlight "v4.0")
| "sl5" | "sl50" -> Some (Silverlight "v5.0")
| "win8" | "win80" | "netcore45" | "win" -> Some (Windows "v4.5")
| "win81" | "netcore46" -> Some (Windows "v4.5.1")
| "wp7" | "wp70" | "sl4-wp7"| "sl4-wp70" -> Some (WindowsPhoneSilverlight "v7.0")
| "wp71" | "sl4-wp71" | "sl4-wp" -> Some (WindowsPhoneSilverlight "v7.1")
| "wp8" | "wp80" -> Some (WindowsPhoneSilverlight "v8.0")
| "wpa00" | "wpa81" -> Some (WindowsPhoneApp "v8.1")
| _ -> None

cache.[path] <- result
result

let DetectFromPath(path : string) : FrameworkIdentifier option =
let path = path.Replace("\\", "/").ToLower()
let fi = new FileInfo(path)

if path.Contains("lib/" + fi.Name.ToLower()) then Some(DotNetFramework(FrameworkVersion.V1))
else
let startPos = path.LastIndexOf("lib/")
let endPos = path.LastIndexOf(fi.Name.ToLower())
if startPos < 0 || endPos < 0 then None
else
path.Substring(startPos + 4, endPos - startPos - 5)
|> Extract


type TargetProfile =
| SinglePlatform of FrameworkIdentifier
| PortableProfile of string * FrameworkIdentifier list
Expand Down
4 changes: 2 additions & 2 deletions src/Paket.Core/LockFile.fs
Original file line number Diff line number Diff line change
Expand Up @@ -197,14 +197,14 @@ module LockFileParser =
operatorSplit.[0]
else
operatorSplit.[1]
match FrameworkIdentifier.Extract(framework) with
match FrameworkDetection.Extract(framework) with
| None -> ()
| Some x ->
if operatorSplit.[0] = ">=" then
if operatorSplit.Length < 4 then
yield FrameworkRestriction.AtLeast x
else
match FrameworkIdentifier.Extract(operatorSplit.[3]) with
match FrameworkDetection.Extract(operatorSplit.[3]) with
| None -> ()
| Some y -> yield FrameworkRestriction.Between(x,y)
else
Expand Down
2 changes: 1 addition & 1 deletion src/Paket.Core/NuGetV2.fs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ let parseODataDetails(nugetURL,packageName,version,raw) =
a.[0],
(if a.Length > 1 then a.[1] else "0"),
(if a.Length > 2 && a.[2] <> "" then
match FrameworkIdentifier.Extract a.[2] with
match FrameworkDetection.Extract a.[2] with
| Some x -> [FrameworkRestriction.Exactly x]
| None -> []
else
Expand Down
6 changes: 3 additions & 3 deletions src/Paket.Core/Nuspec.fs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ type Nuspec =
let parent = node.ParentNode
match parent.Name.ToLower(), parent |> getAttribute "targetFramework" with
| "group", Some framework ->
match FrameworkIdentifier.Extract framework with
match FrameworkDetection.Extract framework with
| Some x -> [FrameworkRestriction.Exactly x]
| None -> []
| _ -> []
Expand All @@ -126,7 +126,7 @@ type Nuspec =
|> Seq.map (fun node ->
match node |> getAttribute "targetFramework" with
| Some framework ->
match FrameworkIdentifier.Extract framework with
match FrameworkDetection.Extract framework with
| Some x -> [PackageName "",VersionRequirement.NoRestriction,[FrameworkRestriction.Exactly x]]
| None -> []
| _ -> [])
Expand Down Expand Up @@ -155,7 +155,7 @@ type Nuspec =
[{ AssemblyName = name; FrameworkRestrictions = [] }]
| Some name, Some targetFrameworks ->
targetFrameworks.Split([|','; ' '|],System.StringSplitOptions.RemoveEmptyEntries)
|> Array.choose FrameworkIdentifier.Extract
|> Array.choose FrameworkDetection.Extract
|> Array.map (fun fw -> { AssemblyName = name; FrameworkRestrictions = [FrameworkRestriction.Exactly fw] })
|> Array.toList
| _ -> []
Expand Down
2 changes: 1 addition & 1 deletion src/Paket.Core/PlatformMatching.fs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ let inline split (path : string) =
path.Split('+')
|> Array.map (fun s -> s.Replace("portable-", ""))

let inline extractPlatforms path = split path |> Array.choose FrameworkIdentifier.Extract
let inline extractPlatforms path = split path |> Array.choose FrameworkDetection.Extract

let private penalties = Collections.Generic.Dictionary<_,_>()

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 @@ -320,7 +320,7 @@ type ProjectFile =
outputType.InnerText }
|> Seq.map (fun s -> // TODO make this a separate function
s.Replace("v","net")
|> FrameworkIdentifier.Extract)
|> FrameworkDetection.Extract)
|> Seq.map (fun o -> o.Value)
|> Seq.head

Expand Down
6 changes: 3 additions & 3 deletions tests/Paket.Tests/InstallModel/FrameworkIdentifierSpecs.fs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ open FsUnit

[<Test>]
let ``should understand basic framework versions net20, net40, net45 ...``() =
FrameworkIdentifier.Extract("net20").Value |> shouldEqual (DotNetFramework(FrameworkVersion.V2))
FrameworkIdentifier.Extract("net40").Value |> shouldEqual (DotNetFramework(FrameworkVersion.V4_Client))
FrameworkIdentifier.Extract("net45").Value |> shouldEqual (DotNetFramework(FrameworkVersion.V4_5))
FrameworkDetection.Extract("net20").Value |> shouldEqual (DotNetFramework(FrameworkVersion.V2))
FrameworkDetection.Extract("net40").Value |> shouldEqual (DotNetFramework(FrameworkVersion.V4_Client))
FrameworkDetection.Extract("net45").Value |> shouldEqual (DotNetFramework(FrameworkVersion.V4_5))

[<Test>]
let ``should serialize basic framework versions net20, net40, net45 ...``() =
Expand Down
Loading

0 comments on commit 94cc943

Please sign in to comment.