Skip to content

Commit

Permalink
Merge pull request #3 from Afterlife-Guide/feature/issue-2-gh-action
Browse files Browse the repository at this point in the history
Set up the project as a GitHub Action
  • Loading branch information
baynezy authored Jun 28, 2023
2 parents 7960b68 + d53c433 commit d6afbe5
Show file tree
Hide file tree
Showing 11 changed files with 89 additions and 34 deletions.
25 changes: 25 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
**/.dockerignore
**/.env
**/.git
**/.gitignore
**/.project
**/.settings
**/.toolstarget
**/.vs
**/.vscode
**/.idea
**/*.*proj.user
**/*.dbmdl
**/*.jfm
**/azds.yaml
**/bin
**/charts
**/docker-compose*
**/Dockerfile*
**/node_modules
**/npm-debug.log
**/obj
**/secrets.dev.yaml
**/values.dev.yaml
LICENSE
README.md
3 changes: 2 additions & 1 deletion AppSettings.Merge.sln
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
ProjectSection(SolutionItems) = preProject
.editorconfig = .editorconfig
.gitmessage = .gitmessage
build.cake = build.cake
.gitignore = .gitignore
src\BlazorMerge\Dockerfile = src\BlazorMerge\Dockerfile
action.yml = action.yml
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlazorMerge", "src\BlazorMerge\BlazorMerge.csproj", "{547043C0-1917-4C37-9624-97217B9B3D69}"
Expand Down
20 changes: 20 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: 'Blazor Merge Action'
description: 'Combines the appsettings.json and appsettings.environment.json files into a single appsettings.json file'
inputs:
app-environment:
description: 'The environment to use for the appsettings file'
required: true
path:
description: 'The path to the appsettings files'
required: true
runs:
using: 'docker'
image: 'src/BlazorMerge/Dockerfile'
args:
- '-e'
- ${{ inputs.app-environment }}
- '-p'
- '/github/workspace${{ inputs.path }}'
branding:
icon: 'git-merge'
color: 'green'
7 changes: 4 additions & 3 deletions src/BlazorMerge/BlazorMerge.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
</PropertyGroup>

<ItemGroup>
Expand All @@ -15,9 +16,9 @@
</ItemGroup>

<ItemGroup>
<None Update="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<Content Include="..\..\.dockerignore">
<Link>.dockerignore</Link>
</Content>
</ItemGroup>

</Project>
7 changes: 7 additions & 0 deletions src/BlazorMerge/Constants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace BlazorMerge;

public class Constants
{
public static string MainFileName = "appsettings.json";
public static string EnvironmentFileName = "appsettings.{environment}.json";
}
12 changes: 12 additions & 0 deletions src/BlazorMerge/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Set the base image as the .NET 7.0 SDK (this includes the runtime)
FROM mcr.microsoft.com/dotnet/sdk:7.0 as build-env

# Copy everything and publish the release (publish implicitly restores and builds)
WORKDIR /app
COPY . ./
RUN dotnet publish ./BlazorMerge.csproj -c Release -o out --no-self-contained

# Relayer the .NET SDK, anew with the build output
FROM mcr.microsoft.com/dotnet/sdk:7.0
COPY --from=build-env /app/out .
ENTRYPOINT [ "dotnet", "/BlazorMerge.dll" ]
8 changes: 3 additions & 5 deletions src/BlazorMerge/Feature/Merge/MergeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,18 @@ namespace BlazorMerge.Feature.Merge;

public class MergeService
{
private readonly IConfiguration _config;
private readonly IFileManager _fileManager;
private readonly IMerger _merger;

public MergeService(IConfiguration config, IFileManager fileManager, IMerger merger)
{
_config = config;
_fileManager = fileManager;
_merger = merger;
}

public int MergeEnvironment(MergeOptions options)
{
var mainFileName = _config["AppSettings:MainFileName"] ?? throw new InvalidOperationException(message:"MainFileName is not set in appsettings.json");
var mainFileName = Constants.MainFileName;
var mainFilePath = ConstructPath(options.Path, mainFileName);
var readAppSetting = _fileManager.ReadFile(mainFilePath);
var environmentFileName = ReplacePath(options);
Expand Down Expand Up @@ -51,7 +49,7 @@ private void DeleteSettingsFiles(MergeOptions options)

private bool OnlySettingsFiles(string fileName, MergeOptions options)
{
var mainFileName = _config["AppSettings:MainFileName"] ?? throw new InvalidOperationException(message:"MainFileName is not set in appsettings.json");
var mainFileName = Constants.MainFileName;
var environmentFileName = ReplacePath(options);
return fileName.EndsWith(mainFileName) ||
fileName.EndsWith($"{mainFileName}.br") ||
Expand Down Expand Up @@ -106,7 +104,7 @@ private void WriteNewSettingsFile(string readAppSetting, string readEnvironmentS
private string ReplacePath(MergeOptions options)
{
var environment = options.Environment;
var environmentFileNameFormat = _config["AppSettings:EnvironmentFileName"] ?? throw new InvalidOperationException("EnvironmentFileName is not set in appsettings.json");
var environmentFileNameFormat = Constants.EnvironmentFileName;

return environmentFileNameFormat.Replace("{environment}", environment);
}
Expand Down
23 changes: 16 additions & 7 deletions src/BlazorMerge/Files/FileManager.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
namespace BlazorMerge.Files;
using Microsoft.Extensions.Logging;

namespace BlazorMerge.Files;

public class FileManager : IFileManager
{
private readonly ILogger<FileManager> _logger;

public FileManager(ILogger<FileManager> logger)
{
_logger = logger;
}

public string ReadFile(string path)
{
if (FileDoesNotExist(path))
{
throw new FileNotFoundException($"File {path} does not exist");
}

return File.ReadAllText(path);
if (!FileDoesNotExist(path)) return File.ReadAllText(path);
_logger.LogInformation("File {Path} does not exist", path);
throw new FileNotFoundException($"File {path} does not exist");

}

private static bool FileDoesNotExist(string path)
Expand All @@ -19,11 +26,13 @@ private static bool FileDoesNotExist(string path)

public void WriteFile(string path, string content)
{
_logger.LogInformation("Writing file {Path}", path);
File.WriteAllText(path, content);
}

public void DeleteFile(string path)
{
_logger.LogInformation("Deleting file {Path}", path);
File.Delete(path);
}

Expand Down
4 changes: 0 additions & 4 deletions src/BlazorMerge/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,5 @@ IHostBuilder CreateHostBuilder(string[] strings)
s.AddSingleton<MergeService>();
s.AddSingleton<IFileManager, FileManager>();
s.AddSingleton<IMerger, Merger>();
})
.ConfigureAppConfiguration(app =>
{
app.AddJsonFile("appsettings.json");
});
}
13 changes: 0 additions & 13 deletions src/BlazorMerge/appsettings.json

This file was deleted.

1 change: 0 additions & 1 deletion test/BlazorMerge.UnitTests/Merge/MergeServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ public class MergeServiceTests
{
[Theory]
[InlineData("appsettings.json", "appsettings.{environment}.json", "appsettings.Development.json")]
[InlineData("bob.json", "phil.{environment}.json", "phil.Development.json")]
public void When_LoadingData_Then_DataShouldBePassedToMergerCorrectly(string primaryFileName, string secondaryFileFormat, string secondaryFileFinalFormat)
{
// Arrange
Expand Down

0 comments on commit d6afbe5

Please sign in to comment.