private void Init()
        {
            // todo 放在获取属性
            var fileSniff = new FileSniff(AppConfigurator);

            fileSniff.Sniff();
        }
        private static int RunInit(InitOption option)
        {
            Log.Info("BuildKit Tool 开始初始化,详细命令请看 https://github.com/dotnet-campus/dotnetcampus.DotNETBuildSDK");

            if (Enum.TryParse(option.Configuration, ignoreCase: true, out ConfigurationEnum configuration))
            {
            }
            else
            {
                configuration = ConfigurationEnum.None;
            }

            InitLog(configuration);

            var fileConfiguration = ConfigurationExtension.MergeConfiguration(option, configuration);
            var appConfigurator   = fileConfiguration.CreateAppConfigurator();

            CheckCommandInstall(appConfigurator);

            // 写入当前能找到的各个文件的配置
            var fileSniff = new FileSniff(appConfigurator);

            fileSniff.Sniff();

            appConfigurator.Of <CompileConfiguration>().SetCommonConfiguration();

            fileConfiguration.SaveAsync().Wait();

            return(0);
        }
Пример #3
0
        static void Main(string[] args)
        {
            Parser.Default.ParseArguments <AssemblyOption>(args).WithParsed(option =>
            {
                var appConfigurator      = AppConfigurator.GetAppConfigurator();
                var compileConfiguration = appConfigurator.Of <CompileConfiguration>();

#if DEBUG
                var fileSniff = new FileSniff(appConfigurator);
                fileSniff.Sniff();
#endif

                var file = option.AssemblyInfoFile;

                if (string.IsNullOrEmpty(file))
                {
                    file = appConfigurator.Default["AssemblyInfoFile"];
                }

                if (string.IsNullOrEmpty(file))
                {
                    throw new ArgumentException($"Can not find AssemblyInfoFile, try to input --AssemblyInfoFile value");
                }

                if (!Path.IsPathRooted(file))
                {
                    var codeDirectory = compileConfiguration.CodeDirectory;
                    file = Path.Combine(codeDirectory, file);
                    file = Path.GetFullPath(file);
                }

                appConfigurator.Default["AssemblyInfoFile"] = file;

                Log.Info($"Start read assmebly info file {file}");

                if (!File.Exists(file))
                {
                    throw new ArgumentException($"The assmebly info file {file} can not be found.");
                }

                var formatRegex = option.VersionFormatRegex;
                if (string.IsNullOrEmpty(formatRegex))
                {
                    formatRegex = "Version = \\\"(\\d+.\\d+.\\d+)\\\";";
                }

                Log.Info($"VersionFormatRegex: {formatRegex}");

                var content = File.ReadAllText(file);
                var match   = Regex.Match(content, formatRegex);
                if (match.Success)
                {
                    var assemblyVersion = match.Groups[1].Value;
                    var fieldCount      = GetVersionFieldCount(assemblyVersion);

                    Log.Info($"assembly version: {assemblyVersion}");
                    appConfigurator.Default["AssemblyVersion"] = assemblyVersion;

                    var lastVersion      = 0;
                    var gitConfiguration = appConfigurator.Of <GitConfiguration>();
                    if (fieldCount == 3 && gitConfiguration.GitCount != null)
                    {
                        Log.Info($"GitCount: {gitConfiguration.GitCount}");
                        lastVersion = gitConfiguration.GitCount.Value;
                    }

                    var appVersion = fieldCount == 3
                        ? $"{assemblyVersion}.{lastVersion}"
                        : assemblyVersion;
                    Log.Info($"app version: {appVersion}");
                    compileConfiguration.AppVersion = appVersion;
                }
                else
                {
                    throw new ArgumentException($"Can not math VersionFormatRegex={formatRegex} in assmebly info file {file} \r\n The file content:\r\n{content}");
                }
            });
        }
        static void Main(string[] args)
        {
            Parser.Default.ParseArguments <AssmeblyOption>(args).WithParsed(option =>
            {
                var appConfigurator      = AppConfigurator.GetAppConfigurator();
                var compileConfiguration = appConfigurator.Of <CompileConfiguration>();

#if DEBUG
                var fileSniff = new FileSniff(appConfigurator);
                fileSniff.Sniff();
#endif

                var file = option.AssemblyInfoFile;

                if (string.IsNullOrEmpty(file))
                {
                    file = appConfigurator.Default["AssemblyInfoFile"];
                }

                if (string.IsNullOrEmpty(file))
                {
                    throw new ArgumentException(
                        $"Can not find AssemblyInfoFile, try to input --AssemblyInfoFile value");
                }

                if (!Path.IsPathRooted(file))
                {
                    var codeDirectory = compileConfiguration.CodeDirectory;
                    file = Path.Combine(codeDirectory, file);
                    file = Path.GetFullPath(file);
                }

                appConfigurator.Default["AssemblyInfoFile"] = file;

                Log.Info($"assmebly info file: {file}");

                if (!File.Exists(file))
                {
                    throw new ArgumentException($"The assmebly info file {file} can not be found.");
                }

                var formatRegex = option.VersionFormatRegex;
                if (string.IsNullOrEmpty(formatRegex))
                {
                    formatRegex = "Version = \\\"(\\d+.\\d+.\\d+)\\\";";
                }

                Log.Info($"VersionFormatRegex: {formatRegex}");

                var appVersion = option.AppVersion;
                if (string.IsNullOrEmpty(appVersion))
                {
                    appVersion = compileConfiguration.AppVersion;
                }

                if (string.IsNullOrEmpty(appVersion))
                {
                    throw new ArgumentException("Can not find app version from command line and configuration file");
                }

                Log.Info($"app version: {appVersion}");

                // 通过 formatRegex 找到匹配的 Assembly 文件的版本号,然后修改版本号,替换为从命令行参数传入的或通过配置读取的版本号

                var content = File.ReadAllText(file);

                var match = Regex.Match(content, formatRegex);

                if (!match.Success)
                {
                    throw new ArgumentException(
                        $"Can not math VersionFormatRegex={formatRegex} in assmebly info file {file} \r\n The file content:\r\n{content}");
                }

                content = content.Replace(match.Value, match.Value.Replace(match.Groups[1].Value, appVersion));

                File.WriteAllText(file, content);

                Log.Info($"Wrote the app verion {appVersion} to assembly file {file}");
            });
        }
        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());
                }
            }
        }