Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented C# friendly API #20

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .config/dotnet-tools.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
"isRoot": true,
"tools": {
"nbgv": {
"version": "3.4.244",
"version": "3.6.133",
"commands": [
"nbgv"
]
},
"paket": {
"version": "7.1.4",
"version": "8.0.0",
"commands": [
"paket"
]
Expand Down
9 changes: 9 additions & 0 deletions src/Bolero.Templating.Server/Attributes.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Bolero.Templating.Server

open System

[<AttributeUsage(AttributeTargets.Parameter ||| AttributeTargets.Field)>]
type internal TimeSpanConstantAttribute(miliseconds) =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm guessing this attribute is supposed to provide the default value for an optional parameter, but I can't get it to work on toy examples, nor find any documentation about using custom operators for this.

inherit Attribute()
member _.Value = TimeSpan.FromMilliseconds miliseconds

Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<OutputType>Library</OutputType>
</PropertyGroup>
<ItemGroup>
<Compile Include="Attributes.fs" />
<Compile Include="Templating.fs" />
<None Include="paket.references" />
<Content Include="paket.template" />
Expand Down
30 changes: 21 additions & 9 deletions src/Bolero.Templating.Server/Templating.fs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ open System.Collections.Concurrent
open System.IO
open System.Threading.Tasks
open System.Runtime.CompilerServices
open System.Runtime.InteropServices
open Microsoft.AspNetCore.Builder
open Microsoft.AspNetCore.Routing
open Microsoft.AspNetCore.SignalR
Expand Down Expand Up @@ -178,27 +179,38 @@ module Impl =
for handler in handlers do
handler.Dispose()

[<Extension>]
type ServerTemplatingExtensions =
[<AutoOpen>]
module private Constants =

let [<Literal>] DefaultTemplateDir = "."
let [<Literal>] DefaultDelay = 100.


[<Extension; AbstractClass; Sealed>]
type ServerTemplatingExtensions() =

[<Extension>]
static member AddHotReload(this: IServiceCollection, configure: WatcherConfig -> WatcherConfig) : IServiceCollection =
static member AddHotReload(this: IServiceCollection, configure: Func<WatcherConfig, WatcherConfig>) : IServiceCollection =
this.AddSignalR().AddJsonProtocol() |> ignore
let config = configure { Directory = "."; Delay = TimeSpan.FromMilliseconds 100. }
let config = configure.Invoke { Directory = DefaultTemplateDir; Delay = TimeSpan.FromMilliseconds DefaultDelay }
this.AddSingleton(config)
.AddSingleton<Watcher>()
.AddTransient<IClient, Client>()

[<Extension>]
static member AddHotReload(this: IServiceCollection, ?templateDir: string, ?delay: TimeSpan) : IServiceCollection =
static member AddHotReload(
this: IServiceCollection,
[<Optional; DefaultParameterValue(DefaultTemplateDir)>] templateDir: string,
[<Optional; TimeSpanConstantAttribute(DefaultDelay)>] delay: TimeSpan)
: IServiceCollection =
ServerTemplatingExtensions.AddHotReload(this, fun config ->
{
Directory = defaultArg templateDir config.Directory
Delay = defaultArg delay config.Delay
Directory = templateDir
Delay = delay
})

[<Extension>]
static member UseHotReload(this: IEndpointRouteBuilder, ?urlPath: string) : unit =
static member UseHotReload(this: IEndpointRouteBuilder, [<Optional; DefaultParameterValue(HotReloadConstants.DefaultUrl)>] urlPath: string) : unit =
this.ServiceProvider.GetService<Watcher>().Start()
let urlPath = defaultArg urlPath HotReloadSettings.Default.Url
let urlPath = urlPath
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might as well remove this line altogether.

this.MapHub<HotReloadHub>(urlPath) |> ignore
10 changes: 8 additions & 2 deletions src/Bolero.Templating/Settings.fs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@

namespace Bolero.Templating

[<RequireQualifiedAccess>]
module HotReloadConstants =

let [<Literal>] DefaultUrl = "/bolero-reload"
let [<Literal>] DefaultReconnectDelay = 5000

type HotReloadSettings =
{
Url: string
Expand All @@ -28,6 +34,6 @@ type HotReloadSettings =

static member Default =
{
Url = "/bolero-reload"
ReconnectDelayInMs = 5000
Url = HotReloadConstants.DefaultUrl
ReconnectDelayInMs = HotReloadConstants.DefaultReconnectDelay
}
Loading