using System.Collections.Generic; using FlubuCore.Context; using FlubuCore.Context.Attributes.BuildProperties; using FlubuCore.Context.FluentInterface.Interfaces; using FlubuCore.IO; using FlubuCore.Scripting; namespace Build { /// /// 构建脚本 /// public class BuildScript : DefaultBuildScript { /// /// 解决方案文件名 /// [SolutionFileName] public string SolutionFileName { get; set; } = "Util.sln"; /// /// 构建配置 /// [BuildConfiguration] public string BuildConfiguration { get; set; } = "Release"; /// /// 源代码目录 /// public FullPath SourceDir => RootDirectory.CombineWith( "src" ); /// /// 源代码目录 /// public FullPath TestDir => RootDirectory.CombineWith( "test" ); /// /// 输出目录 /// public FullPath OutputDir => RootDirectory.CombineWith( "output" ); /// /// 项目文件列表 /// public List ProjectFiles { get; set; } /// /// 测试项目文件列表 /// public List TestProjectFiles { get; set; } /// /// 构建前操作 /// /// 构建任务上下文 protected override void BeforeBuildExecution( ITaskContext context ) { ProjectFiles = context.GetFiles( SourceDir, "*/*.csproj" ); TestProjectFiles = context.GetFiles( TestDir, "*/*.csproj" ); } /// /// 配置构建目标 /// /// 构建任务上下文 protected override void ConfigureTargets( ITaskContext context ) { var clean = CreateCleanTarget( context ); var restore = CreateRestoreTarget( context, clean ); var build = CreateBuildTarget( context, restore ); var test = CreateTestTarget( context ); } /// /// 创建清理步骤 /// private ITarget CreateCleanTarget( ITaskContext context ) { return context.CreateTarget( "Clean" ) .SetDescription( "Clean the solution." ) .AddCoreTask( t => t.Clean().Configuration( "Debug" ).CleanOutputDir() ) .AddCoreTask( t => t.Clean().Configuration( "Release" ).CleanOutputDir() ); } /// /// 创建包还原步骤 /// private ITarget CreateRestoreTarget( ITaskContext context, ITarget target ) { return context.CreateTarget( "Restore" ) .SetDescription( "Restore the solution." ) .DependsOn( target ) .AddCoreTask( t => t.Restore() ); } /// /// 创建构建步骤 /// private ITarget CreateBuildTarget( ITaskContext context, ITarget target ) { return context.CreateTarget( "Build" ) .SetDescription( "Build the solution." ) .DependsOn( target ) .AddCoreTask( t => t.Build() ); } /// /// 创建测试步骤 /// private ITarget CreateTestTarget( ITaskContext context ) { return context.CreateTarget( "Test" ) .SetDescription( "Run all tests." ) .ForEach( TestProjectFiles, ( project, target ) => { target.AddCoreTask( t => t.Test().Project( project ) ); } ); } } }