Exemplo n.º 1
0
        public static SemanticVersion?GetVersionFromJsonFile(UPath versionFile, IFileSystem fileSystem, ILogger logger)
        {
            try
            {
                string?path           = fileSystem.ConvertPathToInternal(versionFile);
                var    jsonFileReader = new JsonFileReader(path);

                var configurationItems = jsonFileReader.ReadConfiguration();

                const string major = nameof(major);
                const string minor = nameof(minor);
                const string patch = nameof(patch);

                var items = new Dictionary <string, int> {
                    [major] = 0, [minor] = 0, [patch] = 0
                };

                foreach (string key in items.Keys.ToArray())
                {
                    string?value = configurationItems.SingleOrDefault(pair =>
                                                                      string.Equals(pair.Key, key, StringComparison.OrdinalIgnoreCase))?.Value;

                    if (!int.TryParse(value, out int intValue) ||
                        intValue < 0)
                    {
                        throw new FormatException("Could not parse {Key} as a positive integer");
                    }

                    items[key] = intValue;
                }

                return(new SemanticVersion(items[major], items[minor], items[patch]));
            }
            catch (Exception ex) when(!ex.IsFatal())
            {
                logger.Error(ex, "Could not get version from file {VersionFile}", versionFile);
                return(null);
            }
        }