{"id":15017766,"url":"https://github.com/jetbrains/teamcity-csharp-interactive","last_synced_at":"2025-10-10T23:33:02.707Z","repository":{"id":37480635,"uuid":"371665517","full_name":"JetBrains/teamcity-csharp-interactive","owner":"JetBrains","description":"The cross platform build automation system: C# scripts + powerful API","archived":false,"fork":false,"pushed_at":"2025-03-18T13:13:42.000Z","size":2248,"stargazers_count":33,"open_issues_count":0,"forks_count":10,"subscribers_count":13,"default_branch":"master","last_synced_at":"2025-09-22T06:02:37.238Z","etag":null,"topics":["build","build-automation","build-tool","c-sharp","csi","docker","dotnet","dotnet-tool","nuget","scripting-language","tools","unit-testing"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/JetBrains.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-05-28T10:33:23.000Z","updated_at":"2025-08-24T08:07:10.000Z","dependencies_parsed_at":"2024-02-07T15:56:35.223Z","dependency_job_id":null,"html_url":"https://github.com/JetBrains/teamcity-csharp-interactive","commit_stats":{"total_commits":293,"total_committers":5,"mean_commits":58.6,"dds":"0.017064846416382284","last_synced_commit":"c06811aa00857e64209f0b0320080b3cffaa0136"},"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"purl":"pkg:github/JetBrains/teamcity-csharp-interactive","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JetBrains%2Fteamcity-csharp-interactive","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JetBrains%2Fteamcity-csharp-interactive/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JetBrains%2Fteamcity-csharp-interactive/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JetBrains%2Fteamcity-csharp-interactive/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/JetBrains","download_url":"https://codeload.github.com/JetBrains/teamcity-csharp-interactive/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JetBrains%2Fteamcity-csharp-interactive/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279005572,"owners_count":26083919,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-10T02:00:06.843Z","response_time":62,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["build","build-automation","build-tool","c-sharp","csi","docker","dotnet","dotnet-tool","nuget","scripting-language","tools","unit-testing"],"created_at":"2024-09-24T19:50:58.043Z","updated_at":"2025-10-10T23:33:02.657Z","avatar_url":"https://github.com/JetBrains.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"## C# script tool for [\u003cimg src=\"https://cdn.worldvectorlogo.com/logos/teamcity.svg\" height=\"20\" align=\"center\"/\u003e](https://www.jetbrains.com/teamcity/)\n\n[\u003cimg src=\"http://jb.gg/badges/official.svg\"/\u003e](https://confluence.jetbrains.com/display/ALL/JetBrains+on+GitHub) [![NuGet TeamCity.csi](https://buildstats.info/nuget/TeamCity.csi?includePreReleases=true)](https://www.nuget.org/packages/TeamCity.csi) ![GitHub](https://img.shields.io/github/license/jetbrains/teamcity-csharp-interactive) [\u003cimg src=\"http://teamcity.jetbrains.com/app/rest/builds/buildType:(id:TeamCityPluginsByJetBrains_TeamCityCScript_BuildAndTestBuildType)/statusIcon.svg\"/\u003e](http://teamcity.jetbrains.com/viewType.html?buildTypeId=TeamCityPluginsByJetBrains_TeamCityCScript_BuildAndTestBuildType\u0026guest=1)\n\nThis is a repository of TeamCity.csi which is an interactive tool for running C# scripts. It can be used as\na [TeamCity build runner](https://github.com/JetBrains/teamcity-dotnet-plugin#c-script-runner) or installed as a\ncommand-line tool on Windows, Linux, or macOS.\n\n## Prerequisites\n\nThe tool requires [.NET 6+ runtime](https://dotnet.microsoft.com/en-us/download).\n\n## Use Inside TeamCity\n\nCurrently, the tool can be used as a TeamCity build runner provided in terms of TeamCity\n2021.2 [Early Access Program](https://www.jetbrains.com/teamcity/nextversion/). Read the runner's [documentation]() for\nmore details.\n\n## Use Outside TeamCity\n\nAfter installing tool you can use this tool independently of TeamCity, to run C# scripts from the command line. TeamCity.csi is available as a [NuGet package](https://www.nuget.org/packages/TeamCity.csi/).\n\nBefore installing TeamCity.csi as a local tool dot not forget to create .NET local tool manifest file if it is not exist:\n\n```Shell\ndotnet new tool-manifest\n```\n\nInstall the tool and add to the local tool manifest:\n\n```Shell\ndotnet tool install TeamCity.csi\n```\n\nOr install the tool for the current user:\n\n```Shell\ndotnet tool install TeamCity.csi -g\n```\n\nLaunch the tool in the interactive mode:\n\n```Shell\ndotnet csi\n```\n\nRun a specified script with a given argument:\n\n```Shell\ndotnet csi Samples/Scripts/hello.csx World \n```\n\nRun a single script located in the _MyDirectory_ directory:\n\n```Shell\ndotnet csi Samples/Build\n```\n\nUsage:\n\n```Shell\ndotnet csi [options] [--] [script] [script arguments]\n```\n\nExecutes a script if specified, otherwise launches an interactive REPL (Read Eval Print Loop).\n\nSupported arguments:\n\n| Option                  | Description                                                                                                                                                     | Alternative form                                                                              |\n|:------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------|\n| script                  | The path to the script file to run. If no such file is found, the command will treat it as a directory and look for a single script file inside that directory. |                                                                                               |\n| script arguments        | Script arguments are accessible in a script via the global list Args[index] by an argument index.                                                               |                                                                                               |\n| --                      | Indicates that the remaining arguments should not be treated as options.                                                                                        |                                                                                               |\n| --help                  | Show how to use the command.                                                                                                                                    | `/?`, `-h`, `/h`, `/help`                                                                     |\n| --version               | Display the tool version.                                                                                                                                       | `/version`                                                                                    |\n| --source                | Specify the NuGet package source to use. Supported formats: URL, or a UNC directory path.                                                                       | `-s`, `/s`, `/source`                                                                         |\n| --property \u003ckey=value\u003e  | Define a key-value pair(s) for the script properties called _Props_, which is accessible in scripts.                                                            | `-p`, `/property`, `/p`                                                                       |\n| --property:\u003ckey=value\u003e  | Define a key-value pair(s) in MSBuild style for the script properties called _Props_, which is accessible in scripts.                                           | `-p:\u003ckey=value\u003e`, `/property:\u003ckey=value\u003e`, `/p:\u003ckey=value\u003e`, `--property:key1=val1;key2=val2` |\n| @file                   | Read the response file for more options.                                                                                                                        |                                                                                               |\n\n```using HostApi;``` directive in a script allows you to use host API types without specifying the fully qualified namespace of these types.\n\n## Debug scripts easy!\n\nInstall the C# script template [TeamCity.CSharpInteractive.Templates](https://www.nuget.org/packages/TeamCity.CSharpInteractive.Templates)\n\n```shell\ndotnet new -i TeamCity.CSharpInteractive.Templates\n```\n\nCreate a console project \"Build\" containing a script from the template *__build__*\n\n```shell\ndotnet new build -o ./Build\n```\n\nThis projects contains the script *__./Build/Program.csx__*. To run this script from the command line from the directory *__Build__*:\n\n```shell\ndotnet csi Build\n```\n\nTo run this script as a console application:\n\n```shell\ndotnet run --project Build\n```\n\nOpen the *__./Build/Build.csproj__* in IDE and debug the script.\n\n## Report and Track Issues\n\nPlease use our YouTrack\nto [report](https://youtrack.jetbrains.com/newIssue?project=TW\u0026description=Expected%20behavior%20and%20actual%20behavior%3A%0A%0ASteps%20to%20reproduce%20the%20problem%3A%0A%0ASpecifications%20like%20the%20tool%20version%2C%20operating%20system%3A%0A%0AResult%20of%20'dotnet%20--info'%3A\u0026c=Subsystem%20Agent%20-%20.NET\u0026c=Assignee%20Nikolay.Pianikov\u0026c=tag%20.NET%20Core\u0026c=tag%20cs%20script%20step)\nrelated issues.\n\n## Usage Scenarios\n\n- Global state\n  - [Using Args](#using-args)\n  - [Using Props dictionary](#using-props-dictionary)\n  - [Using the Host property](#using-the-host-property)\n  - [Get services](#get-services)\n  - [Service collection](#service-collection)\n- Logging\n  - [Write a line to a build log](#write-a-line-to-a-build-log)\n  - [Write a line highlighted with \"Header\" color to a build log](#write-a-line-highlighted-with-\"header\"-color-to-a-build-log)\n  - [Write an empty line to a build log](#write-an-empty-line-to-a-build-log)\n  - [Log an error to a build log](#log-an-error-to-a-build-log)\n  - [Log a warning to a build log](#log-a-warning-to-a-build-log)\n  - [Log information to a build log](#log-information-to-a-build-log)\n  - [Log trace information to a build log](#log-trace-information-to-a-build-log)\n- Command Line API\n  - [Build command lines](#build-command-lines)\n  - [Run a command line](#run-a-command-line)\n  - [Run a command line asynchronously](#run-a-command-line-asynchronously)\n  - [Run and process output](#run-and-process-output)\n  - [Run asynchronously in parallel](#run-asynchronously-in-parallel)\n  - [Cancellation of asynchronous run](#cancellation-of-asynchronous-run)\n  - [Run timeout](#run-timeout)\n- Docker API\n  - [Build a project in a docker container](#build-a-project-in-a-docker-container)\n  - [Running in docker](#running-in-docker)\n- .NET build API\n  - [Build a project](#build-a-project)\n  - [Build a project using MSBuild](#build-a-project-using-msbuild)\n  - [Clean a project](#clean-a-project)\n  - [Pack a project](#pack-a-project)\n  - [Publish a project](#publish-a-project)\n  - [Restore a project](#restore-a-project)\n  - [Restore local tools](#restore-local-tools)\n  - [Run a custom .NET command](#run-a-custom-.net-command)\n  - [Run a project](#run-a-project)\n  - [Run tests under dotCover](#run-tests-under-dotcover)\n  - [Test a project](#test-a-project)\n  - [Test a project using the MSBuild VSTest target](#test-a-project-using-the-msbuild-vstest-target)\n  - [Test an assembly](#test-an-assembly)\n  - [Shuts down build servers](#shuts-down-build-servers)\n- NuGet API\n  - [Restore NuGet a package of newest version](#restore-nuget-a-package-of-newest-version)\n  - [Restore a NuGet package by a version range for the specified .NET and path](#restore-a-nuget-package-by-a-version-range-for-the-specified-.net-and-path)\n- TeamCity Service Messages API\n  - [TeamCity integration via service messages](#teamcity-integration-via-service-messages)\n\n### Using Args\n\n_Args_ have got from the script arguments.\n\n``` CSharp\nif (Args.Count \u003e 0)\n{\n    WriteLine(Args[0]);\n}\n\nif (Args.Count \u003e 1)\n{\n    WriteLine(Args[1]);\n}\n```\n\n\n\n### Using Props dictionary\n\nProperties _Props_ have got from TeamCity system properties automatically.\n\n``` CSharp\nWriteLine(Props[\"TEAMCITY_VERSION\"]);\nWriteLine(Props[\"TEAMCITY_PROJECT_NAME\"]);\n\n// This property will be available at the next TeamCity steps as system parameter _system.Version_\n// and some runners, for instance, the .NET runner, pass it as a build property.\nProps[\"Version\"] = \"1.1.6\";\n```\n\n\n\n### Using the Host property\n\n[_Host_](TeamCity.CSharpInteractive.HostApi/IHost.cs) is actually the provider of all global properties and methods.\n\n``` CSharp\nvar packages = Host.GetService\u003cINuGet\u003e();\nHost.WriteLine(\"Hello\");\n```\n\n\n\n### Get services\n\nThis method might be used to get access to different APIs like [INuGet](TeamCity.CSharpInteractive.HostApi/INuGet.cs) or [ICommandLine](TeamCity.CSharpInteractive.HostApi/ICommandLine.cs).\n\n``` CSharp\nGetService\u003cINuGet\u003e();\n\nvar serviceProvider = GetService\u003cIServiceProvider\u003e();\nserviceProvider.GetService(typeof(INuGet));\n```\n\nBesides that, it is possible to get an instance of [System.IServiceProvider](https://docs.microsoft.com/en-US/dotnet/api/system.iserviceprovider) to access APIs.\n\n### Service collection\n\n\n\n``` CSharp\npublic void Run()\n{\n    var serviceProvider = \n        GetService\u003cIServiceCollection\u003e()\n        .AddTransient\u003cMyTask\u003e()\n        .BuildServiceProvider();\n\n    var myTask = serviceProvider.GetRequiredService\u003cMyTask\u003e();\n    var exitCode = myTask.Run();\n    exitCode.ShouldBe(0);\n}\n\nclass MyTask\n{\n    private readonly ICommandLineRunner _runner;\n\n    public MyTask(ICommandLineRunner runner) =\u003e \n        _runner = runner;\n\n    public int? Run() =\u003e \n        _runner.Run(new CommandLine(\"whoami\"));\n}\n\n```\n\n\n\n### Write a line to a build log\n\n\n\n``` CSharp\nWriteLine(\"Hello\");\n```\n\n\n\n### Write an empty line to a build log\n\n\n\n``` CSharp\nWriteLine();\n```\n\n\n\n### Write a line highlighted with \"Header\" color to a build log\n\n\n\n``` CSharp\nWriteLine(\"Hello\", Header);\n```\n\n\n\n### Log an error to a build log\n\n\n\n``` CSharp\nError(\"Error info\", \"Error identifier\");\n```\n\n\n\n### Log a warning to a build log\n\n\n\n``` CSharp\nWarning(\"Warning info\");\n```\n\n\n\n### Log information to a build log\n\n\n\n``` CSharp\nInfo(\"Some info\");\n```\n\n\n\n### Log trace information to a build log\n\n\n\n``` CSharp\nTrace(\"Some trace info\");\n```\n\n\n\n### Build command lines\n\n\n\n``` CSharp\n// Adds the namespace \"Script.Cmd\" to use Command Line API\nusing HostApi;\n\n// Creates and run a simple command line \n\"whoami\".AsCommandLine().Run();\n\n// Creates and run a simple command line \nnew CommandLine(\"whoami\").Run();\n\n// Creates and run a command line with arguments \nnew CommandLine(\"cmd\", \"/c\", \"echo\", \"Hello\").Run();\n\n// Same as previous statement\nnew CommandLine(\"cmd\", \"/c\")\n    .AddArgs(\"echo\", \"Hello\")\n    .Run();\n\n(new CommandLine(\"cmd\") + \"/c\" + \"echo\" + \"Hello\").Run();\n\n(\"cmd\".AsCommandLine(\"/c\", \"echo\", \"Hello\")).Run();\n\n(\"cmd\".AsCommandLine() + \"/c\" + \"echo\" + \"Hello\").Run();\n\n// Just builds a command line with multiple environment variables\nvar cmd = new CommandLine(\"cmd\", \"/c\", \"echo\", \"Hello\")\n    .AddVars((\"Var1\", \"val1\"), (\"var2\", \"Val2\"));\n\n// Same as previous statement\ncmd = new CommandLine(\"cmd\") + \"/c\" + \"echo\" + \"Hello\" + (\"Var1\", \"val1\") + (\"var2\", \"Val2\");\n\n// Builds a command line to run from a specific working directory \ncmd = new CommandLine(\"cmd\", \"/c\", \"echo\", \"Hello\")\n    .WithWorkingDirectory(\"MyDyrectory\");\n\n// Builds a command line and replaces all command line arguments\ncmd = new CommandLine(\"cmd\", \"/c\", \"echo\", \"Hello\")\n    .WithArgs(\"/c\", \"echo\", \"Hello !!!\");\n```\n\n\n\n### Run a command line\n\n\n\n``` CSharp\n// Adds the namespace \"HostApi\" to use Command Line API\nusing HostApi;\n\nvar exitCode = GetService\u003cICommandLineRunner\u003e().Run(new CommandLine(\"cmd\", \"/c\", \"DIR\"));\nexitCode.ShouldBe(0);\n\n// or the same thing using the extension method\nexitCode = new CommandLine(\"cmd\", \"/c\", \"DIR\").Run();\nexitCode.ShouldBe(0);\n\n// using operator '+'\nvar cmd = new CommandLine(\"cmd\") + \"/c\" + \"DIR\";\nexitCode = cmd.Run();\nexitCode.ShouldBe(0);\n\n// with environment variables\ncmd = new CommandLine(\"cmd\") + \"/c\" + \"DIR\" + (\"MyEnvVar\", \"Some Value\");\nexitCode = cmd.Run();\nexitCode.ShouldBe(0);\n```\n\n\n\n### Run a command line asynchronously\n\n\n\n``` CSharp\n// Adds the namespace \"HostApi\" to use Command Line API\nusing HostApi;\n\nint? exitCode = await GetService\u003cICommandLineRunner\u003e().RunAsync(new CommandLine(\"cmd\", \"/C\", \"DIR\"));\n\n// or the same thing using the extension method\nexitCode = await new CommandLine(\"cmd\", \"/c\", \"DIR\").RunAsync();\n```\n\n\n\n### Run and process output\n\n\n\n``` CSharp\n// Adds the namespace \"HostApi\" to use Command Line API\nusing HostApi;\n\nvar lines = new List\u003cstring\u003e();\nint? exitCode = new CommandLine(\"cmd\", \"/c\", \"SET\")\n    .AddVars((\"MyEnv\", \"MyVal\"))\n    .Run(output =\u003e lines.Add(output.Line));\n\nlines.ShouldContain(\"MyEnv=MyVal\");\n```\n\n\n\n### Run asynchronously in parallel\n\n\n\n``` CSharp\n// Adds the namespace \"HostApi\" to use Command Line API\nusing HostApi;\n\nTask\u003cint?\u003e task = new CommandLine(\"cmd\", \"/c\", \"DIR\").RunAsync();\nint? exitCode = new CommandLine(\"cmd\", \"/c\", \"SET\").Run();\ntask.Wait();\n```\n\n\n\n### Cancellation of asynchronous run\n\nThe cancellation will kill a related process.\n\n``` CSharp\n// Adds the namespace \"HostApi\" to use Command Line API\nusing HostApi;\n\nvar cancellationTokenSource = new CancellationTokenSource();\nTask\u003cint?\u003e task = new CommandLine(\"cmd\", \"/c\", \"TIMEOUT\", \"/T\", \"120\")\n    .RunAsync(default, cancellationTokenSource.Token);\n\ncancellationTokenSource.CancelAfter(TimeSpan.FromMilliseconds(100));\ntask.IsCompleted.ShouldBeFalse();\n```\n\n\n\n### Run timeout\n\nIf timeout expired a process will be killed.\n\n``` CSharp\n// Adds the namespace \"HostApi\" to use Command Line API\nusing HostApi;\n\nint? exitCode = new CommandLine(\"cmd\", \"/c\", \"TIMEOUT\", \"/T\", \"120\")\n    .Run(default, TimeSpan.FromMilliseconds(1));\n\nexitCode.HasValue.ShouldBeFalse();\n```\n\n\n\n### Build a project\n\n\n\n``` CSharp\n// Adds the namespace \"HostApi\" to use .NET build API\nusing HostApi;\n\n// Creates a new library project, running a command like: \"dotnet new classlib -n MyLib --force\"\nvar result = new DotNetNew(\"classlib\", \"-n\", \"MyLib\", \"--force\").Build();\nresult.ExitCode.ShouldBe(0);\n\n// Builds the library project, running a command like: \"dotnet build\" from the directory \"MyLib\"\nresult = new DotNetBuild().WithWorkingDirectory(\"MyLib\").Build();\n\n// The \"result\" variable provides details about a build\nresult.Errors.Any(message =\u003e message.State == BuildMessageState.StdError).ShouldBeFalse();\nresult.ExitCode.ShouldBe(0);\n```\n\n\n\n### Clean a project\n\n\n\n``` CSharp\n// Adds the namespace \"HostApi\" to use .NET build API\nusing HostApi;\n\n// Creates a new library project, running a command like: \"dotnet new classlib -n MyLib --force\"\nvar result = new DotNetNew(\"classlib\", \"-n\", \"MyLib\", \"--force\").Build();\nresult.ExitCode.ShouldBe(0);\n\n// Builds the library project, running a command like: \"dotnet build\" from the directory \"MyLib\"\nresult = new DotNetBuild().WithWorkingDirectory(\"MyLib\").Build();\nresult.ExitCode.ShouldBe(0);\n\n// Clean the project, running a command like: \"dotnet clean\" from the directory \"MyLib\"\nresult = new DotNetClean().WithWorkingDirectory(\"MyLib\").Build();\n\n// The \"result\" variable provides details about a build\nresult.ExitCode.ShouldBe(0);\n```\n\n\n\n### Run a custom .NET command\n\n\n\n``` CSharp\n// Adds the namespace \"HostApi\" to use .NET build API\nusing HostApi;\n\n// Gets the dotnet version, running a command like: \"dotnet --version\"\nNuGetVersion? version = default;\nvar exitCode = new DotNetCustom(\"--version\")\n    .Run(message =\u003e NuGetVersion.TryParse(message.Line, out version));\n\nexitCode.ShouldBe(0);\nversion.ShouldNotBeNull();\n```\n\n\n\n### Test a project using the MSBuild VSTest target\n\n\n\n``` CSharp\n// Adds the namespace \"HostApi\" to use .NET build API\nusing HostApi;\n\n// Creates a new test project, running a command like: \"dotnet new mstest -n MyTests --force\"\nvar result = new DotNetNew(\"mstest\", \"-n\", \"MyTests\", \"--force\").Build();\nresult.ExitCode.ShouldBe(0);\n\n// Runs tests via a command like: \"dotnet msbuild /t:VSTest\" from the directory \"MyTests\"\nresult = new MSBuild()\n    .WithTarget(\"VSTest\")\n    .WithWorkingDirectory(\"MyTests\").Build();\n\n// The \"result\" variable provides details about a build\nresult.ExitCode.ShouldBe(0);\nresult.Summary.Tests.ShouldBe(1);\nresult.Tests.Count(test =\u003e test.State == TestState.Passed).ShouldBe(1);\n```\n\n\n\n### Pack a project\n\n\n\n``` CSharp\n// Adds the namespace \"HostApi\" to use .NET build API\nusing HostApi;\n\n// Creates a new library project, running a command like: \"dotnet new classlib -n MyLib --force\"\nvar result = new DotNetNew(\"classlib\", \"-n\", \"MyLib\", \"--force\").Build();\nresult.ExitCode.ShouldBe(0);\n\n// Creates a NuGet package of version 1.2.3 for the project, running a command like: \"dotnet pack /p:version=1.2.3\" from the directory \"MyLib\"\nresult = new DotNetPack()\n        .WithWorkingDirectory(\"MyLib\")\n        .AddProps((\"version\", \"1.2.3\"))\n        .Build();\n\nresult.ExitCode.ShouldBe(0);\n```\n\n\n\n### Publish a project\n\n\n\n``` CSharp\n// Adds the namespace \"HostApi\" to use .NET build API\nusing HostApi;\n\n// Creates a new library project, running a command like: \"dotnet new classlib -n MyLib --force\"\nvar result = new DotNetNew(\"classlib\", \"-n\", \"MyLib\", \"--force\", \"-f\", \"net6.0\").Build();\nresult.ExitCode.ShouldBe(0);\n\n// Publish the project, running a command like: \"dotnet publish --framework net6.0\" from the directory \"MyLib\"\nresult = new DotNetPublish().WithWorkingDirectory(\"MyLib\").WithFramework(\"net6.0\").Build();\nresult.ExitCode.ShouldBe(0);\n```\n\n\n\n### Restore a project\n\n\n\n``` CSharp\n// Adds the namespace \"HostApi\" to use .NET build API\nusing HostApi;\n\n// Creates a new library project, running a command like: \"dotnet new classlib -n MyLib --force\"\nvar result = new DotNetNew(\"classlib\", \"-n\", \"MyLib\", \"--force\").Build();\nresult.ExitCode.ShouldBe(0);\n\n// Restore the project, running a command like: \"dotnet restore\" from the directory \"MyLib\"\nresult = new DotNetRestore().WithWorkingDirectory(\"MyLib\").Build();\nresult.ExitCode.ShouldBe(0);\n```\n\n\n\n### Run a project\n\n\n\n``` CSharp\n// Adds the namespace \"HostApi\" to use .NET build API\nusing HostApi;\n\n// Creates a new console project, running a command like: \"dotnet new console -n MyApp --force\"\nvar result = new DotNetNew(\"console\", \"-n\", \"MyApp\", \"--force\").Build();\nresult.ExitCode.ShouldBe(0);\n\n// Runs the console project using a command like: \"dotnet run\" from the directory \"MyApp\"\nvar stdOut = new List\u003cstring\u003e();\nresult = new DotNetRun().WithWorkingDirectory(\"MyApp\").Build(message =\u003e stdOut.Add(message.Text));\nresult.ExitCode.ShouldBe(0);\n\n// Checks StdOut\nstdOut.ShouldBe(new[] {\"Hello, World!\"});\n```\n\n\n\n### Test a project\n\n\n\n``` CSharp\n// Adds the namespace \"HostApi\" to use .NET build API\nusing HostApi;\n\n// Creates a new test project, running a command like: \"dotnet new mstest -n MyTests --force\"\nvar result = new DotNetNew(\"mstest\", \"-n\", \"MyTests\", \"--force\").Build();\nresult.ExitCode.ShouldBe(0);\n\n// Runs tests via a command like: \"dotnet test\" from the directory \"MyTests\"\nresult = new DotNetTest().WithWorkingDirectory(\"MyTests\").Build();\n\n// The \"result\" variable provides details about a build\nresult.ExitCode.ShouldBe(0);\nresult.Summary.Tests.ShouldBe(1);\nresult.Tests.Count(test =\u003e test.State == TestState.Passed).ShouldBe(1);\n```\n\n\n\n### Run tests under dotCover\n\n\n\n``` CSharp\n// Adds the namespace \"HostApi\" to use .NET build API\nusing HostApi;\n\n// Creates a new test project, running a command like: \"dotnet new mstest -n MyTests --force\"\nvar exitCode = new DotNetNew(\"mstest\", \"-n\", \"MyTests\", \"--force\").Run();\nexitCode.ShouldBe(0);\n\n// Creates the tool manifest and installs the dotCover tool locally\n// It is better to run the following 2 commands manually\n// and commit these changes to a source control\nexitCode = new DotNetNew(\"tool-manifest\").Run();\nexitCode.ShouldBe(0);\n\nexitCode = new DotNetCustom(\"tool\",  \"install\", \"--local\", \"JetBrains.dotCover.GlobalTool\").Run();\nexitCode.ShouldBe(0);\n\n// Creates a test command\nvar test = new DotNetTest().WithProject(\"MyTests\");\n\nvar dotCoverSnapshot = Path.Combine(\"MyTests\", \"dotCover.dcvr\");\nvar dotCoverReport = Path.Combine(\"MyTests\", \"dotCover.html\");\n// Modifies the test command by putting \"dotCover\" in front of all arguments\n// to have something like \"dotnet dotcover test ...\"\n// and adding few specific arguments to the end\nvar testUnderDotCover = test.Customize(cmd =\u003e\n    cmd.ClearArgs()\n    + \"dotcover\"\n    + cmd.Args\n    + $\"--dcOutput={dotCoverSnapshot}\"\n    + \"--dcFilters=+:module=TeamCity.CSharpInteractive.HostApi;+:module=dotnet-csi\"\n    + \"--dcAttributeFilters=System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage\");\n    \n// Runs tests under dotCover via a command like: \"dotnet dotcover test ...\"\nvar result = testUnderDotCover.Build();\n\n// The \"result\" variable provides details about a build\nresult.ExitCode.ShouldBe(0);\nresult.Tests.Count(test =\u003e test.State == TestState.Passed).ShouldBe(1);\n\n// Generates a HTML code coverage report.\nexitCode = new DotNetCustom(\"dotCover\", \"report\", $\"--source={dotCoverSnapshot}\", $\"--output={dotCoverReport}\", \"--reportType=HTML\").Run();\nexitCode.ShouldBe(0);\n\n// Check for a dotCover report\nFile.Exists(dotCoverReport).ShouldBeTrue();\n```\n\n\n\n### Restore local tools\n\n\n\n``` CSharp\n// Adds the namespace \"HostApi\" to use .NET build API\nusing HostApi;\n\nvar projectDir = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()[..4]);\nDirectory.CreateDirectory(projectDir);\n    \n// Creates a local tool manifest \nvar exitCode = new DotNetNew(\"tool-manifest\").WithWorkingDirectory(projectDir).Run();\nexitCode.ShouldBe(0);\n\n// Restore local tools\nexitCode = new DotNetToolRestore().WithWorkingDirectory(projectDir).Run();\nexitCode.ShouldBe(0);\n```\n\n\n\n### Test an assembly\n\n\n\n``` CSharp\n// Adds the namespace \"HostApi\" to use .NET build API\nusing HostApi;\n\n// Creates a new test project, running a command like: \"dotnet new mstest -n MyTests --force\"\nvar result = new DotNetNew(\"mstest\", \"-n\", \"MyTests\", \"--force\").Build();\nresult.ExitCode.ShouldBe(0);\n\n// Builds the test project, running a command like: \"dotnet build -c Release\" from the directory \"MyTests\"\nresult = new DotNetBuild().WithWorkingDirectory(\"MyTests\").WithConfiguration(\"Release\").WithOutput(\"MyOutput\").Build();\nresult.ExitCode.ShouldBe(0);\n\n// Runs tests via a command like: \"dotnet vstest\" from the directory \"MyTests\"\nresult = new VSTest()\n    .AddTestFileNames(Path.Combine(\"MyOutput\", \"MyTests.dll\"))\n    .WithWorkingDirectory(\"MyTests\")\n    .Build();\n\n// The \"result\" variable provides details about a build\nresult.ExitCode.ShouldBe(0);\nresult.Summary.Tests.ShouldBe(1);\nresult.Tests.Count(test =\u003e test.State == TestState.Passed).ShouldBe(1);\n```\n\n\n\n### Build a project using MSBuild\n\n\n\n``` CSharp\n// Adds the namespace \"HostApi\" to use .NET build API\nusing HostApi;\n\n// Creates a new library project, running a command like: \"dotnet new classlib -n MyLib --force\"\nvar result = new DotNetNew(\"classlib\", \"-n\", \"MyLib\", \"--force\").Build();\nresult.ExitCode.ShouldBe(0);\n\n// Builds the library project, running a command like: \"dotnet msbuild /t:Build -restore /p:configuration=Release -verbosity=detailed\" from the directory \"MyLib\"\nresult = new MSBuild()\n    .WithWorkingDirectory(\"MyLib\")\n    .WithTarget(\"Build\")\n    .WithRestore(true)\n    .AddProps((\"configuration\", \"Release\"))\n    .WithVerbosity(DotNetVerbosity.Detailed)\n    .Build();\n\n// The \"result\" variable provides details about a build\nresult.Errors.Any(message =\u003e message.State == BuildMessageState.StdError).ShouldBeFalse();\nresult.ExitCode.ShouldBe(0);\n```\n\n\n\n### Shuts down build servers\n\n\n\n``` CSharp\n// Adds the namespace \"HostApi\" to use .NET build API\nusing HostApi;\n\n// Shuts down all build servers that are started from dotnet.\nvar exitCode = new DotNetBuildServerShutdown().Run();\n\nexitCode.ShouldBe(0);\n```\n\n\n\n### Restore NuGet a package of newest version\n\n\n\n``` CSharp\n// Adds the namespace \"HostApi\" to use INuGet\nusing HostApi;\n\nIEnumerable\u003cNuGetPackage\u003e packages = GetService\u003cINuGet\u003e().Restore(new NuGetRestoreSettings(\"IoC.Container\").WithVersionRange(VersionRange.All));\n```\n\n\n\n### Restore a NuGet package by a version range for the specified .NET and path\n\n\n\n``` CSharp\n// Adds the namespace \"HostApi\" to use INuGet\nusing HostApi;\n\nvar packagesPath = Path.Combine(\n    Path.GetTempPath(),\n    Guid.NewGuid().ToString()[..4]);\n\nvar settings = new NuGetRestoreSettings(\"IoC.Container\")\n    .WithVersionRange(VersionRange.Parse(\"[1.3, 1.3.8)\"))\n    .WithTargetFrameworkMoniker(\"net5.0\")\n    .WithPackagesPath(packagesPath);\n\nIEnumerable\u003cNuGetPackage\u003e packages = GetService\u003cINuGet\u003e().Restore(settings);\n```\n\n\n\n### Build a project in a docker container\n\n\n\n``` CSharp\n// Adds the namespace \"HostApi\" to use .NET build API and Docker API\nusing HostApi;\n\n// Creates a base docker command line\nvar dockerRun = new DockerRun()\n    .WithAutoRemove(true)\n    .WithImage(\"mcr.microsoft.com/dotnet/sdk\")\n    .WithPlatform(\"linux\")\n    .WithContainerWorkingDirectory(\"/MyProjects\")\n    .AddVolumes((Environment.CurrentDirectory, \"/MyProjects\"));\n\n// Creates a new library project in a docker container\nvar exitCode = dockerRun\n    .WithCommandLine(new DotNetCustom(\"new\", \"classlib\", \"-n\", \"MyLib\", \"--force\"))\n    .Run();\n\nexitCode.ShouldBe(0);\n\n// Builds the library project in a docker container\nvar result = dockerRun\n    .WithCommandLine(new DotNetBuild().WithProject(\"MyLib/MyLib.csproj\"))\n    .Build();\n\n// The \"result\" variable provides details about a build\nresult.Errors.Any(message =\u003e message.State == BuildMessageState.StdError).ShouldBeFalse();\nresult.ExitCode.ShouldBe(0);\n```\n\n\n\n### Running in docker\n\n\n\n``` CSharp\n// Adds the namespace \"HostApi\" to use Command Line API and Docker API\nusing HostApi;\n\n// Creates some command line to run in a docker container\nvar cmd = new CommandLine(\"whoami\");\n\n// Runs the command line in a docker container\nvar result = new DockerRun(cmd, \"mcr.microsoft.com/dotnet/sdk\")\n    .WithAutoRemove(true)\n    .Run();\n\nresult.ShouldBe(0);\n```\n\n\n\n### TeamCity integration via service messages\n\nFor more details how to use TeamCity service message API please see [this](https://github.com/JetBrains/TeamCity.ServiceMessages) page. Instead of creating a root message writer like in the following example:\n``` CSharp\nusing JetBrains.TeamCity.ServiceMessages.Write.Special;\nusing var writer = new TeamCityServiceMessages().CreateWriter(Console.WriteLine);\n```\nuse this statement:\n``` CSharp\nusing JetBrains.TeamCity.ServiceMessages.Write.Special;\nusing var writer = GetService\u003cITeamCityWriter\u003e();\n```\nThis sample opens a block _My Tests_ and reports about two tests:\n\n``` CSharp\n// Adds a namespace to use ITeamCityWriter\nusing JetBrains.TeamCity.ServiceMessages.Write.Special;\n\nusing var writer = GetService\u003cITeamCityWriter\u003e();\nusing (var tests = writer.OpenBlock(\"My Tests\"))\n{\n    using (var test = tests.OpenTest(\"Test1\"))\n    {\n        test.WriteStdOutput(\"Hello\");\n        test.WriteImage(\"TestsResults/Test1Screenshot.jpg\", \"Screenshot\");\n        test.WriteDuration(TimeSpan.FromMilliseconds(10));\n    }\n\n    using (var test = tests.OpenTest(\"Test2\"))\n    {\n        test.WriteIgnored(\"Some reason\");\n    }\n}\n```\n\nFor more information on TeamCity Service Messages, see [this](https://www.jetbrains.com/help/teamcity/service-messages.html) page.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjetbrains%2Fteamcity-csharp-interactive","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjetbrains%2Fteamcity-csharp-interactive","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjetbrains%2Fteamcity-csharp-interactive/lists"}