/// <summary>
        /// Configures the from file using the specified services
        /// </summary>
        /// <param name="services">The services</param>
        /// <param name="configFilePath">The config file path</param>
        public static void ConfigureFromFile(this IServiceCollection services, string?configFilePath = null)
        {
            var documentationOptions = new DocumentationOptions();

            if (!string.IsNullOrWhiteSpace(configFilePath))
            {
                Log.Logger.Debug("Loading config from file: '{ConfigFilePath}'", configFilePath);

                documentationOptions = Yaml.Deserialize <DocumentationOptions>(configFilePath);
            }
            else
            {
                Log.Logger.Verbose("No config file provided. Using default settings");
            }

            // Convert the documentation options into a list of member specific options
            // For example: Class doc options, Ctor doc options, Property...
            // In this way we can avoid passing the entire options object to each documentation
            // strategy that will only receive the portion of config relevant to them
            documentationOptions.ToList()
            .ForEach(d => services.AddSingleton(d.GetType(), d));

            // Add also the entire object
            services.AddSingleton(documentationOptions);
        }
示例#2
0
        public async Task <PackageManifest> LoadManifest(string source)
        {
            _logger.Info($"Loading package manifest from {source}");
            var text = await _fileTransferService.ReadContent(source);

            var manifest = Yaml.Deserialize <PackageManifest>(text);

            return(manifest);
        }
        public void TestConfigSerialization()
        {
            var config = new GenericRecognizer.GenericRecognizerConfig()
            {
                Profiles = new Dictionary <string, object>()
                {
                    { "TestAed", new Aed.AedConfiguration()
                      {
                          BandpassMinimum = 12345
                      } },
                    { "TestOscillation", new OscillationParameters()
                      {
                          DecibelThreshold = 123
                      } },
                    { "TestBlob", new BlobParameters()
                      {
                          BottomHertzBuffer = 456
                      } },
                    { "TestWhistle", new WhistleParameters()
                      {
                          TopHertzBuffer = 789
                      } },
                },
            };

            var target = PathHelper.GetTempFile(this.outputDirectory, ".yml");

            Yaml.Serialize(target, config);

            var lines = target.ReadAllLines();

            CollectionAssert.Contains(lines, "  TestAed: !AedParameters");
            CollectionAssert.Contains(lines, "  TestOscillation: !OscillationParameters");
            CollectionAssert.Contains(lines, "  TestBlob: !BlobParameters");
            CollectionAssert.Contains(lines, "  TestWhistle: !WhistleParameters");

            //lines.ForEach(x => Debug.WriteLine(x));

            var config2 = Yaml.Deserialize <GenericRecognizer.GenericRecognizerConfig>(target);

            Assert.IsNotNull(config2.Profiles);
            Assert.AreEqual(4, config2.Profiles.Count);
            CollectionAssert.AreEquivalent(
                new[] { "TestAed", "TestOscillation", "TestBlob", "TestWhistle" },
                config2.Profiles.Keys);

            Assert.IsInstanceOfType(config2.Profiles["TestAed"], typeof(Aed.AedConfiguration));
            Assert.IsInstanceOfType(config2.Profiles["TestOscillation"], typeof(OscillationParameters));
            Assert.IsInstanceOfType(config2.Profiles["TestBlob"], typeof(BlobParameters));
            Assert.IsInstanceOfType(config2.Profiles["TestWhistle"], typeof(WhistleParameters));

            Assert.AreEqual((config2.Profiles["TestAed"] as Aed.AedConfiguration)?.BandpassMinimum, 12345);
            Assert.AreEqual((config2.Profiles["TestOscillation"] as OscillationParameters)?.DecibelThreshold, 123);
            Assert.AreEqual((config2.Profiles["TestBlob"] as BlobParameters)?.BottomHertzBuffer, 456);
            Assert.AreEqual((config2.Profiles["TestWhistle"] as WhistleParameters)?.TopHertzBuffer, 789);
        }
        public void ConcatenateIndexFilesTestConfigFileChanges()
        {
            var indexPropertiesConfig = PathHelper.ResolveConfigFile("IndexPropertiesConfig.yml");
            var dateString            = "20160726";

            // get the default config file
            var defaultConfigFile = PathHelper.ResolveConfigFile("SpectrogramFalseColourConfig.yml");
            var config            = Yaml.Deserialize <LdSpectrogramConfig>(defaultConfigFile);

            // make changes to config file as required for test
            config.ColorMap1 = "BGN-ENT-PMN";
            config.ColorMap2 = "ACI-RNG-EVN";

            // write new config
            var testConfig = this.TestOutputDirectory.CombineFile("SpectrogramFalseColourConfig.yml");

            Yaml.Serialize(testConfig, config);

            var arguments = new ConcatenateIndexFiles.Arguments
            {
                InputDataDirectories = new[] { indonesiaIndicesDirectory },
                OutputDirectory      = this.TestOutputDirectory,
                DirectoryFilter      = "*.wav",
                FileStemName         = "Test2_Indonesia",
                StartDate            = new DateTimeOffset(2016, 07, 26, 0, 0, 0, TimeSpan.Zero),
                EndDate = new DateTimeOffset(2016, 07, 27, 0, 0, 0, TimeSpan.Zero),
                IndexPropertiesConfig        = indexPropertiesConfig.FullName,
                FalseColourSpectrogramConfig = testConfig.FullName,
                ConcatenateEverythingYouCanLayYourHandsOn = false, // 24 hour blocks only
                TimeSpanOffsetHint = TimeSpan.FromHours(8),
                DrawImages         = true,

                // following two lines can be used to add in a recognizer score track
                EventDataDirectories = null,
                EventFilePattern     = null,
            };

            ConcatenateIndexFiles.Execute(arguments);

            // Make sure files that match our config file are actually created!
            var outputDataDir = this.TestOutputDirectory.Combine(arguments.FileStemName, dateString);
            var prefix        = arguments.FileStemName + "_" + dateString + "__";

            Assert.That.FileExists(outputDataDir.CombineFile(prefix + "Towsey.Acoustic.Indices.csv"));
            Assert.That.FileNotExists(outputDataDir.CombineFile(prefix + "SummaryIndex.csv"));

            var imageFileInfo1 = outputDataDir.CombineFile(prefix + "BGN-ENT-PMN.png");

            Assert.IsTrue(imageFileInfo1.Exists);

            var imageFileInfo2 = outputDataDir.CombineFile(prefix + "ACI-RNG-EVN.png");

            Assert.IsTrue(imageFileInfo2.Exists);
        }
        public void OurDefaultDeserializerSupportsMergingDocumentsAndZio()
        {
            var wrapper = Yaml.Deserialize <YamlTestWrapperClass>(this.testDocument.ToFileEntry());

            Assert.AreEqual(this.wrapperTestCase.InfoA.SomeProperty, wrapper.InfoA.SomeProperty);
            Assert.AreEqual(this.wrapperTestCase.InfoB.SomeProperty, wrapper.InfoB.SomeProperty);
            Assert.AreEqual(this.wrapperTestCase.InfoC.SomeProperty, wrapper.InfoC.SomeProperty);
            Assert.AreEqual(this.wrapperTestCase.InfoA.TestFile, wrapper.InfoA.TestFile);
            Assert.AreEqual(this.wrapperTestCase.InfoB.TestFile, wrapper.InfoB.TestFile);
            Assert.AreEqual(this.wrapperTestCase.InfoC.TestFile, wrapper.InfoC.TestFile);
        }
示例#6
0
        public static DefinitionGraph Load(string path = Program.LockFile)
        {
            if (string.IsNullOrWhiteSpace(path))
            {
                throw new ArgumentNullException(nameof(path));
            }
            if (!File.Exists(path))
            {
                throw new FileNotFoundException("Unable to find the plugin lock file", path);
            }

            return(Yaml.Deserialize <DefinitionGraph>(File.ReadAllText(path)));
        }
示例#7
0
        /// <summary>
        /// Deserialized the specified lock file into a <see cref="DefinitionGraph" />.
        /// </summary>
        /// <param name="path">The path to the lock file.</param>
        /// <returns>The deserialized <see cref="DefinitionGraph" />.</returns>
        /// <exception cref="ArgumentNullException">A valid file path must be specified.</exception>
        /// <exception cref="FileNotFoundException">Unable to find the plugin lock file.</exception>
        public static DefinitionGraph Load(string path = ConfigurationManager.LockFile)
        {
            if (string.IsNullOrWhiteSpace(path))
            {
                throw new ArgumentNullException(nameof(path), "A valid file path must be specified.");
            }
            if (!File.Exists(path))
            {
                throw new FileNotFoundException("Unable to find the plugin lock file.", path);
            }

            return(Yaml.Deserialize <DefinitionGraph>(File.ReadAllText(path)));
        }
示例#8
0
        /// <summary>
        /// Loads a <see cref="Plugin" /> from the specified definition file.
        /// </summary>
        /// <param name="path">The path to the plugin definition file.</param>
        /// <returns>The loaded <see cref="Plugin" />.</returns>
        /// <exception cref="ArgumentNullException">path - A valid file path must be specified.</exception>
        /// <exception cref="FileNotFoundException">Unable to find the plugin definition file.</exception>
        public static Plugin Load(string path)
        {
            if (string.IsNullOrWhiteSpace(path))
            {
                throw new ArgumentNullException(nameof(path), "A valid file path must be specified.");
            }
            if (!File.Exists(path))
            {
                throw new FileNotFoundException("Unable to find the plugin definition file.", path);
            }

            return(Yaml.Deserialize <Plugin>(File.ReadAllText(path)));
        }
        public void SerializerCanDecodePrivateSetters()
        {
            YamlTestDataClass testObject;

            using (var stream = new StringReader(TestObjectYaml))
            {
                testObject = Yaml.Deserialize <YamlTestDataClass>(stream);
            }

            Assert.AreEqual("C:\\Temp\\test.tmp", testObject.TestFile);
            Assert.AreEqual("Hello world", testObject.SomeProperty);
            Assert.IsTrue(testObject.PrivateSetter.HasValue);
            Assert.AreEqual(123456, testObject.PrivateSetter.Value);
        }
        internal static Config Deserialize(TextReader streamReader, string configPath)
        {
            Config config = new Config
            {
                GenericConfig = Yaml.Deserialize <object>(streamReader),
                ConfigPath    = configPath,
            };

            ((IConfig)config).InvokeLoaded();

            Contract.EnsuresNotNull(config.GenericConfig);
            Contract.EnsuresNotNull(config.ConfigPath);
            return(config);
        }
示例#11
0
        public T Load()
        {
            T data = null;

            if (_fileSystem.FileExists(FilePath))
            {
                var content = _fileSystem.ReadAllText(FilePath);
                data = Yaml.Deserialize <T>(content);
            }

            if (data == null)
            {
                data = new T();
            }

            SetDefaults(data);

            return(data);
        }
示例#12
0
 public static T YamlToObject <T>(this string str) => Yaml.Deserialize <T>(str);
        /// <summary>
        /// Reads a config file. Supports both "dynamic" and "static" config files.
        /// Additionally dumps config files into the log as JSON on first read for experimental provenance.
        /// Additionally caches the contents of config file on read based on the fully-qualified path.
        /// All configs returned are clones of the cached copy (even the first config).
        /// </summary>
        /// <remarks>
        /// Support exists for processing recursive config file (where a <see cref="Config"/> objected is nested in another
        /// <see cref="Config"/>.
        /// </remarks>
        /// <typeparam name="T">The type to deserialize.</typeparam>
        /// <param name="path">
        /// The path to the config file to read (will be expanded with <see cref="Path.GetFullPath"/>.
        /// </param>
        /// <param name="factory">
        /// A factory used to create a new config if <typeparamref name="T"/> is exactly the type <see cref="Config"/>.
        /// </param>
        /// <returns>The config object, or a cached copy after the first call.</returns>
        private static T LoadAndCache <T>(string path, Func <T> factory)
            where T : IConfig
        {
            Contract.RequiresNotNull(path, nameof(path));
            path = Path.GetFullPath(path);

            lock (CachedProperties)
            {
                // "cache" path skips this
                if (!CachedProperties.TryGetValue(path, out var cachedConfig))
                {
                    // not cached, load, log, and cache
                    T      loadedConfig;
                    object generic;

                    // if is exactly the Config type (no sub types)
                    if (typeof(T) == typeof(Config))
                    {
                        // "untyped" config
                        Log.Trace($"Reading untyped config file `{path}`");
                        using (var file = File.OpenText(path))
                        {
                            generic = Yaml.Deserialize <object>(file);
                        }

                        loadedConfig = factory();
                    }
                    else
                    {
                        // deserialize typed config
                        Log.Trace($"Reading typed config file `{path}`");
                        (generic, loadedConfig) = Yaml.LoadAndDeserialize <T>(path);
                    }

                    // if implements Config in any subtype (more specific than IConfig)
                    if (loadedConfig is Config config)
                    {
                        config.GenericConfig = generic;
                        Contract.EnsuresNotNull(config.GenericConfig);
                    }

                    loadedConfig.ConfigPath = path;
                    Contract.EnsuresNotNull(loadedConfig.ConfigPath);

                    // dump the config in the log
                    configJsonSerializerSettings = new JsonSerializerSettings();
                    var configDump = Json.SerializeToString(loadedConfig, false, configJsonSerializerSettings);
                    NoConsole.Log.Info($"Config file `{path}` loaded:{Environment.NewLine}{configDump}");

                    // this has the potential to be recursive here if a config file loads another config file.
                    ((IConfig)loadedConfig).InvokeLoaded();

                    // cache the config (with possible nested configs)
                    CachedProperties.AddOrUpdate(path, loadedConfig, (key, existing) => loadedConfig);

                    cachedConfig = loadedConfig;
                }

                // always need to clone a copy to protect from cross-thread mutability
                return(((T)cachedConfig).DeepClone());
            }
        }
示例#14
0
        public static void Execute(Arguments arguments)
        {
            FileInfo manifestFile            = arguments.TemplateManifest;
            FileInfo functionalTemplatesFile = arguments.TemplateDefinitions;

            // Read in all template manifests
            var manifests = Yaml.Deserialize <TemplateManifest[]>(manifestFile);

            // Read current template definitions and convert to dictionary
            var arrayOfFunctionalTemplates   = Json.Deserialize <FunctionalTemplate[]>(functionalTemplatesFile);
            var dictionaryOfCurrentTemplates = DataProcessing.ConvertArrayOfFunctionalTemplatesToDictionary(arrayOfFunctionalTemplates);

            // init a new template list for output.
            var newTemplateList = new List <FunctionalTemplate>();

            // cycle through all the manifests
            foreach (var manifest in manifests)
            {
                var name = manifest.Name;
                if (!dictionaryOfCurrentTemplates.ContainsKey(name))
                {
                    // the current manifest is not an existing template - therefore make it.
                    var newTemplate = new FunctionalTemplate(manifest)
                    {
                        Template       = TemplateManifest.CreateTemplateDefinition(manifest),
                        MostRecentEdit = DateTime.Now,
                    };
                    newTemplateList.Add(newTemplate);
                    continue;
                }

                if (manifest.EditStatus == EditStatus.Edit)
                {
                    // This option edits an existing functional template in the json file. The template definition is (re)calculated.
                    // Effectively the same as creating a new template.
                    var newTemplate = new FunctionalTemplate(manifest)
                    {
                        Template       = TemplateManifest.CreateTemplateDefinition(manifest),
                        MostRecentEdit = DateTime.Now,
                    };
                    newTemplateList.Add(newTemplate);
                    continue;
                }

                if (manifest.EditStatus == EditStatus.Copy)
                {
                    // This option keeps an existing functional template unchanged.
                    var existingTemplate = dictionaryOfCurrentTemplates[name];
                    newTemplateList.Add(existingTemplate);
                    continue;
                }

                if (manifest.EditStatus == EditStatus.Ignore)
                {
                    // Do not output this template to the list of functional templates.
                    continue;
                }
            }

            var functionalTemplatesFileName = functionalTemplatesFile.Name;
            var functionalTemplatesFilePath = Path.Combine(manifestFile.DirectoryName ?? throw new InvalidOperationException(), functionalTemplatesFileName);

            // Save the previous templates file
            string backupPath = Path.Combine(manifestFile.DirectoryName, functionalTemplatesFileName + ".Backup.json");

            if (File.Exists(backupPath))
            {
                File.Delete(backupPath);
            }

            //Now copy the file first
            File.Copy(functionalTemplatesFilePath, backupPath, true);

            //Now Rename the File
            //File.Move(NewFilePath, Path.Combine(NewFileLocation, "File.txt"));

            // No need to move the backup because serializing over-writes the current templates file.
            var opTemplatesFile = new FileInfo(functionalTemplatesFilePath);

            Json.Serialise(opTemplatesFile, newTemplateList.ToArray());

            if (arguments.ListOfTestIndexFiles != null)
            {
                TestTemplates(arguments.ListOfTestIndexFiles, opTemplatesFile, arguments.ImageOfLdfcSpectrogram);
            }
        }
示例#15
0
        internal async Task <int> Main()
        {
            Console.WriteLine("This utility will walk you through setting up a brand new FiveM server with NFive installed.");
            Console.WriteLine();
            Console.WriteLine("If you already have FiveM server installed you should cancel and use `nfpm init`.");
            Console.WriteLine();
            Console.WriteLine("Press ^C at any time to quit.");
            Console.WriteLine();

            var config = new ConfigGenerator();

            config.Hostname = ParseSimple("server name", "NFive");
            var serverMaxPlayers = ParseSimple("server max players", "32");

            config.Tags       = ParseSimple("server tags (separate with space)", "nfive").Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).ToList();
            config.LicenseKey = ParseSimple("server license key (https://keymaster.fivem.net/)", "<skip>");

            Console.WriteLine();

            Directory.CreateDirectory(Path.Combine(Environment.CurrentDirectory, "resources", "nfive"));

            var definition = new Definition
            {
                Name    = "local/nfive-install",
                Version = "1.0.0"
            };

            using (var client = new WebClient())
            {
                Console.WriteLine("Finding latest FiveM Windows server version...");

                var yml = await client.DownloadStringTaskAsync("https://nfive.io/fivem/server-versions-windows.yml");

                var versions = Yaml.Deserialize <List <string> >(yml);
                var latest   = versions.First();

                Console.WriteLine($"Downloading FiveM server v{latest.Split(new[] { '-' }, 2)[0]}...");

                var data = await client.DownloadDataTaskAsync($"https://runtime.fivem.net/artifacts/fivem/build_server_windows/master/{latest}/server.zip");

                Console.WriteLine("Installing FiveM server...");

                using (var stream = new MemoryStream(data))
                    using (var zip = ZipFile.Read(stream))
                    {
                        zip.ExtractAll(Environment.CurrentDirectory, ExtractExistingFileAction.OverwriteSilently);
                    }

                Console.WriteLine();
                Console.WriteLine("Downloading NFive...");

                data = await client.DownloadDataTaskAsync("https://ci.appveyor.com/api/projects/NFive/nfive/artifacts/nfive.zip");

                Console.WriteLine("Installing NFive...");

                using (var stream = new MemoryStream(data))
                    using (var zip = ZipFile.Read(stream))
                    {
                        zip.ExtractAll(Path.Combine(Environment.CurrentDirectory, "resources", "nfive"), ExtractExistingFileAction.OverwriteSilently);
                    }
            }

            config.Serialize().Save(Path.Combine(Environment.CurrentDirectory, "server.cfg"));
            File.WriteAllText(Path.Combine(Environment.CurrentDirectory, "resources", "nfive", "nfive.yml"), Yaml.Serialize(definition));

            Console.WriteLine();
            Console.WriteLine("Installation is complete, you can now start the server with `nfpm start`!");

            return(await Task.FromResult(0));
        }
示例#16
0
        /// <summary>
        /// Gets the documents.
        /// </summary>
        /// <returns>The documents.</returns>
        internal IList <dynamic> GetDocuments()
        {
            var mt = _content.ToFrontMatterType();

            if (mt == Enums.FrontMatterType.Json)
            {
                var items = JsonConvert.DeserializeObject <List <dynamic> >(_content);

                foreach (var i in items)
                {
                    var f2 = i.ToObject <Dictionary <string, object> >();

                    if (f2.Count == 0)
                    {
                        throw new KotoriDocumentException(_identifier, "Data contains document with no meta fields.");
                    }
                }

                if (!items.Any())
                {
                    throw new KotoriDocumentException(_identifier, "Data contains no document.");
                }

                return(items.ToList());
            }

            if (mt == Enums.FrontMatterType.Yaml)
            {
                IDeserializer des = new Yaml();

                var items = _content.Split("---", StringSplitOptions.RemoveEmptyEntries).ToList();

                items.RemoveAll(x => string.IsNullOrEmpty(x.Trim()));

                if (items.Any(x => string.IsNullOrWhiteSpace(x.Replace("\r", "", StringComparison.OrdinalIgnoreCase).Replace("\n", "", StringComparison.OrdinalIgnoreCase).Replace(" ", "", StringComparison.OrdinalIgnoreCase))))
                {
                    throw new KotoriDocumentException(_identifier, "Data contains document with no meta fields.");
                }

                List <dynamic> items2 = new List <dynamic>();
                var            c      = 0;

                foreach (var i in items)
                {
                    try
                    {
                        var d = des.Deserialize(i);
                        items2.Add(d);
                    }
                    catch (Exception ex)
                    {
                        throw new KotoriDocumentException(_identifier, $"Deserialization of data document at index {c} failed with a message: {ex.Message}");
                    }

                    c++;
                }

                if (!items2.Any())
                {
                    throw new KotoriDocumentException(_identifier, "Data contains no document.");
                }

                return(items2);
            }

            throw new KotoriDocumentException(_identifier, "Data content has an unknown format.");
        }
示例#17
0
        public void read_sample_manifest()
        {
            var text = ReadAllText("Manifests\\SampleManifests\\mongodb.yaml");

            Yaml.Deserialize <PackageManifest>(text);
        }