diff --git a/build/CreateBlankAppCenterToken.ps1 b/build/CreateBlankAppCenterToken.ps1 deleted file mode 100644 index ec116dad..00000000 --- a/build/CreateBlankAppCenterToken.ps1 +++ /dev/null @@ -1,34 +0,0 @@ -# Create the tokens folder -$TOKENS_FOLDER_RELATIVE_PATH = Join-Path -Path $PSScriptRoot -ChildPath "..\samples\ComputeSharp.SwapChain.Uwp\Assets\ServiceTokens" -if(!(Test-Path -Path $TOKENS_FOLDER_RELATIVE_PATH)) { - New-Item -ItemType directory -Path $TOKENS_FOLDER_RELATIVE_PATH -} -$TOKENS_FOLDER_PATH = Resolve-Path -Path $TOKENS_FOLDER_RELATIVE_PATH - -# AppCenter tokens -$APPCENTER_TOKENS_NAME = "App.AppCenter.cs" -$APPCENTER_TOKENS_PATH = Join-Path -Path $TOKENS_FOLDER_PATH -ChildPath $APPCENTER_TOKENS_NAME -if (!(Test-Path -Path $APPCENTER_TOKENS_PATH)) -{ - # Prepare the generated source - $APP_APPCENTER_TEXT = -@" -// -#pragma warning disable -#nullable enable - -namespace ComputeSharp.SwapChain.Uwp; - -/// -partial class App -{ - /// - /// The secret token for the AppService connection. - /// - private const string AppServiceSecret = ""; -} -"@.Trim() - - # Output the source to the target file - Add-Content $APP_APPCENTER_TEXT -Path $APPCENTER_TOKENS_PATH -NoNewline -} \ No newline at end of file diff --git a/build/CreateBlankServiceTokens.ps1 b/build/CreateBlankServiceTokens.ps1 new file mode 100644 index 00000000..63cd06bc --- /dev/null +++ b/build/CreateBlankServiceTokens.ps1 @@ -0,0 +1,40 @@ +# Create the service tokens folder +$SERVICE_TOKENS_FOLDER_RELATIVE_PATH = Join-Path -Path $PSScriptRoot -ChildPath "..\samples\ComputeSharp.SwapChain.Uwp\Assets\Generated" +if(!(Test-Path -Path $SERVICE_TOKENS_FOLDER_RELATIVE_PATH)) { + New-Item -ItemType directory -Path $SERVICE_TOKENS_FOLDER_RELATIVE_PATH +} +$SERVICE_TOKENS_FOLDER_PATH = Resolve-Path -Path $SERVICE_TOKENS_FOLDER_RELATIVE_PATH + +# Service tokens +$SERVICE_TOKENS_NAME = "ServiceTokens.g.cs" +$SERVICE_TOKENS_PATH = Join-Path -Path $SERVICE_TOKENS_FOLDER_PATH -ChildPath $SERVICE_TOKENS_NAME +if (!(Test-Path -Path $SERVICE_TOKENS_PATH)) +{ + # Prepare the generated source + $SERVICE_TOKENS_TEXT = +@" +// +#pragma warning disable +#nullable enable + +namespace ComputeSharp.SwapChain.Uwp; + +/// +/// A with some secret service tokens. +/// +public static class ServiceTokens +{ + /// + /// The secret token for the AppService connection. + /// +#if RELEASE + internal const string AppService = ""; +#else + internal const string AppService = ""; +#endif +} +"@.Trim() + + # Output the source to the target file + Add-Content $SERVICE_TOKENS_TEXT -Path $SERVICE_TOKENS_PATH -NoNewline +} \ No newline at end of file diff --git a/samples/ComputeSharp.SwapChain.Uwp/App.xaml.cs b/samples/ComputeSharp.SwapChain.Uwp/App.xaml.cs index 906de4fa..db63ae84 100644 --- a/samples/ComputeSharp.SwapChain.Uwp/App.xaml.cs +++ b/samples/ComputeSharp.SwapChain.Uwp/App.xaml.cs @@ -1,13 +1,6 @@ -#if !DEBUG -using System; -using System.Diagnostics; -#endif using CommunityToolkit.Mvvm.DependencyInjection; -using ComputeSharp.SwapChain.Core.Services; using ComputeSharp.SwapChain.Core.ViewModels; -#if !DEBUG using ComputeSharp.SwapChain.Uwp.Services; -#endif using ComputeSharp.SwapChain.Uwp.Views; using Microsoft.Extensions.DependencyInjection; using Windows.ApplicationModel.Activation; @@ -59,20 +52,8 @@ sealed partial class App : Application { ServiceCollection services = new(); -#if !DEBUG - if (Guid.TryParse(AppServiceSecret, out _) && !Debugger.IsAttached) - { - services.AddSingleton(new AppCenterService(AppServiceSecret)); - } - else - { - services.AddSingleton(); - } -#else - services.AddSingleton(); -#endif - - services.AddTransient(); + _ = services.AddAnalyticsService(); + _ = services.AddTransient(); Ioc.Default.ConfigureServices(services.BuildServiceProvider()); } diff --git a/samples/ComputeSharp.SwapChain.Uwp/Assets/Generated/ServiceTokens.g.cs b/samples/ComputeSharp.SwapChain.Uwp/Assets/Generated/ServiceTokens.g.cs new file mode 100644 index 00000000..ba2fc97b --- /dev/null +++ b/samples/ComputeSharp.SwapChain.Uwp/Assets/Generated/ServiceTokens.g.cs @@ -0,0 +1,20 @@ +// +#pragma warning disable +#nullable enable + +namespace ComputeSharp.SwapChain.Uwp; + +/// +/// A with some secret service tokens. +/// +public static class ServiceTokens +{ + /// + /// The secret token for the AppService connection. + /// +#if RELEASE + internal const string AppService = ""; +#else + internal const string AppService = ""; +#endif +} \ No newline at end of file diff --git a/samples/ComputeSharp.SwapChain.Uwp/ComputeSharp.SwapChain.Uwp.csproj b/samples/ComputeSharp.SwapChain.Uwp/ComputeSharp.SwapChain.Uwp.csproj index 1877ab6d..cbeb3220 100644 --- a/samples/ComputeSharp.SwapChain.Uwp/ComputeSharp.SwapChain.Uwp.csproj +++ b/samples/ComputeSharp.SwapChain.Uwp/ComputeSharp.SwapChain.Uwp.csproj @@ -78,9 +78,10 @@ App.xaml - + + MainView.xaml @@ -209,6 +210,6 @@ - powershell.exe -ExecutionPolicy Bypass -NoProfile -NonInteractive -File $(SolutionDir)\build\CreateBlankAppCenterToken.ps1 + powershell.exe -ExecutionPolicy Bypass -NoProfile -NonInteractive -File $(SolutionDir)\build\CreateBlankServiceTokens.ps1 \ No newline at end of file diff --git a/samples/ComputeSharp.SwapChain.Uwp/Services/ServiceCollectionExtensions.cs b/samples/ComputeSharp.SwapChain.Uwp/Services/ServiceCollectionExtensions.cs new file mode 100644 index 00000000..17096d43 --- /dev/null +++ b/samples/ComputeSharp.SwapChain.Uwp/Services/ServiceCollectionExtensions.cs @@ -0,0 +1,31 @@ +using System; +using System.Diagnostics; +using ComputeSharp.SwapChain.Core.Services; +using Microsoft.Extensions.DependencyInjection; + +namespace ComputeSharp.SwapChain.Uwp.Services; + +/// +/// Extensions for the type. +/// +public static class ServiceCollectionExtensions +{ + /// + /// Adds the correct registration to the given instance. + /// + /// The input instance to use to register services. + /// The same instance as . + public static IServiceCollection AddAnalyticsService(this IServiceCollection services) + { + if (Guid.TryParse(ServiceTokens.AppService, out _) && !Debugger.IsAttached) + { + _ = services.AddSingleton(new AppCenterService(ServiceTokens.AppService)); + } + else + { + _ = services.AddSingleton(); + } + + return services; + } +}