You probably already know that you can work with Peachpie in Visual Studio Code with our official extension. But did you know that you can also use the full Visual Studio 2017 for your Peachpie projects? In this article, we will show how and what kinds of powerful features this will give you access to.
Choosing the right IDE
Visual Studio Code is a fantastic lightweight editor that is completely free and open-source. With our Peachpie extension, you can be up and running and experimenting with Peachpie within seconds. There are certainly usecases that are better suited for the lightweight VSCode as opposed to a giant development environment that takes quite a while to start and set up. However, most developers would agree that larger projects require a more powerful IDE, such as the brand new Visual Studio 2017. Even without any special extension, you can already work with Peachpie in VS2017; this article will show you exactly how.
- Solutions in Visual Studio
- Create a project file
- Open and work with a new project
- Perform compilation and debugging
- Code and performance diagnostics
The video below shows the entire tutorial in a visual format:
Solutions in Visual Studio
A major change in the new Visual Studio is the deprecation of
project.json and the introduction of the new MSBuild 15.0. As we announced in the last article, Peachpie has gone with the flow as well; Visual Studio therefore implicitly understands Peachpie projects, because they have the compatible
msbuild project file allowing us to distribute the core compiler itself as a NuGet package instead of having to provide an installation package.
Visual Studio works within the so-called Solution, which contains the projects. Each project is defined by a project file, typically
.vbproj. Any further language (such as Peachpie, in this instance) can introduce its own custom project file type or use the generic
.msbuildproj. This comes in particularly handy in our case, because we can define how this project is to be compiled and let MSBuild and Visual Studio do the rest of the work.
Creating the project file
In the aforementioned article explaining the shift from project.json to msbuild, we show a sample project file that you can simply create or copy the
.msbuildproj file from any our Peachpie samples.
As of now, it is necessary to do this step manually, as we have yet to provide a collection of project templates for VS2017 that would take care of extending your
File | New Project. However, we are working on a way to provide a more comprehensive Peachpie development experience within VS2017. Our
.msbuildproj file should look as follows:
In our msbuild project, we define the following directives, leading MSBuild to work with Peachpie compiler:
- <OutputType> can be set to `exe`, `library` or other values acceptable for C# projects. This tells the compiler to either generate an executable (console app) or a DLL library that you may reference from other projects.
- <TargetFramework> in this case is `netcoreapp1.1`. In the world of cross-platform and portability, you can use a combination of more frameworks using <TargetFrameworks> instead. Peachpie currently provides support for `netstandard1.5`, `net46` and anything higher.
- <Compile> with the value of `**/*.php` includes all PHP files including subfolders into the compilation.
- The key directive here is <DotNetCliToolReference> which instructs MSBuild to download our tooling – Peachpie.Compiler.Tools – containing the compiler into the local NuGet cache. The binaries contained in this package become available for our target files provided in the following directive.
- <PackageReference> with the latest `Peachpie.NET.Sdk` package declares the targets defining the `CoreCompile` tasks. This intuitively invokes the compiler from the tooling above upon calling
In the bottom section, we added a quick temporary workaround for opening projects in VS2017.
Opening and working with a project
Either add the project file to the existing solution (Add | Existing Project) or open it directly (File | Open Project). Visual Studio 2017 provides the traditional compilation and debugging options. On top of that, however, you can leverage the power of the MSBuild platform, with which you can extend your project as needed, define pre-build and post-build tasks etc. Don’t forget to restore your NuGet packages first, as the Peachpie project will import them and requires a restore for any further steps, such as the compilation.
Now that we opened our project in VS2017, we can start building and debugging. You can now step through your code, step into, over and out, just like you are used to with other .NET languages.
The for-loop in our sample console app is a wonderful example of the accuracy of .NET breakpoints, which can even attach to brackets. The breakpoints are accurate to a single character, even multiple commands in one line can be distinguished and statements across several lines don’t cause any issues. Another great feature is the indication of how many milliseconds a statement took to execute, as well as the locals window:
You can nicely see the values of the variables and the results of the evaluation operators. We can glance into the internals of Peachpie here, since some of the variables are what is called “compiler-generated”.
The indication of the time a line took to execute after each debug step is just the tip of the iceberg that is Visual Studio’s diagnostic toolset. The diagnostic tools are only available in the higher edition of VS2017, so this could be particularly interesting for companies and enterprise users. The tools give you access to performance and memory monitoring, CPU usage or garbage collection, for instance – all of these analyses are available for your PHP apps thanks to Peachpie, just as if it were any other .NET language:
You can monitor what percentage of total CPU time the functions took in the context of the whole application. Even more of a revelation, you can display the Call Tree:
The Call Tree can also give you an indication if the problematic function is an internal part of the project or referenced by an external library. These diagnostic tools are a must-have for any serious application, as well as a great tool to reveal possible bottlenecks. Another amazing feature of VS2017 is the ability to create a snapshot of the Managed Memory and display a list of the instantiated objects with their count and memory taken up:
The snapshot also shows which entity is maintaining a reference to the listed objects. There are plenty of other diagnostics you can take advantage of and we will of course show many other features in the future. Keep in mind this is just an introduction of how to work with Peachpie in VS2017. If there are any particular things you would like us to include in the future, please let us know in the comments section. Stay tuned for a much more simple and powerful way of using Visual Studio 2017 with Peachpie.