Skip to content

Commit

Permalink
Improved templates
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolayPianikov committed Dec 21, 2024
1 parent 13bbe3b commit bb869bd
Show file tree
Hide file tree
Showing 17 changed files with 99 additions and 47 deletions.
4 changes: 2 additions & 2 deletions .run/Deploy Template.run.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Deploy Template" type="DotNetProject" factoryName=".NET Project" folderName="Targets">
<option name="EXE_PATH" value="$PROJECT_DIR$/build/bin/Debug/net8.0/build.exe" />
<option name="EXE_PATH" value="$PROJECT_DIR$/build/bin/Debug/net9.0/build.exe" />
<option name="PROGRAM_PARAMETERS" value="template" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$" />
<option name="PASS_PARENT_ENVS" value="1" />
Expand All @@ -12,7 +12,7 @@
<option name="PROJECT_ARGUMENTS_TRACKING" value="1" />
<option name="PROJECT_WORKING_DIRECTORY_TRACKING" value="0" />
<option name="PROJECT_KIND" value="DotNetCore" />
<option name="PROJECT_TFM" value="net8.0" />
<option name="PROJECT_TFM" value="net9.0" />
<method v="2">
<option name="Build" />
</method>
Expand Down
20 changes: 20 additions & 0 deletions .run/Install Template.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Install Template" type="DotNetProject" factoryName=".NET Project" folderName="Targets">
<option name="EXE_PATH" value="$PROJECT_DIR$/build/bin/Debug/net9.0/build.exe" />
<option name="PROGRAM_PARAMETERS" value="install" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$" />
<option name="PASS_PARENT_ENVS" value="1" />
<option name="USE_EXTERNAL_CONSOLE" value="0" />
<option name="USE_MONO" value="0" />
<option name="RUNTIME_ARGUMENTS" value="" />
<option name="PROJECT_PATH" value="$PROJECT_DIR$/build/build.csproj" />
<option name="PROJECT_EXE_PATH_TRACKING" value="1" />
<option name="PROJECT_ARGUMENTS_TRACKING" value="1" />
<option name="PROJECT_WORKING_DIRECTORY_TRACKING" value="0" />
<option name="PROJECT_KIND" value="DotNetCore" />
<option name="PROJECT_TFM" value="net9.0" />
<method v="2">
<option name="Build" />
</method>
</configuration>
</component>
4 changes: 2 additions & 2 deletions .run/Update readme.run.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Update readme" type="DotNetProject" factoryName=".NET Project">
<option name="EXE_PATH" value="$PROJECT_DIR$/build/bin/Debug/net8.0/build.exe" />
<option name="EXE_PATH" value="$PROJECT_DIR$/build/bin/Debug/net9.0/build.exe" />
<option name="PROGRAM_PARAMETERS" value="readme" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$" />
<option name="PASS_PARENT_ENVS" value="1" />
Expand All @@ -12,7 +12,7 @@
<option name="PROJECT_ARGUMENTS_TRACKING" value="1" />
<option name="PROJECT_WORKING_DIRECTORY_TRACKING" value="0" />
<option name="PROJECT_KIND" value="DotNetCore" />
<option name="PROJECT_TFM" value="net8.0" />
<option name="PROJECT_TFM" value="net9.0" />
<method v="2">
<option name="Build" />
</method>
Expand Down
2 changes: 1 addition & 1 deletion build/GeneratorTarget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public async Task<Package> RunAsync(CancellationToken cancellationToken)
var mergedPackagePath = Path.GetFullPath(Path.Combine(generatorProjectDirectory, PackagesDir, PackageName));
var generatorPackages = CreateGeneratorPackagesAsync(generatorProjectDirectory, cancellationToken);
var mergedPackage = await packages.MergeAsync(generatorPackages, mergedPackagePath, cancellationToken);
return new Package(mergedPackage, true);
return new Package(mergedPackage, true, settings.NextVersion);
}

private async IAsyncEnumerable<string> CreateGeneratorPackagesAsync(string generatorProjectDirectory, [EnumeratorCancellation] CancellationToken cancellationToken)
Expand Down
28 changes: 28 additions & 0 deletions build/InstallTemplateTarget.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// ReSharper disable StringLiteralTypo
// ReSharper disable ClassNeverInstantiated.Global

namespace Build;

internal class InstallTemplateTarget(
Commands commands,
[Tag(typeof(TemplateTarget))] ITarget<Package> templateTarget)
: IInitializable, ITarget<Package>
{
public Task InitializeAsync(CancellationToken cancellationToken) => commands.RegisterAsync(
this, "Install template", "install", "i");

public async Task<Package> RunAsync(CancellationToken cancellationToken)
{
var package = await templateTarget.RunAsync(cancellationToken);
await new DotNetNewUninstall()
.WithPackage(TemplateTarget.ProjectName)
.RunAsync(cancellationToken: cancellationToken).EnsureSuccess();

await new DotNetNewInstall()
.WithPackage($"{TemplateTarget.ProjectName}::{package.Version}")
.AddSources(Path.GetFullPath(Path.GetDirectoryName(package.Path) ?? "."))
.RunAsync(cancellationToken: cancellationToken).EnsureSuccess();

return package;
}
}
5 changes: 3 additions & 2 deletions build/LibrariesTarget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,20 @@ public Task InitializeAsync(CancellationToken cancellationToken) => commands.Reg
[SuppressMessage("Performance", "CA1861:Avoid constant arrays as arguments")]
public async Task<IReadOnlyCollection<Library>> RunAsync(CancellationToken cancellationToken)
{
var version = settings.NextVersion;
// Libraries
List<Library> libraries =
[
new("Pure.DI.MS",
new Package(GetPackagePath("Pure.DI.MS", settings.NextVersion), false),
new Package(GetPackagePath("Pure.DI.MS", settings.NextVersion), false, version),
sdk.Versions
.Where(i => i.Version.Major >= 7)
.Select(v => $"net{v.Version.Major}.{v.Version.Minor}")
.ToArray(),
["webapi"]),

new("Pure.DI.Abstractions",
new Package(GetPackagePath("Pure.DI.Abstractions", settings.NextVersion), false),
new Package(GetPackagePath("Pure.DI.Abstractions", settings.NextVersion), false, version),
sdk.Versions
.Where(i => i.Version.Major >= 7)
.Select(v => $"net{v.Version.Major}.{v.Version.Minor}")
Expand Down
5 changes: 4 additions & 1 deletion build/Package.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
namespace Build;

using NuGet.Versioning;

internal record Package(
string Path,
bool Deploy);
bool Deploy,
NuGetVersion Version);
4 changes: 3 additions & 1 deletion build/Program.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using Build;
using static Pure.DI.Lifetime;

DI.Setup(nameof(Composition))
.Root<RootTarget>("RootTarget")
.DefaultLifetime(Lifetime.PerBlock)
.DefaultLifetime(PerBlock)
.Bind().To<RootCommand>()
.Bind().To<Settings>()
.Bind<ITeamCityArtifactsWriter>().To(_ => GetService<ITeamCityWriter>())
Expand All @@ -18,6 +19,7 @@
.Bind(Tag.Type).To<BenchmarksTarget>()
.Bind(Tag.Type).To<DeployTarget>()
.Bind(Tag.Type).To<TemplateTarget>()
.Bind(Tag.Type).To<InstallTemplateTarget>()
.Bind(Tag.Type).To<UpdateTarget>()
.Bind(Tag.Type).To<PublishBlazorTarget>()
.Bind(Tag.Type).To<PerformanceTestsTarget>();
Expand Down
33 changes: 19 additions & 14 deletions build/TemplateTarget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,36 @@ internal class TemplateTarget(
Commands commands,
Versions versions,
ITeamCityArtifactsWriter artifactsWriter)
: IInitializable, ITarget<string>
: IInitializable, ITarget<Package>
{
private const string ProjectName = "Pure.DI.Templates";
public const string ProjectName = "Pure.DI.Templates";

public Task InitializeAsync(CancellationToken cancellationToken) => commands.RegisterAsync(
this, "Creates and deploys templates", "template", "t");
this, "Creates and deploys template", "template", "t");

public async Task<string> RunAsync(CancellationToken cancellationToken)
public async Task<Package> RunAsync(CancellationToken cancellationToken)
{
var packageVersion = versions.GetNext(new NuGetRestoreSettings("Pure.DI"), Settings.VersionRange, 0).ToString();
foreach (var jsonFile in new[]
{
"src/Pure.DI.Templates/Templates/Pure.DI.Template.ClassLibrary/.template.config/template.json",
"src/Pure.DI.Templates/Templates/Pure.DI.Template.ConsoleApp/.template.config/template.json"
})
var packageVersion = versions.GetNext(new NuGetRestoreSettings("Pure.DI"), Settings.VersionRange, 0);
var packageVersionStr = packageVersion.ToString();
var templatesPath = Path.Combine("src", ProjectName, "Templates");
var jsonConfigPath = Path.Combine(".template.config", "template.json");
var jsonConfigs = new[]
{
Path.Combine(templatesPath, "Pure.DI.Template.ClassLibrary", jsonConfigPath),
Path.Combine(templatesPath, "Pure.DI.Template.ConsoleApp", jsonConfigPath)
};

foreach (var jsonFile in jsonConfigs)
{
var content = await File.ReadAllTextAsync(jsonFile, cancellationToken);
content = content.Replace("$(version)", packageVersion);
content = content.Replace("$(version)", packageVersionStr);
await File.WriteAllTextAsync(jsonFile, content, cancellationToken);
}

var props = new[]
{
("configuration", settings.Configuration),
("version", packageVersion)
("version", packageVersionStr)
};

var projectDirectory = Path.Combine("src", ProjectName);
Expand All @@ -47,7 +52,7 @@ public async Task<string> RunAsync(CancellationToken cancellationToken)
if (string.IsNullOrWhiteSpace(settings.NuGetKey))
{
Warning($"The NuGet key was not specified, the package {targetPackage} will not be pushed.");
return targetPackage;
return new Package(targetPackage, false, packageVersion);
}

await new DotNetNuGetPush()
Expand All @@ -56,6 +61,6 @@ public async Task<string> RunAsync(CancellationToken cancellationToken)
.WithApiKey(settings.NuGetKey)
.BuildAsync(cancellationToken: cancellationToken).EnsureSuccess();

return targetPackage;
return new Package(targetPackage, false, packageVersion);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
"type": "parameter",
"datatype": "string",
"description": "Version of Pure.DI that will be referenced.",
"defaultValue": "$(version)",
"defaultValue": "2.1.44",
"replaces": "$(PureDIVersion)"
}
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
global using System;
global using _PureDIProjectName_;
global using Pure.DI;
global using static Pure.DI.Lifetime;
global using static Pure.DI.CompositionKind;
global using static Pure.DI.Lifetime;
global using static Pure.DI.Tag;
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

internal class $(CompositionName)
{
private void Setup() => DI.Setup(kind: CompositionKind.Global)
private void Setup() => DI.Setup(kind: Global)
.Bind().As(Singleton).To<ConsoleAdapter>();
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
"type": "parameter",
"datatype": "string",
"description": "Version of Pure.DI that will be referenced.",
"defaultValue": "$(version)",
"defaultValue": "2.1.44",
"replaces": "$(PureDIVersion)"
}
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
global using _PureDIProjectName_;
global using System;
global using System;
global using _PureDIProjectName_;
global using Pure.DI;
global using static Pure.DI.Lifetime;
global using static Pure.DI.CompositionKind;
global using static Pure.DI.Lifetime;
global using static Pure.DI.Tag;
Original file line number Diff line number Diff line change
@@ -1,23 +1,14 @@
var composition = new $(CompositionName)();
return composition.Root.Run(args);

internal partial class Program
internal partial class Program(IInput input, IOutput output)
{
private readonly IInput _input;
private readonly IOutput _output;

internal Program(IInput input, IOutput output)
{
_input = input ?? throw new ArgumentNullException(nameof(input));
_output = output ?? throw new ArgumentNullException(nameof(output));
}

private int Run(string[] args)
{
_output.WriteLine("Hello!");
output.WriteLine("Hello!");

_output.WriteLine("Press the Enter key to exit.");
_input.ReadLine();
output.WriteLine("Press the Enter key to exit.");
input.ReadLine();

return 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ internal partial class $(CompositionName)
{
private void Setup() => DI.Setup()
.Bind().As(Singleton).To<ConsoleAdapter>()
.Root<Program>("Root");
.Root<Program>(nameof(Root));
}
3 changes: 0 additions & 3 deletions src/Pure.DI.Templates/install-from-source.bat

This file was deleted.

0 comments on commit bb869bd

Please sign in to comment.