Ejemplo n.º 1
0
        public static IEnumerable<string> GetPatternsCollection(JsonObject rawProject, string projectDirectory, string projectFilePath, string propertyName, IEnumerable<string> defaultPatterns = null)
        {
            defaultPatterns = defaultPatterns ?? Enumerable.Empty<string>();

            try
            {
                if (!rawProject.Keys.Contains(propertyName))
                {
                    return CreateCollection(projectDirectory, defaultPatterns.ToArray());
                }

                var valueInString = rawProject.ValueAsString(propertyName);
                if (valueInString != null)
                {
                    return CreateCollection(projectDirectory, valueInString);
                }

                var valuesInArray = rawProject.ValueAsStringArray(propertyName);
                if (valuesInArray != null)
                {
                    return CreateCollection(projectDirectory, valuesInArray.Select(s => s.ToString()).ToArray());
                }
            }
            catch (Exception ex)
            {
                throw FileFormatException.Create(ex, rawProject.Value(propertyName), projectFilePath);
            }

            throw FileFormatException.Create("Value must be either string or array.", rawProject.Value(propertyName), projectFilePath);
        }
Ejemplo n.º 2
0
        private static List<CompilationMessage> ValueAsCompilationMessages(JsonObject obj, string key)
        {
            var messages = new List<CompilationMessage>();

            var arrayValue = obj.Value(key) as JsonArray;
            for (int i = 0; i < arrayValue.Length; i++)
            {
                var item = arrayValue[i] as JsonObject;

                var message = new CompilationMessage
                {
                    Message = item.ValueAsString(nameof(ICompilationMessage.Message)),
                    FormattedMessage = item.ValueAsString(nameof(ICompilationMessage.FormattedMessage)),
                    SourceFilePath = item.ValueAsString(nameof(ICompilationMessage.SourceFilePath)),
                    Severity = (CompilationMessageSeverity)item.ValueAsInt(nameof(ICompilationMessage.Severity)),
                    StartColumn = item.ValueAsInt(nameof(ICompilationMessage.StartColumn)),
                    StartLine = item.ValueAsInt(nameof(ICompilationMessage.StartLine)),
                    EndColumn = item.ValueAsInt(nameof(ICompilationMessage.EndColumn)),
                    EndLine = item.ValueAsInt(nameof(ICompilationMessage.EndLine)),
                };

                messages.Add(message);
            }

            return messages;
        }
Ejemplo n.º 3
0
        public static IDictionary<string, string> ReadNamedResources(JsonObject rawProject, string projectFilePath)
        {
            if (!rawProject.Keys.Contains("namedResource"))
            {
                return new Dictionary<string, string>();
            }

            var namedResourceToken = rawProject.ValueAsJsonObject("namedResource");
            if (namedResourceToken == null)
            {
                throw FileFormatException.Create("Value must be object.", rawProject.Value("namedResource"), projectFilePath);
            }

            var namedResources = new Dictionary<string, string>();

            foreach (var namedResourceKey in namedResourceToken.Keys)
            {
                var resourcePath = namedResourceToken.ValueAsString(namedResourceKey);
                if (resourcePath == null)
                {
                    throw FileFormatException.Create("Value must be string.", namedResourceToken.Value(namedResourceKey), projectFilePath);
                }

                if (resourcePath.Value.Contains("*"))
                {
                    throw FileFormatException.Create("Value cannot contain wildcards.", resourcePath, projectFilePath);
                }

                var resourceFileFullPath = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(projectFilePath), resourcePath));

                if (namedResources.ContainsKey(namedResourceKey))
                {
                    throw FileFormatException.Create(
                        string.Format("The named resource {0} already exists.", namedResourceKey),
                        resourcePath,
                        projectFilePath);
                }

                namedResources.Add(
                    namedResourceKey,
                    resourceFileFullPath);
            }

            return namedResources;
        }
Ejemplo n.º 4
0
        internal static PatternGroup Build(JsonObject rawProject,
                                           string projectDirectory,
                                           string projectFilePath,
                                           string name,
                                           string legacyName,
                                           IEnumerable<string> fallbackIncluding = null,
                                           IEnumerable<string> additionalIncluding = null,
                                           IEnumerable<string> additionalExcluding = null,
                                           bool includePatternsOnly = false,
                                           ICollection<ICompilationMessage> warnings = null)
        {
            string includePropertyName = name;

            if (!rawProject.Keys.Contains(name) &&
                legacyName != null &&
                rawProject.Keys.Contains(legacyName))
            {
                includePropertyName = legacyName;
                if (warnings != null)
                {
                    warnings.Add(new FileFormatMessage(
                        string.Format("Property \"{0}\" is deprecated. It is replaced by \"{1}\".", legacyName, name),
                        projectFilePath,
                        CompilationMessageSeverity.Warning,
                        rawProject.Value(legacyName)));
                }
            }

            additionalIncluding = additionalIncluding ?? Enumerable.Empty<string>();
            var includePatterns = PatternsCollectionHelper.GetPatternsCollection(rawProject, projectDirectory, projectFilePath, includePropertyName, defaultPatterns: fallbackIncluding)
                                                          .Concat(additionalIncluding)
                                                          .Distinct();

            if (includePatternsOnly)
            {
                return new PatternGroup(includePatterns);
            }

            additionalExcluding = additionalExcluding ?? Enumerable.Empty<string>();
            var excludePatterns = PatternsCollectionHelper.GetPatternsCollection(rawProject, projectDirectory, projectFilePath, propertyName: name + "Exclude")
                                                          .Concat(additionalExcluding)
                                                          .Distinct();

            var includeLiterals = PatternsCollectionHelper.GetPatternsCollection(rawProject, projectDirectory, projectFilePath, propertyName: name + "Files")
                                                          .Distinct();

            return new PatternGroup(includePatterns, excludePatterns, includeLiterals);
        }