PatchResult Patch(AssemblyInfoFile assemblyInfoFile,
                          AssemblyVersion assemblyVersion,
                          AssemblyFileVersion assemblyFileVersion, AssemblyMetaData assemblyMetaData = null)
        {
            CheckArguments(assemblyInfoFile, assemblyVersion, assemblyFileVersion);


            var patchResult = new PatchResult();

            AssemblyInfoPatchResult result = PatchAssemblyInfo(assemblyVersion, assemblyFileVersion, assemblyInfoFile, assemblyMetaData);

            patchResult.Add(result);

            return(patchResult);
        }
        public IEnumerable <PatchResult> Unpatch(PatchResult patchResult)
        {
            _logger?.Invoke(
                $"Unpatching from patch result {string.Join(Environment.NewLine, patchResult.Select(item => item.ToString()))}");

            List <PatchResult> result =
                patchResult.Select(
                    file =>
            {
                var assemblyInfoFile = new AssemblyInfoFile(file.FullPath);
                return(Patch(assemblyInfoFile, file.OldAssemblyVersion, file.OldAssemblyFileVersion, file.OldAssemblyMetadata));
            }).ToList();

            DeleteEmptySubDirs(new DirectoryInfo(BackupBasePath()), true);

            return(result);
        }
        public PatchResult Patch(IReadOnlyCollection <AssemblyInfoFile> assemblyInfoFiles, AssemblyVersion assemblyVersion, AssemblyFileVersion assemblyFileVersion, AssemblyMetaData assemblyMetaData = null)
        {
            CheckArguments(assemblyInfoFiles, assemblyVersion, assemblyFileVersion);

            if (!assemblyInfoFiles.Any())
            {
                return(new PatchResult());
            }

            var patchResult = new PatchResult();


            List <AssemblyInfoPatchResult> patchResults = assemblyInfoFiles
                                                          .Select(assemblyInfoFile =>
                                                                  PatchAssemblyInfo(assemblyVersion, assemblyFileVersion, assemblyInfoFile, assemblyMetaData))
                                                          .ToList();

            patchResults.Where(result => result.Succeeded).ToList().ForEach(patchResult.Add);

            return(patchResult);
        }
        public void Unpatch(string sourceBase)
        {
            var    patcher        = new AssemblyPatcher(sourceBase, _logger);
            string resultFilePath = Path.Combine(patcher.BackupBasePath(), "Patched.txt");

            if (!File.Exists(resultFilePath))
            {
                Console.WriteLine("Could not find any file '{0}'. No files were patched.", resultFilePath);
                return;
            }

            string json         = File.ReadAllText(resultFilePath, Encoding.UTF8);
            var    deserialized = JsonConvert.DeserializeObject <List <AssemblyInfoPatchResult> >(json);

            var patchResult = new PatchResult();

            deserialized.ForEach(patchResult.Add);

            List <AssemblyInfoPatchResult> unpatched =
                patcher.Unpatch(patchResult)
                .SelectMany(
                    results =>
                    results.Where(
                        result =>
                        result.AssemblyVersion.Version != result.OldAssemblyVersion.Version ||
                        result.AssemblyFileVersion.Version != result.OldAssemblyFileVersion.Version))
                .ToList();

            string verb = unpatched.Count == 1 ? "was" : "were";

            Console.WriteLine("{0} items {1} patched", unpatched.Count, verb);

            foreach (AssemblyInfoPatchResult result in unpatched)
            {
                Console.WriteLine("{0} changed from version {1} to version {2}",
                                  result.FullPath,
                                  result.OldAssemblyVersion,
                                  result.AssemblyVersion);
            }
        }
        public void Patch(
            AssemblyVersion assemblyVersion,
            AssemblyFileVersion assemblyFileVersion,
            string sourceBase,
            IEnumerable <AssemblyInfoFile> assemblyInfoFiles,
            AssemblyMetaData assemblyMetaData = null)
        {
            if (assemblyVersion == null)
            {
                throw new ArgumentNullException(nameof(assemblyVersion));
            }

            if (assemblyFileVersion == null)
            {
                throw new ArgumentNullException(nameof(assemblyFileVersion));
            }

            if (sourceBase == null)
            {
                throw new ArgumentNullException(nameof(sourceBase));
            }

            if (assemblyInfoFiles == null)
            {
                throw new ArgumentNullException(nameof(assemblyInfoFiles));
            }

            var patcher = new AssemblyPatcher(sourceBase, _logger);

            PatchResult result = patcher.Patch(assemblyInfoFiles.ToList(),
                                               assemblyVersion,
                                               assemblyFileVersion,
                                               assemblyMetaData);

            patcher.SavePatchResult(result);
        }
        public void SavePatchResult(PatchResult patchResult)
        {
            string backupBasePath = BackupBasePath();

            if (!Directory.Exists(backupBasePath))
            {
                Directory.CreateDirectory(backupBasePath);
            }

            string resultFilePath = Path.Combine(backupBasePath, "Patched.txt");

            if (!File.Exists(resultFilePath))
            {
                using (File.Create(resultFilePath))
                {
                }
            }

            DeleteEmptySubDirs(new DirectoryInfo(backupBasePath));

            string json = JsonConvert.SerializeObject(patchResult.ToArray(), Formatting.Indented);

            File.WriteAllText(resultFilePath, json, Encoding.UTF8);
        }