Skip to content

Commit

Permalink
[build] locate MSBuild properly (#44)
Browse files Browse the repository at this point in the history
Context: jonathanpeppers/Xamarin.Forms.Mocks#43

I tried to build this repo on a new machine that only has VS 2019 installed. It didn't build...

I pulled a fix over that NUnit is using in their Cake script.

I also just use MSBuild restore now instead of NuGet.
  • Loading branch information
jonathanpeppers authored Jun 23, 2019
1 parent ff8886f commit ebae223
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
12 changes: 2 additions & 10 deletions build.cake
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,17 @@ Task("Clean")
CleanDirectory(dir);
});

Task("Restore-NuGet-Packages")
.IsDependentOn("Clean")
.Does(() =>
{
NuGetRestore(sln);
});

Task("Build")
.IsDependentOn("Restore-NuGet-Packages")
.Does(() =>
{
MSBuild(sln, settings => settings.SetConfiguration(configuration));
MSBuild(sln, MSBuildSettings());
});

Task("Install")
.IsDependentOn("Build")
.Does(() =>
{
MSBuild("./glidex.forms.sample/glidex.forms.sample.csproj", settings => settings.SetConfiguration(configuration).WithTarget("Install").WithTarget("_Run"));
MSBuild("./glidex.forms.sample/glidex.forms.sample.csproj", MSBuildSettings().WithTarget("Install").WithTarget("_Run"));
});

Task("NuGet-Package")
Expand Down
31 changes: 31 additions & 0 deletions helpers.cake
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,34 @@ void push(string file)
ApiKey = apiKey
});
}

MSBuildSettings MSBuildSettings()
{
var settings = new MSBuildSettings { Configuration = configuration };

if (IsRunningOnWindows())
{
// Find MSBuild for Visual Studio 2019 and newer
DirectoryPath vsLatest = VSWhereLatest();
FilePath msBuildPath = vsLatest?.CombineWithFilePath("./MSBuild/Current/Bin/MSBuild.exe");

// Find MSBuild for Visual Studio 2017
if (msBuildPath != null && !FileExists(msBuildPath))
msBuildPath = vsLatest.CombineWithFilePath("./MSBuild/15.0/Bin/MSBuild.exe");

// Have we found MSBuild yet?
if (!FileExists(msBuildPath))
{
throw new Exception($"Failed to find MSBuild: {msBuildPath}");
}

Information("Building using MSBuild at " + msBuildPath);
settings.ToolPath = msBuildPath;
}
else
{
settings.ToolPath = Context.Tools.Resolve("msbuild");
}

return settings.WithRestore();
}

0 comments on commit ebae223

Please sign in to comment.