This repository was archived by the owner on Feb 6, 2026. It is now read-only.
generated from pmdevers/nuget-package-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Introduces build tasks for code generation #90
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
src/K8sOperator.NET.BuildTasks/GenerateDockerfileTask.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| using Microsoft.Build.Framework; | ||
| using Microsoft.Build.Utilities; | ||
| using System; | ||
| using System.IO; | ||
| using System.Text.RegularExpressions; | ||
|
|
||
| namespace K8sOperator.NET.BuildTasks; | ||
|
|
||
| public class GenerateDockerfileTask : Task | ||
| { | ||
| [Required] | ||
| public string ProjectDirectory { get; set; } = string.Empty; | ||
|
|
||
| [Required] | ||
| public string ProjectName { get; set; } = string.Empty; | ||
|
|
||
| [Required] | ||
| public string TargetFramework { get; set; } = string.Empty; | ||
|
|
||
| [Required] | ||
| public string OperatorName { get; set; } = string.Empty; | ||
|
|
||
| [Required] | ||
| public string ContainerRegistry { get; set; } = string.Empty; | ||
|
|
||
| [Required] | ||
| public string ContainerRepository { get; set; } = string.Empty; | ||
|
|
||
| [Required] | ||
| public string ContainerTag { get; set; } = string.Empty; | ||
|
|
||
| public override bool Execute() | ||
| { | ||
| try | ||
| { | ||
| var dockerfilePath = Path.Combine(ProjectDirectory, "Dockerfile"); | ||
| var dockerignorePath = Path.Combine(ProjectDirectory, ".dockerignore"); | ||
|
|
||
| // Extract .NET version from TargetFramework | ||
| var dotnetVersion = ExtractDotNetVersion(TargetFramework); | ||
|
|
||
| // Read templates from embedded resources | ||
| var dockerfileContent = ReadEmbeddedResource("Dockerfile.template"); | ||
| var dockerignoreContent = ReadEmbeddedResource(".dockerignore.template"); | ||
|
|
||
| // Replace placeholders | ||
| dockerfileContent = dockerfileContent | ||
| .Replace("{PROJECT_NAME}", ProjectName) | ||
| .Replace("{DOTNET_VERSION}", dotnetVersion); | ||
|
|
||
| if (!File.Exists(dockerfilePath)) | ||
| { | ||
| File.WriteAllText(dockerfilePath, dockerfileContent); | ||
|
|
||
| Log.LogMessage(MessageImportance.High, $"Generated Dockerfile at: {dockerfilePath}"); | ||
|
|
||
| } | ||
|
|
||
| if (!File.Exists(dockerignorePath)) | ||
| { | ||
| File.WriteAllText(dockerignorePath, dockerignoreContent); | ||
|
|
||
| Log.LogMessage(MessageImportance.High, $"Generated .dockerignore at: {dockerignorePath}"); | ||
| } | ||
|
|
||
| // Log success | ||
| Log.LogMessage(MessageImportance.High, $"Operator: {OperatorName}"); | ||
| Log.LogMessage(MessageImportance.High, $" .NET Version: {dotnetVersion}"); | ||
| Log.LogMessage(MessageImportance.High, $" Image: {ContainerRegistry}/{ContainerRepository}:{ContainerTag}"); | ||
| Log.LogMessage(MessageImportance.High, ""); | ||
| Log.LogMessage(MessageImportance.High, "To build the image:"); | ||
| Log.LogMessage(MessageImportance.High, $" docker build -t {ContainerRegistry}/{ContainerRepository}:{ContainerTag} ."); | ||
| Log.LogMessage(MessageImportance.High, ""); | ||
| Log.LogMessage(MessageImportance.High, "To push the image:"); | ||
| Log.LogMessage(MessageImportance.High, $" docker push {ContainerRegistry}/{ContainerRepository}:{ContainerTag}"); | ||
| Log.LogMessage(MessageImportance.High, ""); | ||
|
|
||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return true; | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| Log.LogError($"Failed to generate Dockerfile: {ex.Message}"); | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| private static string ExtractDotNetVersion(string targetFramework) | ||
| { | ||
| // Handle formats like "net10.0", "net8.0", "netcoreapp3.1" | ||
| var match = Regex.Match(targetFramework, @"net(?:coreapp)?(\d+\.\d+)"); | ||
| if (match.Success) | ||
| { | ||
| return match.Groups[1].Value; | ||
| } | ||
|
|
||
| // Fallback | ||
| return "10.0"; | ||
| } | ||
|
|
||
| private static string ReadEmbeddedResource(string resourceName) | ||
| { | ||
| var assembly = typeof(GenerateDockerfileTask).Assembly; | ||
| var fullResourceName = $"K8sOperator.NET.BuildTasks.Templates.{resourceName}"; | ||
|
|
||
| using var stream = assembly.GetManifestResourceStream(fullResourceName) | ||
| ?? throw new InvalidOperationException($"Could not find embedded resource: {fullResourceName}"); | ||
| using var reader = new StreamReader(stream); | ||
| return reader.ReadToEnd(); | ||
| } | ||
| } | ||
62 changes: 62 additions & 0 deletions
62
src/K8sOperator.NET.BuildTasks/GenerateLaunchSettingsTask.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| using Microsoft.Build.Framework; | ||
| using Microsoft.Build.Utilities; | ||
| using System; | ||
| using System.IO; | ||
|
|
||
| namespace K8sOperator.NET.BuildTasks; | ||
|
|
||
| public class GenerateLaunchSettingsTask : Task | ||
| { | ||
| [Required] | ||
| public string ProjectDirectory { get; set; } = string.Empty; | ||
|
|
||
| public override bool Execute() | ||
| { | ||
| try | ||
| { | ||
| var propertiesDir = Path.Combine(ProjectDirectory, "Properties"); | ||
| var launchSettingsPath = Path.Combine(propertiesDir, "launchSettings.json"); | ||
|
|
||
| if (File.Exists(launchSettingsPath)) | ||
| { | ||
| Log.LogMessage(MessageImportance.Normal, $"launchSettings.json already exists at {launchSettingsPath}, skipping generation"); | ||
| return true; | ||
| } | ||
|
|
||
| // Read template from embedded resources | ||
| var launchSettingsContent = ReadEmbeddedResource("launchSettings.json.template"); | ||
|
|
||
| // Create Properties directory if needed | ||
| Directory.CreateDirectory(propertiesDir); | ||
|
|
||
| // Write launchSettings.json | ||
| File.WriteAllText(launchSettingsPath, launchSettingsContent); | ||
|
|
||
| // Log success | ||
| Log.LogMessage(MessageImportance.High, ""); | ||
| Log.LogMessage(MessageImportance.High, $"Generated launchSettings.json at: {launchSettingsPath}"); | ||
| Log.LogMessage(MessageImportance.High, $" Profiles: Operator, Install, Version, Help"); | ||
| Log.LogMessage(MessageImportance.High, ""); | ||
|
|
||
| return true; | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| Log.LogError($"Failed to generate launchSettings.json: {ex.Message}"); | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| private static string ReadEmbeddedResource(string resourceName) | ||
| { | ||
| var assembly = typeof(GenerateLaunchSettingsTask).Assembly; | ||
| var fullResourceName = $"K8sOperator.NET.BuildTasks.Templates.{resourceName}"; | ||
|
|
||
| using var stream = assembly.GetManifestResourceStream(fullResourceName) | ||
| ?? throw new InvalidOperationException($"Could not find embedded resource: {fullResourceName}"); | ||
| using var reader = new StreamReader(stream); | ||
| return reader.ReadToEnd(); | ||
| } | ||
| } | ||
|
|
||
|
|
24 changes: 24 additions & 0 deletions
24
src/K8sOperator.NET.BuildTasks/K8sOperator.NET.BuildTasks.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>netstandard2.0</TargetFramework> | ||
| <LangVersion>latest</LangVersion> | ||
| <Nullable>enable</Nullable> | ||
| <IsPackable>false</IsPackable> | ||
| <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Microsoft.Build.Framework" Version="17.12.*" /> | ||
| <PackageReference Include="Microsoft.Build.Utilities.Core" Version="17.12.*" /> | ||
| </ItemGroup> | ||
|
|
||
| <!-- Embed template files as resources --> | ||
| <ItemGroup> | ||
| <EmbeddedResource Include="Templates\Dockerfile.template" /> | ||
| <EmbeddedResource Include="Templates\.dockerignore.template" /> | ||
| <EmbeddedResource Include="Templates\launchSettings.json.template" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> | ||
|
|
File renamed without changes.
File renamed without changes.
37 changes: 37 additions & 0 deletions
37
src/K8sOperator.NET.BuildTasks/Templates/launchSettings.json.template
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,37 @@ | ||||||||||||||||||
| { | ||||||||||||||||||
| "profiles": { | ||||||||||||||||||
| "Operator": { | ||||||||||||||||||
| "commandName": "Project", | ||||||||||||||||||
| "commandLineArgs": "operator", | ||||||||||||||||||
| "environmentVariables": { | ||||||||||||||||||
| "ASPNETCORE_ENVIRONMENT": "Development" | ||||||||||||||||||
| }, | ||||||||||||||||||
| "dotnetRunMessages": true | ||||||||||||||||||
| }, | ||||||||||||||||||
| "Install": { | ||||||||||||||||||
| "commandName": "Project", | ||||||||||||||||||
| "commandLineArgs": "install > install.yaml", | ||||||||||||||||||
| "environmentVariables": { | ||||||||||||||||||
|
Comment on lines
+11
to
+14
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shell redirection won’t work in generated launchSettings. The task writes this into launchSettings, but 🔧 Suggested fix (avoid redirection in template)- "commandLineArgs": "install > install.yaml",
+ "commandLineArgs": "install",📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||
| "ASPNETCORE_ENVIRONMENT": "Development" | ||||||||||||||||||
| }, | ||||||||||||||||||
| "dotnetRunMessages": true | ||||||||||||||||||
| }, | ||||||||||||||||||
| "Version": { | ||||||||||||||||||
| "commandName": "Project", | ||||||||||||||||||
| "commandLineArgs": "version", | ||||||||||||||||||
| "environmentVariables": { | ||||||||||||||||||
| "ASPNETCORE_ENVIRONMENT": "Development" | ||||||||||||||||||
| }, | ||||||||||||||||||
| "dotnetRunMessages": true | ||||||||||||||||||
| }, | ||||||||||||||||||
| "Help": { | ||||||||||||||||||
| "commandName": "Project", | ||||||||||||||||||
| "commandLineArgs": "help", | ||||||||||||||||||
| "environmentVariables": { | ||||||||||||||||||
| "ASPNETCORE_ENVIRONMENT": "Development" | ||||||||||||||||||
| }, | ||||||||||||||||||
| "dotnetRunMessages": true | ||||||||||||||||||
| } | ||||||||||||||||||
| }, | ||||||||||||||||||
| "$schema": "http://json.schemastore.org/launchsettings.json" | ||||||||||||||||||
| } | ||||||||||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shell redirection won’t work in launchSettings.
commandLineArgsis passed directly todotnet run; the>redirection isn’t interpreted, soinstall.yamlwon’t be created. Prefer a supported output flag in the command or keep redirection as a manual shell step.🔧 Suggested fix (avoid redirection in launchSettings)
📝 Committable suggestion
🤖 Prompt for AI Agents