using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Web.Script.Serialization; namespace TGServerService { /// /// Repository specific information for a /// sealed class RepoConfig : IEquatable { /// /// If this json is setup to support /// public readonly bool ChangelogSupport; /// /// Path to the repository's changelog generator script /// public readonly string PathToChangelogPy; /// /// Arguments for the changelog gennerator script /// public readonly string ChangelogPyArguments; /// /// List of python pip dependencies for the changelog generator script /// public readonly IList PipDependancies = new List(); /// /// Paths to commit and push to the remote repository /// public readonly IList PathsToStage = new List(); /// /// Directory's whose contents should not be touched when the updates /// public readonly IList StaticDirectoryPaths = new List(); /// /// DLL's used by DreamDaemon call()() operations. Must be handled as symlinks to avoid lockups during update operations /// public readonly IList DLLPaths = new List(); /// /// Construct a RepoConfig /// /// Path to the config JSON to use public RepoConfig(string path) { if (!File.Exists(path)) return; var rawdata = File.ReadAllText(path); var Deserializer = new JavaScriptSerializer(); var json = Deserializer.Deserialize>(rawdata); try { var details = (IDictionary)json["changelog"]; PathToChangelogPy = (string)details["script"]; ChangelogPyArguments = (string)details["arguments"]; ChangelogSupport = true; try { PipDependancies = LoadArray(details["pip_dependancies"]); } catch { } } catch { ChangelogSupport = false; } try { PathsToStage = LoadArray(json["synchronize_paths"]); } catch { } try { StaticDirectoryPaths = LoadArray(json["static_directories"]); } catch { } try { DLLPaths = LoadArray(json["dlls"]); } catch { } } /// /// Convert an array of s to a of s /// /// The array to convert /// private static IList LoadArray(object o) { var array = (object[])o; var res = new List(); foreach (var I in array) res.Add((string)I); return res; } /// public override bool Equals(object obj) { return Equals(obj as RepoConfig); } /// /// Check if two s have the same contents /// /// The first /// The second /// if the s match, otherwise private static bool ListEquals(IList A, IList B) { return A.All(B.Contains) && A.Count == B.Count; } public bool Equals(RepoConfig other) { return ChangelogSupport == other.ChangelogSupport && PathToChangelogPy == other.PathToChangelogPy && ChangelogPyArguments == other.ChangelogPyArguments && ListEquals(PipDependancies, other.PipDependancies) && ListEquals(PathsToStage, other.PathsToStage) && ListEquals(StaticDirectoryPaths, other.StaticDirectoryPaths) && ListEquals(DLLPaths, other.DLLPaths); } public override int GetHashCode() { var hashCode = 1890628544; hashCode = hashCode * -1521134295 + ChangelogSupport.GetHashCode(); hashCode = hashCode * -1521134295 + EqualityComparer.Default.GetHashCode(PathToChangelogPy); hashCode = hashCode * -1521134295 + EqualityComparer.Default.GetHashCode(ChangelogPyArguments); hashCode = hashCode * -1521134295 + EqualityComparer>.Default.GetHashCode(PipDependancies); hashCode = hashCode * -1521134295 + EqualityComparer>.Default.GetHashCode(PathsToStage); hashCode = hashCode * -1521134295 + EqualityComparer>.Default.GetHashCode(StaticDirectoryPaths); hashCode = hashCode * -1521134295 + EqualityComparer>.Default.GetHashCode(DLLPaths); return hashCode; } public static bool operator ==(RepoConfig config1, RepoConfig config2) { return EqualityComparer.Default.Equals(config1, config2); } public static bool operator !=(RepoConfig config1, RepoConfig config2) { return !(config1 == config2); } } }