private ServiceConfiguration CreateServiceConfiguration(JsonData modelNode, JsonData serviceVersions, string serviceDirectoryPath, string serviceModelFileName)
        {
            var modelFullPath = Path.Combine(serviceDirectoryPath, serviceModelFileName);

            JsonData metadata = JsonMapper.ToObject(File.ReadAllText(modelFullPath))[ServiceModel.MetadataKey];

            // A new config that the api generates from
            var modelName = Path.GetFileName(serviceDirectoryPath);
            var config    = new ServiceConfiguration
            {
                ModelName                = modelName,
                ModelPath                = modelFullPath,
                Namespace                = Utils.JsonDataToString(modelNode[ModelsSectionKeys.NamespaceKey]), // Namespace of the service if it's different from basename
                ClassNameOverride        = Utils.JsonDataToString(modelNode[ModelsSectionKeys.BaseNameKey]),
                DefaultRegion            = Utils.JsonDataToString(modelNode[ModelsSectionKeys.DefaultRegionKey]),
                GenerateConstructors     = modelNode[ModelsSectionKeys.GenerateClientConstructorsKey] == null || (bool)modelNode[ModelsSectionKeys.GenerateClientConstructorsKey], // A way to prevent generating basic constructors
                SupportedMobilePlatforms = modelNode[ModelsSectionKeys.PlatformsKey] == null ? new List <string>() : (from object pcf in modelNode[ModelsSectionKeys.PlatformsKey]
                                                                                                                      select pcf.ToString()).ToList(),
                EnableXamarinComponent = modelNode.PropertyNames.Contains(ModelsSectionKeys.EnableXamarinComponent) && (bool)modelNode[ModelsSectionKeys.EnableXamarinComponent]
            };

            if (modelNode[ModelsSectionKeys.PclVariantsKey] != null)
            {
                config.PclVariants = (from object pcf in modelNode[ModelsSectionKeys.PclVariantsKey]
                                      select pcf.ToString()).ToList();
            }

            if (modelNode[ModelsSectionKeys.NugetPackageTitleSuffix] != null)
            {
                config.NugetPackageTitleSuffix = modelNode[ModelsSectionKeys.NugetPackageTitleSuffix].ToString();
            }


            if (modelNode[ModelsSectionKeys.ReferenceDependenciesKey] != null)
            {
                config.ReferenceDependencies = new Dictionary <string, List <Dependency> >();
                foreach (KeyValuePair <string, JsonData> kvp in modelNode[ModelsSectionKeys.ReferenceDependenciesKey])
                {
                    var platformDependencies = new List <Dependency>();
                    foreach (JsonData item in kvp.Value)
                    {
                        var platformDependency = new Dependency
                        {
                            Name     = item[ModelsSectionKeys.DependencyNameKey].ToString(),
                            Version  = item.PropertyNames.Contains(ModelsSectionKeys.DependencyVersionKey) ? item[ModelsSectionKeys.DependencyVersionKey].ToString() : "0.0.0.0",
                            HintPath = item[ModelsSectionKeys.DependencyHintPathKey].ToString(),
                        };
                        platformDependencies.Add(platformDependency);
                    }
                    config.ReferenceDependencies.Add(kvp.Key, platformDependencies);
                }
            }

            if (modelNode[ModelsSectionKeys.NugetDependenciesKey] != null)
            {
                config.NugetDependencies = new Dictionary <string, List <Dependency> >();
                foreach (KeyValuePair <string, JsonData> kvp in modelNode[ModelsSectionKeys.NugetDependenciesKey])
                {
                    var nugetDependencies = new List <Dependency>();
                    foreach (JsonData item in kvp.Value)
                    {
                        var nugetDependency = new Dependency
                        {
                            Name    = item[ModelsSectionKeys.DependencyNameKey].ToString(),
                            Version = item[ModelsSectionKeys.DependencyVersionKey].ToString(),
                        };
                        nugetDependencies.Add(nugetDependency);
                    }
                    config.NugetDependencies.Add(kvp.Key, nugetDependencies);
                }
            }

            config.Tags = new List <string>();
            if (modelNode[ModelsSectionKeys.TagsKey] != null)
            {
                foreach (JsonData tag in modelNode[ModelsSectionKeys.TagsKey])
                {
                    config.Tags.Add(tag.ToString());
                }
            }

            // Provides a way to specify a customizations file rather than using a generated one
            config.CustomizationsPath = modelNode[ModelsSectionKeys.CustomizationFileKey] == null
                ? DetermineCustomizationsPath(config.ServiceDirectoryName)
                : Path.Combine(serviceDirectoryPath, modelNode[ModelsSectionKeys.CustomizationFileKey].ToString());

            if (modelNode[ModelsSectionKeys.MaxRetriesKey] != null && modelNode[ModelsSectionKeys.MaxRetriesKey].IsInt)
            {
                config.OverrideMaxRetries = Convert.ToInt32(modelNode[ModelsSectionKeys.MaxRetriesKey].ToString());
            }

            if (modelNode[ModelsSectionKeys.SynopsisKey] != null)
            {
                config.Synopsis = (string)modelNode[ModelsSectionKeys.SynopsisKey];
            }

            if (modelNode[ModelsSectionKeys.CoreCLRSupportKey] != null)
            {
                config.CoreCLRSupport = (bool)modelNode[ModelsSectionKeys.CoreCLRSupportKey];
            }
            else
            {
                config.CoreCLRSupport = true;
            }

            config.ServiceDependencies = new Dictionary <string, string>(StringComparer.Ordinal);
            if (modelNode[ModelsSectionKeys.DependenciesKey] != null && modelNode[ModelsSectionKeys.DependenciesKey].IsArray)
            {
                foreach (var d in modelNode[ModelsSectionKeys.DependenciesKey])
                {
                    config.ServiceDependencies.Add(d.ToString(), null);
                }
            }

            if (modelNode[ModelsSectionKeys.UsePclProjectDependenciesKey] != null && modelNode[ModelsSectionKeys.UsePclProjectDependenciesKey].IsBoolean)
            {
                config.UsePclProjectDependencies = bool.Parse(modelNode[ModelsSectionKeys.UsePclProjectDependenciesKey].ToString());
            }
            else
            {
                config.UsePclProjectDependencies = false;
            }

            if (modelNode[ModelsSectionKeys.LicenseUrlKey] != null && modelNode[ModelsSectionKeys.LicenseUrlKey].IsString)
            {
                config.LicenseUrl = modelNode[ModelsSectionKeys.LicenseUrlKey].ToString();
                config.RequireLicenseAcceptance = true;
            }
            else
            {
                config.LicenseUrl = ApacheLicenseURL;
            }

            var serviceName     = config.ServiceNameRoot;
            var versionInfoJson = serviceVersions[serviceName];

            if (versionInfoJson != null)
            {
                var dependencies = versionInfoJson["Dependencies"];
                foreach (var name in dependencies.PropertyNames)
                {
                    var version = dependencies[name].ToString();
                    config.ServiceDependencies[name] = version;
                }


                var versionText = versionInfoJson["Version"].ToString();
                config.ServiceFileVersion = versionText;

                var assemblyVersionOverride = versionInfoJson["AssemblyVersionOverride"];
                if (assemblyVersionOverride != null)
                {
                    config.ServiceAssemblyVersionOverride = assemblyVersionOverride.ToString();
                }

                if (versionInfoJson["InPreview"] != null && (bool)versionInfoJson["InPreview"])
                {
                    config.InPreview = true;
                }
                else
                {
                    config.InPreview = this.DefaultToPreview;
                }
            }
            else
            {
                config.ServiceDependencies["Core"] = CoreFileVersion;
                var versionTokens = CoreVersion.Split('.');
                config.ServiceFileVersion = string.Format("{0}.{1}.0.0", versionTokens[0], versionTokens[1]);
                config.InPreview          = this.DefaultToPreview;
            }

            return(config);
        }
Beispiel #2
0
        /// <summary>
        /// Parses the service configuration metadata from the supplied manifest document,
        /// fixing up file references to the actual service model and customization files.
        /// Sets the ServiceConfigurations member on exit with the collection of loaded
        /// configurations.
        /// </summary>
        /// <param name="document"></param>
        /// <param name="modelsFolder"></param>
        void LoadServiceConfigurations(JsonData manifest, string coreVersion, JsonData versions, string modelsFolder)
        {
            var serviceConfigurations = new List <ServiceConfiguration>();

            var modelsNode = manifest[ModelsSectionKeys.ModelsKey];

            foreach (JsonData modelNode in modelsNode)
            {
                var activeNode = modelNode[ModelsSectionKeys.ActiveKey];
                if (activeNode != null && activeNode.IsBoolean && !(bool)activeNode) // skip models with active set to false
                {
                    continue;
                }

                // A new config that the api generates from
                var modelName = modelNode[ModelsSectionKeys.ModelKey].ToString();
                var config    = new ServiceConfiguration
                {
                    ModelName                 = modelName,
                    ModelPath                 = DetermineModelPath(modelName, modelsFolder),                                                                     // Path to the file servicename-*-.normal.json
                    Namespace                 = modelNode[ModelsSectionKeys.NamespaceKey] != null ? modelNode[ModelsSectionKeys.NamespaceKey].ToString() : null, // Namespace of the service if it's different from basename
                    LockedApiVersion          = modelNode[ModelsSectionKeys.LockedApiVersionKey] != null ? modelNode[ModelsSectionKeys.LockedApiVersionKey].ToString() : null,
                    BaseName                  = modelNode[ModelsSectionKeys.BaseNameKey].ToString(),                                                             // The name that is used as the client name and base request name
                    RegionLookupName          = modelNode[ModelsSectionKeys.RegionLookupNameKey].ToString(),
                    AuthenticationServiceName = modelNode[ModelsSectionKeys.AuthenticationServiceNameKey] != null ? modelNode[ModelsSectionKeys.AuthenticationServiceNameKey].ToString() : null,
                    ServiceUrl                = modelNode[ModelsSectionKeys.ServiceUrlKey] != null ? modelNode[ModelsSectionKeys.ServiceUrlKey].ToString() : null,
                    DefaultRegion             = modelNode[ModelsSectionKeys.DefaultRegionKey] != null ? modelNode[ModelsSectionKeys.DefaultRegionKey].ToString() : null,
                    GenerateConstructors      = modelNode[ModelsSectionKeys.GenerateClientConstructorsKey] == null || (bool)modelNode[ModelsSectionKeys.GenerateClientConstructorsKey], // A way to prevent generating basic constructors
                    SupportedMobilePlatforms  = modelNode[ModelsSectionKeys.PlatformsKey] == null ? new List <string>() : (from object pcf in modelNode[ModelsSectionKeys.PlatformsKey]
                                                                                                                           select pcf.ToString()).ToList(),
                    EnableXamarinComponent = modelNode.PropertyNames.Contains(ModelsSectionKeys.EnableXamarinComponent) && (bool)modelNode[ModelsSectionKeys.EnableXamarinComponent]
                };

                if (modelNode[ModelsSectionKeys.PclVariantsKey] != null)
                {
                    config.PclVariants = (from object pcf in modelNode[ModelsSectionKeys.PclVariantsKey]
                                          select pcf.ToString()).ToList();
                }

                if (modelNode[ModelsSectionKeys.NugetPackageTitleSuffix] != null)
                {
                    config.NugetPackageTitleSuffix = modelNode[ModelsSectionKeys.NugetPackageTitleSuffix].ToString();
                }


                if (modelNode[ModelsSectionKeys.ReferenceDependenciesKey] != null)
                {
                    config.ReferenceDependencies = new Dictionary <string, List <Dependency> >();
                    foreach (KeyValuePair <string, JsonData> kvp in modelNode[ModelsSectionKeys.ReferenceDependenciesKey])
                    {
                        var platformDependencies = new List <Dependency>();
                        foreach (JsonData item in kvp.Value)
                        {
                            var platformDependency = new Dependency
                            {
                                Name     = item[ModelsSectionKeys.DependencyNameKey].ToString(),
                                Version  = item.PropertyNames.Contains(ModelsSectionKeys.DependencyVersionKey) ? item[ModelsSectionKeys.DependencyVersionKey].ToString() : "0.0.0.0",
                                HintPath = item[ModelsSectionKeys.DependencyHintPathKey].ToString(),
                            };
                            platformDependencies.Add(platformDependency);
                        }
                        config.ReferenceDependencies.Add(kvp.Key, platformDependencies);
                    }
                }

                if (modelNode[ModelsSectionKeys.NugetDependenciesKey] != null)
                {
                    config.NugetDependencies = new Dictionary <string, List <Dependency> >();
                    foreach (KeyValuePair <string, JsonData> kvp in modelNode[ModelsSectionKeys.NugetDependenciesKey])
                    {
                        var nugetDependencies = new List <Dependency>();
                        foreach (JsonData item in kvp.Value)
                        {
                            var nugetDependency = new Dependency
                            {
                                Name    = item[ModelsSectionKeys.DependencyNameKey].ToString(),
                                Version = item[ModelsSectionKeys.DependencyVersionKey].ToString(),
                            };
                            nugetDependencies.Add(nugetDependency);
                        }
                        config.NugetDependencies.Add(kvp.Key, nugetDependencies);
                    }
                }

                config.Tags = new List <string>();
                if (modelNode[ModelsSectionKeys.TagsKey] != null)
                {
                    foreach (JsonData tag in modelNode[ModelsSectionKeys.TagsKey])
                    {
                        config.Tags.Add(tag.ToString());
                    }
                }

                // Provides a way to specify a customizations file rather than using a generated one
                config.CustomizationsPath = modelNode[ModelsSectionKeys.CustomizationFileKey] == null
                    ? DetermineCustomizationsPath(modelNode[ModelsSectionKeys.ModelKey].ToString())
                    : Path.Combine(modelsFolder, modelNode[ModelsSectionKeys.CustomizationFileKey].ToString());

                if (modelNode[ModelsSectionKeys.AppendServiceKey] != null && (bool)modelNode[ModelsSectionKeys.AppendServiceKey])
                {
                    config.BaseName += "Service";
                }

                if (modelNode[ModelsSectionKeys.MaxRetriesKey] != null && modelNode[ModelsSectionKeys.MaxRetriesKey].IsInt)
                {
                    config.OverrideMaxRetries = Convert.ToInt32(modelNode[ModelsSectionKeys.MaxRetriesKey].ToString());
                }

                if (modelNode[ModelsSectionKeys.SynopsisKey] != null)
                {
                    config.Synopsis = (string)modelNode[ModelsSectionKeys.SynopsisKey];
                }

                if (modelNode[ModelsSectionKeys.CoreCLRSupportKey] != null)
                {
                    config.CoreCLRSupport = (bool)modelNode[ModelsSectionKeys.CoreCLRSupportKey];
                }
                else
                {
                    config.CoreCLRSupport = true;
                }

                config.ServiceDependencies = new Dictionary <string, string>(StringComparer.Ordinal);
                if (modelNode[ModelsSectionKeys.DependenciesKey] != null && modelNode[ModelsSectionKeys.DependenciesKey].IsArray)
                {
                    foreach (var d in modelNode[ModelsSectionKeys.DependenciesKey])
                    {
                        config.ServiceDependencies.Add(d.ToString(), null);
                    }
                }

                if (modelNode[ModelsSectionKeys.UsePclProjectDependenciesKey] != null && modelNode[ModelsSectionKeys.UsePclProjectDependenciesKey].IsBoolean)
                {
                    config.UsePclProjectDependencies = bool.Parse(modelNode[ModelsSectionKeys.UsePclProjectDependenciesKey].ToString());
                }
                else
                {
                    config.UsePclProjectDependencies = false;
                }

                if (modelNode[ModelsSectionKeys.LicenseUrlKey] != null && modelNode[ModelsSectionKeys.LicenseUrlKey].IsString)
                {
                    config.LicenseUrl = modelNode[ModelsSectionKeys.LicenseUrlKey].ToString();
                    config.RequireLicenseAcceptance = true;
                }
                else
                {
                    config.LicenseUrl = ApacheLicenseURL;
                }

                var serviceName     = config.ServiceNameRoot;
                var versionInfoJson = versions[serviceName];
                if (versionInfoJson != null)
                {
                    var dependencies = versionInfoJson["Dependencies"];
                    foreach (var name in dependencies.PropertyNames)
                    {
                        var version = dependencies[name].ToString();
                        config.ServiceDependencies[name] = version;
                    }


                    var versionText = versionInfoJson["Version"].ToString();
                    config.ServiceFileVersion = versionText;

                    if (versionInfoJson["InPreview"] != null && (bool)versionInfoJson["InPreview"])
                    {
                        config.InPreview = true;
                    }
                    else
                    {
                        config.InPreview = this.DefaultToPreview;
                    }
                }
                else
                {
                    config.ServiceDependencies["Core"] = coreVersion;
                    var versionTokens = coreVersion.Split('.');
                    config.ServiceFileVersion = string.Format("{0}.{1}.0.0", versionTokens[0], versionTokens[1]);
                    config.InPreview          = this.DefaultToPreview;
                }

                // The parent model for current model, if set, the client will be generated
                // in the same namespace and share common types.
                var parentModelName = modelNode[ModelsSectionKeys.ParentBaseNameKey] != null ? modelNode[ModelsSectionKeys.ParentBaseNameKey].ToString() : null;
                if (parentModelName != null)
                {
                    try
                    {
                        config.ParentConfig = serviceConfigurations.Single(c => c.BaseName.Equals(parentModelName));
                    }
                    catch (KeyNotFoundException exception)
                    {
                        // Note : the parent model should be defined in the manifest before being referred by a child model
                        throw new KeyNotFoundException(
                                  string.Format("A parent model with name {0} is not defined in the manifest", parentModelName),
                                  exception);;
                    }
                }

                serviceConfigurations.Add(config);
            }

            ServiceConfigurations = serviceConfigurations
                                    .OrderBy(sc => sc.ServiceDependencies.Count)
                                    .ToList();
            //ServiceVersions = serviceVersions;
        }
Beispiel #3
0
        private ServiceConfiguration CreateServiceConfiguration(JsonData modelNode, JsonData serviceVersions, string serviceDirectoryPath, string serviceModelFileName, string servicePaginatorsFileName)
        {
            var modelFullPath      = Path.Combine(serviceDirectoryPath, serviceModelFileName);
            var paginatorsFullPath = Path.Combine(serviceDirectoryPath, servicePaginatorsFileName);

            JsonData metadata = JsonMapper.ToObject(File.ReadAllText(modelFullPath))[ServiceModel.MetadataKey];

            // A new config that the api generates from
            var modelName = Path.GetFileName(serviceDirectoryPath);
            var config    = new ServiceConfiguration
            {
                ModelName            = modelName,
                ModelPath            = modelFullPath,
                PaginatorsPath       = paginatorsFullPath,
                Namespace            = Utils.JsonDataToString(modelNode[ModelsSectionKeys.NamespaceKey]), // Namespace of the service if it's different from basename
                ClassNameOverride    = Utils.JsonDataToString(modelNode[ModelsSectionKeys.BaseNameKey]),
                DefaultRegion        = Utils.JsonDataToString(modelNode[ModelsSectionKeys.DefaultRegionKey]),
                GenerateConstructors = modelNode[ModelsSectionKeys.GenerateClientConstructorsKey] == null || (bool)modelNode[ModelsSectionKeys.GenerateClientConstructorsKey], // A way to prevent generating basic constructors
                IsTestService        = modelNode[ModelsSectionKeys.TestServiceKey] != null && (bool)modelNode[ModelsSectionKeys.TestServiceKey]
            };

            if (modelNode[ModelsSectionKeys.NugetPackageTitleSuffix] != null)
            {
                config.NugetPackageTitleSuffix = modelNode[ModelsSectionKeys.NugetPackageTitleSuffix].ToString();
            }


            if (modelNode[ModelsSectionKeys.ReferenceDependenciesKey] != null)
            {
                config.ReferenceDependencies = new Dictionary <string, List <Dependency> >();
                foreach (KeyValuePair <string, JsonData> kvp in modelNode[ModelsSectionKeys.ReferenceDependenciesKey])
                {
                    var platformDependencies = new List <Dependency>();
                    foreach (JsonData item in kvp.Value)
                    {
                        var platformDependency = new Dependency
                        {
                            Name     = item[ModelsSectionKeys.DependencyNameKey].ToString(),
                            Version  = item.PropertyNames.Contains(ModelsSectionKeys.DependencyVersionKey) ? item[ModelsSectionKeys.DependencyVersionKey].ToString() : "0.0.0.0",
                            HintPath = item[ModelsSectionKeys.DependencyHintPathKey].ToString(),
                        };
                        platformDependencies.Add(platformDependency);
                    }
                    config.ReferenceDependencies.Add(kvp.Key, platformDependencies);
                }
            }

            if (modelNode[ModelsSectionKeys.NugetDependenciesKey] != null)
            {
                config.NugetDependencies = new Dictionary <string, List <Dependency> >();
                foreach (KeyValuePair <string, JsonData> kvp in modelNode[ModelsSectionKeys.NugetDependenciesKey])
                {
                    var nugetDependencies = new List <Dependency>();
                    foreach (JsonData item in kvp.Value)
                    {
                        var nugetDependency = new Dependency
                        {
                            Name    = item[ModelsSectionKeys.DependencyNameKey].ToString(),
                            Version = item[ModelsSectionKeys.DependencyVersionKey].ToString(),
                        };
                        nugetDependencies.Add(nugetDependency);
                    }
                    config.NugetDependencies.Add(kvp.Key, nugetDependencies);
                }
            }

            config.Tags = new List <string>();
            if (modelNode[ModelsSectionKeys.TagsKey] != null)
            {
                foreach (JsonData tag in modelNode[ModelsSectionKeys.TagsKey])
                {
                    config.Tags.Add(tag.ToString());
                }
            }

            // Provides a way to specify a customizations file rather than using a generated one
            config.CustomizationsPath = modelNode[ModelsSectionKeys.CustomizationFileKey] == null
                ? DetermineCustomizationsPath(config.ServiceDirectoryName)
                : Path.Combine(serviceDirectoryPath, modelNode[ModelsSectionKeys.CustomizationFileKey].ToString());

            if (modelNode[ModelsSectionKeys.MaxRetriesKey] != null && modelNode[ModelsSectionKeys.MaxRetriesKey].IsInt)
            {
                config.OverrideMaxRetries = Convert.ToInt32(modelNode[ModelsSectionKeys.MaxRetriesKey].ToString());
            }

            if (modelNode[ModelsSectionKeys.SynopsisKey] != null)
            {
                config.Synopsis = (string)modelNode[ModelsSectionKeys.SynopsisKey];
            }

            if (modelNode[ModelsSectionKeys.NetStandardSupportKey] != null)
            {
                config.NetStandardSupport = (bool)modelNode[ModelsSectionKeys.NetStandardSupportKey];
            }
            else
            {
                config.NetStandardSupport = true;
            }

            config.ServiceDependencies = new Dictionary <string, string>(StringComparer.Ordinal);
            if (modelNode[ModelsSectionKeys.DependenciesKey] != null && modelNode[ModelsSectionKeys.DependenciesKey].IsArray)
            {
                foreach (var d in modelNode[ModelsSectionKeys.DependenciesKey])
                {
                    config.ServiceDependencies.Add(d.ToString(), null);
                }
            }

            if (modelNode[ModelsSectionKeys.LicenseUrlKey] != null && modelNode[ModelsSectionKeys.LicenseUrlKey].IsString)
            {
                config.LicenseUrl = modelNode[ModelsSectionKeys.LicenseUrlKey].ToString();
                config.RequireLicenseAcceptance = true;
            }
            else
            {
                config.LicenseUrl = ApacheLicenseURL;
            }

            var serviceName     = config.ServiceNameRoot;
            var versionInfoJson = serviceVersions[serviceName];

            if (versionInfoJson != null)
            {
                var dependencies = versionInfoJson["Dependencies"];
                foreach (var name in dependencies.PropertyNames)
                {
                    var version = dependencies[name].ToString();
                    config.ServiceDependencies[name] = version;
                }


                var versionText = versionInfoJson["Version"].ToString();
                config.ServiceFileVersion = versionText;

                var assemblyVersionOverride = versionInfoJson["AssemblyVersionOverride"];
                if (assemblyVersionOverride != null)
                {
                    config.ServiceAssemblyVersionOverride = assemblyVersionOverride.ToString();
                }

                if (versionInfoJson["InPreview"] != null && (bool)versionInfoJson["InPreview"])
                {
                    config.InPreview = true;
                }
                else
                {
                    config.InPreview = this.DefaultToPreview;
                }
            }
            else
            {
                config.ServiceDependencies["Core"] = CoreFileVersion;
                config.InPreview = this.DefaultToPreview;

                config.ServiceFileVersion = DefaultAssemblyVersion;
                var versionTokens = CoreVersion.Split('.');
                if (!DefaultAssemblyVersion.StartsWith($"{versionTokens[0]}.{versionTokens[1]}"))
                {
                    throw new NotImplementedException($"{nameof(DefaultAssemblyVersion)} should be updated to match the AWSSDK.Core minor version number.");
                }
            }

            return(config);
        }
Beispiel #4
0
        /// <summary>
        /// Parses the service configuration metadata from the supplied manifest document,
        /// fixing up file references to the actual service model and customization files.
        /// Sets the ServiceConfigurations member on exit with the collection of loaded
        /// configurations.
        /// </summary>
        /// <param name="document"></param>
        /// <param name="modelsFolder"></param>
        void LoadServiceConfigurations(JsonData manifest, string coreVersion, JsonData versions, string modelsFolder)
        {
            var serviceConfigurations = new List<ServiceConfiguration>();

            var modelsNode = manifest[ModelsSectionKeys.ModelsKey];
            foreach (JsonData modelNode in modelsNode)
            {
                var activeNode = modelNode[ModelsSectionKeys.ActiveKey];
                if (activeNode != null && activeNode.IsBoolean && !(bool)activeNode) // skip models with active set to false
                    continue;

                // A new config that the api generates from
                var modelName = modelNode[ModelsSectionKeys.ModelKey].ToString();
                var config = new ServiceConfiguration
                {
                    ModelName = modelName,
                    ModelPath = DetermineModelPath(modelName, modelsFolder), // Path to the file servicename-*-.normal.json
                    Namespace = modelNode[ModelsSectionKeys.NamespaceKey] != null ? modelNode[ModelsSectionKeys.NamespaceKey].ToString() : null, // Namespace of the service if it's different from basename
                    LockedApiVersion = modelNode[ModelsSectionKeys.LockedApiVersionKey] != null ? modelNode[ModelsSectionKeys.LockedApiVersionKey].ToString() : null,
                    BaseName = modelNode[ModelsSectionKeys.BaseNameKey].ToString(), // The name that is used as the client name and base request name
                    RegionLookupName = modelNode[ModelsSectionKeys.RegionLookupNameKey].ToString(),
                    AuthenticationServiceName = modelNode[ModelsSectionKeys.AuthenticationServiceNameKey] != null ? modelNode[ModelsSectionKeys.AuthenticationServiceNameKey].ToString() : null,
                    ServiceUrl = modelNode[ModelsSectionKeys.ServiceUrlKey] != null ? modelNode[ModelsSectionKeys.ServiceUrlKey].ToString() : null,
                    DefaultRegion = modelNode[ModelsSectionKeys.DefaultRegionKey] != null ? modelNode[ModelsSectionKeys.DefaultRegionKey].ToString() : null,
                    GenerateConstructors = modelNode[ModelsSectionKeys.GenerateClientConstructorsKey] == null || (bool)modelNode[ModelsSectionKeys.GenerateClientConstructorsKey], // A way to prevent generating basic constructors
                    SupportedMobilePlatforms = modelNode[ModelsSectionKeys.PlatformsKey] == null ? new List<string>() : (from object pcf in modelNode[ModelsSectionKeys.PlatformsKey]
                                                                                                                         select pcf.ToString()).ToList(),
                    EnableXamarinComponent = modelNode.PropertyNames.Contains(ModelsSectionKeys.EnableXamarinComponent) && (bool)modelNode[ModelsSectionKeys.EnableXamarinComponent]
                };

                if (modelNode[ModelsSectionKeys.PclVariantsKey] != null)
                {
                    config.PclVariants = (from object pcf in modelNode[ModelsSectionKeys.PclVariantsKey]
                     select pcf.ToString()).ToList();
                }

                if (modelNode[ModelsSectionKeys.NugetPackageTitleSuffix] != null)
                    config.NugetPackageTitleSuffix = modelNode[ModelsSectionKeys.NugetPackageTitleSuffix].ToString();


                if (modelNode[ModelsSectionKeys.ReferenceDependenciesKey] != null)
                {
                    config.ReferenceDependencies = new Dictionary<string, List<Dependency>>();
                    foreach (KeyValuePair<string, JsonData> kvp in modelNode[ModelsSectionKeys.ReferenceDependenciesKey])
                    {
                        var platformDependencies = new List<Dependency>();
                        foreach (JsonData item in kvp.Value)
                        {
                            var platformDependency = new Dependency
                            {
                                Name = item[ModelsSectionKeys.DependencyNameKey].ToString(),
                                Version = item.PropertyNames.Contains(ModelsSectionKeys.DependencyVersionKey) ? item[ModelsSectionKeys.DependencyVersionKey].ToString() : "0.0.0.0",
                                HintPath = item[ModelsSectionKeys.DependencyHintPathKey].ToString(),
                            };
                            platformDependencies.Add(platformDependency);
                        }
                        config.ReferenceDependencies.Add(kvp.Key, platformDependencies);
                    }
                }

                if (modelNode[ModelsSectionKeys.NugetDependenciesKey] != null)
                {
                    config.NugetDependencies = new Dictionary<string, List<Dependency>>();
                    foreach (KeyValuePair<string, JsonData> kvp in modelNode[ModelsSectionKeys.NugetDependenciesKey])
                    {
                        var nugetDependencies = new List<Dependency>();
                        foreach (JsonData item in kvp.Value)
                        {
                            var nugetDependency = new Dependency
                            {
                                Name = item[ModelsSectionKeys.DependencyNameKey].ToString(),
                                Version = item[ModelsSectionKeys.DependencyVersionKey].ToString(),
                            };
                            nugetDependencies.Add(nugetDependency);
                        }
                        config.NugetDependencies.Add(kvp.Key, nugetDependencies);
                    }
                }

                config.Tags = new List<string>();
                if (modelNode[ModelsSectionKeys.TagsKey] != null)
                {
                    foreach (JsonData tag in modelNode[ModelsSectionKeys.TagsKey])
                    {
                        config.Tags.Add(tag.ToString());
                    }
                }

                // Provides a way to specify a customizations file rather than using a generated one
                config.CustomizationsPath = modelNode[ModelsSectionKeys.CustomizationFileKey] == null
                    ? DetermineCustomizationsPath(modelNode[ModelsSectionKeys.ModelKey].ToString())
                    : Path.Combine(modelsFolder, modelNode[ModelsSectionKeys.CustomizationFileKey].ToString());

                if (modelNode[ModelsSectionKeys.AppendServiceKey] != null && (bool)modelNode[ModelsSectionKeys.AppendServiceKey])
                    config.BaseName += "Service";

                if (modelNode[ModelsSectionKeys.MaxRetriesKey] != null && modelNode[ModelsSectionKeys.MaxRetriesKey].IsInt)
                    config.OverrideMaxRetries = Convert.ToInt32(modelNode[ModelsSectionKeys.MaxRetriesKey].ToString());

                if (modelNode[ModelsSectionKeys.SynopsisKey] != null)
                    config.Synopsis = (string)modelNode[ModelsSectionKeys.SynopsisKey];

                if (modelNode[ModelsSectionKeys.CoreCLRSupportKey] != null)
                    config.CoreCLRSupport = (bool)modelNode[ModelsSectionKeys.CoreCLRSupportKey];
                else
                    config.CoreCLRSupport = true;

                config.ServiceDependencies = new Dictionary<string, string>(StringComparer.Ordinal);
                if (modelNode[ModelsSectionKeys.DependenciesKey] != null && modelNode[ModelsSectionKeys.DependenciesKey].IsArray)
                {
                    foreach (var d in modelNode[ModelsSectionKeys.DependenciesKey])
                    {
                        config.ServiceDependencies.Add(d.ToString(), null);
                    }
                }

                if (modelNode[ModelsSectionKeys.UsePclProjectDependenciesKey] != null && modelNode[ModelsSectionKeys.UsePclProjectDependenciesKey].IsBoolean)
                    config.UsePclProjectDependencies = bool.Parse(modelNode[ModelsSectionKeys.UsePclProjectDependenciesKey].ToString());
                else
                    config.UsePclProjectDependencies = false;

                if (modelNode[ModelsSectionKeys.LicenseUrlKey] != null && modelNode[ModelsSectionKeys.LicenseUrlKey].IsString)
                {
                    config.LicenseUrl = modelNode[ModelsSectionKeys.LicenseUrlKey].ToString();
                    config.RequireLicenseAcceptance = true;
                }
                else
                    config.LicenseUrl = ApacheLicenseURL;

                var serviceName = config.ServiceNameRoot;
                var versionInfoJson = versions[serviceName];
                if (versionInfoJson != null)
                {
                    var dependencies = versionInfoJson["Dependencies"];
                    foreach (var name in dependencies.PropertyNames)
                    {
                        var version = dependencies[name].ToString();
                        config.ServiceDependencies[name] = version;
                    }


                    var versionText = versionInfoJson["Version"].ToString();
                    config.ServiceFileVersion = versionText;

                    if(versionInfoJson["InPreview"] != null && (bool)versionInfoJson["InPreview"])
                        config.InPreview = true;
                    else
                        config.InPreview = this.DefaultToPreview;
                }
                else
                {
                    config.ServiceDependencies["Core"] = coreVersion;
                    var versionTokens = coreVersion.Split('.');
                    config.ServiceFileVersion = string.Format("{0}.{1}.0.0", versionTokens[0], versionTokens[1]);
                    config.InPreview = this.DefaultToPreview;
                }

                // The parent model for current model, if set, the client will be generated
                // in the same namespace and share common types.
                var parentModelName = modelNode[ModelsSectionKeys.ParentBaseNameKey] != null ? modelNode[ModelsSectionKeys.ParentBaseNameKey].ToString() : null;
                if (parentModelName != null)
                {
                    try
                    {
                        config.ParentConfig = serviceConfigurations.Single(c => c.BaseName.Equals(parentModelName));
                    }
                    catch (KeyNotFoundException exception)
                    {
                        // Note : the parent model should be defined in the manifest before being referred by a child model
                        throw new KeyNotFoundException(
                            string.Format("A parent model with name {0} is not defined in the manifest", parentModelName),
                            exception); ;
                    }
                }

                serviceConfigurations.Add(config);
            }

            ServiceConfigurations = serviceConfigurations
                .OrderBy(sc => sc.ServiceDependencies.Count)
                .ToList();
            //ServiceVersions = serviceVersions;
        }