|
1 | 1 |
|
2 | | -## Usage Scenarios |
| 2 | +## API |
3 | 3 |
|
4 | | -- Global state |
5 | | - - [Using Args](#using-args) |
6 | | - - [Using Props](#using-props) |
7 | | - - [Using the Host property](#using-the-host-property) |
8 | | - - [Get services](#get-services) |
9 | | - - [Service collection](#service-collection) |
10 | | -- Logging |
| 4 | +- Output, logging and tracing |
11 | 5 | - [Write a line to a build log](#write-a-line-to-a-build-log) |
12 | 6 | - [Write a line highlighted with "Header" color to a build log](#write-a-line-highlighted-with-"header"-color-to-a-build-log) |
13 | 7 | - [Write an empty line to a build log](#write-an-empty-line-to-a-build-log) |
14 | 8 | - [Log an error to a build log](#log-an-error-to-a-build-log) |
15 | 9 | - [Log a warning to a build log](#log-a-warning-to-a-build-log) |
16 | 10 | - [Log information to a build log](#log-information-to-a-build-log) |
17 | 11 | - [Log trace information to a build log](#log-trace-information-to-a-build-log) |
18 | | -- Command Line API |
| 12 | +- Arguments and parameters |
| 13 | + - [Using Args](#using-args) |
| 14 | + - [Using Props](#using-props) |
| 15 | +- Microsoft DI |
| 16 | + - [Using the Host property](#using-the-host-property) |
| 17 | + - [Get services](#get-services) |
| 18 | + - [Service collection](#service-collection) |
| 19 | +- NuGet |
| 20 | + - [Restore NuGet a package of newest version](#restore-nuget-a-package-of-newest-version) |
| 21 | + - [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) |
| 22 | +- Command Line |
19 | 23 | - [Build command lines](#build-command-lines) |
20 | 24 | - [Run a command line](#run-a-command-line) |
21 | 25 | - [Run a command line asynchronously](#run-a-command-line-asynchronously) |
22 | 26 | - [Run and process output](#run-and-process-output) |
23 | 27 | - [Run asynchronously in parallel](#run-asynchronously-in-parallel) |
24 | 28 | - [Cancellation of asynchronous run](#cancellation-of-asynchronous-run) |
25 | 29 | - [Run timeout](#run-timeout) |
26 | | -- Docker API |
| 30 | +- Docker CLI |
27 | 31 | - [Build a project in a docker container](#build-a-project-in-a-docker-container) |
28 | 32 | - [Running in docker](#running-in-docker) |
29 | | -- .NET build API |
| 33 | +- .NET CLI |
30 | 34 | - [Build a project](#build-a-project) |
31 | 35 | - [Build a project using MSBuild](#build-a-project-using-msbuild) |
32 | 36 | - [Clean a project](#clean-a-project) |
|
41 | 45 | - [Test a project using the MSBuild VSTest target](#test-a-project-using-the-msbuild-vstest-target) |
42 | 46 | - [Test an assembly](#test-an-assembly) |
43 | 47 | - [Shuts down build servers](#shuts-down-build-servers) |
44 | | -- NuGet API |
45 | | - - [Restore NuGet a package of newest version](#restore-nuget-a-package-of-newest-version) |
46 | | - - [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) |
47 | | -- TeamCity Service Messages API |
| 48 | +- TeamCity API |
48 | 49 | - [TeamCity integration via service messages](#teamcity-integration-via-service-messages) |
49 | 50 |
|
| 51 | +### Write a line to a build log |
| 52 | + |
| 53 | + |
| 54 | + |
| 55 | +``` CSharp |
| 56 | +WriteLine("Hello"); |
| 57 | +``` |
| 58 | + |
| 59 | + |
| 60 | + |
| 61 | +### Write an empty line to a build log |
| 62 | + |
| 63 | + |
| 64 | + |
| 65 | +``` CSharp |
| 66 | +WriteLine(); |
| 67 | +``` |
| 68 | + |
| 69 | + |
| 70 | + |
| 71 | +### Write a line highlighted with "Header" color to a build log |
| 72 | + |
| 73 | + |
| 74 | + |
| 75 | +``` CSharp |
| 76 | +WriteLine("Hello", Header); |
| 77 | +``` |
| 78 | + |
| 79 | + |
| 80 | + |
| 81 | +### Log an error to a build log |
| 82 | + |
| 83 | + |
| 84 | + |
| 85 | +``` CSharp |
| 86 | +Error("Error info", "Error identifier"); |
| 87 | +``` |
| 88 | + |
| 89 | + |
| 90 | + |
| 91 | +### Log a warning to a build log |
| 92 | + |
| 93 | + |
| 94 | + |
| 95 | +``` CSharp |
| 96 | +Warning("Warning info"); |
| 97 | +``` |
| 98 | + |
| 99 | + |
| 100 | + |
| 101 | +### Log information to a build log |
| 102 | + |
| 103 | + |
| 104 | + |
| 105 | +``` CSharp |
| 106 | +Info("Some info"); |
| 107 | +``` |
| 108 | + |
| 109 | + |
| 110 | + |
| 111 | +### Log trace information to a build log |
| 112 | + |
| 113 | + |
| 114 | + |
| 115 | +``` CSharp |
| 116 | +Trace("Some trace info"); |
| 117 | +``` |
| 118 | + |
| 119 | + |
| 120 | + |
50 | 121 | ### Using Args |
51 | 122 |
|
52 | 123 | _Args_ have got from the script arguments. |
@@ -133,72 +204,38 @@ private class MyTask(ICommandLineRunner runner) |
133 | 204 |
|
134 | 205 |
|
135 | 206 |
|
136 | | -### Write a line to a build log |
137 | | - |
138 | | - |
139 | | - |
140 | | -``` CSharp |
141 | | -WriteLine("Hello"); |
142 | | -``` |
143 | | - |
144 | | - |
145 | | - |
146 | | -### Write an empty line to a build log |
147 | | - |
148 | | - |
149 | | - |
150 | | -``` CSharp |
151 | | -WriteLine(); |
152 | | -``` |
153 | | - |
154 | | - |
155 | | - |
156 | | -### Write a line highlighted with "Header" color to a build log |
157 | | - |
158 | | - |
159 | | - |
160 | | -``` CSharp |
161 | | -WriteLine("Hello", Header); |
162 | | -``` |
163 | | - |
164 | | - |
165 | | - |
166 | | -### Log an error to a build log |
| 207 | +### Restore NuGet a package of newest version |
167 | 208 |
|
168 | 209 |
|
169 | 210 |
|
170 | 211 | ``` CSharp |
171 | | -Error("Error info", "Error identifier"); |
172 | | -``` |
173 | | - |
174 | | - |
175 | | - |
176 | | -### Log a warning to a build log |
177 | | - |
178 | | - |
| 212 | +// Adds the namespace "HostApi" to use INuGet |
| 213 | +using HostApi; |
179 | 214 |
|
180 | | -``` CSharp |
181 | | -Warning("Warning info"); |
| 215 | +IEnumerable<NuGetPackage> packages = GetService<INuGet>() |
| 216 | + .Restore(new NuGetRestoreSettings("IoC.Container").WithVersionRange(VersionRange.All)); |
182 | 217 | ``` |
183 | 218 |
|
184 | 219 |
|
185 | 220 |
|
186 | | -### Log information to a build log |
| 221 | +### Restore a NuGet package by a version range for the specified .NET and path |
187 | 222 |
|
188 | 223 |
|
189 | 224 |
|
190 | 225 | ``` CSharp |
191 | | -Info("Some info"); |
192 | | -``` |
193 | | - |
194 | | - |
195 | | - |
196 | | -### Log trace information to a build log |
| 226 | +// Adds the namespace "HostApi" to use INuGet |
| 227 | +using HostApi; |
197 | 228 |
|
| 229 | +var packagesPath = Path.Combine( |
| 230 | + Path.GetTempPath(), |
| 231 | + Guid.NewGuid().ToString()[..4]); |
198 | 232 |
|
| 233 | +var settings = new NuGetRestoreSettings("IoC.Container") |
| 234 | + .WithVersionRange(VersionRange.Parse("[1.3, 1.3.8)")) |
| 235 | + .WithTargetFrameworkMoniker("net5.0") |
| 236 | + .WithPackagesPath(packagesPath); |
199 | 237 |
|
200 | | -``` CSharp |
201 | | -Trace("Some trace info"); |
| 238 | +IEnumerable<NuGetPackage> packages = GetService<INuGet>().Restore(settings); |
202 | 239 | ``` |
203 | 240 |
|
204 | 241 |
|
@@ -380,6 +417,66 @@ exitCode.HasValue.ShouldBeFalse(); |
380 | 417 |
|
381 | 418 |
|
382 | 419 |
|
| 420 | +### Build a project in a docker container |
| 421 | + |
| 422 | + |
| 423 | + |
| 424 | +``` CSharp |
| 425 | +// Adds the namespace "HostApi" to use .NET build API and Docker API |
| 426 | +using HostApi; |
| 427 | + |
| 428 | +// Creates a base docker command line |
| 429 | +var dockerRun = new DockerRun() |
| 430 | + .WithAutoRemove(true) |
| 431 | + .WithInteractive(true) |
| 432 | + .WithImage("mcr.microsoft.com/dotnet/sdk") |
| 433 | + .WithPlatform("linux") |
| 434 | + .WithContainerWorkingDirectory("/MyProjects") |
| 435 | + .AddVolumes((ToAbsoluteLinuxPath(Environment.CurrentDirectory), "/MyProjects")); |
| 436 | + |
| 437 | + |
| 438 | +// Creates a new library project in a docker container |
| 439 | +dockerRun |
| 440 | + .WithCommandLine(new DotNetCustom("new", "classlib", "-n", "MyLib", "--force")) |
| 441 | + .Run() |
| 442 | + .EnsureSuccess(); |
| 443 | + |
| 444 | +// Builds the library project in a docker container |
| 445 | +var result = dockerRun |
| 446 | + .WithCommandLine(new DotNetBuild().WithProject("MyLib/MyLib.csproj")) |
| 447 | + .Build() |
| 448 | + .EnsureSuccess(); |
| 449 | + |
| 450 | +// The "result" variable provides details about a build |
| 451 | +result.Errors.Any(message => message.State == BuildMessageState.StdError).ShouldBeFalse(); |
| 452 | +result.ExitCode.ShouldBe(0); |
| 453 | + |
| 454 | +string ToAbsoluteLinuxPath(string path) => |
| 455 | + "/" + path.Replace(":", "").Replace('\\', '/'); |
| 456 | +``` |
| 457 | + |
| 458 | + |
| 459 | + |
| 460 | +### Running in docker |
| 461 | + |
| 462 | + |
| 463 | + |
| 464 | +``` CSharp |
| 465 | +// Adds the namespace "HostApi" to use Command Line API and Docker API |
| 466 | +using HostApi; |
| 467 | + |
| 468 | +// Creates some command line to run in a docker container |
| 469 | +var cmd = new CommandLine("whoami"); |
| 470 | + |
| 471 | +// Runs the command line in a docker container |
| 472 | +var result = new DockerRun(cmd, "mcr.microsoft.com/dotnet/sdk") |
| 473 | + .WithAutoRemove(true) |
| 474 | + .Run() |
| 475 | + .EnsureSuccess(); |
| 476 | +``` |
| 477 | + |
| 478 | + |
| 479 | + |
383 | 480 | ### Build a project |
384 | 481 |
|
385 | 482 |
|
@@ -812,102 +909,6 @@ new DotNetBuildServerShutdown() |
812 | 909 |
|
813 | 910 |
|
814 | 911 |
|
815 | | -### Restore NuGet a package of newest version |
816 | | - |
817 | | - |
818 | | - |
819 | | -``` CSharp |
820 | | -// Adds the namespace "HostApi" to use INuGet |
821 | | -using HostApi; |
822 | | - |
823 | | -IEnumerable<NuGetPackage> packages = GetService<INuGet>() |
824 | | - .Restore(new NuGetRestoreSettings("IoC.Container").WithVersionRange(VersionRange.All)); |
825 | | -``` |
826 | | - |
827 | | - |
828 | | - |
829 | | -### Restore a NuGet package by a version range for the specified .NET and path |
830 | | - |
831 | | - |
832 | | - |
833 | | -``` CSharp |
834 | | -// Adds the namespace "HostApi" to use INuGet |
835 | | -using HostApi; |
836 | | - |
837 | | -var packagesPath = Path.Combine( |
838 | | - Path.GetTempPath(), |
839 | | - Guid.NewGuid().ToString()[..4]); |
840 | | - |
841 | | -var settings = new NuGetRestoreSettings("IoC.Container") |
842 | | - .WithVersionRange(VersionRange.Parse("[1.3, 1.3.8)")) |
843 | | - .WithTargetFrameworkMoniker("net5.0") |
844 | | - .WithPackagesPath(packagesPath); |
845 | | - |
846 | | -IEnumerable<NuGetPackage> packages = GetService<INuGet>().Restore(settings); |
847 | | -``` |
848 | | - |
849 | | - |
850 | | - |
851 | | -### Build a project in a docker container |
852 | | - |
853 | | - |
854 | | - |
855 | | -``` CSharp |
856 | | -// Adds the namespace "HostApi" to use .NET build API and Docker API |
857 | | -using HostApi; |
858 | | - |
859 | | -// Creates a base docker command line |
860 | | -var dockerRun = new DockerRun() |
861 | | - .WithAutoRemove(true) |
862 | | - .WithInteractive(true) |
863 | | - .WithImage("mcr.microsoft.com/dotnet/sdk") |
864 | | - .WithPlatform("linux") |
865 | | - .WithContainerWorkingDirectory("/MyProjects") |
866 | | - .AddVolumes((ToAbsoluteLinuxPath(Environment.CurrentDirectory), "/MyProjects")); |
867 | | - |
868 | | - |
869 | | -// Creates a new library project in a docker container |
870 | | -dockerRun |
871 | | - .WithCommandLine(new DotNetCustom("new", "classlib", "-n", "MyLib", "--force")) |
872 | | - .Run() |
873 | | - .EnsureSuccess(); |
874 | | - |
875 | | -// Builds the library project in a docker container |
876 | | -var result = dockerRun |
877 | | - .WithCommandLine(new DotNetBuild().WithProject("MyLib/MyLib.csproj")) |
878 | | - .Build() |
879 | | - .EnsureSuccess(); |
880 | | - |
881 | | -// The "result" variable provides details about a build |
882 | | -result.Errors.Any(message => message.State == BuildMessageState.StdError).ShouldBeFalse(); |
883 | | -result.ExitCode.ShouldBe(0); |
884 | | - |
885 | | -string ToAbsoluteLinuxPath(string path) => |
886 | | - "/" + path.Replace(":", "").Replace('\\', '/'); |
887 | | -``` |
888 | | - |
889 | | - |
890 | | - |
891 | | -### Running in docker |
892 | | - |
893 | | - |
894 | | - |
895 | | -``` CSharp |
896 | | -// Adds the namespace "HostApi" to use Command Line API and Docker API |
897 | | -using HostApi; |
898 | | - |
899 | | -// Creates some command line to run in a docker container |
900 | | -var cmd = new CommandLine("whoami"); |
901 | | - |
902 | | -// Runs the command line in a docker container |
903 | | -var result = new DockerRun(cmd, "mcr.microsoft.com/dotnet/sdk") |
904 | | - .WithAutoRemove(true) |
905 | | - .Run() |
906 | | - .EnsureSuccess(); |
907 | | -``` |
908 | | - |
909 | | - |
910 | | - |
911 | 912 | ### TeamCity integration via service messages |
912 | 913 |
|
913 | 914 | For 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: |
|
0 commit comments