Skip to content

Commit

Permalink
Merge pull request #6 from dotnet-campus/t/lindexi/App
Browse files Browse the repository at this point in the history
加上测试代码
  • Loading branch information
TheUnknowName authored Mar 17, 2022
2 parents 7999a46 + c36e1ed commit 7c020cd
Show file tree
Hide file tree
Showing 30 changed files with 628 additions and 4 deletions.
13 changes: 13 additions & 0 deletions demo/WPFDemo/WPFDemo.Api/CommandLines/Options.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using dotnetCampus.Cli;

namespace WPFDemo.Api.CommandLines
{
/// <summary>
/// 启动参数
/// </summary>
public class Options
{
[Option("Name")]
public string? Name { set; get; }
}
}
6 changes: 6 additions & 0 deletions demo/WPFDemo/WPFDemo.Api/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
using dotnetCampus.ApplicationStartupManager;
using dotnetCampus.Telescope;

using WPFDemo.Api.StartupTaskFramework;

[assembly: MarkExport(typeof(StartupTask), typeof(StartupTaskAttribute))]
22 changes: 22 additions & 0 deletions demo/WPFDemo/WPFDemo.Api/Startup/Foo1Startup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using dotnetCampus.ApplicationStartupManager;

using WPFDemo.Api.StartupTaskFramework;

namespace WPFDemo.Api.Startup
{
[StartupTask(BeforeTasks = StartupNodes.CoreUI, AfterTasks = StartupNodes.Foundation)]
public class Foo1Startup : StartupTask
{
protected override Task RunAsync(StartupContext context)
{
context.Logger.LogInfo("Foo1 Startup");
return base.RunAsync(context);
}
}
}
16 changes: 16 additions & 0 deletions demo/WPFDemo/WPFDemo.Api/Startup/OptionStartup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using dotnetCampus.ApplicationStartupManager;
using WPFDemo.Api.StartupTaskFramework;

namespace WPFDemo.Api.Startup
{
[StartupTask(BeforeTasks = StartupNodes.Foundation, AfterTasks = "LibStartup")]
class OptionStartup : StartupTask
{
protected override Task RunAsync(StartupContext context)
{
context.Logger.LogInfo("Command " + context.CommandLineOptions.Name);

return CompletedTask;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System.Reflection;
using dotnetCampus.ApplicationStartupManager;
using dotnetCampus.Telescope;

namespace WPFDemo.Api.StartupTaskFramework
{
public class AssemblyMetadataExporter
{
public AssemblyMetadataExporter(Assembly[] assemblies)
{
_assemblies = assemblies;
}

public IEnumerable<StartupTaskMetadata> ExportStartupTasks()
{
var collection = Export<StartupTask, StartupTaskAttribute>();
return collection.Select(x => new StartupTaskMetadata(x.RealType.Name.Replace("Startup", ""), x.CreateInstance)
{
Scheduler = x.Attribute.Scheduler,
BeforeTasks = x.Attribute.BeforeTasks,
AfterTasks = x.Attribute.AfterTasks,
//Categories = x.Attribute.Categories,
CriticalLevel = x.Attribute.CriticalLevel,
});
}

public IEnumerable<AttributedTypeMetadata<TBaseClassOrInterface, TAttribute>> Export<TBaseClassOrInterface, TAttribute>() where TAttribute : Attribute
{
return AttributedTypes.FromAssembly<TBaseClassOrInterface, TAttribute>(_assemblies);
}

private readonly Assembly[] _assemblies;
}
}
42 changes: 42 additions & 0 deletions demo/WPFDemo/WPFDemo.Api/StartupTaskFramework/StartupContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using dotnetCampus.ApplicationStartupManager;
using dotnetCampus.Cli;
using dotnetCampus.Configurations;
using dotnetCampus.Configurations.Core;
using WPFDemo.Api.CommandLines;

namespace WPFDemo.Api.StartupTaskFramework
{
public class StartupContext : IStartupContext
{
public StartupContext(IStartupContext startupContext, CommandLine commandLine, StartupLogger logger, FileConfigurationRepo configuration, IAppConfigurator configs)
{
_startupContext = startupContext;
Logger = logger;
Configuration = configuration;
Configs = configs;
CommandLine = commandLine;
CommandLineOptions = CommandLine.As<Options>();
}

public StartupLogger Logger { get; }

public CommandLine CommandLine { get; }

public Options CommandLineOptions { get; }

public FileConfigurationRepo Configuration { get; }

public IAppConfigurator Configs { get; }

public Task<string> ReadCacheAsync(string key, string @default = "")
{
return Configuration.TryReadAsync(key, @default);
}

private readonly IStartupContext _startupContext;
public Task WaitStartupTaskAsync(string startupKey)
{
return _startupContext.WaitStartupTaskAsync(startupKey);
}
}
}
29 changes: 29 additions & 0 deletions demo/WPFDemo/WPFDemo.Api/StartupTaskFramework/StartupLogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System.Diagnostics;
using System.Text;

using dotnetCampus.ApplicationStartupManager;

namespace WPFDemo.Api.StartupTaskFramework
{
/// <summary>
/// 和项目关联的日志
/// </summary>
public class StartupLogger : StartupLoggerBase
{
public void LogInfo(string message)
{
Debug.WriteLine(message);
}

public override void ReportResult(IReadOnlyList<IStartupTaskWrapper> wrappers)
{
var stringBuilder = new StringBuilder();
foreach (var keyValuePair in MilestoneDictionary)
{
stringBuilder.AppendLine($"{keyValuePair.Key} - [{keyValuePair.Value.threadName}] Start:{keyValuePair.Value.start} Elapsed:{keyValuePair.Value.elapsed}");
}

Debug.WriteLine(stringBuilder.ToString());
}
}
}
25 changes: 25 additions & 0 deletions demo/WPFDemo/WPFDemo.Api/StartupTaskFramework/StartupManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using dotnetCampus.ApplicationStartupManager;
using dotnetCampus.Cli;
using dotnetCampus.Configurations.Core;

namespace WPFDemo.Api.StartupTaskFramework
{
/// <summary>
/// 和项目关联的启动管理器,用来注入业务相关的逻辑
/// </summary>
public class StartupManager : StartupManagerBase
{
public StartupManager(CommandLine commandLine, FileConfigurationRepo configuration, Func<Exception, Task> fastFailAction, IMainThreadDispatcher mainThreadDispatcher) : base(new StartupLogger(), fastFailAction, mainThreadDispatcher)
{
var appConfigurator = configuration.CreateAppConfigurator();
Context = new StartupContext(StartupContext, commandLine, (StartupLogger) Logger, configuration, appConfigurator);
}

private StartupContext Context { get; }

protected override Task<string> ExecuteStartupTaskAsync(StartupTaskBase startupTask, IStartupContext context, bool uiOnly)
{
return base.ExecuteStartupTaskAsync(startupTask, Context, uiOnly);
}
}
}
36 changes: 36 additions & 0 deletions demo/WPFDemo/WPFDemo.Api/StartupTaskFramework/StartupNodes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
namespace WPFDemo.Api.StartupTaskFramework
{
/// <summary>
/// 包含预设的启动节点。
/// </summary>
public class StartupNodes
{
/// <summary>
/// 基础服务(日志、异常处理、容器、生命周期管理等)请在此节点之前启动,其他业务请在此之后启动。
/// </summary>
public const string Foundation = "Foundation";

/// <summary>
/// 需要在任何一个 Window 创建之前启动的任务请在此节点之前。
/// 此节点之后将开始启动 UI。
/// </summary>
public const string CoreUI = "CoreUI";

/// <summary>
/// 需要在主 <see cref="Window"/> 创建之后启动的任务请在此节点之后。
/// 此节点完成则代表主要 UI 已经初始化完毕(但不一定已显示)。
/// </summary>
public const string UI = "UI";

/// <summary>
/// 应用程序已完成启动。如果应该显示一个窗口,则此窗口已布局、渲染完毕,对用户完全可见,可开始交互。
/// 不被其他业务依赖的模块可在此节点之后启动。
/// </summary>
public const string AppReady = "AppReady";

/// <summary>
/// 任何不关心何时启动的启动任务应该设定为在此节点之前完成。
/// </summary>
public const string StartupCompleted = "StartupCompleted";
}
}
24 changes: 24 additions & 0 deletions demo/WPFDemo/WPFDemo.Api/StartupTaskFramework/StartupTask.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using dotnetCampus.ApplicationStartupManager;

namespace WPFDemo.Api.StartupTaskFramework
{
/// <summary>
/// 表示一个和当前业务强相关的启动任务
/// </summary>
public class StartupTask : StartupTaskBase
{
protected sealed override Task RunAsync(IStartupContext context)
{
return RunAsync((StartupContext) context);
}

protected virtual Task RunAsync(StartupContext context)
{
return CompletedTask;
}
}
}
20 changes: 20 additions & 0 deletions demo/WPFDemo/WPFDemo.Api/WPFDemo.Api.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\src\dotnetCampus.ApplicationStartupManager\dotnetCampus.ApplicationStartupManager.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="dotnetCampus.CommandLine" Version="3.3.1-alpha03"/>
<PackageReference Include="dotnetCampus.Configurations" Version="1.6.9" />
<PackageReference Include="dotnetCampus.TelescopeSource" Version="1.0.0-alpha02" />
</ItemGroup>

</Project>
8 changes: 8 additions & 0 deletions demo/WPFDemo/WPFDemo.App/App.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Application x:Class="WPFDemo.App.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WPFDemo.App">
<Application.Resources>

</Application.Resources>
</Application>
17 changes: 17 additions & 0 deletions demo/WPFDemo/WPFDemo.App/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;

namespace WPFDemo.App
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
}
}
10 changes: 10 additions & 0 deletions demo/WPFDemo/WPFDemo.App/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Windows;

[assembly: ThemeInfo(
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
//(used if a resource is not found in the page,
// or application resource dictionaries)
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
//(used if a resource is not found in the page,
// app, or any theme specific resource dictionaries)
)]
12 changes: 12 additions & 0 deletions demo/WPFDemo/WPFDemo.App/MainWindow.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Window x:Class="WPFDemo.App.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WPFDemo.App"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>

</Grid>
</Window>
26 changes: 26 additions & 0 deletions demo/WPFDemo/WPFDemo.App/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WPFDemo.App
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
}
Loading

0 comments on commit 7c020cd

Please sign in to comment.