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

Use creds from nuget config on convert #198

Merged
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
124 changes: 66 additions & 58 deletions src/Paket/NugetConvert.fs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,18 @@ open System.IO
open System.Xml
open Paket.Logging
open Paket.Nuget
open Paket.PackageSources

let private readPackageSources(configFile : FileInfo) =
let doc = XmlDocument()
doc.Load configFile.FullName
[for node in doc.SelectNodes("//packageSources/add[@value]") -> node.Attributes.["value"].Value]
[for node in doc.SelectNodes("//packageSources/add[@value]") ->
{PackageSources.NugetSource.Url = node.Attributes.["value"].Value
PackageSources.NugetSource.Auth = doc.SelectNodes(sprintf "//packageSourceCredentials/%s" node.Attributes.["key"].Value)
|> Seq.cast<XmlNode>
|> Seq.firstOrDefault
|> Option.map (fun node -> {Username = node.SelectSingleNode("//add[@key='Username']").Attributes.["value"].Value
Password = node.SelectSingleNode("//add[@key='ClearTextPassword']").Attributes.["value"].Value})} ]

let removeFileIfExists file =
if File.Exists file then
Expand All @@ -31,67 +38,68 @@ let private convertNugetsToDepFile(nugetPackagesConfigs) =
name
(versions |> Seq.map string |> Seq.toList)

let latestVersions = allVersions |> Seq.map (fun (name,versions) -> name, versions |> Seq.max |> string)
let latestVersions = allVersions |> Seq.map (fun (name,versions) -> name, versions |> Seq.max |> string) |> Seq.toList

let existingDepFile =
if File.Exists Constants.DependenciesFile
then Some(DependenciesFile.ReadFromFile(Constants.DependenciesFile))
else None

let confictingPackages, packagesToAdd =
match existingDepFile with
| Some depFile -> latestVersions |> List.partition (fun (name,_) -> depFile.HasPackage name)
| None -> [], latestVersions

let depFileExists = File.Exists Constants.DependenciesFile
let existingPackages =
if depFileExists
then (DependenciesFile.ReadFromFile Constants.DependenciesFile).Packages |> List.map (fun p -> p.Name.ToLower()) |> Set.ofList
else Set.empty
let confictingPackages = Set.intersect existingPackages (latestVersions |> Seq.map fst |> Seq.map (fun n -> n.ToLower()) |> Set.ofSeq)
confictingPackages |> Set.iter (fun name -> traceWarnfn "Package %s is already defined in %s" name Constants.DependenciesFile)

let dependencyLines =
latestVersions
|> Seq.filter (fun (name,_) -> not (confictingPackages |> Set.contains (name.ToLower())))
|> Seq.map (fun (name,version) -> sprintf "nuget %s %s" name version)
|> Seq.toList
for (name, _) in confictingPackages do traceWarnfn "Package %s is already defined in %s" name Constants.DependenciesFile

if not depFileExists
then
let packageSources =
match FindAllFiles(".", "nuget.config") |> Seq.firstOrDefault with
| Some configFile ->
let sources = readPackageSources(configFile)
removeFileIfExists configFile.FullName
sources @ [Constants.DefaultNugetStream]
| None -> [Constants.DefaultNugetStream]
|> Set.ofList
|> Set.toList
|> List.map (sprintf "source %s")

File.WriteAllLines(Constants.DependenciesFile, packageSources @ [String.Empty] @ dependencyLines)
tracefn "Generated %s file" Constants.DependenciesFile
elif not (dependencyLines |> Seq.isEmpty)
then
File.WriteAllLines(Constants.DependenciesFile, Seq.append (File.ReadAllLines(Constants.DependenciesFile)) dependencyLines)
traceWarnfn "Overwritten %s file" Constants.DependenciesFile
else tracefn "%s is up to date" Constants.DependenciesFile
let defaultNugetSource = {NugetSource.Url = Constants.DefaultNugetStream; NugetSource.Auth = None}

let nugetPackageRequirement (name: string, v: string, nugetSources : list<NugetSource>) =
{Requirements.PackageRequirement.Name = name
Requirements.PackageRequirement.VersionRequirement = VersionRequirement(VersionRange.Specific(SemVer.parse v), PreReleaseStatus.No)
Requirements.PackageRequirement.ResolverStrategy = Max
Requirements.PackageRequirement.Sources = nugetSources |> List.map (fun n -> PackageSources.PackageSource.Nuget(n))
Requirements.PackageRequirement.Parent = Requirements.PackageRequirementSource.DependenciesFile(Constants.DependenciesFile)}

match existingDepFile with
| None ->
let nugetSources =
match FindAllFiles(".", "nuget.config") |> Seq.firstOrDefault with
| Some configFile ->
let sources = readPackageSources(configFile)
removeFileIfExists configFile.FullName
sources @ [defaultNugetSource]
| None -> [defaultNugetSource]
|> Set.ofList
|> Set.toList

let packages = packagesToAdd |> List.map (fun (name,v) -> nugetPackageRequirement(name,v,nugetSources))
DependenciesFile(Constants.DependenciesFile, false, packages, []).Save()
| Some depFile ->
if not (packagesToAdd |> List.isEmpty)
then (packagesToAdd |> List.fold (fun (d : DependenciesFile) (name,version) -> d.Add(name,version)) depFile).Save()
else tracefn "%s is up to date" depFile.FileName

let private convertNugetToRefFile(nugetPackagesConfig) =
let refFile = Path.Combine(nugetPackagesConfig.File.DirectoryName, Constants.ReferencesFile)
let refFileExists = File.Exists refFile
let existingReferences =
if refFileExists
then (File.ReadAllLines refFile) |> Array.map (fun p -> p.ToLower()) |> Set.ofArray
else Set.empty
let confictingReferences = Set.intersect existingReferences (nugetPackagesConfig.Packages |> List.map fst |> Seq.map (fun n -> n.ToLower()) |> Set.ofSeq)
confictingReferences |> Set.iter (fun name -> traceWarnfn "Reference %s is already defined in %s" name refFile)

let referencesLines =
nugetPackagesConfig.Packages
|> List.map fst
|> List.filter (fun name -> not (confictingReferences |> Set.contains (name.ToLower())))

if not refFileExists
then
File.WriteAllLines(refFile, referencesLines)
tracefn "Converted %s to %s" nugetPackagesConfig.File.FullName Constants.ReferencesFile
elif not (referencesLines |> List.isEmpty)
then
File.WriteAllLines(refFile, Seq.append (File.ReadAllLines(refFile)) referencesLines)
traceWarnfn "Overwritten %s file" refFile
else tracefn "%s is up to date" refFile
let refFilePath = Path.Combine(nugetPackagesConfig.File.DirectoryName, Constants.ReferencesFile)
let existingRefFile = if File.Exists refFilePath then Some <| ReferencesFile.FromFile(refFilePath) else None

let confictingRefs, refsToAdd =
match existingRefFile with
| Some refFile ->
nugetPackagesConfig.Packages
|> List.partition (fun (name,_) ->
refFile.NugetPackages |> List.exists (fun np -> String.Equals(name,np,StringComparison.InvariantCultureIgnoreCase)))
| _ -> [], nugetPackagesConfig.Packages

for (name,_) in confictingRefs do traceWarnfn "Reference %s is already defined in %s" name refFilePath

match existingRefFile with
| None -> {ReferencesFile.FileName = refFilePath; NugetPackages = refsToAdd |> List.map fst; GitHubFiles = []}.Save()
| Some refFile ->
if not (refsToAdd |> List.isEmpty)
then (refsToAdd |> List.fold (fun (refFile : ReferencesFile) (name,_) -> refFile.AddNugetRef(name)) refFile).Save()
else tracefn "%s is up to date" refFilePath

/// Converts all projects from NuGet to Paket
let ConvertFromNuget(force, installAfter, initAutoRestore, dependenciesFileName) =
Expand Down
15 changes: 12 additions & 3 deletions src/Paket/ReferencesFile.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,35 @@

open System
open System.IO
open Logging

type ReferencesFile =
{ FileName: string
NugetPackages: list<string>
GithubFiles: list<string> }
GitHubFiles: list<string> }

static member FromLines(lines : string[]) =
let isGitHubFile (line: string) = line.StartsWith "File:"
let notEmpty (line: string) = not <| System.String.IsNullOrWhiteSpace line

{ FileName = ""
NugetPackages = lines |> Array.filter notEmpty |> Array.filter (isGitHubFile >> not) |> Array.toList
GithubFiles = lines |> Array.filter notEmpty |> Array.filter isGitHubFile |> Array.map (fun s -> s.Replace("File:","")) |> Array.toList }
GitHubFiles = lines |> Array.filter notEmpty |> Array.filter isGitHubFile |> Array.map (fun s -> s.Replace("File:","")) |> Array.toList }

static member FromFile(fileName : string) =
let lines = File.ReadAllLines(fileName)
{ ReferencesFile.FromLines lines with FileName = fileName }

member this.AddNugetRef(reference : string) =
tracefn "Adding %s to %s" reference (this.FileName)
{ this with NugetPackages = this.NugetPackages @ [reference] }

member this.Save() =
File.WriteAllText(this.FileName, this.ToString())
tracefn "References file saved to %s" this.FileName

override this.ToString() =
List.append
this.NugetPackages
(this.GithubFiles |> List.map (fun s -> "File:" + s))
(this.GitHubFiles |> List.map (fun s -> "File:" + s))
|> String.concat Environment.NewLine
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
source http://nuget.org/api/v2
source http://teamcity/httpAuth/app/nuget/v1/FeedService.svc/ username: "somebody" password: "secret"
source https://www.nuget.org/api/v2/

nuget Castle.Core 3.3.1
nuget Castle.Windsor 3.3.0
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Castle.Core
Castle.Windsor
16 changes: 16 additions & 0 deletions system-tests/convert-creds-in-config-185/original/nuget.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<config>
<add key="repositoryPath" value="..\..\..\tmp\packages" />
</config>
<packageSources>
<add key="nuget.org" value="https://www.nuget.org/api/v2/" />
<add key="TeamCity" value="http://teamcity/httpAuth/app/nuget/v1/FeedService.svc/" />
</packageSources>
<packageSourceCredentials>
<TeamCity>
<add key="Username" value="somebody" />
<add key="ClearTextPassword" value="secret" />
</TeamCity>
</packageSourceCredentials>
</configuration>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Castle.Core" version="3.3.1" targetFramework="net451" />
<package id="Castle.Windsor" version="3.3.0" targetFramework="net451" />
</packages>
1 change: 1 addition & 0 deletions system-tests/convert-creds-in-config-185/original/run.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
paket convert-from-nuget --no-install
6 changes: 3 additions & 3 deletions tests/Paket.Tests/ReferencesFile/ReferencesFileSpecs.fs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ let ``should parse lines correctly``() =
refFile.NugetPackages.Length |> shouldEqual 3
refFile.NugetPackages.Head |> shouldEqual "Castle.Windsor"
refFile.NugetPackages.Tail.Tail.Head |> shouldEqual "jQuery"
refFile.GithubFiles.Length |> shouldEqual 1
refFile.GithubFiles.Head |> shouldEqual "FsUnit.fs"
refFile.GitHubFiles.Length |> shouldEqual 1
refFile.GitHubFiles.Head |> shouldEqual "FsUnit.fs"

[<Test>]
let ``should serialize itself correctly``() =
let refFile = {FileName = ""; NugetPackages = ["A"; "B"]; GithubFiles = ["FromGithub.fs"]}
let refFile = {FileName = ""; NugetPackages = ["A"; "B"]; GitHubFiles = ["FromGithub.fs"]}
let expected = [|"A"; "B"; "File:FromGithub.fs"|]

refFile.ToString() |> toLines |> shouldEqual expected