July 27, 2015

Visual Studio Code and How It Works

Buying and selling a house sucks. One of the reasons I have not written in a while. But I have been playing around with Visual Studio Code to see how it replaces VS.net. While Visual Studio Code can read in existing projects, there are a few files that need to be configured. I found out some of the things that you need to configure a project.

If you have the latest version of Visual Studio Code: https://code.visualstudio.com/, then some features you probably have noticed is that you can load in a project created from VS.net or create your own using yeoman's aspnet generator.. Now if you use the yeoman generator to create your new project, it automatically creates a project.json file. Here is an example:

 { "version": "1.0.0-\*", "dependencies": {}, "commands": { "run": "run" }, "frameworks": { "dnx451": { }, "dnxcore50": { "dependencies": { "System.Console": "4.0.0-beta-\*" } } } } 

In terms of dnx, which is the new .net build system, the project.json file is the 'csproj' file. It tells dnx how to build the project, pull down nuget dependences and even sets up environments to unit test, build, debug or run your application. In the case of a Visual Studio project, it doesn't use this type of file, so we instead need to do a few things to make use of a csproj file instead.

First, we will need to create a task.json file:

 { "version": "0.1.0", "command": "xbuild", "args": [ // Ask msbuild to generate full paths for file names. "/property:GenerateFullPaths=true;main=sortFile.ProgramTest" //"/property:GenerateFullPaths=true;" ], "taskSelector": "/t:", //"showOutput": "silent", "tasks": [ { "taskName": "build", // Show the output window only if unrecognized errors occur. "showOutput": "silent", // Use the standard MS compiler pattern to detect errors, warnings // and infos in the output. "problemMatcher": "$msCompile" } ] } 

If you are familar with gulp or grunt, this is VSC's new task building file. xbuild is the new build command on a mac in VSC and you will give it args, like MSBuild. The tasks section is there to tell VSC how to build code together. For example, if you have less or typescript code, you could also have a section about how to build that code as well.

This task.json file should go in your .settings folder to register build actions. The next file you should have is a launch.json file:

 { "version": "0.1.0", // List of configurations. Add new configurations or edit existing ones. // ONLY "node" and "mono" are supported, change "type" to switch. "configurations": [ { // Name of configuration; appears in the launch configuration drop down menu. "name": "Debug ConsoleApplication", // Type of configuration. Possible values: "node", "mono". "type": "mono", // Workspace relative or absolute path to the program. "program": "./testOutput/bin/Debug/testOutput.exe", // Automatically stop program after launch. "stopOnEntry": true, // Command line arguments passed to the program. "args": [], // Workspace relative or absolute path to the working directory of the program being debugged. Default is the current workspace. "cwd": ".", // Workspace relative or absolute path to the runtime executable to be used. Default is the runtime executable on the PATH. "runtimeExecutable": null, // Environment variables passed to the program. "env": { } }, { "name": "Attach", "type": "mono", // TCP/IP address. Default is "localhost". "address": "localhost", // Port to attach to. "port": 5858 }, { "name": "Run", "type": "mono", "program": "./testOutput/bin/Debug/testOutput.exe", "args": [], // Workspace relative or absolute path to the working directory of the program being debugged. Default is the current workspace. "cwd": ".", // Workspace relative or absolute path to the runtime executable to be used. Default is the runtime executable on the PATH. "runtimeExecutable": null, // Environment variables passed to the program. "env": { } } ] } 

This file is for creating the different tasks you can execute when you type the ol' CTRL+P button. In this file are 3 different types of actions for launching the program: 'Run', 'Attach' and 'Debug ConsoleApplication'. 'Debug ConsoleApplication' will build the project with the settings from the task.json file. 'Attach' will debug the program, and 'Run' will run the program. With these 2 files, now our csproj file should work:

    Debug x86 8.0.30703 2.0 {DDC9BBEC-9732-4E10-9CB8-E5092ED892E1} Exe testOutput testOutput v4.5 sortFile.ProgramTest   true full false bin\Debug DEBUG; prompt 4 true   full true bin\Release prompt 4 true x86      ..\packages\Microsoft.Net.Http.2.2.18\lib\net45\System.Net.Http.Primitives.dll   ..\packages\Microsoft.Net.Http.2.2.18\lib\net45\System.Net.Http.Extensions.dll     ..\packages\Newtonsoft.Json.6.0.1\lib\net45\Newtonsoft.Json.dll            

From here the project knows what project the main entry point file is and what assemblies to pull in to build your code. While the csproj file is gross to look at and complicated to find where everything is at, you can always convert to the project.json way. But mind you, this is still a beta, so hopefully we get some better tools to manage all these different files.