예제 #1
0
        public void TestBuildSnapAppsFromYamlString()
        {
            var snapAppsBefore = _baseFixture.BuildSnapApps();

            Assert.Single(snapAppsBefore.Apps);
            var snapAppsYamlString = _snapAppWriter.ToSnapAppsYamlString(snapAppsBefore);

            var snapAppsAfter = _snapAppReader.BuildSnapAppsFromYamlString(snapAppsYamlString);

            AssertSnapApps(snapAppsBefore, snapAppsAfter, new NugetOrgOfficialV3PackageSources());
        }
예제 #2
0
        static SnapApps BuildSnapAppsFromDirectory(
            [NotNull] ISnapFilesystem filesystem, [NotNull] ISnapAppReader reader, [NotNull] string workingDirectory)
        {
            if (filesystem == null)
            {
                throw new ArgumentNullException(nameof(filesystem));
            }
            if (reader == null)
            {
                throw new ArgumentNullException(nameof(reader));
            }
            if (workingDirectory == null)
            {
                throw new ArgumentNullException(nameof(workingDirectory));
            }

            const string snapxYamlFilename = "snapx.yml";

            var snapsFilename = filesystem.PathCombine(workingDirectory, ".snapx", snapxYamlFilename);

            if (!filesystem.FileExists(snapsFilename))
            {
                SnapLogger.Error($"Unable to find application YML definition, it does not exist: {snapsFilename}");
                goto error;
            }

            var content = filesystem.FileReadAllText(snapsFilename);

            if (string.IsNullOrWhiteSpace(content))
            {
                SnapLogger.Error($"Application yml file is empty (does not containy any YML): {snapsFilename}");
                goto error;
            }

            try
            {
                var snapApps = reader.BuildSnapAppsFromYamlString(content);
                if (snapApps == null)
                {
                    goto error;
                }

                const int expectedSchemaVersion = 1;
                if (snapApps.Schema != expectedSchemaVersion)
                {
                    throw new Exception($"Invalid schema version: {snapApps.Schema}. Expected schema version: {expectedSchemaVersion}.");
                }

                snapApps.Generic ??= new SnapAppsGeneric();
                snapApps.Apps ??= new List <SnapsApp>();
                snapApps.Channels ??= new List <SnapsChannel>();

                return(snapApps);
            }
            catch (YamlException yamlException)
            {
                var moreHelpfulExceptionMaybe = yamlException.InnerException ?? yamlException;
                SnapLogger.ErrorException($"{snapxYamlFilename} file contains incorrect yaml syntax. Error message: {moreHelpfulExceptionMaybe.Message}.", moreHelpfulExceptionMaybe);
            }
            catch (Exception e)
            {
                SnapLogger.ErrorException($"Unknown error deserializing {snapxYamlFilename}", e);
            }

error:
            return(new SnapApps());
        }