-
Notifications
You must be signed in to change notification settings - Fork 526
/
Copy pathDependenciesFile.fs
865 lines (734 loc) · 43.9 KB
/
DependenciesFile.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
namespace Paket
/// [omit]
module DependenciesFileSerializer =
open Requirements
open System
let formatVersionRange strategy (versionRequirement : VersionRequirement) : string =
let prefix =
match strategy with
| Some ResolverStrategy.Min -> "!"
| Some ResolverStrategy.Max -> "@"
| None -> ""
let preReleases =
match versionRequirement.PreReleases with
| No -> ""
| PreReleaseStatus.All -> "prerelease"
| Concrete list -> String.Join(" ",list)
let version =
match versionRequirement.Range with
| Minimum x when strategy = None && x = SemVer.Zero -> ""
| Minimum x -> ">= " + x.ToString()
| GreaterThan x -> "> " + x.ToString()
| Specific x when strategy = None -> x.ToString()
| Specific x -> "= " + x.ToString()
| VersionRange.Range(_, from, _, _)
when DependenciesFileParser.parseVersionRequirement ("~> " + from.ToString() + " " + preReleases) = versionRequirement ->
"~> " + from.ToString()
| _ -> versionRequirement.ToString()
let text = prefix + version
if text <> "" && preReleases <> "" then text + " " + preReleases else text + preReleases
let sourceString source = "source " + source
let packageString kind packageName versionRequirement resolverStrategy (settings:InstallSettings) =
let version = formatVersionRange resolverStrategy versionRequirement
let s = settings.ToString()
let kindString =
match kind with
| PackageRequirementKind.DotnetCliTool -> "clitool"
| PackageRequirementKind.Package -> "nuget"
sprintf "%s %O%s%s" kindString packageName (if version <> "" then " " + version else "") (if s <> "" then " " + s else s)
let githubString repository file version =
sprintf "github %s%s%s" repository (if version <> "" then ":" + version else "") (if file <> "" then " " + file else "")
let gitString repository version =
sprintf "git %s%s" repository (if version <> "" then " " + version else "")
open Domain
open System
open Requirements
open ModuleResolver
open System.IO
open Logging
open PackageResolver
/// Allows to parse and analyze paket.dependencies files.
type DependenciesFile(fileName,groups:Map<GroupName,DependenciesGroup>, textRepresentation:string []) =
let tryMatchPackageLine packageNamePredicate (line : string) =
let tokens = line.Split([|' '|], StringSplitOptions.RemoveEmptyEntries) |> Array.map (fun s -> s.ToLowerInvariant().Trim())
match List.ofArray tokens with
| "nuget"::packageName::_ when packageNamePredicate packageName -> Some packageName
| _ -> None
let isPackageLine name line = tryMatchPackageLine ((=) name) line |> Option.isSome
let findGroupBorders groupName (textRepresentation:System.Collections.Generic.List<string>) =
let _,_,firstLine,lastLine =
textRepresentation
|> Seq.fold (fun (i,currentGroup,firstLine,lastLine) line ->
if line.TrimStart().StartsWith "group " then
let group = line.Replace("group","").Trim()
if currentGroup = groupName then
i+1,GroupName group,firstLine,(i - 1)
else
if GroupName group = groupName then
i+1,GroupName group,(i + 1),lastLine
else
i+1,GroupName group,firstLine,lastLine
else
i+1,currentGroup,firstLine,lastLine)
(0,Constants.MainDependencyGroup,0,textRepresentation.Count)
firstLine,lastLine
let tryFindPackageLine groupName (packageName:PackageName) =
let name = packageName.CompareString
let _,_,found =
textRepresentation
|> Array.fold (fun (i,currentGroup,found) line ->
match found with
| Some _ -> i+1,currentGroup,found
| None ->
if currentGroup = groupName && isPackageLine name line then
i+1,currentGroup,Some i
else
if line.StartsWith "group " then
let group = line.Replace("group","").Trim()
i+1,GroupName group,found
else
i+1,currentGroup,found)
(0,Constants.MainDependencyGroup,None)
found
member __.FileName = fileName
member __.FileInfo =
try Some (FileInfo fileName )
with _ -> None
member __.Groups = groups
/// Directory info for the parent of this paket.dependencies file
member this.DirectoryInfo =
try Some this.FileInfo.Value.Directory
with _ -> None
/// The full path of the directory that containes this paket.dependencies file
member this.Directory =
try this.DirectoryInfo.Value.FullName
with _ -> String.Empty
member __.Lines = textRepresentation
member __.RootPath =
if String.IsNullOrWhiteSpace fileName then String.Empty
else FileInfo(fileName).Directory.FullName
/// Returns all direct NuGet dependencies in the given group.
member __.GetDependenciesInGroup(groupName:GroupName) =
match groups |> Map.tryFind groupName with
| None -> failwithf "Group %O doesn't exist in the paket.dependencies file." groupName
| Some group ->
group.Packages
|> Seq.map (fun p -> p.Name, p.VersionRequirement)
|> Map.ofSeq
member __.CheckIfPackageExistsInAnyGroup (packageName:PackageName) =
match groups |> Seq.tryFind (fun g -> g.Value.Packages |> List.exists (fun p -> p.Name = packageName)) with
| Some group -> sprintf "%sHowever, %O was found in group %O." Environment.NewLine packageName group.Value.Name
| None -> ""
member this.SimplifyFrameworkRestrictions() =
let transform (dependenciesFile:DependenciesFile) (group:DependenciesGroup) =
let getRestrictionList =
let projectFrameworks = lazy (
let lockFile = dependenciesFile.FindLockFile()
let dir = (lockFile : FileInfo).DirectoryName
let projects = ProjectFile.FindAllProjects dir
let frameworks = projects |> Seq.collect ProjectFile.getTargetProfiles |> Seq.distinct
let restrictions = frameworks |> Seq.map FrameworkRestriction.ExactlyPlatform
if restrictions |> Seq.isEmpty then FrameworkRestriction.NoRestriction
else restrictions |> Seq.fold FrameworkRestriction.combineRestrictionsWithOr FrameworkRestriction.EmptySet
)
fun restrictions ->
match restrictions with
| ExplicitRestriction l -> l
| AutoDetectFramework -> projectFrameworks.Force()
if group.Options.Settings.FrameworkRestrictions |> getRestrictionList <> FrameworkRestriction.NoRestriction then dependenciesFile else
match group.Packages with
| [] -> dependenciesFile
| package::rest -> dependenciesFile
// TODO: Fix simplifier
//let commonRestrictions =
// group.Packages
// |> Seq.map (fun p' -> p'.Settings.FrameworkRestrictions)
// |> Seq.fold (fun a b -> FrameworkRestriction.And [a;b.GetExplicitRestriction()]) FrameworkRestriction.EmptySet
// //package.Settings.FrameworkRestrictions
// //|> getRestrictionList
// //|> List.filter (fun r ->
// // rest |> Seq.forall (fun p' -> p'.Settings.FrameworkRestrictions |> getRestrictionList |> List.contains r))
//
//match commonRestrictions.RepresentedFrameworks with
//| [] -> dependenciesFile
//// Only take when our commonRestriction formula is actually "simple"
//| _ when commonRestrictions.OrFormulas |> List.forall (fun andFormula -> andFormula.Literals.Length <= 1) ->
// let newDependenciesFile = dependenciesFile.AddFrameworkRestriction(group.Name,commonRestrictions)
// let newNegatedLiterals =
// commonRestrictions.OrFormulas |> List.collect (fun andFormala -> andFormala.Literals)
// |> List.map (fun lit -> FrameworkRestriction.FromLiteral { lit with IsNegated = not lit.IsNegated })
// |> List.fold (fun a b -> FrameworkRestriction.Or [ a; b]) FrameworkRestriction.EmptySet
//
// group.Packages
// |> List.fold (fun (d:DependenciesFile) package ->
// let oldRestrictions = package.Settings.FrameworkRestrictions |> getRestrictionList
// let newRestrictions = FrameworkRestriction.Or [ oldRestrictions; newNegatedLiterals ]// |> List.filter (fun r -> commonRestrictions |> List.contains r |> not)
// if oldRestrictions = newRestrictions then d else
// let (d:DependenciesFile) = d.Remove(group.Name,package.Name)
// let installSettings = { package.Settings with FrameworkRestrictions = ExplicitRestriction newRestrictions }
// let vr = { VersionRequirement = package.VersionRequirement; ResolverStrategy = package.ResolverStrategyForDirectDependencies }
// d.AddAdditionalPackage(group.Name, package.Name,vr,installSettings)) newDependenciesFile
//| _ -> dependenciesFile
this.Groups
|> Seq.map (fun kv -> kv.Value)
|> Seq.fold transform this
member this.GetGroup groupName =
match this.Groups |> Map.tryFind groupName with
| Some g -> g
| None -> failwithf "Group %O was not found in %s." groupName fileName
member __.HasPackage (groupName, name : PackageName) =
match groups |> Map.tryFind groupName with
| None -> false
| Some g -> g.Packages |> List.exists (fun p -> p.Name = name)
member __.GetPackage (groupName, name : PackageName) = groups.[groupName].Packages |> List.find (fun p -> p.Name = name)
member self.TryGetPackage (groupName, name : PackageName) =
try self.GetPackage (groupName,name) |> Some
with _ -> None
member this.Resolve(force, getSha1, getVersionF, getPreferredVersionF, getPackageDetailsF, getPackageRuntimeGraph, groupsToResolve:Map<GroupName,_>, updateMode) =
let resolveGroup groupName _ =
let group = this.GetGroup groupName
let storageConfig = group.Options.Settings.StorageConfig
let resolveSourceFile (file:ResolvedSourceFile) : PackageRequirement list * UnresolvedSource list =
let remoteDependenciesFile =
RemoteDownload.downloadDependenciesFile(force,Path.GetDirectoryName fileName, groupName, DependenciesFile.FromSource, file)
|> Async.RunSynchronously
// We do not support groups in reference files yet
let mainGroup = remoteDependenciesFile.Groups.[Constants.MainDependencyGroup]
mainGroup.Packages,mainGroup.RemoteFiles
let remoteFiles = ModuleResolver.Resolve(resolveSourceFile,getSha1,group.RemoteFiles)
let remoteDependencies =
remoteFiles
|> List.map (fun f -> f.Dependencies)
|> List.fold Set.union Set.empty
|> Seq.map (fun (n, v) ->
{ Name = n
VersionRequirement = v
ResolverStrategyForDirectDependencies = Some ResolverStrategy.Max
ResolverStrategyForTransitives = Some ResolverStrategy.Max
Parent = PackageRequirementSource.DependenciesFile(fileName,0)
Graph = Set.empty
Sources = group.Sources
Kind = PackageRequirementKind.Package
TransitivePrereleases = v.PreReleases <> PreReleaseStatus.No
Settings = group.Options.Settings })
|> Seq.toList
let externalLockDependencies =
group.ExternalLocks
|> List.map (fun x ->
if x.ToLower().StartsWith("http://") || x.ToLower().StartsWith("https://") then
let auth = if x.ToLower().Contains("://github.com") then
Environment.GetEnvironmentVariable "PAKET_GITHUB_API_TOKEN" |> fun x -> if isNull(x) then None else Some(Token(x))
else
None
let client = createHttpClient(x, auth)
let folder = DirectoryInfo(Path.Combine(Path.GetTempPath(),"external_paket",(hash x).ToString()))
if not folder.Exists then
folder.Create()
let fileName = Path.Combine(folder.FullName,"paket.lock")
client.DownloadFile(x,fileName)
x, LockFile.LoadFrom fileName
else
let fi = try FileInfo x with | exn -> failwithf "The external lock file %s that was referenced in group %O has invalid path. Message: %s" x group.Name exn.Message
if not (File.Exists fi.FullName) then
failwithf "The external lock file %s that was referenced in group %O does not exist." fi.FullName group.Name
x, LockFile.LoadFrom fi.FullName)
|> Seq.collect (fun (lockName, lockFile) ->
match lockFile.Groups |> Map.tryFind group.Name with
| None -> Seq.empty
| Some externalGroup ->
externalGroup.Resolution
|> Seq.map (fun kv ->
let p = kv.Value
{ Name = p.Name
VersionRequirement =
VersionRequirement.VersionRequirement(
VersionRange.Specific p.Version,
match p.Version.PreRelease with
| Some pre -> PreReleaseStatus.All // common dependencies must be present in the same paket.lock anyway
| None -> PreReleaseStatus.No )
ResolverStrategyForDirectDependencies = Some ResolverStrategy.Max
ResolverStrategyForTransitives = Some ResolverStrategy.Max
Parent = PackageRequirementSource.DependenciesLock(fileName,lockName)
Graph = Set.empty
Sources = group.Sources
Kind = PackageRequirementKind.Package
TransitivePrereleases = p.Version.PreRelease <> None
Settings = group.Options.Settings }))
|> Seq.toList
if String.IsNullOrWhiteSpace fileName |> not then
RemoteDownload.DownloadSourceFiles(Path.GetDirectoryName fileName, groupName, force, remoteFiles)
// 1. Package resolution
let step1Deps = remoteDependencies @ externalLockDependencies @ group.Packages |> Set.ofList
let resolution =
PackageResolver.Resolve(
getVersionF,
getPreferredVersionF,
getPackageDetailsF,
groupName,
group.Options.ResolverStrategyForDirectDependencies,
group.Options.ResolverStrategyForTransitives,
group.Options.Settings.FrameworkRestrictions,
step1Deps,
updateMode)
// 2. Runtime package resolution, see https://github.com/fsprojects/Paket/pull/2255
let runtimeResolution =
match resolution with
| Resolution.Ok resolved when Environment.GetEnvironmentVariable "PAKET_DISABLE_RUNTIME_RESOLUTION" <> "true" ->
tracefn "Calculating the runtime graph..."
// We first need to calculate the graph, note that this might already download packages into the cache...
let runtimeGraph =
resolved
|> Map.toSeq |> Seq.map snd
|> Seq.choose (getPackageRuntimeGraph storageConfig groupName)
|> RuntimeGraph.mergeSeq
// now we need to get the runtime deps and add them to the resolution
let rids = RuntimeGraph.getKnownRids runtimeGraph
let runtimeDeps =
resolved
|> Map.toSeq |> Seq.map snd
|> Seq.collect (fun p ->
rids
|> Seq.collect(fun rid -> RuntimeGraph.findRuntimeDependencies rid p.Name runtimeGraph))
|> Seq.map (fun (name, versionReq) ->
{ Name = name
VersionRequirement = versionReq
ResolverStrategyForDirectDependencies = group.Options.ResolverStrategyForDirectDependencies
ResolverStrategyForTransitives = group.Options.ResolverStrategyForTransitives
Parent = PackageRequirementSource.RuntimeDependency
Graph = Set.empty
Sources = group.Sources
Kind = PackageRequirementKind.Package
TransitivePrereleases = versionReq.PreReleases <> PreReleaseStatus.No
Settings = group.Options.Settings })
|> Seq.toList
// We want to tell the resolver:
// "We don't really want Package A, but if you need it take Version X (from our resolution above)"
// We do this by adding all packages from the above resolution but locked at their version.
tracefn "Trying to find a valid resolution considering runtime dependencies..."
let runtimeResolutionDeps =
resolved
|> Seq.map (fun kv ->
let p = kv.Value
let oldDepsInfo = step1Deps |> Seq.tryFind (fun d -> d.Name = p.Name)
{ Name = p.Name
VersionRequirement = VersionRequirement (VersionRange.OverrideAll (SemVer.Parse p.Version.AsString), PreReleaseStatus.All)
ResolverStrategyForDirectDependencies =
match oldDepsInfo with
| Some d -> d.ResolverStrategyForDirectDependencies
| None -> group.Options.ResolverStrategyForDirectDependencies
ResolverStrategyForTransitives =
match oldDepsInfo with
| Some d -> d.ResolverStrategyForTransitives
| None -> group.Options.ResolverStrategyForTransitives
Parent = PackageRequirementSource.RuntimeDependency
Graph = Set.empty
Sources = group.Sources
Kind = PackageRequirementKind.Package
TransitivePrereleases = true
Settings =
match oldDepsInfo with
| Some d -> d.Settings
| None -> p.Settings })
|> fun des -> Seq.append des runtimeDeps
|> Seq.distinctBy (fun p -> p.Name) |> Set.ofSeq
if Environment.GetEnvironmentVariable "PAKET_DEBUG_RUNTIME_DEPS" = "true" then
tracefn "Runtime dependencies: "
let (runtimeDepsFile:DependenciesFile) = DependenciesFile.FromSource ""
let (runtimeDepsFile:DependenciesFile) = runtimeDepsFile.AddFrameworkRestriction(Constants.MainDependencyGroup, getExplicitRestriction group.Options.Settings.FrameworkRestrictions)
let (runtimeDepsFile:DependenciesFile) = runtimeDepsFile.AddResolverStrategyForTransitives(Constants.MainDependencyGroup, group.Options.ResolverStrategyForTransitives)
let (runtimeDepsFile:DependenciesFile) = runtimeDepsFile.AddResolverStrategyForDirectDependencies(Constants.MainDependencyGroup, group.Options.ResolverStrategyForDirectDependencies)
let runtimeDepsFile =
runtimeResolutionDeps
|> Seq.fold (fun (deps:DependenciesFile) dep ->
deps.AddAdditionalPackage(Constants.MainDependencyGroup, dep.Name, dep.VersionRequirement, dep.ResolverStrategyForTransitives, dep.Settings, PackageRequirementKind.Package)) runtimeDepsFile
tracefn "Depsfile: \n%O" runtimeDepsFile
let runtimeResolution =
PackageResolver.Resolve(
getVersionF,
getPreferredVersionF,
getPackageDetailsF,
groupName,
group.Options.ResolverStrategyForDirectDependencies,
group.Options.ResolverStrategyForTransitives,
group.Options.Settings.FrameworkRestrictions,
runtimeResolutionDeps,
updateMode)
// Combine with existing resolution and mark runtime packages.
// TODO: Warn if a runtime package contains a runtime.json? -> We don't download them here :/
match runtimeResolution with
| Resolution.Ok runtimeResolved ->
runtimeResolved
|> Map.map (fun n v ->
match resolved |> Map.tryFind n with
| Some old ->
if old.Version <> v.Version then
// that shouldn't happen because we locked everything?
failwithf "Version of %O was changed from %O to %O because of runtime dependencies" n old.Version v.Version
v
| None -> // pulled because of runtime resolution
{ v with IsRuntimeDependency = true })
|> Resolution.Ok
|> Resolution.addErrors (runtimeResolution.GetErrors())
| _ ->
runtimeResolution
|> Resolution.addError (exn "Runtime resolution failed while regular resolution was OK, try to use 'PAKET_DEBUG_RUNTIME_DEPS=true'")
|> Resolution.addErrors (resolution.GetErrors())
| Resolution.Ok _ -> resolution
| Resolution.Conflict _ -> resolution
{ ResolvedPackages = runtimeResolution
ResolvedSourceFiles = remoteFiles }
groupsToResolve
|> Map.map resolveGroup
member private this.AddFrameworkRestriction(groupName, frameworkRestriction:FrameworkRestriction) =
if frameworkRestriction = FrameworkRestriction.NoRestriction then this else
let restrictionString = sprintf "restriction: %O" frameworkRestriction
let list = new System.Collections.Generic.List<_>()
list.AddRange textRepresentation
match groups |> Map.tryFind groupName with
| None -> list.Add(restrictionString)
| Some group ->
let firstGroupLine,_ = findGroupBorders groupName list
let pos = ref firstGroupLine
while list.Count > !pos && list.[!pos].TrimStart().StartsWith "source" do
pos := !pos + 1
list.Insert(!pos,restrictionString)
DependenciesFile(
list
|> Seq.toArray
|> DependenciesFileParser.parseDependenciesFile fileName false)
member private this.AddResolverStrategyForTransitives(groupName, strategy:ResolverStrategy option) =
match strategy with
| None -> this
| Some strategy ->
let strategyString = sprintf "strategy: %s" (match strategy with ResolverStrategy.Max -> "max" | ResolverStrategy.Min -> "min")
let list = new System.Collections.Generic.List<_>()
list.AddRange textRepresentation
match groups |> Map.tryFind groupName with
| None -> list.Add(strategyString)
| Some group ->
let firstGroupLine,_ = findGroupBorders groupName list
let pos = ref firstGroupLine
while list.Count > !pos && list.[!pos].TrimStart().StartsWith "source" do
pos := !pos + 1
list.Insert(!pos,strategyString)
DependenciesFile(
list
|> Seq.toArray
|> DependenciesFileParser.parseDependenciesFile fileName false)
member private this.AddResolverStrategyForDirectDependencies(groupName, strategy:ResolverStrategy option) =
match strategy with
| None -> this
| Some strategy ->
let strategyString = sprintf "lowest_matching: %s" (match strategy with ResolverStrategy.Max -> "false" | ResolverStrategy.Min -> "true")
let list = new System.Collections.Generic.List<_>()
list.AddRange textRepresentation
match groups |> Map.tryFind groupName with
| None -> list.Add(strategyString)
| Some _ ->
let firstGroupLine,_ = findGroupBorders groupName list
let pos = ref firstGroupLine
while list.Count > !pos && list.[!pos].TrimStart().StartsWith "source" do
pos := !pos + 1
list.Insert(!pos,strategyString)
DependenciesFile(
list
|> Seq.toArray
|> DependenciesFileParser.parseDependenciesFile fileName false)
member private __.InsertGroup(groupName, list:System.Collections.Generic.List<_>) =
match groups |> Map.tryFind groupName with
| None ->
if list.Count > 0 then
list.Add("")
list.Add(sprintf "group %O" groupName)
list.Add(DependenciesFileSerializer.sourceString Constants.DefaultNuGetStream)
list.Add("")
true
| _ -> false
member this.AddAdditionalPackage(groupName, packageName:PackageName,versionRequirement,resolverStrategy,settings,kind,?pinDown) =
let pinDown = defaultArg pinDown false
let packageString = DependenciesFileSerializer.packageString kind packageName versionRequirement resolverStrategy settings
// Try to find alphabetical matching position to insert the package
let isPackageInLastSource =
match groups |> Map.tryFind groupName with
| None -> true
| Some group ->
match group.Sources with
| [] -> true
| sources ->
let lastSource = Seq.last sources
group.Sources |> Seq.exists (fun s -> s = lastSource)
let sourceCount =
match groups |> Map.tryFind groupName with
| None -> 0
| Some group -> List.length group.Sources
let smaller =
match groups |> Map.tryFind groupName with
| None -> []
| Some group ->
group.Packages
|> Seq.takeWhile (fun (p:PackageRequirement) -> p.Name <= packageName || not isPackageInLastSource)
|> List.ofSeq
let list = new System.Collections.Generic.List<_>()
list.AddRange textRepresentation
let newGroupInserted =
this.InsertGroup(groupName, list)
match tryFindPackageLine groupName packageName with
| Some pos ->
let package = DependenciesFileParser.parsePackageLine(groups.[groupName].Sources,PackageRequirementSource.DependenciesFile(fileName,pos),list.[pos])
if versionRequirement.Range.IsIncludedIn(package.VersionRequirement.Range) then
list.[pos] <- packageString
else
list.Insert(pos + 1, packageString)
| None ->
let firstGroupLine,lastGroupLine = findGroupBorders groupName list
if pinDown || sourceCount > 1 then
if newGroupInserted then
list.Add(packageString)
else
list.Insert(lastGroupLine, packageString)
else
match smaller with
| [] ->
match groups |> Map.tryFind groupName with
| None -> list.Add(packageString)
| Some group ->
match group.Packages with
| [] ->
if group.RemoteFiles <> [] then
list.Insert(firstGroupLine,"")
match group.Sources with
| [] ->
list.Insert(firstGroupLine,packageString)
list.Insert(firstGroupLine,"")
list.Insert(firstGroupLine,DependenciesFileSerializer.sourceString Constants.DefaultNuGetStream)
| _ -> list.Insert(lastGroupLine, packageString)
| p::_ ->
match tryFindPackageLine groupName p.Name with
| None -> list.Add packageString
| Some pos -> list.Insert(pos,packageString)
| _ ->
let p = Seq.last smaller
match tryFindPackageLine groupName p.Name with
| None -> list.Add packageString
| Some found ->
let pos = ref (found + 1)
let skipped = ref false
while !pos < textRepresentation.Length - 1 &&
(String.IsNullOrWhiteSpace textRepresentation.[!pos] ||
String.startsWithIgnoreCase "source" textRepresentation.[!pos] ||
String.startsWithIgnoreCase "cache" textRepresentation.[!pos]) do
if (String.startsWithIgnoreCase "source" textRepresentation.[!pos]) ||
(String.startsWithIgnoreCase "cache" textRepresentation.[!pos])
then
skipped := true
pos := !pos + 1
if !skipped then
list.Insert(!pos,packageString)
else
list.Insert(found + 1,packageString)
DependenciesFile(
list
|> Seq.toArray
|> DependenciesFileParser.parseDependenciesFile fileName false)
member this.AddAdditionalPackage(groupName, packageName:PackageName,version:string,settings,kind) =
let vr = DependenciesFileParser.parseVersionString version
this.AddAdditionalPackage(groupName, packageName,vr,settings,kind)
member this.AddAdditionalPackage(groupName, packageName:PackageName,vr:VersionStrategy,settings,kind:PackageRequirementKind) =
this.AddAdditionalPackage(groupName, packageName,vr.VersionRequirement,vr.ResolverStrategy,settings,kind)
member this.AddFixedPackage(groupName, packageName:PackageName,version:string,settings) =
let vr = DependenciesFileParser.parseVersionString version
let resolverStrategy,versionRequirement =
match groups |> Map.tryFind groupName with
| None -> vr.ResolverStrategy,vr.VersionRequirement
| Some group ->
match group.Packages |> List.tryFind (fun p -> p.Name = packageName) with
| Some package ->
package.ResolverStrategyForTransitives,
match package.VersionRequirement.Range with
| OverrideAll _ -> package.VersionRequirement
| _ -> vr.VersionRequirement
| None -> vr.ResolverStrategy,vr.VersionRequirement
this.AddAdditionalPackage(groupName, packageName,versionRequirement,resolverStrategy,settings,PackageRequirementKind.Package,true)
member this.AddFixedPackage(groupName, packageName:PackageName,version:string) =
this.AddFixedPackage(groupName, packageName,version,InstallSettings.Default)
member this.RemovePackage(groupName, packageName:PackageName) =
match tryFindPackageLine groupName packageName with
| None -> this
| Some pos ->
let removeElementAt index myArr =
[| for i = 0 to Array.length myArr - 1 do
if i <> index then yield myArr.[ i ] |]
let fileName, groups, lines =
removeElementAt pos textRepresentation
|> DependenciesFileParser.parseDependenciesFile fileName false
let filteredGroups, filteredLines =
groups
|> Seq.map(fun item -> item.Value)
|> Seq.filter(fun group -> group.Packages.IsEmpty && group.RemoteFiles.IsEmpty && group.Name <> Constants.MainDependencyGroup)
|> Seq.fold(fun (groups, lines:string[]) emptyGroup ->
groups
|> Map.remove emptyGroup.Name,
lines
|> Array.filter(fun line -> not(line.StartsWith "group " && GroupName(line.Replace("group","")) = emptyGroup.Name))
) (groups, lines)
DependenciesFile(fileName, filteredGroups, filteredLines)
member this.Add(groupName, packageName,versionRange:VersionRange,installSettings : InstallSettings) =
this.Add(groupName, packageName,versionRange,installSettings,PackageRequirementKind.Package)
member this.Add(groupName, packageName,versionRange:VersionRange,installSettings : InstallSettings,kind) =
let version =
match versionRange with
| vr when vr = VersionRange.AtLeast "0" -> ""
| VersionRange.Minimum v -> ">= " + string v
| VersionRange.Specific v -> string v
| _ -> ""
this.Add(groupName, packageName,version,installSettings,kind)
member this.Add(groupName, packageName,version:string) =
this.Add(groupName, packageName, version, InstallSettings.Default)
member this.Add(groupName, packageName,version:string, installSettings : InstallSettings) =
this.Add(groupName, packageName, version, installSettings, PackageRequirementKind.Package)
member this.Add(groupName, packageName,version:string, installSettings : InstallSettings, kind : PackageRequirementKind) =
if this.HasPackage(groupName, packageName) && String.IsNullOrWhiteSpace version then
traceWarnfn "%s contains package %O in group %O already. ==> Ignored" fileName packageName groupName
this
else
match version, groupName.Name with
| "", "Main" -> tracefn "Adding package '%O'" packageName
| "", _ -> tracefn "Adding package '%O' into group %O" packageName groupName
| _, "Main" -> tracefn "Adding package '%O' %s" packageName version
| _, _ -> tracefn "Adding package '%O' %s into group %O" packageName version groupName
this.AddAdditionalPackage(groupName, packageName,version,installSettings,kind)
member this.AddGithub(groupName, repository) =
this.AddGithub(groupName, repository, "", "")
member this.AddGithub(groupName, repository, file, version) =
tracefn "Adding %s into group %O" (repository + (if version <> "" then ":" + version else "") + (if file <> "" then "/" + file else "")) groupName
let packageString = DependenciesFileSerializer.githubString repository file version
let list = new System.Collections.Generic.List<_>()
list.AddRange textRepresentation
this.InsertGroup(groupName,list) |> ignore
let firstGroupLine,lastGroupLine = findGroupBorders groupName list
let repoExists =
Seq.mapi (fun i x -> (i,x)) list
|> Seq.filter(fun (i,_) -> i >= firstGroupLine && i <= lastGroupLine)
|> Seq.exists (fun (_,x) -> x = packageString)
if not repoExists then
list.Insert(lastGroupLine, packageString);
DependenciesFile(
list
|> Seq.toArray
|> DependenciesFileParser.parseDependenciesFile fileName false)
member this.AddGit(groupName, repository) =
this.AddGit(groupName, repository, "")
member this.AddGit(groupName, repository, version) =
tracefn "Adding %s into group %O" (repository + (if version <> "" then ":" + version else "")) groupName
let packageString = DependenciesFileSerializer.gitString repository version
let list = new System.Collections.Generic.List<_>()
list.AddRange textRepresentation
this.InsertGroup(groupName,list) |> ignore
let firstGroupLine,lastGroupLine = findGroupBorders groupName list
let repoExists =
Seq.mapi (fun i x -> (i,x)) list
|> Seq.filter(fun (i,_) -> i >= firstGroupLine && i <= lastGroupLine)
|> Seq.exists (fun (_,x) -> x = packageString)
if not repoExists then
list.Insert(lastGroupLine, packageString);
DependenciesFile(
list
|> Seq.toArray
|> DependenciesFileParser.parseDependenciesFile fileName false)
member this.Remove(groupName, packageName) =
if this.HasPackage(groupName, packageName) then
tracefn "Removing %O from %s (group %O)" packageName fileName groupName
this.RemovePackage(groupName, packageName)
else
traceWarnfn "%s doesn't contain package %O in group %O. ==> Ignored" fileName packageName groupName
this
member this.UpdatePackageVersion(groupName, packageName, version:string) =
if this.HasPackage(groupName,packageName) then
let vr = DependenciesFileParser.parseVersionString version
tracefn "Updating %O to version %s in %s group %O" packageName version fileName groupName
let newLines =
this.Lines
|> Array.map (fun l ->
let name = packageName.CompareString
if isPackageLine name l then
let p = this.GetPackage(groupName,packageName)
match vr.VersionRequirement.Range with
| Specific _ ->
let v = SemVer.Parse version
if not (p.VersionRequirement.IsInRange v) then
failwithf "Version %O doesn't match the version requirement %O for package %O that was specified in paket.dependencies" v p.VersionRequirement packageName
| _ -> ()
DependenciesFileSerializer.packageString PackageRequirementKind.Package packageName vr.VersionRequirement vr.ResolverStrategy p.Settings
else l)
DependenciesFile(DependenciesFileParser.parseDependenciesFile this.FileName false newLines)
else
traceWarnfn "%s doesn't contain package %O in group %O. ==> Ignored" fileName packageName groupName
this
member this.UpdateFilteredPackageVersion(groupName, packageFilter: PackageFilter, version:string) =
let vr = DependenciesFileParser.parseVersionString version
tracefn "Updating %O to version %s in %s group %O" packageFilter version fileName groupName
let newLines =
this.Lines
|> Array.map (fun l ->
match tryMatchPackageLine (PackageName >> packageFilter.Match) l with
| Some matchedName ->
let matchedPackageName = PackageName matchedName
let p = this.GetPackage(groupName,matchedPackageName)
DependenciesFileSerializer.packageString PackageRequirementKind.Package matchedPackageName vr.VersionRequirement vr.ResolverStrategy p.Settings
| None -> l)
DependenciesFile(DependenciesFileParser.parseDependenciesFile this.FileName false newLines)
override __.ToString() = String.Join(Environment.NewLine, textRepresentation |> Array.skipWhile String.IsNullOrWhiteSpace)
member this.Save() =
File.WriteAllText(fileName, this.ToString())
verbosefn "Dependencies files saved to %s" fileName
static member FromSource(rootPath,source:string) : DependenciesFile =
let source = source.Replace("\r\n","\n").Replace("\r","\n").Split('\n')
DependenciesFile(DependenciesFileParser.parseDependenciesFile (Path.Combine(rootPath,Constants.DependenciesFileName)) true source)
static member FromSource(source:string) : DependenciesFile =
let source = source.Replace("\r\n","\n").Replace("\r","\n").Split('\n')
DependenciesFile(DependenciesFileParser.parseDependenciesFile "" true source)
static member ReadFromFile fileName : DependenciesFile =
if verbose then
verbosefn "Loading %s" fileName
let source = File.ReadAllLines fileName
if verbose then
verbosefn "Parsing:"
verbosefn "%A" source
let parsed = DependenciesFileParser.parseDependenciesFile fileName true source
if verbose then
verbosefn "Parsed."
DependenciesFile(parsed)
/// Find the matching lock file to a dependencies file
static member FindLockfile(dependenciesFileName) =
FileInfo(Path.Combine(FileInfo(dependenciesFileName).Directory.FullName, Constants.LockFileName))
static member FindLocalfile(dependenciesFileName) =
FileInfo(Path.Combine(FileInfo(dependenciesFileName).Directory.FullName, Constants.LocalFileName))
/// Find the matching lock file to a dependencies file
member this.FindLockFile() = DependenciesFile.FindLockfile this.FileName
type PaketFiles =
| JustDependencies of DependenciesFile
| DependenciesAndLock of DependenciesFile * LockFile
static member LocateFromDirectory (directory: DirectoryInfo) =
let rec findInPath (dir:DirectoryInfo , withError) =
let path = Path.Combine (dir.FullName, Constants.DependenciesFileName)
if File.Exists path then path else
match dir.Parent with
| null ->
if withError then
failwithf "Could not find '%s'. To use Paket with this solution, please run 'paket init' first.\n\
If you have already run 'paket.init' then ensure that '%s' is located in the top level directory of your repository.\n\
Like this:\n\
- MySourceDir\n\
- .paket\n\
- paket.dependencies\n"
Constants.DependenciesFileName Constants.DependenciesFileName
else
Constants.DependenciesFileName
| _ -> findInPath(dir.Parent, withError)
let dependenciesFile = findInPath (directory,true) |> DependenciesFile.ReadFromFile
let file = dependenciesFile.FindLockFile()
if file.Exists then
let lockFile = file.FullName |> LockFile.LoadFrom
DependenciesAndLock(dependenciesFile, lockFile)
else
JustDependencies dependenciesFile