using System; using System.Collections.Generic; using System.IO; using System.Linq; 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"; /// /// 构建配置 /// [FromArg( "c|configuration" )] [BuildConfiguration] public string BuildConfiguration { get; set; } = "Release"; /// /// Nuget推送地址 /// [FromArg( "nugetUrl" )] public string NugetUrl { get; set; } = "https://api.nuget.org/v3/index.json"; /// /// Nuget密钥 /// [FromArg( "nugetKey", "Nuget api key for publishing nuget packages." )] public string NugetApiKey { get; set; } /// /// 源代码目录 /// public FullPath SourceDir => RootDirectory.CombineWith( "../src" ); /// /// 测试目录 /// public FullPath TestDir => RootDirectory.CombineWith( "../test" ); /// /// 输出目录 /// public FullPath OutputDir => RootDirectory.CombineWith( "../output" ); /// /// 项目文件列表 /// public List Projects { get; set; } /// /// 单元测试项目文件列表 /// public List UnitTestProjecs { get; set; } /// /// 集成测试项目文件列表 /// public List IntegrationTestProjecs { get; set; } /// /// 忽略测试项目文件列表 /// public List IgnoreTestProjecs { get; set; } /// /// 获取集成测试项目文件列表 /// protected List GetIntegrationTestProjecs() { return IntegrationTestProjecs.Where( t => IgnoreTestProjecs.Exists( p => p.FileName == t.FileName ) == false ).ToList(); } /// /// 构建前操作 /// /// 构建任务上下文 protected override void BeforeBuildExecution( ITaskContext context ) { Projects = context.GetFiles( SourceDir, "*/*.csproj" ); UnitTestProjecs = context.GetFiles( TestDir, "*/*.Tests.csproj" ); IntegrationTestProjecs = context.GetFiles( TestDir, "*/*.Tests.Integration.csproj" ); IgnoreTestProjecs = new List(); AddIgnoreTestProjecs( context ); } /// /// 添加忽略测试项目文件列表 /// private void AddIgnoreTestProjecs( ITaskContext context ) { IgnoreTestProjecs.AddRange( context.GetFiles( TestDir, "*/*.Oracle.Tests.Integration.csproj" ) ); } /// /// 配置构建目标 /// /// 构建任务上下文 protected override void ConfigureTargets( ITaskContext context ) { var clean = Clean( context ); var restore = Restore( context, clean ); var build = Build( context, restore ); var test = Test( context ); var pack = Pack( context, clean ); PublishNuGetPackage( context, pack ); } /// /// 清理解决方案 /// private ITarget Clean( ITaskContext context ) { return context.CreateTarget( "clean" ) .SetDescription( "Clean the solution." ) .AddCoreTask( t => t.Clean().AddDirectoryToClean( OutputDir, false ) ); } /// /// 还原包 /// private ITarget Restore( ITaskContext context, params ITarget[] dependTargets ) { return context.CreateTarget( "restore" ) .SetDescription( "Restore the solution." ) .DependsOn( dependTargets ) .AddCoreTask( t => t.Restore() ); } /// /// 编译解决方案 /// private ITarget Build( ITaskContext context, params ITarget[] dependTargets ) { return context.CreateTarget( "compile" ) .SetDescription( "Compiles the solution." ) .DependsOn( dependTargets ) .AddCoreTask( t => t.Build() ); } /// /// 运行测试 /// private ITarget Test( ITaskContext context, params ITarget[] dependTargets ) { var unitTest = UnitTest( context, dependTargets ); var integrationTest = IntegrationTest( context, dependTargets ); return context.CreateTarget( "test" ) .SetDescription( "Run all tests." ) .DependsOn( unitTest, integrationTest ); } /// /// 运行单元测试 /// private ITarget UnitTest( ITaskContext context, params ITarget[] dependTargets ) { return context.CreateTarget( "unit.test" ) .SetDescription( "Run unit tests." ) .DependsOn( dependTargets ) .ForEach( UnitTestProjecs, ( project, target ) => { target.AddCoreTask( t => t.Test().Project( project ) ); } ); } /// /// 运行集成测试 /// private ITarget IntegrationTest( ITaskContext context, params ITarget[] dependTargets ) { return context.CreateTarget( "integration.test" ) .SetDescription( "Run integration tests." ) .DependsOn( dependTargets ) .ForEach( GetIntegrationTestProjecs(), ( project, target ) => { target.AddCoreTask( t => t.Test().Project( project ) ); } ); } /// /// 创建nuget包 /// private ITarget Pack( ITaskContext context, params ITarget[] dependTargets ) { return context.CreateTarget( "pack" ) .SetDescription( "Create nuget packages." ) .DependsOn( dependTargets ) .ForEach( Projects, ( project, target ) => { target.AddCoreTask( t => t.Pack() .Project( project ) .IncludeSymbols() .OutputDirectory( OutputDir ) ); } ); } /// /// 发布nuget包 /// private void PublishNuGetPackage( ITaskContext context, params ITarget[] dependTargets ) { context.CreateTarget( "nuget.publish" ) .SetDescription( "Publishes nuget packages" ) .DependsOn( dependTargets ) .Do( t => { var packages = Directory.GetFiles( OutputDir, "*.nupkg" ); foreach ( var package in packages ) { if( package.EndsWith( "symbols.nupkg", StringComparison.OrdinalIgnoreCase ) ) continue; context.CoreTasks().NugetPush( package ) .DoNotFailOnError( ex => { Console.WriteLine( $"Failed to publish {package}.exception: {ex.Message}" ); } ) .ServerUrl( NugetUrl ) .ApiKey( NugetApiKey ) .Execute( context ); } } ); } } }