From 3f491cdaebeddee4af3095a45d237ff572ff74a4 Mon Sep 17 00:00:00 2001 From: Nikolay Pianikov Date: Fri, 8 Mar 2024 13:02:15 +0300 Subject: [PATCH] Publish Blazor example --- .github/workflows/main.yml | 32 +++++++++++++------- .gitignore | 1 + .run/Publish Blazor example.run.xml | 20 ++++++++++++ build/Program.cs | 28 +++++++++-------- build/PublishBlazorTarget.cs | 47 +++++++++++++++++++++++++++++ 5 files changed, 105 insertions(+), 23 deletions(-) create mode 100644 .run/Publish Blazor example.run.xml create mode 100644 build/PublishBlazorTarget.cs diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index dce44f28..fd967e96 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -1,19 +1,29 @@ name: Pure.DI check -on: [push] +on: [ push ] jobs: build: runs-on: ubuntu-latest - - steps: - - uses: actions/checkout@v4 - - - name: Setup dotnet - uses: actions/setup-dotnet@v3 - with: - dotnet-version: '8.0.x' - - name: Build and check - run: dotnet run --project ./build -- check + steps: + - uses: actions/checkout@v4 + + - name: Setup dotnet + uses: actions/setup-dotnet@v3 + with: + dotnet-version: '8.0.x' + + #- name: Build and check + #run: dotnet run --project ./build -- check + + - name: Publish Blazor example + run: dotnet run --project ./build -- publish + + - name: Commit wwwroot to GitHub Pages + uses: JamesIves/github-pages-deploy-action@3.7.1 + with: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + BRANCH: gh-pages + FOLDER: samples/BlazorWebAssemblyApp/bin/root diff --git a/.gitignore b/.gitignore index b6ba230c..ec2c6cdd 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,4 @@ benchmarks/data/results/*.md _ReSharper.Caches/ .idea .logs + diff --git a/.run/Publish Blazor example.run.xml b/.run/Publish Blazor example.run.xml new file mode 100644 index 00000000..a10e319e --- /dev/null +++ b/.run/Publish Blazor example.run.xml @@ -0,0 +1,20 @@ + + + + \ No newline at end of file diff --git a/build/Program.cs b/build/Program.cs index e3795940..45dc9e32 100644 --- a/build/Program.cs +++ b/build/Program.cs @@ -2,20 +2,24 @@ DI.Setup(nameof(Composition)) .Root("RootTarget") + .DefaultLifetime(Lifetime.PerBlock) - .Bind().To() - .Bind().To() + + .Bind().To() + .Bind().To() .Bind().To(_ => GetService()) - .Bind().To(_ => GetService()) + .Bind().To(_ => GetService()) + // Targets - .Bind>(typeof(GeneratorTarget)).To() - .Bind>>(typeof(LibrariesTarget)).To() - .Bind>>(typeof(CompatibilityCheckTarget)).To() - .Bind>>(typeof(PackTarget)).To() - .Bind(typeof(ReadmeTarget)).To() - .Bind>(typeof(BenchmarksTarget)).To() - .Bind(typeof(DeployTarget)).To() - .Bind(typeof(TemplateTarget)).To() - .Bind(typeof(UpdateTarget)).To(); + .Bind(Tag.Type).To() + .Bind(Tag.Type).To() + .Bind(Tag.Type).To() + .Bind(Tag.Type).To() + .Bind(Tag.Type).To() + .Bind(Tag.Type).To() + .Bind(Tag.Type).To() + .Bind(Tag.Type).To() + .Bind(Tag.Type).To() + .Bind(Tag.Type).To(); return await new Composition().RootTarget.RunAsync(CancellationToken.None); \ No newline at end of file diff --git a/build/PublishBlazorTarget.cs b/build/PublishBlazorTarget.cs new file mode 100644 index 00000000..2052d5a0 --- /dev/null +++ b/build/PublishBlazorTarget.cs @@ -0,0 +1,47 @@ +// ReSharper disable StringLiteralTypo +// ReSharper disable HeapView.DelegateAllocation +// ReSharper disable HeapView.ClosureAllocation +// ReSharper disable ClassNeverInstantiated.Global +// ReSharper disable ReturnTypeCanBeEnumerable.Local +// ReSharper disable InvertIf + +namespace Build; + +internal class PublishBlazorTarget( + Commands commands) + : IInitializable, ITarget +{ + public Task InitializeAsync() => commands.Register( + this, + "Publish balazor web sssembly example", + "publish", + "pb"); + + [SuppressMessage("Performance", "CA1861:Avoid constant arrays as arguments")] + public async Task RunAsync(CancellationToken cancellationToken) + { + var projectPath = Path.Combine("samples", "BlazorWebAssemblyApp"); + var rootPath = Path.Combine("bin", "root"); + var result = await new DotNetPublish() + .WithWorkingDirectory(projectPath) + .WithConfiguration("Release") + .WithOutput(rootPath) + .RunAsync(cancellationToken: cancellationToken); + + var wwwrootPath = Path.Combine(projectPath, rootPath, "wwwroot"); + + // Change the base-tag in index.html from '/' to 'BlazorWebAssemblyApp' to match GitHub Pages repository subdirectory + var indexFile = Path.Combine(wwwrootPath, "index.html"); + var indexContent = await File.ReadAllTextAsync(indexFile, cancellationToken); + indexContent = indexContent.Replace("""""", """"""); + await File.WriteAllTextAsync(indexFile, indexContent, cancellationToken); + + // Copy index.html to 404.html to serve the same file when a file is not found + File.Copy(indexFile, Path.Combine(wwwrootPath, "404.html")); + + // Add .nojekyll file to tell GitHub pages to not treat this as a Jekyll project. (Allow files and folders starting with an underscore) + await File.AppendAllTextAsync(Path.Combine(wwwrootPath, ".nojekyll"), "", cancellationToken); + + return result ?? 1; + } +} \ No newline at end of file