/// <summary>
        /// 创建构建过程使用的工具
        /// </summary>
        /// <param name="appConfigurator"></param>
        /// <param name="logger"></param>
        protected DotNetBuildTool(IAppConfigurator appConfigurator, ILogger logger = null)
        {
            AppConfigurator = appConfigurator
                              ?? dotnetCampus.DotNETBuild.Context.AppConfigurator.GetAppConfigurator();

            Logger = logger ?? AppConfigurator.Of <LogConfiguration>().GetLogger();
        }
Пример #2
0
        /// <summary>
        /// 发布指定的文件
        /// </summary>
        public void PublishNupkg(FileInfo nupkgFile, bool skipDuplicate = true)
        {
            if (!File.Exists(nupkgFile.FullName))
            {
                throw new FileNotFoundException($"找不到 {nupkgFile} 文件");
            }

            var toolConfiguration  = AppConfigurator.Of <ToolConfiguration>();
            var nugetConfiguration = AppConfigurator.Of <NugetConfiguration>();
            var nugetPath          = toolConfiguration.NugetPath;

            Logger.LogInformation($"开始发布主要的源 {nugetConfiguration.NuGetSource}");
            RunPublishNupkg(nugetPath, nupkgFile.FullName, nugetConfiguration.NuGetSource,
                            nugetConfiguration.NuGetApiKey, skipDuplicate);

            // 推送额外的源,推送失败自动忽略
            var attachedSource = nugetConfiguration.AttachedNuGetSource;

            if (attachedSource.Length > 0)
            {
                Logger.LogInformation($"额外推送的源 NugetConfiguration.AttachedSource 有 {attachedSource.Length} 个");
                for (var i = 0; i < attachedSource.Length; i++)
                {
                    try
                    {
                        // 依据 Url 里面不包含空格,可以使用空格分开 APIKey 和源
                        var splitSource = attachedSource[i].Split(" ");
                        if (splitSource.Length > 0)
                        {
                            string source = splitSource[0];
                            string apiKey = null;
                            if (splitSource.Length > 1)
                            {
                                apiKey = splitSource[1];
                            }

                            if (!string.IsNullOrEmpty(source))
                            {
                                Logger.LogInformation($"正在推送第 {i}/{attachedSource.Length} 个额外 NuGet 源 {source}");
                                RunPublishNupkg(nugetPath, nupkgFile.FullName, source, apiKey, skipDuplicate);
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        Logger.LogWarning(e, "推送额外 NuGet 源");
                    }
                }
            }
        }
        public void CompileAllCommitAndCopy()
        {
            _git.FetchAll();

            var commitList = GetCommitList().Reverse().ToList();

            for (var i = 0; i < commitList.Count; i++)
            {
                var commit = commitList[i];
                try
                {
                    Log($"开始 {commit} 二分,本次任务第{i + 1}次构建,总共{commitList.Count}次构建");

                    if (!CheckNeedCompile(commit))
                    {
                        Log($"已构建过 {commit} 无须再次运行,跳过此构建");
                        continue;
                    }

                    CleanDirectory(commit);

                    // 如果没有指定使用 bat 脚本构建,那么执行通用构建

                    var compilerBatFile = BinaryChopCompileConfiguration.CompilerBatFile;
                    if (string.IsNullOrEmpty(compilerBatFile) || !File.Exists(compilerBatFile))
                    {
                        Log($"找不到指定的 bat 构建脚本文件 {compilerBatFile} 将使用默认的方式构建");

                        // 这里是代码里面自己带的构建配置文件
                        var appConfigurator = GetCurrentBuildConfiguration();

                        var currentBuildLogFile = GetCurrentBuildLogFile(appConfigurator);

                        // 填充一下文件路径
                        var fileSniff = new FileSniff(appConfigurator);
                        fileSniff.Sniff();

                        var msbuildConfiguration = AppConfigurator.Of <MsbuildConfiguration>();

                        var msBuildCompiler = new MSBuild(appConfigurator);
                        msBuildCompiler.Build(new MSBuildCommandOptions()
                        {
                            ShouldRestore = msbuildConfiguration.ShouldRestore,
                            MaxCpuCount   = msbuildConfiguration.MaxCpuCount,
                        });

                        MoveFile(commit, currentBuildLogFile);
                    }
                    else
                    {
                        Log($"开始执行 {compilerBatFile} 构建脚本文件");

                        var(success, output) = ProcessCommand.ExecuteCommand(compilerBatFile, null);
                        // 将输出写入到文件里面
                        var logFile = Path.GetTempFileName();
                        File.WriteAllText(logFile, output);
                        MoveFile(commit, new FileInfo(logFile));
                    }

                    LastCommit = commit;

                    Log($"构建 {commit} 完成,休息一下。休息 {BinaryChopCompileConfiguration.SecondTimeToRest} 秒中");
                    // 构建完成,休息一下
                    // 同步的等待,这里是调度任务,不需要使用异步
                    Task.Delay(TimeSpan.FromSeconds(BinaryChopCompileConfiguration.SecondTimeToRest)).Wait();
                }
                catch (Exception e)
                {
                    Log(e.ToString());
                }
            }
        }