Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#addin "Cake.FileHelpers&version=3.2.0"
void RestoreAll(string root = "", string glob = "**/*.csproj")
{
var csprojFiles = GetFiles(root + glob);
if (csprojFiles.Any())
{
var tempSln = new FilePath(root + "BuildSolution.sln");
FileWriteText(tempSln, CreateTempSolution(csprojFiles));
NuGetRestore(tempSln, GetRestoreSettings());
}
}
string CreateTempSolution(IEnumerable<FilePath> projects)
{
var slnContents = new StringBuilder();
slnContents.AppendLine("Microsoft Visual Studio Solution File, Format Version 12.00");
slnContents.AppendLine("# Visual Studio Version 16");
slnContents.AppendLine("VisualStudioVersion = 16.0.28803.202");
slnContents.AppendLine("MinimumVisualStudioVersion = 10.0.40219.1");
var projGuids = new List<string>();
foreach(var project in projects)
{
string projGuid;
var projContents = FileReadText(project);
var from = projContents.IndexOf("<ProjectGuid>{");
if (from == -1) projGuid = Guid.NewGuid().ToString().ToUpperInvariant();
else projGuid = projContents.Substring(from + 14, 36);
projGuids.Add(projGuid);
slnContents.AppendFormat("Project(\"{{{0}}}\") = \"{1}\", \"{2}\", \"{{{3}}}\" \r\n", Guid.NewGuid().ToString().ToUpperInvariant(), project.GetFilenameWithoutExtension(), project.FullPath.ToString(), projGuid);
slnContents.Append("EndProject \r\n");
}
slnContents.Append("Global \r\n");
slnContents.Append("\tGlobalSection(SolutionConfigurationPlatforms) = preSolution \r\n");
slnContents.Append("\t\tDebug|Any CPU = Debug|Any CPU \r\n");
slnContents.Append("\t\tRelease|Any CPU = Release|Any CPU \r\n");
slnContents.Append("\tEndGlobalSection \r\n");
slnContents.Append("\tGlobalSection(ProjectConfigurationPlatforms) = postSolution \r\n");
foreach(var guid in projGuids)
{
slnContents.AppendFormat("\t\t{{{0}}}.Debug|Any CPU.ActiveCfg = Debug|Any CPU \r\n", guid);
slnContents.AppendFormat("\t\t{{{0}}}.Debug|Any CPU.Build.0 = Debug|Any CPU \r\n", guid);
slnContents.AppendFormat("\t\t{{{0}}}.Release|Any CPU.ActiveCfg = Release|Any CPU \r\n", guid);
slnContents.AppendFormat("\t\t{{{0}}}.Release|Any CPU.Build.0 = Release|Any CPU \r\n", guid);
}
slnContents.Append("\tEndGlobalSection \r\n");
slnContents.Append("\tGlobalSection(SolutionProperties) = preSolution \r\n");
slnContents.Append("\t\tHideSolutionNode = FALSE \r\n");
slnContents.Append("\tEndGlobalSection \r\n");
slnContents.Append("\tGlobalSection(ExtensibilityGlobals) = postSolution \r\n");
slnContents.AppendFormat("\t\tSolutionGuid = {{{0}}} \r\n", Guid.NewGuid().ToString().ToUpperInvariant());
slnContents.Append("\tEndGlobalSection \r\n");
slnContents.Append("EndGlobal \r\n");
return slnContents.ToString();
}
NuGetRestoreSettings GetRestoreSettings()
{
return new NuGetRestoreSettings
{
NonInteractive = true,
Verbosity = NuGetVerbosity.Quiet,
ConfigFile = new FilePath(@"tools\nuget.config")
};
}
Task("RestoreAll").Does(() => RestoreAll());
RunTarget(Argument<string>("Target", ""));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment