visual studio codeでc#コンソールアプリケーションの動かし方

今回はc#macで動かしたいと思います。 c#を動かすと聞けばvisual studio(VS)を思いつくと思いますがmacなんでVSさんは動きません! そこでVSさんの代わりにMSが提供しているVS codeでやってみたいと思います。

下準備

macにはc#コンパイラなどは始めからは入っていません。そこで.NETの互換のmonoをbrewでインストールします。

brew install mono

コンソールアプリケーションの雛形をつくるyeomanをインストール

npm install yo bower grunt-cli -g

DNVMのインストール*1

curl -sSL https://raw.githubusercontent.com/aspnet/Home/dev/dnvminstall.sh | DNX_BRANCH=dev sh && source ~/.dnx/dnvm/dnvm.sh

dnvm upgrade -r coreclr
dnvm upgrade -r mono

ターミナルで雛形作成

下準備でインストールしたyeomanでコンソールアプリケーションの雛形を作っていきます。

yo aspnet

するとさまざまな選択肢がでてきますが2番目のConsole Application を選択します。

f:id:mo121_7:20160104214806p:plain

選択するとアプリケーションの名前を聞かれます。

f:id:mo121_7:20160104215215p:plain

今回はデフォルトの名前でいきます。 ここで次のコマンドを実行しろと言われたので実行しときます。

cd "ConsoleApplication"
dnu restore
dnu build 
dnx ConsoleApplication

やっとvs codeの出番です。

vs codeでの設定

先ほど作成したコンソールアプリケーションの雛形をファイルごと開きます。 雛形では

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace ConsoleApplication
{
    public class Program
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Hello World");
            Console.Read();
        }
    }
}

このようなコードになっています。

f:id:mo121_7:20160104215751p:plain

デバッグを選択肢しスタートをクリックします。 f:id:mo121_7:20160105010901p:plain

c#ですのでmonoを選択すとlaunch.jsonが生成されます。

次に「comm+shift+B」を押します。

f:id:mo121_7:20160105011516p:plain

configure taske runnerを選択するとtasks.jsonが生成されます。 現在のtasks.jsonはtypescript用なので書き換える必要があり、次のように変えます。

{
    "version": "0.1.0",

    // The command is tsc. Assumes that tsc has been installed using npm install -g typescript
    "command": "xbuild",


    // Show the output window only if unrecognized errors occur.
    "showOutput": "silent",
    "taskSelector": "/t:",
    // args is the HelloWorld program to compile.
    "args": ["/property:GenerateFullPaths=true"],

    // use the standard tsc problem matcher to find compile problems
    // in the output.
    "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"
        }
    ]
}

また、プロジェクトファイルが必要のため作成します。 ConsleApplication.csprojの中身は

<Project DefaultTargets = "Build"
    xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

    <!-- Specify the inputs by type and file name -->
    <ItemGroup>
        <CSFile Include = "Program.cs"/>
    </ItemGroup>

    <Target Name = "Build">
        <!-- Run the Visual C# compilation using input files of type CSFile -->
        <CSC  Sources = "@(CSFile)"
            DebugType="full"
            Optimize="no"
            OutputAssembly="Program.exe" >

            <!-- Set the OutputAssembly attribute of the CSC task to the name of the executable file that is created -->
            <Output TaskParameter="OutputAssembly"
                ItemName = "EXEFile" />
        </CSC>
    </Target>
</Project>

となります。

f:id:mo121_7:20160105012847p:plain

コンパイル

やっときたコンパイル

tasks.jsonを作った時と同じく[comm+shift+B]を押すと

f:id:mo121_7:20160105013135p:plain

exeファイルができました!!

デバックの画面でスタートのボタンを押すと

f:id:mo121_7:20160105013349p:plain

出力されてますね。

f:id:mo121_7:20160105013543p:plain

きちんと動いてます。

*1:詳しくないのでこちらを