Skip to content

Commit

Permalink
Publish Blazor example
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolayPianikov committed Mar 9, 2024
1 parent 386215b commit 6843918
Show file tree
Hide file tree
Showing 14 changed files with 115 additions and 32 deletions.
32 changes: 21 additions & 11 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -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/[email protected]
with:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
BRANCH: gh-pages
FOLDER: samples/BlazorWebAssemblyApp/bin/wwwroot
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ benchmarks/data/results/*.md
_ReSharper.Caches/
.idea
.logs

20 changes: 20 additions & 0 deletions .run/Publish Blazor example.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Publish Blazor example" type="DotNetProject" factoryName=".NET Project">
<option name="EXE_PATH" value="$PROJECT_DIR$/build/bin/roslyn4.3/Debug/net8.0/build.exe" />
<option name="PROGRAM_PARAMETERS" value="publish" />
<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="net8.0" />
<method v="2">
<option name="Build" />
</method>
</configuration>
</component>
28 changes: 16 additions & 12 deletions build/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,24 @@

DI.Setup(nameof(Composition))
.Root<RootTarget>("RootTarget")

.DefaultLifetime(Lifetime.PerBlock)
.Bind<RootCommand>().To<RootCommand>()
.Bind<Settings>().To<Settings>()

.Bind().To<RootCommand>()
.Bind().To<Settings>()
.Bind<ITeamCityArtifactsWriter>().To(_ => GetService<ITeamCityWriter>())
.Bind<INuGet>().To(_ => GetService<INuGet>())
.Bind().To(_ => GetService<INuGet>())

// Targets
.Bind<IInitializable, ITarget<Package>>(typeof(GeneratorTarget)).To<GeneratorTarget>()
.Bind<IInitializable, ITarget<IReadOnlyCollection<Library>>>(typeof(LibrariesTarget)).To<LibrariesTarget>()
.Bind<IInitializable, ITarget<IReadOnlyCollection<Package>>>(typeof(CompatibilityCheckTarget)).To<CompatibilityCheckTarget>()
.Bind<IInitializable, ITarget<IReadOnlyCollection<Package>>>(typeof(PackTarget)).To<PackTarget>()
.Bind<IInitializable>(typeof(ReadmeTarget)).To<ReadmeTarget>()
.Bind<IInitializable, ITarget<int>>(typeof(BenchmarksTarget)).To<BenchmarksTarget>()
.Bind<IInitializable>(typeof(DeployTarget)).To<DeployTarget>()
.Bind<IInitializable>(typeof(TemplateTarget)).To<TemplateTarget>()
.Bind<IInitializable>(typeof(UpdateTarget)).To<UpdateTarget>();
.Bind(Tag.Type).To<GeneratorTarget>()
.Bind(Tag.Type).To<LibrariesTarget>()
.Bind(Tag.Type).To<CompatibilityCheckTarget>()
.Bind(Tag.Type).To<PackTarget>()
.Bind(Tag.Type).To<ReadmeTarget>()
.Bind(Tag.Type).To<BenchmarksTarget>()
.Bind(Tag.Type).To<DeployTarget>()
.Bind(Tag.Type).To<TemplateTarget>()
.Bind(Tag.Type).To<UpdateTarget>()
.Bind(Tag.Type).To<PublishBlazorTarget>();

return await new Composition().RootTarget.RunAsync(CancellationToken.None);
46 changes: 46 additions & 0 deletions build/PublishBlazorTarget.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// 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<int>
{
public Task InitializeAsync() => commands.Register(
this,
"Publish balazor web sssembly example",
"publish",
"pb");

[SuppressMessage("Performance", "CA1861:Avoid constant arrays as arguments")]
public async Task<int> RunAsync(CancellationToken cancellationToken)
{
var projectPath = Path.Combine("samples", "BlazorWebAssemblyApp");
var publishPath = Path.Combine(projectPath, "bin");
var rootPath = Path.Combine(publishPath, "wwwroot");
var result = await new DotNetPublish()
.WithProject(projectPath)
.WithConfiguration("Release")
.WithOutput(publishPath)
.RunAsync(cancellationToken: cancellationToken);

// Change the base-tag in index.html from '/' to 'BlazorWebAssemblyApp' to match GitHub Pages repository subdirectory
var indexFile = Path.Combine(rootPath, "index.html");
var indexContent = await File.ReadAllTextAsync(indexFile, cancellationToken);
indexContent = indexContent.Replace("""<base href="/" />""", """<base href="/Pure.DI/" />""");
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(rootPath, "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(rootPath, ".nojekyll"), "", cancellationToken);

return result ?? 1;
}
}
2 changes: 2 additions & 0 deletions readme/BlazorWebAssemblyAppPageTemplate.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

[![CSharp](https://img.shields.io/badge/C%23-code-blue.svg)](/samples/BlazorServerApp)

[Here's an example](https://devteam.github.io/Pure.DI/) on github.io

This example demonstrates the creation of a [Blazor WebAssembly](https://learn.microsoft.com/en-us/aspnet/core/blazor/hosting-models#blazor-webassembly) application in the pure DI paradigm using the _Pure.DI_ code generator.

Composition setup file is [Composition.cs](/samples/BlazorWebAssemblyApp/Composition.cs):
Expand Down
2 changes: 1 addition & 1 deletion samples/BlazorServerApp/Pages/Index.razor
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<h1>Hello, world!</h1>

Welcome to your new app.
Welcome to your Pure.DI Blazor server example.

<hr/>

Expand Down
4 changes: 2 additions & 2 deletions samples/BlazorServerApp/Shared/MainLayout.razor
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@inherits LayoutComponentBase

<PageTitle>BlazorServerApp</PageTitle>
<PageTitle>Pure.DI Blazor server example</PageTitle>

<div class="page">
<div class="sidebar">
Expand All @@ -9,7 +9,7 @@

<main>
<div class="top-row px-4">
<a href="https://docs.microsoft.com/aspnet/" target="_blank">About</a>
<a href="https://github.com/DevTeam/Pure.DI/blob/master/readme/BlazorServerApp.md" target="_blank">About</a>
</div>

<article class="content px-4">
Expand Down
2 changes: 1 addition & 1 deletion samples/BlazorServerApp/Shared/NavMenu.razor
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div class="top-row ps-3 navbar navbar-dark">
<div class="container-fluid">
<a class="navbar-brand" href="">BlazorServerApp</a>
<a class="navbar-brand" href="">Pure.DI example</a>
<button title="Navigation menu" class="navbar-toggler" @onclick="ToggleNavMenu">
<span class="navbar-toggler-icon"></span>
</button>
Expand Down
2 changes: 1 addition & 1 deletion samples/BlazorWebAssemblyApp/Pages/Index.razor
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<h1>Hello, world!</h1>

Welcome to your new app.
Welcome to your Pure.DI Blazor WebAssembly example.

<hr/>

Expand Down
2 changes: 1 addition & 1 deletion samples/BlazorWebAssemblyApp/Shared/MainLayout.razor
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<main>
<div class="top-row px-4">
<a href="https://docs.microsoft.com/aspnet/" target="_blank">About</a>
<a href="https://github.com/DevTeam/Pure.DI/blob/master/readme/BlazorWebAssemblyApp.md" target="_blank">About</a>
</div>

<article class="content px-4">
Expand Down
2 changes: 1 addition & 1 deletion samples/BlazorWebAssemblyApp/Shared/NavMenu.razor
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div class="top-row ps-3 navbar navbar-dark">
<div class="container-fluid">
<a class="navbar-brand" href="">BlazorWebAssemblyApp</a>
<a class="navbar-brand" href="">Pure.DI example</a>
<button title="Navigation menu" class="navbar-toggler" @onclick="ToggleNavMenu">
<span class="navbar-toggler-icon"></span>
</button>
Expand Down
2 changes: 1 addition & 1 deletion samples/BlazorWebAssemblyApp/wwwroot/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<title>BlazorWebAssemblyApp</title>
<title>Pure.DI Blazor WebAssembly example</title>
<base href="/" />
<link href="css/bootstrap/bootstrap.min.css" rel="stylesheet" />
<link href="css/app.css" rel="stylesheet" />
Expand Down
2 changes: 1 addition & 1 deletion samples/WebApp/Models/IndexViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace WebApp.Models;
public class IndexViewModel(
IWeatherForecastService weatherForecastService,
string title = "Welcome",
string aboutUrl = "https://github.com/DevTeam/Pure.DI")
string aboutUrl = "https://github.com/DevTeam/Pure.DI/blob/master/readme/WebApp.md")
{
public string Title { get; } = title;

Expand Down

0 comments on commit 6843918

Please sign in to comment.