【先定一个小目标】dotnet core 命令详解

本篇博客来了解一下dotnet这个神奇的命令。我会依次对dotnet,dotnet new,dotnet restore,dotnet build,dotnet test,dotnet run,dotnet pack,dotnet publish这些个命令的用法做个简单的介绍以及演示。

1、基本的信息命令

dotnet命令主要是用来查看一些基本的信息,如平台、版本号等。经常会用到的参数有–version,–info,–help,下面依次运行下看看输出

dotnet –version 
1.0.0-preview2-003121 
dotnet –info 
.NET Command Line Tools (1.0.0-preview2-003121)

Product Information: 
Version: 1.0.0-preview2-003121 
Commit SHA-1 hash: 1e9d529bc5

Runtime Environment: 
OS Name: Mac OS X 
OS Version: 10.11 
OS Platform: Darwin 
RID: osx.10.11-x64 
dotnet –help 
.NET Command Line Tools (1.0.0-preview2-003121) 
Usage: dotnet [host-options] [command] [arguments] [common-options]

Arguments: 
[command] The command to execute 
[arguments] Arguments to pass to the command 
[host-options] Options specific to dotnet (host) 
[common-options] Options common to all commands

Common options: 
-v|–verbose Enable verbose output 
-h|–help Show help

Host options (passed before the command): 
-v|–verbose Enable verbose output 
–version Display .NET CLI Version Number 
–info Display .NET CLI Info

Common Commands: 
new Initialize a basic .NET project 
restore Restore dependencies specified in the .NET project 
build Builds a .NET project 
publish Publishes a .NET project for deployment (including the runtime) 
run Compiles and immediately executes a .NET project 
test Runs unit tests using the test runner specified in the project 
pack Creates a NuGet package

2、dotnet new

dotnet new命令用来创建一个.net core项目,该命令包含两个选项,分别是-t(或–type)和-l(或-lang),用来指定项目类型和编程语言。

-l, –lang [C#|F#] 
-l的默认值是c#,也可以设置为f#。VB的话应该很快就能支持了。

-t, –type [console|web|lib|xunittest] 
-t的默认值是concole,控制台项目。其它几个参数分别代表web项目,类库项目和测试项目。例如:dotnet new命令会创建一个控制台项目,dotnet new -t web会创建一个web项目(asp.NET mvc)。

3、dotnet restore

dotnet restore [–source] [–packages] [–disable-parallel] [–fallbacksource] [–configfile] [–verbosity] [] 

dotnet restore命令通过NuGet来下载定义在project.json文件中的依赖,然后放到用户目录下的.nuget/packages文件夹中。默认情况下,下载依赖组建的过程是并行进行的。 
–packages选项可以指定存放已下载组件的位置,默认是用户目录下的.nuget/packages文件夹。 
–disable-parallel选项用来禁用并行下载。 
–configfile选项用来指定使用哪个NuGet.config文件。 
dotnet restore命令运行完毕后,会生成project.lock.json文件。

4、dotnet build

dotnet build [–output] [–build-base-path] [–framework] [–configuration] [–runtime] [–version-suffix] 
[–build-profile] [–no-incremental] [–no-dependencies] [] 
dotnet build命令将项目中的所有源代码文件和依赖的组件编译为二进制的dll文件。该命令会读取project.lock.json,如果项目中找不到该文件,则需要先运行dotnet restore命令。 
执行完dotnet build命令后,会在bin/Debug/netcoreapp1.0文件夹下生成.dll文件和.pbd文件等,例如:

bash-3.2$ ls -al bin/Debug/netcoreapp1.0/ 
total 80 
drwxr-xr-x 11 jingjing staff 374 9 6 22:08 . 
drwxr-xr-x 6 jingjing staff 204 9 6 21:52 .. 
-rw-r–r–@ 1 jingjing staff 6148 9 6 23:13 .DS_Store 
-rwxr–r– 1 jingjing staff 1636 9 5 22:38 app.deps.json 
-rwxr–r– 1 jingjing staff 4608 9 5 22:38 app.dll 
-rwxr–r– 1 jingjing staff 496 9 5 22:38 app.pdb 
-rwxr–r– 1 jingjing staff 107 9 5 22:38 app.runtimeconfig.dev.json 
-rwxr–r– 1 jingjing staff 118 9 5 22:38 app.runtimeconfig.json 
-rwxr–r– 1 jingjing staff 3584 9 5 22:05 library.dll 
-rwxr–r– 1 jingjing staff 452 9 5 22:05 library.pdb 
drwxr-xr-x 10 jingjing staff 340 9 6 22:08 publish

5、dotnet test

dotnet test [–configuration] [–output] [–build-base-path] [–framework] [–runtime] [–no-build] [–parentProcessId] 
[–port] [] 
dotnet test命令用来运行单元测试项目中的测试代码,单元测试项目需要依赖一个单元测试框架(如nunit或xunit)以及对应的单元测试运行器,单元测试运行器在project.json文件中通过testRunner节点来指定。下面是一个使用xunit作为单元测试框架的项目的project.json文件

{
  "version": "1.0.0-*",
  "buildOptions": {
    "debugType": "portable"
  },
  "dependencies": {
    "System.Runtime.Serialization.Primitives": "4.1.1",
    "xunit": "2.1.0",
    "dotnet-test-xunit": "1.0.0-rc2-192208-24"
  },
  "testRunner": "xunit",
  "frameworks": {
    "netcoreapp1.0": {
      "dependencies": {
        "Microsoft.NETCore.App": {
          "type": "platform",
          "version": "1.0.0"
        }
      },
      "imports": [
        "dotnet5.4",
        "portable-net451+win8"
      ]
    }
  }
}

其中,”testRunner”: “xunit”指明了需要的单元测试运行器。

6、dotnet run

dotnet run [–framework] [–configuration] [–project] [–help] [–] 
dotnet run命令是一个比较便捷的运行代码的命令。它会编译代码,输出信息,并运行代码。

7、dotnet pack

dotnet pack [–output] [–no-build] [–build-base-path] [–configuration] [–version-suffix] [] 
dotnet pack命令编译代码并生成一个NuGet包,具体来说就是在binDebug目录下生成一个.nupkg文件和一个.symbols.nupkg文件。

8、dotnet publish

dotnet publish [–framework] [–runtime] [–build-base-path] [–output] [–version-suffix] [–configuration] [] 
dotnet publish命令会编译代码,然后读取project.json文件中定义的所有依赖组件,最后将这些东西输出到一个文件夹中。生成的文件默认会输出到inDebug etcoreapp1.0publish中,你可以通过-o或–output选项来修改输出位置。当你需要发布你的代码时,该命令的输出文件将是你所需要的全部文件。

原文地址:https://www.cnblogs.com/CrazyAnts/p/6322700.html