Skip to content
This repository has been archived by the owner on Jul 18, 2024. It is now read-only.

Commit

Permalink
Lots of minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolayPianikov committed Jul 11, 2024
1 parent c380e48 commit 6bbc2e1
Show file tree
Hide file tree
Showing 17 changed files with 176 additions and 80 deletions.
9 changes: 1 addition & 8 deletions .config/dotnet-tools.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,11 @@
"isRoot": true,
"tools": {
"jetbrains.dotcover.globaltool": {
"version": "2022.1.2",
"version": "2023.2.5",
"commands": [
"dotnet-dotcover"
],
"rollForward": false
},
"dotnet-csi": {
"version": "1.0.7",
"commands": [
"dotnet-csi"
],
"rollForward": false
}
}
}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
*.user
.teamcity/target/
.reports/
.packages
51 changes: 38 additions & 13 deletions Build/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@
.EnsureSuccess();

var dotCoverReportXml = Path.Combine(reportDir, "dotCover.xml");
new DotNetCustom("dotCover", "report", $"--source={dotCoverSnapshot}", $"--output={dotCoverReportXml}", "--reportType=TeamCityXml").WithShortName("Generating the code coverage reports").Run().EnsureSuccess();
new DotNetCustom("dotCover", "report", $"--source={dotCoverSnapshot}", $"--output={dotCoverReportXml}", "--reportType=TeamCityXml").WithShortName("Generating the code coverage reports")
.Run().EnsureSuccess();

if (TryGetCoverage(dotCoverReportXml, out coveragePercentage))
{
Expand Down Expand Up @@ -176,20 +177,22 @@
var installTool = new DotNetCustom("tool", "install", toolPackageId, "-g", "--version", packageVersion.ToString(), "--add-source", Path.Combine(outputDir, "CSharpInteractive.Tool"))
.WithShortName("Installing tool");

if (installTool.Run(output => WriteLine(output.Line)).ExitCode != 0)
installTool.Run(output =>
{
Warning($"{installTool} failed.");
}
output.Handled = true;
WriteLine(output.Line);
}).EnsureSuccess(r => default);

new DotNetCustom("csi", "/?").WithShortName("Checking tool").Run().EnsureSuccess();

var uninstallTemplates = new DotNetCustom("new", "uninstall", templatesPackageId)
.WithShortName("Uninstalling template");

if (uninstallTemplates.Run(output => WriteLine(output.Line)).ExitCode != 0)
uninstallTemplates.Run(output =>
{
Warning($"{uninstallTemplates} failed.");
}
output.Handled = true;
WriteLine(output.Line);
}).EnsureSuccess(r => default);;

var installTemplates = new DotNetCustom("new", "install", $"{templatesPackageId}::{packageVersion.ToString()}", "--nuget-source", templateOutputDir)
.WithShortName("Installing template");
Expand All @@ -204,10 +207,28 @@
try
{
var sampleProjectDir = Path.Combine("Samples", "MySampleLib", "MySampleLib.Tests");
new DotNetNew("build", $"--package-version={packageVersion}", "-T", framework, "--no-restore").WithWorkingDirectory(buildProjectDir).WithShortName($"Creating a new {sampleProjectName}").Run().EnsureSuccess();
new DotNetBuild().WithProject(buildProjectDir).WithSources(defaultNuGetSource, Path.Combine(outputDir, "CSharpInteractive")).WithShortName($"Building the {sampleProjectName}").Build().EnsureSuccess();
new DotNetRun().WithProject(buildProjectDir).WithNoBuild(true).WithWorkingDirectory(sampleProjectDir).WithShortName($"Running a build for the {sampleProjectName}").Run().EnsureSuccess();
new DotNetCustom("csi", Path.Combine(buildProjectDir, "Program.csx")).WithWorkingDirectory(sampleProjectDir).WithShortName($"Running a build as a C# script for the {sampleProjectName}").Run().EnsureSuccess();
new DotNetNew("build", $"--package-version={packageVersion}", "-T", framework, "--no-restore")
.WithWorkingDirectory(buildProjectDir)
.WithShortName($"Creating a new {sampleProjectName}")
.Run().EnsureSuccess();

new DotNetBuild()
.WithProject(buildProjectDir)
.WithSources(defaultNuGetSource, Path.Combine(outputDir, "CSharpInteractive"))
.WithShortName($"Building the {sampleProjectName}")
.Build().EnsureSuccess();

new DotNetRun()
.WithProject(buildProjectDir)
.WithNoBuild(true)
.WithWorkingDirectory(sampleProjectDir)
.WithShortName($"Running a build for the {sampleProjectName}")
.Run().EnsureSuccess();

new DotNetCustom("csi", Path.Combine(buildProjectDir, "Program.csx"))
.WithWorkingDirectory(sampleProjectDir)
.WithShortName($"Running a build as a C# script for the {sampleProjectName}")
.Run().EnsureSuccess();
}
finally
{
Expand All @@ -220,7 +241,9 @@
var push = new DotNetNuGetPush().WithApiKey(apiKey).WithSources(defaultNuGetSource);
foreach (var package in packages.Where(i => i.Publish))
{
push.WithPackage(package.Package).WithShortName($"Pushing {Path.GetFileName(package.Package)}").Build().EnsureSuccess();
push.WithPackage(package.Package)
.WithShortName($"Pushing {Path.GetFileName(package.Package)}")
.Build().EnsureSuccess();
}
}
else
Expand All @@ -232,7 +255,9 @@
{
var logicOp = integrationTests && dockerLinuxTests ? "|" : "&";
var filter = $"Integration={integrationTests}{logicOp}Docker={dockerLinuxTests}";
test.WithFilter(filter).Build().EnsureSuccess();
test
.WithFilter(filter)
.Build().EnsureSuccess();
}

WriteLine("To use the csi tool:", Color.Highlighted);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public static CommandLine AddMSBuildLoggers(this CommandLine cmd, IHost host, Do
return settings.LoggersAreRequired
? cmd
.AddArgs("/noconsolelogger")
.AddMSBuildArgs(("/l", $"TeamCity.MSBuild.Logger.TeamCityMSBuildLogger,{virtualContext.Resolve(settings.DotNetMSBuildLoggerDirectory)}/TeamCity.MSBuild.Logger.dll;TeamCity;plain"))
.AddMSBuildArgs(("/l", $"TeamCity.MSBuild.Logger.TeamCityMSBuildLogger,{virtualContext.Resolve(settings.DotNetMSBuildLoggerDirectory)}/TeamCity.MSBuild.Logger.dll;TEAMCITY;PLAIN;NOSUMMARY"))
.AddProps("-p",
("VSTestLogger", TeamcityLoggerName),
("VSTestTestAdapterPath", virtualContext.Resolve(settings.DotNetVSTestLoggerDirectory)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<Title>C# script Templates</Title>
<Description>$(Company) $(Title) $(Version) $(TargetFramework)</Description>
<PackageType>Template</PackageType>
<PackageReadmeFile>content/ConsoleApplication-CSharp/README.md</PackageReadmeFile>
<SuppressDependenciesWhenPacking>True</SuppressDependenciesWhenPacking>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using HostApi;

// Builds a dotnet solution or project
return new DotNetBuild().Build().ExitCode ?? 1;
// Build a dotnet solution or project
new DotNetBuild().Build().EnsureSuccess();
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@
// To add an assembly reference:
// #r "MyAssembly.dll"

// To include code from the file in the order in which it should be executed:
// To include code from the file
// in the order in which it should be executed:
// #load "MyClass.cs"

// More information can be found on the page:
// https://github.com/DevTeam/csharp-interactive

#load "Program.cs"
#load "Program.cs"
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Install the C# script template [CSharpInteractive.Templates](https://www.nuget.org/packages/CSharpInteractive.Templates)

```shell
dotnet new -i CSharpInteractive.Templates
```

Create a console project "Build" containing a script from the template *__build__*

```shell
dotnet new build -o ./Build
```

This projects contains the script *__./Build/Program.csx__*. To run this script from the command line from the directory *__Build__*:

```shell
dotnet csi Build
```

To run as a .NET console application:

```shell
dotnet run --project Build
```
70 changes: 29 additions & 41 deletions CSharpInteractive.Tests/README_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,8 @@ public void Run()

private class MyTask(ICommandLineRunner runner)
{

public int? Run() =>
runner.Run(new CommandLine("whoami"));
runner.Run(new CommandLine("whoami")).ExitCode;
}

```
Expand Down Expand Up @@ -256,22 +255,18 @@ cmd = new CommandLine("cmd", "/c", "echo", "Hello")
// Adds the namespace "HostApi" to use Command Line API
using HostApi;

var exitCode = GetService<ICommandLineRunner>().Run(new CommandLine("cmd", "/c", "DIR"));
exitCode.ShouldBe(0);
GetService<ICommandLineRunner>().Run(new CommandLine("cmd", "/c", "DIR")).EnsureSuccess();

// or the same thing using the extension method
exitCode = new CommandLine("cmd", "/c", "DIR").Run();
exitCode.ShouldBe(0);
new CommandLine("cmd", "/c", "DIR").Run().EnsureSuccess();

// using operator '+'
var cmd = new CommandLine("cmd") + "/c" + "DIR";
exitCode = cmd.Run();
exitCode.ShouldBe(0);
cmd.Run().EnsureSuccess();

// with environment variables
cmd = new CommandLine("cmd") + "/c" + "DIR" + ("MyEnvVar", "Some Value");
exitCode = cmd.Run();
exitCode.ShouldBe(0);
cmd.Run().EnsureSuccess();
```


Expand All @@ -284,10 +279,10 @@ exitCode.ShouldBe(0);
// Adds the namespace "HostApi" to use Command Line API
using HostApi;

int? exitCode = await GetService<ICommandLineRunner>().RunAsync(new CommandLine("cmd", "/C", "DIR"));
var task = await GetService<ICommandLineRunner>().RunAsync(new CommandLine("cmd", "/C", "DIR"));

// or the same thing using the extension method
exitCode = await new CommandLine("cmd", "/c", "DIR").RunAsync();
task = await new CommandLine("cmd", "/c", "DIR").RunAsync();
```


Expand All @@ -303,7 +298,7 @@ using HostApi;
var lines = new List<string>();
int? exitCode = new CommandLine("cmd", "/c", "SET")
.AddVars(("MyEnv", "MyVal"))
.Run(output => lines.Add(output.Line));
.Run(output => lines.Add(output.Line)).ExitCode;

lines.ShouldContain("MyEnv=MyVal");
```
Expand All @@ -318,8 +313,8 @@ lines.ShouldContain("MyEnv=MyVal");
// Adds the namespace "HostApi" to use Command Line API
using HostApi;

Task<int?> task = new CommandLine("cmd", "/c", "DIR").RunAsync();
int? exitCode = new CommandLine("cmd", "/c", "SET").Run();
var task = new CommandLine("cmd", "/c", "DIR").RunAsync();
var result = new CommandLine("cmd", "/c", "SET").Run();
task.Wait();
```

Expand All @@ -334,7 +329,7 @@ The cancellation will kill a related process.
using HostApi;

var cancellationTokenSource = new CancellationTokenSource();
Task<int?> task = new CommandLine("cmd", "/c", "TIMEOUT", "/T", "120").RunAsync(default, cancellationTokenSource.Token);
var task = new CommandLine("cmd", "/c", "TIMEOUT", "/T", "120").RunAsync(default, cancellationTokenSource.Token);

cancellationTokenSource.CancelAfter(TimeSpan.FromMilliseconds(100));
task.IsCompleted.ShouldBeFalse();
Expand All @@ -350,7 +345,7 @@ If timeout expired a process will be killed.
// Adds the namespace "HostApi" to use Command Line API
using HostApi;

int? exitCode = new CommandLine("cmd", "/c", "TIMEOUT", "/T", "120").Run(default, TimeSpan.FromMilliseconds(1));
int? exitCode = new CommandLine("cmd", "/c", "TIMEOUT", "/T", "120").Run(default, TimeSpan.FromMilliseconds(1)).ExitCode;

exitCode.HasValue.ShouldBeFalse();
```
Expand Down Expand Up @@ -420,9 +415,10 @@ using HostApi;

// Gets the dotnet version, running a command like: "dotnet --version"
NuGetVersion? version = default;
var exitCode = new DotNetCustom("--version").Run(message => NuGetVersion.TryParse(message.Line, out version));
var exitCode = new DotNetCustom("--version")
.Run(message => NuGetVersion.TryParse(message.Line, out version))
.EnsureSuccess();

exitCode.ShouldBe(0);
version.ShouldNotBeNull();
```

Expand Down Expand Up @@ -569,17 +565,16 @@ result.Tests.Count(test => test.State == TestState.Finished).ShouldBe(1);
using HostApi;

// Creates a new test project, running a command like: "dotnet new mstest -n MyTests --force"
var exitCode = new DotNetNew("mstest", "-n", "MyTests", "--force").Run();
exitCode.ShouldBe(0);
new DotNetNew("mstest", "-n", "MyTests", "--force")
.Run().EnsureSuccess();

// Creates the tool manifest and installs the dotCover tool locally
// It is better to run the following 2 commands manually
// and commit these changes to a source control
exitCode = new DotNetNew("tool-manifest").Run();
exitCode.ShouldBe(0);
new DotNetNew("tool-manifest").Run().EnsureSuccess();

exitCode = new DotNetCustom("tool", "install", "--local", "JetBrains.dotCover.GlobalTool").Run();
exitCode.ShouldBe(0);
new DotNetCustom("tool", "install", "--local", "JetBrains.dotCover.GlobalTool")
.Run().EnsureSuccess();

// Creates a test command
var test = new DotNetTest().WithProject("MyTests");
Expand All @@ -605,8 +600,8 @@ result.ExitCode.ShouldBe(0);
result.Tests.Count(i => i.State == TestState.Finished).ShouldBe(1);

// Generates a HTML code coverage report.
exitCode = new DotNetCustom("dotCover", "report", $"--source={dotCoverSnapshot}", $"--output={dotCoverReport}", "--reportType=HTML").Run();
exitCode.ShouldBe(0);
new DotNetCustom("dotCover", "report", $"--source={dotCoverSnapshot}", $"--output={dotCoverReport}", "--reportType=HTML")
.Run().EnsureSuccess();

// Check for a dotCover report
File.Exists(dotCoverReport).ShouldBeTrue();
Expand All @@ -626,12 +621,10 @@ var projectDir = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()[..4]
Directory.CreateDirectory(projectDir);

// Creates a local tool manifest
var exitCode = new DotNetNew("tool-manifest").WithWorkingDirectory(projectDir).Run();
exitCode.ShouldBe(0);
new DotNetNew("tool-manifest").WithWorkingDirectory(projectDir).Run().EnsureSuccess();

// Restore local tools
exitCode = new DotNetToolRestore().WithWorkingDirectory(projectDir).Run();
exitCode.ShouldBe(0);
new DotNetToolRestore().WithWorkingDirectory(projectDir).Run().EnsureSuccess();
```


Expand Down Expand Up @@ -703,9 +696,7 @@ result.ExitCode.ShouldBe(0);
using HostApi;

// Shuts down all build servers that are started from dotnet.
var exitCode = new DotNetBuildServerShutdown().Run();

exitCode.ShouldBe(0);
new DotNetBuildServerShutdown().Run().EnsureSuccess();
```


Expand Down Expand Up @@ -764,11 +755,9 @@ var dockerRun = new DockerRun()


// Creates a new library project in a docker container
var exitCode = dockerRun
dockerRun
.WithCommandLine(new DotNetCustom("new", "classlib", "-n", "MyLib", "--force"))
.Run();

exitCode.ShouldBe(0);
.Run().EnsureSuccess();

// Builds the library project in a docker container
var result = dockerRun
Expand Down Expand Up @@ -799,9 +788,8 @@ var cmd = new CommandLine("whoami");
// Runs the command line in a docker container
var result = new DockerRun(cmd, "mcr.microsoft.com/dotnet/sdk")
.WithAutoRemove(true)
.Run();

result.ShouldBe(0);
.Run()
.EnsureSuccess();
```


Expand Down
1 change: 1 addition & 0 deletions CSharpInteractive.sln.DotSettings
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@
<s:Boolean x:Key="/Default/UserDictionary/Words/=Nikolay/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Ninject/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=nologo/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=nosummary/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=nupkg/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Parallelize/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Pianikov/@EntryIndexedValue">True</s:Boolean>
Expand Down
1 change: 0 additions & 1 deletion CSharpInteractive/Core/Statistics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ public IReadOnlyCollection<Text[]> Warnings
}
}
}


public TimeSpan TimeElapsed => _stopwatch.Elapsed;

Expand Down
2 changes: 0 additions & 2 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
<Copyright>Copyright (C) $([System.DateTime]::Now.Year) $(Company)</Copyright>
<RepositoryType>git</RepositoryType>
<RepositoryUrl>https://github.com/DevTeam/csharp-interactive</RepositoryUrl>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>obj/Generated</CompilerGeneratedFilesOutputPath>
<ImmutypeAPI>False</ImmutypeAPI>
</PropertyGroup>

Expand Down
Loading

0 comments on commit 6bbc2e1

Please sign in to comment.