Build Configuration in Visual Studio

All projects targeting the .NET platform have build properties, build events, and many other options we commonly define. This post depicts the well known project property pages in Visual Studio, which are also implicitly available to PeachPie projects.

Since PeachPie effectively makes PHP a first-class citizen in .NET, we can take advantage of all the benefits the Microsoft ecosystem has to offer. In this article, we are leveraging the PeachPie for Visual Studio extension for Visual Studio 2017 and newer, which we highly recommend using if you’re working or playing around with PeachPie.

Start by opening a PeachPie project in Visual Studio. Either use one of the samples from github.com/iolevel/peachpie-samples or create a new project using [Ctrl+Shift+N]:

VS2019-NewProject-PeachPie
New project in Visual Studio 2019 with the currently available PeachPie project templates.

The project is a standard MSBuild project file; perfectly understood by Visual Studio, MSBuild 15 or newer and the dotnet command line utility. In Visual Studio, you’ll see the newly opened project in the Solution Explorer, which should look something like this:

Solution Explorer with a PeachPie (PHP) project.

Project Properties

Properties affect various aspects of the compilation and debugging. There are different project properties, some of which are shared across all the project types, while others are specific to PeachPie. Advanced users usually edit the project file directly in order to alter it. You can see the common properties at docs.peachpie.io/php/msbuild/ and docs.microsoft.com/en-us/visualstudio/msbuild/common-msbuild-project-properties.

Properties Window

The properties Window provides a way of managing common build and debug aspects, organized in property pages and groups:

Opening the Project Properties Window from the Solution Explorer.

Debug Property Page

Executable projects have launch settings. You can specify one or more profiles and how to launch them. The most common option is to launch the project itself, with optional command line arguments and additional environment variables.

Debug Property Page

Build Events Page

The simplest way of invoking custom scripts before and after the compilation are the build events properties.

Build Events Property Page

To find out more about this option, check out
https://docs.microsoft.com/en-us/visualstudio/ide/specifying-custom-build-events-in-visual-studio?view=vs-2019 (they should add PHP to the list).

Signing Page

.NET assemblies can be signed with a private key. This makes it uniquely identifiable with your unique signature and much more. You can find out more about this feature at docs.microsoft.com/en-us/dotnet/standard/assembly/strong-named.

Signing Property Page

This option is the equivalent of adding the following XML snippet into the project file. See more at docs.peachpie.io/php/msbuild/#sign-with-a-strong-name.

<PropertyGroup>
  <AssemblyOriginatorKeyFile>my-key.snk</AssemblyOriginatorKeyFile>
  <SignAssembly>true</SignAssembly>
</PropertyGroup>

Package Page

The project can also be packed into a NuGet upon a successful build. The Package Page allows you to set the corresponding properties, specify the authors, license, project URL and more.

Package Property Page

The “Generate NuGet package on build” option allows to create the package upon successful build. Enabling this option may slightly degrade the build speed, so keep this in mind when enabling this option.

Packing individual components or libraries into NuGets and cross-referencing them is a great way to organize and manage large PHP projects. Additionally, it allows you to distribute your application as a securely signed soure-less package.

Posted on February 23, 2020, in category Information, Tutorial, tags: , , , , ,