Katteker/Katteker/KattekerConfig.cs

57 lines
1.8 KiB
C#

using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
namespace Katteker
{
/// <summary>
/// Configuration of a Katteker-Deployment
/// </summary>
[DataContract]
public class KattekerConfig
{
/// <summary>
/// Publish path
/// </summary>
[DataMember]
public string Publish { get; set; }
/// <summary>
/// Name of the changelog file.
/// </summary>
[DataMember]
public string Changelog { get; set; }
/// <summary>
/// Read file and deserialize content.
/// </summary>
/// <param name="path">path of the file.</param>
/// <returns>this object</returns>
/// <exception cref="FileNotFoundException"></exception>
public static KattekerConfig ReadFromFile(string path)
{
if (!File.Exists(path)) throw new FileNotFoundException();
var dataContractJsonSerializer = new DataContractJsonSerializer(typeof(KattekerConfig));
using (var fileStream = File.OpenRead(path))
{
var obj = dataContractJsonSerializer.ReadObject(fileStream);
return (KattekerConfig) obj;
}
}
/// <summary>
/// Serialize object and write to file.
/// </summary>
/// <param name="path">path of the file.</param>
public void WriteToFile(string path)
{
if (File.Exists(path)) File.Delete(path);
using (var fileStream = File.OpenWrite(path))
{
var dataContractJsonSerializer = new DataContractJsonSerializer(typeof(KattekerConfig));
dataContractJsonSerializer.WriteObject(fileStream, this);
}
}
}
}