Created
June 24, 2017 06:39
-
-
Save siennathesane/089f53ec4cfced8962eab42fed4c7c87 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using Microsoft.Extensions.Configuration; | |
| using Newtonsoft.Json; | |
| using System; | |
| using System.Collections.Generic; | |
| using System.IO; | |
| using System.Runtime.InteropServices; | |
| using Newtonsoft.Json.Serialization; | |
| using System.Reflection; | |
| using System.Linq; | |
| namespace Automata | |
| { | |
| public class WorkSpace | |
| { | |
| public Config Config { get; set; } | |
| public string key { get; set; } | |
| public string internalsPath { get; set; } | |
| public string ManifestURL { get; set; } | |
| private IConfiguration userConfig; | |
| private string userHome = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) | |
| ? Environment.GetEnvironmentVariable("USERPROFILE") | |
| : Environment.GetEnvironmentVariable("HOME"); | |
| private string settingsFile; | |
| public WorkSpace(string key, string internalsPath, string ManifestURL) | |
| { | |
| if (Environment.GetEnvironmentVariable("TEST") == "true") | |
| { | |
| this.settingsFile = Path.Combine(userHome, ".automata.test.json"); | |
| } | |
| else | |
| { | |
| this.settingsFile = Path.Combine(userHome, ".automata.json"); | |
| } | |
| this.key = key; | |
| this.internalsPath = internalsPath; | |
| this.ManifestURL = ManifestURL; | |
| this.loadUserConfig(this.settingsFile); | |
| } | |
| public WorkSpace(string key, string internalsPath, string ManifestURL, string userName, string userEmail) | |
| { | |
| if (Environment.GetEnvironmentVariable("TEST") == "true") | |
| { | |
| this.settingsFile = Path.Combine(userHome, ".automata.test.json"); | |
| } | |
| else | |
| { | |
| this.settingsFile = Path.Combine(userHome, ".automata.json"); | |
| } | |
| this.key = key; | |
| this.internalsPath = internalsPath; | |
| this.ManifestURL = ManifestURL; | |
| this.Config = new Config(userName, userEmail); | |
| this.loadUserConfig(this.settingsFile); | |
| } | |
| private void loadUserConfig(string path) | |
| { | |
| if (!File.Exists(path)) | |
| { | |
| try | |
| { | |
| var newFile = File.Create(path); | |
| newFile.Dispose(); | |
| } | |
| catch (IOException ioe) | |
| { | |
| Console.WriteLine("Cannot create new settings file at {0} with error: {1}", new List<string> { path, ioe.ToString() }); | |
| } | |
| } | |
| try | |
| { | |
| this.userConfig = new ConfigurationBuilder() | |
| .AddJsonFile( | |
| Path.Combine( | |
| this.userHome, | |
| (Environment.GetEnvironmentVariable("TEST") == "true") | |
| ? ".automata.json" | |
| : ".automata.test.json"), | |
| optional: false) | |
| .SetBasePath(this.userHome) | |
| .Build(); | |
| } | |
| catch (FileNotFoundException fne) | |
| { | |
| var newFile = File.Create(path); | |
| newFile.Dispose(); | |
| } | |
| } | |
| public bool Save() | |
| { | |
| string configFile = File.ReadAllText(this.settingsFile); | |
| var fileWorkSpaceObj = JsonConvert.DeserializeObject<IConfiguration>(configFile); | |
| this.MergeWorkSpaceObjs<IConfiguration>(userConfig, fileWorkSpaceObj); | |
| var targetConfig = JsonConvert.SerializeObject(userConfig, | |
| Formatting.Indented, | |
| new JsonSerializerSettings | |
| { | |
| ContractResolver = new CamelCasePropertyNamesContractResolver(), | |
| }); | |
| try | |
| { | |
| File.WriteAllText(this.settingsFile, targetConfig); | |
| } | |
| catch (IOException ioe) | |
| { | |
| Console.WriteLine("Cannot save WorkSpace config. Error: {0}", ioe); | |
| return false; | |
| } | |
| return true; | |
| } | |
| public void MergeWorkSpaceObjs<T>(T source, T target) | |
| { | |
| Type t = typeof(T); | |
| var properties = t.GetProperties().Where(prop => | |
| prop != null && | |
| prop.CanRead && | |
| prop.CanWrite); | |
| foreach (var prop in properties) | |
| { | |
| var value = prop.GetValue(source, null); | |
| if (value != null) | |
| { | |
| prop.SetValue(target, value); | |
| } | |
| } | |
| var fields = t.GetFields(); | |
| foreach (var fi in fields) | |
| { | |
| var targetField = t.GetField(fi.Name); | |
| targetField.SetValue(target, fi.GetValue(source)); | |
| } | |
| } | |
| } | |
| public class Config | |
| { | |
| public string User { get; set; } | |
| public string Email { get; set; } | |
| public string UserHome | |
| { | |
| set | |
| { | |
| this.UserHome = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) | |
| ? Environment.GetEnvironmentVariable("USERPROFILE") | |
| : Environment.GetEnvironmentVariable("HOME"); | |
| } | |
| get { return this.UserHome; } | |
| } | |
| public Config(string user, string email) | |
| { | |
| this.User = user; | |
| this.Email = email; | |
| } | |
| } | |
| public class Parent | |
| { | |
| public WorkSpace ws; | |
| public string Location; | |
| public List<Child> Children; | |
| public bool Update; | |
| // TODO add repo here. | |
| } | |
| public class Child | |
| { | |
| public Parent parent; | |
| public string RelativePath; | |
| // TODO add Repo here. | |
| } | |
| public class UserConfigs | |
| { | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment