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

Commit

Permalink
Improved build
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolayPianikov committed Jun 27, 2024
1 parent 51edc29 commit 23a3eb0
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 86 deletions.
10 changes: 9 additions & 1 deletion .config/dotnet-tools.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,15 @@
"version": "2022.1.2",
"commands": [
"dotnet-dotcover"
]
],
"rollForward": false
},
"dotnet-csi": {
"version": "1.0.7",
"commands": [
"dotnet-csi"
],
"rollForward": false
}
}
}
2 changes: 1 addition & 1 deletion .run/Build.run.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Build" type="DotNetProject" factoryName=".NET Project">
<option name="EXE_PATH" value="$PROJECT_DIR$/Build/bin/Debug/net8.0/Build.exe" />
<option name="PROGRAM_PARAMETERS" value="-p version=1.0.0-beta" />
<option name="PROGRAM_PARAMETERS" value="-p version=1.0.8" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$" />
<option name="PASS_PARENT_ENVS" value="1" />
<option name="USE_EXTERNAL_CONSOLE" value="0" />
Expand Down
2 changes: 1 addition & 1 deletion Build/Build.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CSharpInteractive" Version="1.0.7"/>
<PackageReference Include="CSharpInteractive" Version="1.0.8" />
</ItemGroup>

</Project>
45 changes: 19 additions & 26 deletions Build/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
var dockerLinuxTests = HasLinuxDocker();
if (!dockerLinuxTests)
{
Warning("The docker Linux container is not available. Integration tests are skipped.");
Warning("The docker Linux container is unavailable. Integration tests are skipped.");
}

var packageVersion = new[]
Expand All @@ -40,31 +40,27 @@
{
new PackageInfo(
packageId,
Path.Combine("CSharpInteractive", "CSharpInteractive.csproj"),
Path.Combine(outputDir, "CSharpInteractive", $"{packageId}.{packageVersion.ToString()}.nupkg"),
true),

new PackageInfo(
toolPackageId,
Path.Combine("CSharpInteractive", "CSharpInteractive.Tool.csproj"),
Path.Combine(outputDir, "CSharpInteractive.Tool", $"{toolPackageId}.{packageVersion.ToString()}.nupkg"),
true),

new PackageInfo(
templatesPackageId,
Path.Combine("CSharpInteractive.Templates", "CSharpInteractive.Templates.csproj"),
Path.Combine(templateOutputDir, $"{templatesPackageId}.{packageVersion.ToString()}.nupkg"),
false)
};

Succeed(new DotNetToolRestore().Run(), "Restore tools");
Run(new DotNetToolRestore().WithShortName("Restoring tools"));

Succeed(
Build(
new DotNetClean()
.WithProject(solutionFile)
.WithVerbosity(DotNetVerbosity.Quiet)
.WithConfiguration(configuration)
.Build()
);

foreach (var package in packages)
Expand All @@ -83,18 +79,16 @@
}

var buildProps = new[] {("version", packageVersion.ToString())};
Succeed(new MSBuild()
Build(new MSBuild()
.WithProject(Path.Combine(currentDir, "CSharpInteractive", "CSharpInteractive.Tool.csproj"))
.WithRestore(true)
.WithTarget("Rebuild;GetDependencyTargetPaths")
.WithProps(buildProps)
.Build());
.WithProps(buildProps));

Succeed(new DotNetPack()
Build(new DotNetPack()
.WithProject(solutionFile)
.WithConfiguration(configuration)
.WithProps(buildProps)
.Build());
.WithProps(buildProps));

foreach (var package in packages)
{
Expand Down Expand Up @@ -123,19 +117,18 @@
{
var reportDir = Path.Combine(currentDir, ".reports");
var dotCoverSnapshot = Path.Combine(reportDir, "dotCover.dcvr");
Succeed(
Run(
test
.Customize(cmd =>
cmd.WithArgs("dotcover")
.AddArgs(cmd.Args)
.AddArgs(
$"--dcOutput={dotCoverSnapshot}",
"--dcFilters=+:module=CSharpInteractive.HostApi;+:module=dotnet-csi",
"--dcAttributeFilters=System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage"))
.Build());
"--dcAttributeFilters=System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage")));

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

if (TryGetCoverage(dotCoverReportXml, out coveragePercentage))
{
Expand Down Expand Up @@ -183,7 +176,7 @@
Warning($"{installTool} failed.");
}

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

var uninstallTemplates = new DotNetCustom("new", "uninstall", templatesPackageId)
.WithShortName("Uninstalling template");
Expand All @@ -196,7 +189,7 @@
var installTemplates = new DotNetCustom("new", "install", $"{templatesPackageId}::{packageVersion.ToString()}", "--nuget-source", templateOutputDir)
.WithShortName("Installing template");

Succeed(installTemplates.Run(), installTemplates.ShortName);
Run(installTemplates.WithShortName(installTemplates.ShortName));

foreach (var framework in frameworks)
{
Expand All @@ -206,10 +199,10 @@
try
{
var sampleProjectDir = Path.Combine("Samples", "MySampleLib", "MySampleLib.Tests");
Succeed(new DotNetNew("build", $"--package-version={packageVersion}", "-T", framework, "--no-restore").WithWorkingDirectory(buildProjectDir).Run(), $"Creating a new {sampleProjectName}");
Succeed(new DotNetBuild().WithProject(buildProjectDir).WithSources(defaultNuGetSource, Path.Combine(outputDir, "CSharpInteractive")).WithShortName($"Building the {sampleProjectName}").Build());
Succeed(new DotNetRun().WithProject(buildProjectDir).WithNoBuild(true).WithWorkingDirectory(sampleProjectDir).Run(), $"Running a build for the {sampleProjectName}");
Succeed(new DotNetCustom("csi", Path.Combine(buildProjectDir, "Program.csx")).WithWorkingDirectory(sampleProjectDir).Run(), $"Running a build as a C# script for the {sampleProjectName}");
Run(new DotNetNew("build", $"--package-version={packageVersion}", "-T", framework, "--no-restore").WithWorkingDirectory(buildProjectDir).WithShortName($"Creating a new {sampleProjectName}"));
Run(new DotNetBuild().WithProject(buildProjectDir).WithSources(defaultNuGetSource, Path.Combine(outputDir, "CSharpInteractive")).WithShortName($"Building the {sampleProjectName}"));
Run(new DotNetRun().WithProject(buildProjectDir).WithNoBuild(true).WithWorkingDirectory(sampleProjectDir).WithShortName($"Running a build for the {sampleProjectName}"));
Run(new DotNetCustom("csi", Path.Combine(buildProjectDir, "Program.csx")).WithWorkingDirectory(sampleProjectDir).WithShortName($"Running a build as a C# script for the {sampleProjectName}"));
}
finally
{
Expand All @@ -222,7 +215,7 @@
var push = new DotNetNuGetPush().WithApiKey(apiKey).WithSources(defaultNuGetSource);
foreach (var package in packages.Where(i => i.Publish))
{
Succeed(push.WithPackage(package.Package).Run(), $"Pushing {Path.GetFileName(package.Package)}");
Run(push.WithPackage(package.Package).WithShortName($"Pushing {Path.GetFileName(package.Package)}"));
}
}
else
Expand All @@ -234,7 +227,7 @@
{
var logicOp = integrationTests && dockerLinuxTests ? "|" : "&";
var filter = $"Integration={integrationTests}{logicOp}Docker={dockerLinuxTests}";
Succeed(test.WithFilter(filter).Build());
Build(test.WithFilter(filter));
}

WriteLine("To use the csi tool:", Color.Highlighted);
Expand All @@ -250,4 +243,4 @@

return 0;

internal record PackageInfo(string Id, string Project, string Package, bool Publish);
internal record PackageInfo(string Id, string Package, bool Publish);
59 changes: 14 additions & 45 deletions Build/Tools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,26 +59,12 @@ public static string GetProperty(string name, string defaultProp, bool showWarni
return defaultProp;
}

public static bool Succeed(int? exitCode, string shortName)
{
if (exitCode == 0)
{
return true;
}

Error($"{shortName} failed.");
Exit();
return false;
}

public static async Task<bool> Succeed(Task<int?> exitCodeTask, string shortName) =>
Succeed(await exitCodeTask, shortName);

private static bool CheckBuildResult(IBuildResult result)
public static void Build(ICommandLine commandLine)
{
var result = commandLine.Build();
if (result.ExitCode == 0)
{
return true;
return;
}

foreach (var failedTest in
Expand All @@ -90,39 +76,22 @@ select testResult.ToString())
}

Error($"{result.StartInfo.ShortName} failed");
return false;
}

public static void Succeed(IBuildResult result)
{
if (!CheckBuildResult(result))
{
Exit();
}
}

public static async Task<bool> Succeed(Task<IBuildResult> resultTask)
{
if (CheckBuildResult(await resultTask))
{
return true;
}

Exit();
return true;
throw new OperationCanceledException();
}

public static async Task<bool> Succeed(Task<IBuildResult[]> resultsTask)
public static void Run(ICommandLine commandLine)
{
if ((await resultsTask).All(CheckBuildResult))
var exitCode = commandLine.Run();
if (exitCode == 0)
{
return true;
return;
}

Exit();
return true;
var startInfo = commandLine.GetStartInfo(GetService<IHost>());
Error($"{startInfo.ShortName} failed");
throw new OperationCanceledException();
}

public static void Exit()
{
if (!Console.IsInputRedirected && !UnderTeamCity)
Expand Down Expand Up @@ -159,7 +128,7 @@ public static void Exit()
}
}

Environment.Exit(1);
throw new OperationCanceledException();
}

public static bool TryGetCoverage(string dotCoverReportXml, out int coveragePercentage)
Expand Down
17 changes: 11 additions & 6 deletions CSharpInteractive.Tests/README_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ result.ExitCode.ShouldBe(0);
result = new DotNetTest().WithWorkingDirectory("MyLib").Build();
result.ExitCode.ShouldBe(0);
result.Summary.Tests.ShouldBe(1);
result.Tests.Count(test => test.State == TestState.Passed).ShouldBe(1);
result.Tests.Count(test => test.State == TestState.Finished).ShouldBe(1);
```


Expand Down Expand Up @@ -448,7 +448,7 @@ result = new MSBuild()
// The "result" variable provides details about a build
result.ExitCode.ShouldBe(0);
result.Summary.Tests.ShouldBe(1);
result.Tests.Count(test => test.State == TestState.Passed).ShouldBe(1);
result.Tests.Count(test => test.State == TestState.Finished).ShouldBe(1);
```


Expand Down Expand Up @@ -555,7 +555,7 @@ result = new DotNetTest().WithWorkingDirectory("MyTests").Build();
// The "result" variable provides details about a build
result.ExitCode.ShouldBe(0);
result.Summary.Tests.ShouldBe(1);
result.Tests.Count(test => test.State == TestState.Passed).ShouldBe(1);
result.Tests.Count(test => test.State == TestState.Finished).ShouldBe(1);
```


Expand Down Expand Up @@ -602,7 +602,7 @@ var result = testUnderDotCover.Build();

// The "result" variable provides details about a build
result.ExitCode.ShouldBe(0);
result.Tests.Count(i => i.State == TestState.Passed).ShouldBe(1);
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();
Expand Down Expand Up @@ -661,7 +661,7 @@ result = new VSTest()
// The "result" variable provides details about a build
result.ExitCode.ShouldBe(0);
result.Summary.Tests.ShouldBe(1);
result.Tests.Count(test => test.State == TestState.Passed).ShouldBe(1);
result.Tests.Count(test => test.State == TestState.Finished).ShouldBe(1);
```


Expand Down Expand Up @@ -756,10 +756,12 @@ using HostApi;
// Creates a base docker command line
var dockerRun = new DockerRun()
.WithAutoRemove(true)
.WithInteractive(true)
.WithImage("mcr.microsoft.com/dotnet/sdk")
.WithPlatform("linux")
.WithContainerWorkingDirectory("/MyProjects")
.AddVolumes((Environment.CurrentDirectory, "/MyProjects"));
.AddVolumes((ToAbsoluteLinuxPath(Environment.CurrentDirectory), "/MyProjects"));


// Creates a new library project in a docker container
var exitCode = dockerRun
Expand All @@ -776,6 +778,9 @@ var result = dockerRun
// The "result" variable provides details about a build
result.Errors.Any(message => message.State == BuildMessageState.StdError).ShouldBeFalse();
result.ExitCode.ShouldBe(0);

string ToAbsoluteLinuxPath(string path) =>
"/" + path.Replace(":", "").Replace('\\', '/');
```


Expand Down
8 changes: 2 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,15 +223,11 @@ public void Run()
exitCode.ShouldBe(0);
}

private class MyTask
private class MyTask(ICommandLineRunner runner)
{
private readonly ICommandLineRunner _runner;

public MyTask(ICommandLineRunner runner) =>
_runner = runner;

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

```
Expand Down

0 comments on commit 23a3eb0

Please sign in to comment.