Last active
August 29, 2015 14:18
-
-
Save Thorium/c155e8404dedc7b5610b to your computer and use it in GitHub Desktop.
Create NuGet packages.config files for csproj-files.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This program will create packages.config files for each .csproj. | |
// It will enumerate your projects dll references to know the corresponding NuGet packages. | |
// packages.config files are used by Paket when converting from NuGet. | |
#if INTERACTIVE | |
#r "System.Xml.dll" //for scripts or interactive | |
#r "System.Xml.Linq.dll" //add reference if using F# as library | |
#else | |
module GeneratePaketConfigs | |
#endif | |
open System | |
open System.IO | |
open System.Linq | |
open System.Xml.Linq | |
/// Directory of your source control project root folder | |
// Environment.CurrentDirectory <- @"c:\git\myProject" | |
let directory = Environment.CurrentDirectory | |
/// Possible HintPaths of your NuGet package locations in your hard drive. | |
let nugetFolder, nugetEnvVariable = @"packages\", @"$(NugetPath)\" | |
let readFile files = | |
Directory.GetFiles(directory, files, SearchOption.AllDirectories) | |
|> Array.toSeq | |
type System.String with | |
member x.has i = x.IndexOf(i, StringComparison.OrdinalIgnoreCase) | |
let ``find packages from projects`` (``project file name``:string) = | |
let xn ns s = XName.Get(s,ns) | |
let xml = XDocument.Load ``project file name`` | |
let xns = xn (xml.Root.Attribute(XName.Get("xmlns")).Value) | |
let ``find packages``= | |
xml.Element(xns "Project") | |
.Elements(xns "ItemGroup") | |
.Elements(xns "Reference") | |
|> Seq.map(fun x -> match x.Element(xns "HintPath") with null -> "" | v -> v.Value) | |
|> Seq.filter(fun path -> path.has nugetFolder + path.has nugetEnvVariable >= -1) | |
|> Seq.map(fun path -> | |
let startPosition = | |
match path.has nugetFolder, path.has nugetEnvVariable with | |
| -1, x -> x + nugetEnvVariable.Length | |
| x, -1 -> x + nugetFolder.Length | |
| _ -> failwith path | |
let endPosition = path.IndexOf(@"\", startPosition)-startPosition | |
let target = | |
match path.IndexOf(@"lib\", endPosition) with | |
| -1 -> None | |
| targetStart -> | |
match path.IndexOf(@"\", targetStart+4) with | |
| -1 -> None | |
| targetEnd -> path.Substring(targetStart+4, targetEnd-targetStart-4) |> Some | |
let _, name, version = | |
let ``split version from first number`` = | |
fun (found,n,v) -> fun i -> | |
let ok = i |> Int64.TryParse |> fst | |
match found || ok with | |
| false -> false, n+"."+i, v | |
| true -> true, n, v+"."+i | |
path.Substring(startPosition, endPosition).Split '.' | |
|> Seq.fold ``split version from first number`` (false,"","") | |
match name.Length = 0 || version.Length = 0, target with | |
| false, Some t -> " <package id=\"" + name.Substring(1) + "\" version=\"" + version.Substring(1) + "\" targetFramework=\"" + t + "\" />" | |
| false, _ -> " <package id=\"" + name.Substring(1) + "\" version=\"" + version.Substring(1) + "\" />" | |
| true, _ -> | |
printf "%s was not a NuGet package" path | |
"") | |
|> Seq.toArray | |
let ``update package.configs`` = | |
let ``package config`` = | |
``project file name``.Substring(0,1+``project file name``.LastIndexOf @"\") + "packages.config" | |
match ``find packages``.Length, File.Exists(``package config``) with | |
| 0, true -> ``package config`` + " was ok." | |
| 0, false -> ``package config`` + " not needed." | |
| _, false -> | |
File.WriteAllText( ``package config``, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<packages>\r\n") | |
File.AppendAllLines( ``package config``, ``find packages``) | |
File.AppendAllText( ``package config``, "</packages>") | |
``package config`` + " created." | |
| _, true -> | |
let lines = File.ReadAllLines ``package config`` | |
let ``except exsisting`` = | |
Array.filter (function // rip target framework from check | |
| (p:string) when p.Length > 3 -> | |
let ``no target`` = p.Substring(0, p.Length-3) | |
lines.Any (fun i -> ``no target`` |> i.StartsWith) |> not | |
| _ -> false) | |
let ``new content`` = | |
seq { | |
yield! lines.Take(lines.Length-1) | |
yield "\r\n" | |
yield! ``find packages`` |> ``except exsisting`` | |
yield lines.Last() | |
} | |
File.Delete ``package config`` | |
File.WriteAllLines( ``package config``, ``new content``) | |
``package config`` + " updated." | |
``update package.configs`` | |
let projectFiles = ("*.csproj" |> readFile).AsParallel().Select ``find packages from projects`` | |
|> Seq.filter(fun p -> p.Any()) |> Seq.toArray | |
// projectFiles |> Seq.iter(printfn "%s") | |
// Now you should be able to run (path depending your version of course): | |
// nuget install Paket | |
// packages\Paket.0.38.4\tools\paket.exe convert-from-nuget |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment