Skip to content

Commit

Permalink
(GH-364) Only update the config if changed
Browse files Browse the repository at this point in the history
Do not try to write to the config file unless something has changed.
  • Loading branch information
ferventcoder committed Sep 18, 2015
1 parent 4fa6362 commit d5dd42f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@ public abstract class FilesServiceSpecsBase : TinySpec
{
protected FilesService Service;
protected IFileSystem FileSystem = new DotNetFileSystem();
protected IHashProvider HashProvider;

public override void Context()
{
Service = new FilesService(new XmlService(FileSystem), FileSystem, new CrytpoHashProvider(FileSystem, CryptoHashProviderType.Md5));
HashProvider = new CrytpoHashProvider(FileSystem, CryptoHashProviderType.Md5);
Service = new FilesService(new XmlService(FileSystem, HashProvider), FileSystem, new CrytpoHashProvider(FileSystem, CryptoHashProviderType.Md5));
}
}

Expand Down
11 changes: 9 additions & 2 deletions src/chocolatey/infrastructure/services/XmlService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ namespace chocolatey.infrastructure.services
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using cryptography;
using filesystem;
using tolerance;

Expand All @@ -28,10 +29,12 @@ namespace chocolatey.infrastructure.services
public sealed class XmlService : IXmlService
{
private readonly IFileSystem _fileSystem;
private readonly IHashProvider _hashProvider;

public XmlService(IFileSystem fileSystem)
public XmlService(IFileSystem fileSystem, IHashProvider hashProvider)
{
_fileSystem = fileSystem;
_hashProvider = hashProvider;
}

public XmlType deserialize<XmlType>(string xmlFilePath)
Expand Down Expand Up @@ -74,7 +77,11 @@ public void serialize<XmlType>(XmlType xmlType, string xmlFilePath)
textWriter.Close();
textWriter.Dispose();
_fileSystem.copy_file(xmlUpdateFilePath, xmlFilePath, overwriteExisting: true);
if (!_hashProvider.hash_file(xmlFilePath).is_equal_to(_hashProvider.hash_file(xmlUpdateFilePath)))
{
_fileSystem.copy_file(xmlUpdateFilePath, xmlFilePath, overwriteExisting: true);
}
_fileSystem.delete_file(xmlUpdateFilePath);
},
"Error serializing type {0}".format_with(typeof(XmlType)),
Expand Down

0 comments on commit d5dd42f

Please sign in to comment.