やってみる

アウトプットすべく己を導くためのブログ。その試行錯誤すらたれ流す。

Raspbian stretch dotnet コマンドについて調査する

 プロジェクトを自由に作れるようになりたくて。

対象環境

前回まで

 dotnetコマンドは.NETプロジェクトを作成するコマンド。.NET Coreをインストールすることで使用できる。

現状の問題

 MonoDevelopでテスト用プロジェクトを実行できない。dotnetならできないかと期待。

やりたいこと

  • dotnet
    • プロジェクト作成
      • ソリューションファイル作成
        • 任意プロジェクトを追加する
      • プロジェクトファイル作成
        • .NET Core, Standard, Framework の各種ライブラリ用プロジェクト
        • Console, UnitTest, Library
        • 別プロジェクトとの依存関係
        • NuGetによるパッケージ追加
      • カスタムテンプレート作成

 特に以下のような単体テストとクラスライブラリのソリューション作成方法を知りたい。これは開発において基本となる構成なので。

  • ソリューション
    • プロジェクト(NUnit [.NET Core/Standard])
      • プロジェクト(Library [.NET Core/Standard])

 GUIに関してはdotnetで開発できなさそう。MonoDevelopにてEto.Formsを使う。.NET Standardを用いているので移植できるよう.NET Standard用プロジェクトを作成できるか確認したい。

今回わかったこと

  • テスト系プロジェクトは.NET Standardが使えない(.NET Core, Frameworkのみ可)
    • Eto.Formsは.NET Standardを使うのでどうしたものか

今回わからなかったこと

  • dotnet new nunitdotnet new nunit-testの違い
  • --typeproject, item, otherとは一体何なのか

調査

dotnetシステム

コマンド 概要
dotnet -h サブコマンド一覧
dotnet サブコマンド -h 各サブコマンドの引数詳細
dotnet --version バージョン確認 2.2.101
dotnet --list-sdks 2.2.101 [/home/pi/root/lib/.NETCore/2.2.101/sdk]
dotnet --list-runtimes Microsoft.AspNetCore.All 2.2.0 [/home/pi/root/lib/.NETCore/2.2.101/shared/Microsoft.AspNetCore.All]
Microsoft.AspNetCore.App 2.2.0 [/home/pi/root/lib/.NETCore/2.2.101/shared/Microsoft.AspNetCore.App]
Microsoft.NETCore.App 2.2.0 [/home/pi/root/lib/.NETCore/2.2.101/shared/Microsoft.NETCore.App]

プロジェクト関係

 以下のコマンドが主に使いそう。

コマンド 概要
dotnet new 新規作成する。ソリューション、プロジェクト等
dotnet sln ソリューションにプロジェクトを追加・削除する
dotnet add reference プロジェクトに参照を追加する

情報源

dotnet -h

$ dotnet -h
.NET Command Line Tools (2.2.101)
Usage: dotnet [runtime-options] [path-to-application] [arguments]

Execute a .NET Core application.

runtime-options:
  --additionalprobingpath <path>     Path containing probing policy and assemblies to probe for.
  --additional-deps <path>           Path to additional deps.json file.
  --fx-version <version>             Version of the installed Shared Framework to use to run the application.
  --roll-forward-on-no-candidate-fx  Roll forward on no candidate shared framework is enabled.

path-to-application:
  The path to an application .dll file to execute.

Usage: dotnet [sdk-options] [command] [command-options] [arguments]

Execute a .NET Core SDK command.

sdk-options:
  -d|--diagnostics  Enable diagnostic output.
  -h|--help         Show command line help.
  --info            Display .NET Core information.
  --list-runtimes   Display the installed runtimes.
  --list-sdks       Display the installed SDKs.
  --version         Display .NET Core SDK version in use.

SDK commands:
  add               Add a package or reference to a .NET project.
  build             Build a .NET project.
  build-server      Interact with servers started by a build.
  clean             Clean build outputs of a .NET project.
  help              Show command line help.
  list              List project references of a .NET project.
  migrate           Migrate a project.json project to an MSBuild project.
  msbuild           Run Microsoft Build Engine (MSBuild) commands.
  new               Create a new .NET project or file.
  nuget             Provides additional NuGet commands.
  pack              Create a NuGet package.
  publish           Publish a .NET project for deployment.
  remove            Remove a package or reference from a .NET project.
  restore           Restore dependencies specified in a .NET project.
  run               Build and run a .NET project output.
  sln               Modify Visual Studio solution files.
  store             Store the specified assemblies in the runtime package store.
  test              Run unit tests using the test runner specified in a .NET project.
  tool              Install or manage tools that extend the .NET experience.
  vstest            Run Microsoft Test Engine (VSTest) commands.

Additional commands from bundled tools:
  dev-certs         Create and manage development certificates.
  ef                Entity Framework Core command-line tools.
  sql-cache         SQL Server cache command-line tools.
  user-secrets      Manage development user secrets.
  watch             Start a file watcher that runs a command when files change.

Run 'dotnet [command] --help' for more information on a command.

dotnet new -h

 ソリューションやプロジェクトファイル一式の作成。

$ dotnet new -h
Usage: new [options]

Options:
  -h, --help          Displays help for this command.
  -l, --list          Lists templates containing the specified name. If no name is specified, lists all templates.
  -n, --name          The name for the output being created. If no name is specified, the name of the current directory is used.
  -o, --output        Location to place the generated output.
  -i, --install       Installs a source or a template pack.
  -u, --uninstall     Uninstalls a source or a template pack.
  --nuget-source      Specifies a NuGet source to use during install.
  --type              Filters templates based on available types. Predefined values are "project", "item" or "other".
  --dry-run           Displays a summary of what would happen if the given command line were run if it would result in a template creation.
  --force             Forces content to be generated even if it would change existing files.
  -lang, --language   Filters templates based on language and specifies the language of the template to create.


Templates                                         Short Name         Language          Tags                                 
----------------------------------------------------------------------------------------------------------------------------
Console Application                               console            [C#], F#, VB      Common/Console                       
Class library                                     classlib           [C#], F#, VB      Common/Library                       
Unit Test Project                                 mstest             [C#], F#, VB      Test/MSTest                          
NUnit 3 Test Project                              nunit              [C#], F#, VB      Test/NUnit                           
NUnit 3 Test Item                                 nunit-test         [C#], F#, VB      Test/NUnit                           
xUnit Test Project                                xunit              [C#], F#, VB      Test/xUnit                           
Razor Page                                        page               [C#]              Web/ASP.NET                          
MVC ViewImports                                   viewimports        [C#]              Web/ASP.NET                          
MVC ViewStart                                     viewstart          [C#]              Web/ASP.NET                          
ASP.NET Core Empty                                web                [C#], F#          Web/Empty                            
ASP.NET Core Web App (Model-View-Controller)      mvc                [C#], F#          Web/MVC                              
ASP.NET Core Web App                              webapp             [C#]              Web/MVC/Razor Pages                  
ASP.NET Core with Angular                         angular            [C#]              Web/MVC/SPA                          
ASP.NET Core with React.js                        react              [C#]              Web/MVC/SPA                          
ASP.NET Core with React.js and Redux              reactredux         [C#]              Web/MVC/SPA                          
Razor Class Library                               razorclasslib      [C#]              Web/Razor/Library/Razor Class Library
ASP.NET Core Web API                              webapi             [C#], F#          Web/WebAPI                           
global.json file                                  globaljson                           Config                               
NuGet Config                                      nugetconfig                          Config                               
Web Config                                        webconfig                            Config                               
Solution File                                     sln                                  Solution                             

Examples:
    dotnet new mvc --auth Individual
    dotnet new nunit -f net472
    dotnet new --help

dotnet new nunit -h

 テスト用プロジェクト作成。

$ dotnet new nunit -h
Usage: new [options]

Options:
  -h, --help          Displays help for this command.
  -l, --list          Lists templates containing the specified name. If no name is specified, lists all templates.
  -n, --name          The name for the output being created. If no name is specified, the name of the current directory is used.
  -o, --output        Location to place the generated output.
  -i, --install       Installs a source or a template pack.
  -u, --uninstall     Uninstalls a source or a template pack.
  --nuget-source      Specifies a NuGet source to use during install.
  --type              Filters templates based on available types. Predefined values are "project", "item" or "other".
  --dry-run           Displays a summary of what would happen if the given command line were run if it would result in a template creation.
  --force             Forces content to be generated even if it would change existing files.
  -lang, --language   Filters templates based on language and specifies the language of the template to create.


NUnit 3 Test Project (C#)
Author: Aleksei Kharlov aka halex2005 (codeofclimber.ru)
Description: A project that contains NUnit tests that can run on .NET Core on Windows, Linux and macOS
Options:                                                                                    
  -f|--framework    The target framework for the project.                                   
                        netcoreapp1.0    - Target netcoreapp1.0                             
                        netcoreapp1.1    - Target netcoreapp1.1                             
                        netcoreapp2.0    - Target netcoreapp2.0                             
                        netcoreapp2.1    - Target netcoreapp2.1                             
                        netcoreapp2.2    - Target netcoreapp2.2                             
                        netcoreapp3.0    - Target netcoreapp3.0                             
                        net35            - Target net35                                     
                        net40            - Target net40                                     
                        net45            - Target net45                                     
                        net451           - Target net451                                    
                        net452           - Target net452                                    
                        net46            - Target net46                                     
                        net461           - Target net461                                    
                        net462           - Target net462                                    
                        net47            - Target net47                                     
                        net471           - Target net471                                    
                        net472           - Target net472                                    
                        net48            - Target net48                                     
                    Default: netcoreapp2.2                                                  

  -p|--enable-pack  Whether or not to enable packaging (via ("dotnet pack") for the project.
                    bool - Optional                                                         
                    Default: false / (*) true                                               

  --no-restore      If specified, skips the automatic restore of the project on create.     
                    bool - Optional                                                         
                    Default: false / (*) true                                               


* Indicates the value used if the switch is provided without a value.

 netstandard1.6などがない。

 どうやらNUnit, xUnit, MSUnitのテスト系プロジェクトはすべて.NET Standardでは実行できないらしい。テスト実行は特定のプラットフォームである必要があるとか。つまりStandardはクロスプラットフォーム用ライブラリだからテストできないということか。それひどくない?

 解法としては以下か? テストプロジェクトと本番プロジェクトが別になってしまうため、変更するたびにコピペ作業が発生してしまう。

  1. .NET Core or Frameowrkでクラスライブラリプロジェクトを作成する
  2. .NET Core or Frameowrkで1のテストプロジェクトを作成する
  3. Eto.Forms(.NET Standard)プロジェクトに1のソースコードをコピペする

 もしくはクラスライブラリプロジェクトを2種類作成する。

  1. .NET Core or Frameowrkでクラスライブラリプロジェクトを作成する
  2. .NET Core or Frameowrkで1のテストプロジェクトを作成する
  3. .NET Standardでクラスライブラリプロジェクトを作成して1のコードをコピペする
  4. Eto.Forms(.NET Standard)プロジェクトを作成し3を参照する

 3のプロジェクトが1と重複するが仕方ない。
 3ソースコード1シンボリックリンクにするのが良いか? しかし、GitHubなど別OSにアップロードすればリンク情報は消えてしまうだろう。冗長さは諦めるしかないか。

 ソリューションを2つに分けるか一つにするか。テスト用とUI用の二つに分離したほうが見やすいが、管理のしやすさなら一つにまとめたほうがいい。一つにまとめた場合はスタートアッププロジェクトを切り替えることでテストとUI起動を変更できるはず。

dotnet new nunit-test -h

$ dotnet new nunit-test -h
Usage: new [options]

Options:
  -h, --help          Displays help for this command.
  -l, --list          Lists templates containing the specified name. If no name is specified, lists all templates.
  -n, --name          The name for the output being created. If no name is specified, the name of the current directory is used.
  -o, --output        Location to place the generated output.
  -i, --install       Installs a source or a template pack.
  -u, --uninstall     Uninstalls a source or a template pack.
  --nuget-source      Specifies a NuGet source to use during install.
  --type              Filters templates based on available types. Predefined values are "project", "item" or "other".
  --dry-run           Displays a summary of what would happen if the given command line were run if it would result in a template creation.
  --force             Forces content to be generated even if it would change existing files.
  -lang, --language   Filters templates based on language and specifies the language of the template to create.


NUnit 3 Test Item (C#)
Author: Aleksei Kharlov aka halex2005 (codeofclimber.ru)
Description: A item that contains NUnit tests
    (No Parameters)

 --typeproject, item, otherとは一体何なのか。dotnet new nunitdotnet new nunit-testの違いは?

dotnet new classlib -h

 クラスライブラリ用プロジェクト作成。

$ dotnet new classlib -h
Usage: new [options]

Options:
  -h, --help          Displays help for this command.
  -l, --list          Lists templates containing the specified name. If no name is specified, lists all templates.
  -n, --name          The name for the output being created. If no name is specified, the name of the current directory is used.
  -o, --output        Location to place the generated output.
  -i, --install       Installs a source or a template pack.
  -u, --uninstall     Uninstalls a source or a template pack.
  --nuget-source      Specifies a NuGet source to use during install.
  --type              Filters templates based on available types. Predefined values are "project", "item" or "other".
  --dry-run           Displays a summary of what would happen if the given command line were run if it would result in a template creation.
  --force             Forces content to be generated even if it would change existing files.
  -lang, --language   Filters templates based on language and specifies the language of the template to create.


Class library (C#)
Author: Microsoft
Description: A project for creating a class library that targets .NET Standard or .NET Core
Options:                                                                             
  -f|--framework  The target framework for the project.                              
                      netcoreapp2.2     - Target netcoreapp2.2                       
                      netstandard2.0    - Target netstandard2.0                      
                  Default: netstandard2.0                                            

  --langVersion   Sets langVersion in the created project file                       
                  text - Optional                                                    

  --no-restore    If specified, skips the automatic restore of the project on create.
                  bool - Optional                                                    
                  Default: false / (*) true                                          


* Indicates the value used if the switch is provided without a value.

 デフォルトがnetstandard2.0だが、NUnitnetstandardが使えない。テストするときはどうするのが一般的なの?

dotnet sln -h

 ソリューションにプロジェクトを追加する。dotnet newでソリューションとプロジェクトを作成してから実行する。

$ dotnet sln -h
Usage: dotnet sln [options] <SLN_FILE> [command]

Arguments:
  <SLN_FILE>   The solution file to operate on. If not specified, the command will search the current directory for one.

Options:
  -h, --help   Show command line help.

Commands:
  add <PROJECT_PATH>      Add one or more projects to a solution file.
  list                    List all projects in a solution file.
  remove <PROJECT_PATH>   Remove one or more projects from a solution file.

dotnet add -h

 プロジェクトの参照(依存関係)を設定する。dotnet newでプロジェクトを作成してから実行する。

$ dotnet add -h
Usage: dotnet add [options] <PROJECT> [command]

Arguments:
  <PROJECT>   The project file to operate on. If a file is not specified, the command will search the current directory for one.

Options:
  -h, --help   Show command line help.

Commands:
  package <PACKAGE_NAME>     Add a NuGet package reference to the project.
  reference <PROJECT_PATH>   Add a project-to-project reference to the project.

dotnet add reference -h

$ dotnet add reference -h
Usage: dotnet add <PROJECT> reference [options] <PROJECT_PATH>

Arguments:
  <PROJECT>        The project file to operate on. If a file is not specified, the command will search the current directory for one.
  <PROJECT_PATH>   The paths to the projects to add as references.

Options:
  -h, --help                    Show command line help.
  -f, --framework <FRAMEWORK>   Add the reference only when targeting a specific framework.