Skip to content

Commit c4739fc

Browse files
Add comments
1 parent 28cdd7a commit c4739fc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+1284
-165
lines changed

CSharpInteractive.HostApi/DotNetCommands.cs

Lines changed: 267 additions & 13 deletions
Large diffs are not rendered by default.

CSharpInteractive.HostApi/DotNetCommands.tt

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<#@ import namespace="System.Linq" #>
2-
<#@ import namespace="System.Linq" #>
3-
<#@ import namespace="System.Linq" #>
42
<#@ import namespace="System.Collections.Generic" #>
3+
<#@ import namespace="System.IO" #>
4+
<#@ import namespace="System.Xml" #>
55
// ReSharper disable UnusedMember.Global
66
// ReSharper disable InconsistentNaming
77
namespace HostApi;
@@ -17,7 +17,7 @@ using Internal;
1717

1818
string CreateCliRef(string command) =>
1919
$"<br/><a href=\"https://learn.microsoft.com/en-us/dotnet/core/tools/{command}\">.NET CLI command</a><br/>";
20-
20+
2121
var projectArg = new Arg("Project", "", "string", "The project or solution file to operate on. If not specified, the command searches the current directory for one. If more than one solution or project is found, an error is thrown.") { IsProject = true };
2222
var solutionArg = new Arg("Solution", "", "string", "The solution file to use. If this argument is omitted, the command searches the current directory for one. If it finds no solution file or multiple solution files, the command fails.") { IsProject = true };
2323
var propsArg = new Arg("Props", "--property", "IEnumerable<(string name, string value)>", "MSBuild options for setting properties.") { IsCollection = true };
@@ -229,15 +229,6 @@ using Internal;
229229
paraStart,
230230
"For executable projects targeting .NET Core 3.0 and later, library dependencies are copied to the output folder. This means that if there isn't any other publish-specific logic (such as Web projects have), the build output should be deployable.",
231231
paraFinish,
232-
exampleStart,
233-
codeStart,
234-
"var configuration = Props.Get(\"configuration\", \"Release\");",
235-
"",
236-
"",
237-
"new DotNetBuild().WithConfiguration(configuration)",
238-
" .Build().EnsureSuccess();",
239-
codeFinish,
240-
exampleFinish,
241232
CreateCliRef("dotnet-build")
242233
],
243234
["build", "$Project"],
@@ -1088,19 +1079,6 @@ using Internal;
10881079
paraStart,
10891080
"To run the application, the dotnet run command resolves the dependencies of the application that are outside of the shared runtime from the NuGet cache. Because it uses cached dependencies, it's not recommended to use dotnet run to run applications in production. Instead, create a deployment using the dotnet publish command and deploy the published output.",
10901081
paraFinish,
1091-
exampleStart,
1092-
codeStart,
1093-
"new DotNetNew()",
1094-
" .WithTemplateName(\"console\")",
1095-
" .WithName(\"MyApp\")",
1096-
" .WithForce(true)",
1097-
" .Run().EnsureSuccess();",
1098-
"",
1099-
"",
1100-
"new DotNetRun().WithWorkingDirectory(\"MyApp\")",
1101-
" .Build().EnsureSuccess();",
1102-
codeFinish,
1103-
exampleFinish,
11041082
CreateCliRef("dotnet-run")
11051083
],
11061084
["run"],
@@ -1636,6 +1614,33 @@ using Internal;
16361614
/// <#= comment #>
16371615
<#
16381616
}
1617+
1618+
var exampleFile = Path.Combine("..", "CSharpInteractive.Tests", "UsageScenarios", "Comments", $"{command.Name}Scenario.txt");
1619+
if (File.Exists(exampleFile))
1620+
{
1621+
var exampleLines = File.ReadAllLines(exampleFile);
1622+
if (exampleLines.Length > 0)
1623+
{
1624+
#>
1625+
/// <#= exampleStart #>
1626+
///<#= codeStart #>
1627+
<#
1628+
var doc = new XmlDocument();
1629+
foreach (var exampleLine in exampleLines.Select(i => i.TrimEnd()))
1630+
{
1631+
var node = doc.CreateElement("root");
1632+
node.InnerText = exampleLine;
1633+
var line = node.InnerXml;
1634+
#>
1635+
/// <#= line #>
1636+
<#
1637+
}
1638+
#>
1639+
///<#= codeFinish #>
1640+
/// <#= exampleFinish #>
1641+
<#
1642+
}
1643+
}
16391644
#>
16401645
/// </summary>
16411646
/// <param name="Args">Specifies the set of command line arguments to use when starting the tool.</param>

CSharpInteractive.Tests/README_TEMPLATE.md

Lines changed: 103 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,19 @@
3434
- [Build a project](#build-a-project)
3535
- [Build a project using MSBuild](#build-a-project-using-msbuild)
3636
- [Clean a project](#clean-a-project)
37+
- [Execute a dotnet application](#execute-a-dotnet-application)
3738
- [Pack a project](#pack-a-project)
3839
- [Publish a project](#publish-a-project)
3940
- [Restore a project](#restore-a-project)
4041
- [Restore local tools](#restore-local-tools)
4142
- [Run a custom .NET command](#run-a-custom-.net-command)
43+
- [Run a dotnet application](#run-a-dotnet-application)
4244
- [Run a project](#run-a-project)
4345
- [Run tests under dotCover](#run-tests-under-dotcover)
4446
- [Test a project](#test-a-project)
4547
- [Test a project using the MSBuild VSTest target](#test-a-project-using-the-msbuild-vstest-target)
4648
- [Test an assembly](#test-an-assembly)
49+
- [Run C# script](#run-c#-script)
4750
- [Shuts down build servers](#shuts-down-build-servers)
4851
- TeamCity API
4952
- [TeamCity integration via service messages](#teamcity-integration-via-service-messages)
@@ -411,8 +414,6 @@ int? exitCode = new CommandLine("cmd", "/c", "TIMEOUT", "/T", "120")
411414
.Run(default, TimeSpan.FromMilliseconds(1))
412415
.EnsureSuccess()
413416
.ExitCode;
414-
415-
exitCode.HasValue.ShouldBeFalse();
416417
```
417418

418419

@@ -447,10 +448,6 @@ var result = dockerRun
447448
.Build()
448449
.EnsureSuccess();
449450

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-
454451
string ToAbsoluteLinuxPath(string path) =>
455452
"/" + path.Replace(":", "").Replace('\\', '/');
456453
```
@@ -493,11 +490,13 @@ new DotNetNew()
493490
.Build().EnsureSuccess();
494491

495492
// Builds the library project, running a command like: "dotnet build" from the directory "MyLib"
493+
var messages = new List<BuildMessage>();
496494
var result = new DotNetBuild()
497495
.WithWorkingDirectory("MyLib")
498-
.Build().EnsureSuccess();
496+
.Build(message => messages.Add(message)).EnsureSuccess();
499497

500498
// The "result" variable provides details about a build
499+
messages.Count.ShouldBeGreaterThan(0, result.ToString());
501500
result.Errors.Any(message => message.State == BuildMessageState.StdError).ShouldBeFalse();
502501
result.ExitCode.ShouldBe(0);
503502

@@ -506,10 +505,6 @@ result = new DotNetTest()
506505
.WithWorkingDirectory("MyLib")
507506
.Build()
508507
.EnsureSuccess();
509-
510-
result.ExitCode.ShouldBe(0);
511-
result.Summary.Tests.ShouldBe(1);
512-
result.Tests.Count(test => test.State == TestState.Finished).ShouldBe(1);
513508
```
514509

515510

@@ -568,6 +563,34 @@ version.ShouldNotBeNull();
568563

569564

570565

566+
### Execute a dotnet application
567+
568+
569+
570+
``` CSharp
571+
// Adds the namespace "HostApi" to use .NET build API
572+
using HostApi;
573+
574+
// Creates a new console project, running a command like: "dotnet new console -n MyApp --force"
575+
new DotNetNew()
576+
.WithTemplateName("console")
577+
.WithName("MyApp")
578+
.WithForce(true)
579+
.Build().EnsureSuccess();
580+
581+
var tempDirectory = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
582+
new DotNetPublish()
583+
.WithWorkingDirectory("MyApp")
584+
.WithOutput(tempDirectory)
585+
.Build().EnsureSuccess();
586+
587+
new DotNetExec()
588+
.WithPathToApplication(Path.Combine(tempDirectory, "MyApp.dll"))
589+
.Run().EnsureSuccess();
590+
```
591+
592+
593+
571594
### Test a project using the MSBuild VSTest target
572595

573596

@@ -621,8 +644,6 @@ result = new DotNetPack()
621644
.WithWorkingDirectory("MyLib")
622645
.AddProps(("version", "1.2.3"))
623646
.Build().EnsureSuccess();
624-
625-
result.ExitCode.ShouldBe(0);
626647
```
627648

628649

@@ -650,8 +671,6 @@ result = new DotNetPublish()
650671
.WithWorkingDirectory("MyLib")
651672
.WithFramework("net8.0")
652673
.Build().EnsureSuccess();
653-
654-
result.ExitCode.ShouldBe(0);
655674
```
656675

657676

@@ -677,8 +696,6 @@ result.ExitCode.ShouldBe(0);
677696
result = new DotNetRestore()
678697
.WithWorkingDirectory("MyLib")
679698
.Build().EnsureSuccess();
680-
681-
result.ExitCode.ShouldBe(0);
682699
```
683700

684701

@@ -702,7 +719,8 @@ result.ExitCode.ShouldBe(0);
702719

703720
// Runs the console project using a command like: "dotnet run" from the directory "MyApp"
704721
var stdOut = new List<string>();
705-
result = new DotNetRun().WithWorkingDirectory("MyApp")
722+
result = new DotNetRun()
723+
.WithWorkingDirectory("MyApp")
706724
.Build(message => stdOut.Add(message.Text))
707725
.EnsureSuccess();
708726

@@ -714,6 +732,34 @@ stdOut.ShouldBe(new[] {"Hello, World!"});
714732

715733

716734

735+
### Run a dotnet application
736+
737+
738+
739+
``` CSharp
740+
// Adds the namespace "HostApi" to use .NET build API
741+
using HostApi;
742+
743+
// Creates a new console project, running a command like: "dotnet new console -n MyApp --force"
744+
new DotNetNew()
745+
.WithTemplateName("console")
746+
.WithName("MyApp")
747+
.WithForce(true)
748+
.Build().EnsureSuccess();
749+
750+
var tempDirectory = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
751+
new DotNetPublish()
752+
.WithWorkingDirectory("MyApp")
753+
.WithOutput(tempDirectory)
754+
.Build().EnsureSuccess();
755+
756+
new DotNet()
757+
.WithPathToApplication(Path.Combine(tempDirectory, "MyApp.dll"))
758+
.Run().EnsureSuccess();
759+
```
760+
761+
762+
717763
### Test a project
718764

719765

@@ -738,9 +784,9 @@ result = new DotNetTest()
738784
.EnsureSuccess();
739785

740786
// The "result" variable provides details about a build
741-
result.ExitCode.ShouldBe(0);
742-
result.Summary.Tests.ShouldBe(1);
743-
result.Tests.Count(test => test.State == TestState.Finished).ShouldBe(1);
787+
result.ExitCode.ShouldBe(0, result.ToString());
788+
result.Summary.Tests.ShouldBe(1, result.ToString());
789+
result.Tests.Count(test => test.State == TestState.Finished).ShouldBe(1, result.ToString());
744790
```
745791

746792

@@ -792,8 +838,8 @@ var result = testUnderDotCover
792838
.Build().EnsureSuccess();
793839

794840
// The "result" variable provides details about a build
795-
result.ExitCode.ShouldBe(0);
796-
result.Tests.Count(i => i.State == TestState.Finished).ShouldBe(1);
841+
result.ExitCode.ShouldBe(0, result.ToString());
842+
result.Tests.Count(i => i.State == TestState.Finished).ShouldBe(1, result.ToString());
797843

798844
// Generates a HTML code coverage report.
799845
new DotNetCustom("dotCover", "report", $"--source={dotCoverSnapshot}", $"--output={dotCoverReport}", "--reportType=HTML")
@@ -847,7 +893,7 @@ var result = new DotNetNew()
847893
.WithForce(true)
848894
.Build().EnsureSuccess();
849895

850-
result.ExitCode.ShouldBe(0);
896+
result.ExitCode.ShouldBe(0, result.ToString());
851897

852898
// Builds the test project, running a command like: "dotnet build -c Release" from the directory "MyTests"
853899
result = new DotNetBuild()
@@ -856,7 +902,7 @@ result = new DotNetBuild()
856902
.WithOutput("MyOutput")
857903
.Build().EnsureSuccess();
858904

859-
result.ExitCode.ShouldBe(0);
905+
result.ExitCode.ShouldBe(0, result.ToString());
860906

861907
// Runs tests via a command like: "dotnet vstest" from the directory "MyTests"
862908
result = new VSTest()
@@ -865,9 +911,9 @@ result = new VSTest()
865911
.Build().EnsureSuccess();
866912

867913
// The "result" variable provides details about a build
868-
result.ExitCode.ShouldBe(0);
869-
result.Summary.Tests.ShouldBe(1);
870-
result.Tests.Count(test => test.State == TestState.Finished).ShouldBe(1);
914+
result.ExitCode.ShouldBe(0, result.ToString());
915+
result.Summary.Tests.ShouldBe(1, result.ToString());
916+
result.Tests.Count(test => test.State == TestState.Finished).ShouldBe(1, result.ToString());
871917
```
872918

873919

@@ -887,7 +933,7 @@ var result = new DotNetNew()
887933
.WithForce(true)
888934
.Build().EnsureSuccess();
889935

890-
result.ExitCode.ShouldBe(0);
936+
result.ExitCode.ShouldBe(0, result.ToString());
891937

892938
// Builds the library project, running a command like: "dotnet msbuild /t:Build -restore /p:configuration=Release -verbosity=detailed" from the directory "MyLib"
893939
result = new MSBuild()
@@ -899,8 +945,8 @@ result = new MSBuild()
899945
.Build().EnsureSuccess();
900946

901947
// The "result" variable provides details about a build
902-
result.Errors.Any(message => message.State == BuildMessageState.StdError).ShouldBeFalse();
903-
result.ExitCode.ShouldBe(0);
948+
result.Errors.Any(message => message.State == BuildMessageState.StdError).ShouldBeFalse(result.ToString());
949+
result.ExitCode.ShouldBe(0, result.ToString());
904950
```
905951

906952

@@ -921,6 +967,32 @@ new DotNetBuildServerShutdown()
921967

922968

923969

970+
### Run C# script
971+
972+
973+
974+
``` CSharp
975+
// Adds the namespace "HostApi" to use .NET build API
976+
using HostApi;
977+
978+
var script = Path.GetTempFileName();
979+
File.WriteAllText(script, "Console.WriteLine($\"Hello, {Args[0]}!\");");
980+
981+
var stdOut = new List<string>();
982+
var result = new DotNetCsi()
983+
.WithScript(script)
984+
.AddArgs("World")
985+
.Build(message => stdOut.Add(message.Text))
986+
.EnsureSuccess();
987+
988+
result.ExitCode.ShouldBe(0);
989+
990+
// Checks StdOut
991+
stdOut.Contains("Hello, World!").ShouldBeTrue(result.ToString());
992+
```
993+
994+
995+
924996
### TeamCity integration via service messages
925997

926998
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

Comments
 (0)