Beispiel #1
0
        public PackOptions Clone()
        {
            var clonedObject = new PackOptions();

            clonedObject.PackageType         = new List <PackageType>(PackageType);
            clonedObject.IncludeExcludeFiles = IncludeExcludeFiles?.Clone();
            if (Mappings != null)
            {
                clonedObject.Mappings = new Dictionary <string, IncludeExcludeFiles>();
                foreach (var kvp in Mappings)
                {
                    clonedObject.Mappings.Add(kvp.Key, kvp.Value.Clone());
                }
            }
            return(clonedObject);
        }
        private static PackOptions GetPackOptions(PackageSpec packageSpec, JObject rawPackageSpec)
        {
            var rawPackOptions = rawPackageSpec.Value <JToken>(PackOptions) as JObject;

            if (rawPackOptions == null)
            {
                packageSpec.Owners = new string[] { };
                packageSpec.Tags   = new string[] { };
                return(new PackOptions
                {
                    PackageType = new PackageType[0]
                });
            }
            var owners = rawPackOptions["owners"];
            var tags   = rawPackOptions["tags"];

            packageSpec.Owners       = owners == null ? new string[0] {
            } : owners.ValueAsArray <string>();
            packageSpec.Tags         = tags == null ? new string[0] {
            } : tags.ValueAsArray <string>();
            packageSpec.ProjectUrl   = rawPackOptions.GetValue <string>("projectUrl");
            packageSpec.IconUrl      = rawPackOptions.GetValue <string>("iconUrl");
            packageSpec.Summary      = rawPackOptions.GetValue <string>("summary");
            packageSpec.ReleaseNotes = rawPackOptions.GetValue <string>("releaseNotes");
            packageSpec.LicenseUrl   = rawPackOptions.GetValue <string>("licenseUrl");

            packageSpec.RequireLicenseAcceptance = GetBoolOrFalse(rawPackOptions, "requireLicenseAcceptance", packageSpec.FilePath);

            var rawPackageType = rawPackOptions[PackageType];

            if (rawPackageType != null &&
                rawPackageType.Type != JTokenType.String &&
                (rawPackageType.Type != JTokenType.Array || // The array must be all strings.
                 rawPackageType.Type == JTokenType.Array && rawPackageType.Any(t => t.Type != JTokenType.String)) &&
                rawPackageType.Type != JTokenType.Null)
            {
                throw FileFormatException.Create(
                          string.Format(
                              CultureInfo.CurrentCulture,
                              Strings.InvalidPackageType,
                              PackageSpec.PackageSpecFileName),
                          rawPackageType,
                          packageSpec.FilePath);
            }

            IEnumerable <string> packageTypeNames;

            if (!TryGetStringEnumerableFromJArray(rawPackageType, out packageTypeNames))
            {
                packageTypeNames = Enumerable.Empty <string>();
            }

            Dictionary <string, IncludeExcludeFiles> mappings = null;
            IncludeExcludeFiles files = null;
            var rawFiles = rawPackOptions[Files] as JObject;

            if (rawFiles != null)
            {
                files = new IncludeExcludeFiles();
                if (!files.HandleIncludeExcludeFiles(rawFiles))
                {
                    files = null;
                }

                var rawMappings = rawFiles["mappings"] as JObject;

                if (rawMappings != null)
                {
                    mappings = new Dictionary <string, IncludeExcludeFiles>();
                    foreach (var pair in rawMappings)
                    {
                        var key   = pair.Key;
                        var value = pair.Value;
                        if (value.Type == JTokenType.String ||
                            value.Type == JTokenType.Array)
                        {
                            IEnumerable <string> includeFiles;
                            TryGetStringEnumerableFromJArray(value, out includeFiles);
                            var includeExcludeFiles = new IncludeExcludeFiles()
                            {
                                Include = includeFiles?.ToList()
                            };
                            mappings.Add(key, includeExcludeFiles);
                        }
                        else if (value.Type == JTokenType.Object)
                        {
                            var includeExcludeFiles = new IncludeExcludeFiles();
                            if (includeExcludeFiles.HandleIncludeExcludeFiles(value as JObject))
                            {
                                mappings.Add(key, includeExcludeFiles);
                            }
                        }
                    }
                }
            }

            return(new PackOptions
            {
                PackageType = packageTypeNames
                              .Select(name => new PackageType(name, Packaging.Core.PackageType.EmptyVersion))
                              .ToList(),
                IncludeExcludeFiles = files,
                Mappings = mappings
            });
        }