コード例 #1
0
        private static async Task AddCommonAssemblyInfoToProjectFilesAsync(string projectFilePath, string content)
        {
            if (string.IsNullOrWhiteSpace(content))
            {
                return;
            }

            Regex findRegEx = new Regex(@"<Compile Include=""(..\\)+CommonAssemblyInfo.cs"">",
                                        RegexOptions.Singleline);

            if (findRegEx.IsMatch(content))
            {
                return;
            }

            Regex replaceRegEx = new Regex(@"<ItemGroup>", RegexOptions.RightToLeft);

            string relativePath = await FileOperationHelper.GetRelativePath(projectFilePath, CommonAssemblyInfoPath);


            string newContent = replaceRegEx.Replace(content,
                                                     $@"<ItemGroup>
    <Compile Include=""{relativePath}"">
        <Link>Properties\CommonAssemblyInfo.cs</Link>
    </Compile>", 1);

            await FileOperationHelper.WriteAllTextAsync(projectFilePath, newContent);
        }
コード例 #2
0
        private static async Task UpdateAssemblyInfoFileAsync(string projectFilePath, string commonAssemblyPath)
        {
            string assemblyInfoFilePath = await FileOperationHelper.GetAssemblyInfoFilePathFromProject(projectFilePath);

            string[] commonAssemblyInfoContent = await File.ReadAllLinesAsync(commonAssemblyPath);

            IEnumerable <string> assemblyInfoContentLines =
                from line in commonAssemblyInfoContent
                let pattern = "[assembly: Assembly"
                              where line.StartsWith(pattern)
                              let regex = new Regex(@"(?<line>^\[assembly: Assembly\w+\()", RegexOptions.Multiline)
                                          select regex.Match(line)
                                          into match
                                              where match.Success
                                          select match.Captures.FirstOrDefault()?.Value;

            string assemblyInfoContent = assemblyInfoContentLines.Aggregate(
                await File.ReadAllTextAsync(assemblyInfoFilePath),
                (current, s) =>
                current.Replace($"\r\n{s ?? throw new InvalidOperationException()}", $"\r\n// {s}"));

            await FileOperationHelper.WriteAllTextAsync(assemblyInfoFilePath, assemblyInfoContent);
        }