Exemple #1
0
        /// <summary>
        /// Extract the underlying service api version from the service's configuration class in the SDK
        /// </summary>
        /// <param name="clientNamespace"></param>
        /// <param name="clientName"></param>
        /// <returns></returns>
        private string GetServiceVersion(string clientNamespace, string clientName)
        {
            string version           = null;
            var    configClassPrefix = clientName.EndsWith("Client", StringComparison.Ordinal) ? clientName.Substring(0, clientName.Length - 6) : clientName;
            var    configType        = CurrentServiceAssembly.GetType(string.Format("{0}.{1}Config", clientNamespace, configClassPrefix));

            if (configType != null && configType.GetConstructor(System.Type.EmptyTypes) != null)
            {
                var config   = Activator.CreateInstance(configType, null);
                var property = configType.GetProperty("ServiceVersion");
                if (property != null)
                {
                    version = property.GetValue(config, null) as string;
                }
            }
            if (string.IsNullOrEmpty(version))
            {
                Logger.LogError("Unable to retrieve version for client " + clientName + " (" + string.Format("{0}.{1}Config", clientNamespace, configClassPrefix) + ")");
            }
            return(version);
        }
Exemple #2
0
        private void GenerateClientAndCmdlets()
        {
            if (CurrentModel.SkipCmdletGeneration)
            {
                Logger.Log("...skipping cmdlet generation, ExcludeCmdletGeneration set true for service");
                return;
            }

            Logger.Log("...generating cmdlets against interface '{0}'", CurrentModel.ServiceClientInterface);

            var clientInterfaceTypeName = string.Format("{0}.{1}", CurrentModel.ServiceNamespace, CurrentModel.ServiceClientInterface);
            var clientInterfaceType     = CurrentServiceAssembly.GetType(clientInterfaceTypeName);

            if (clientInterfaceType == null)
            {
                Logger.LogError("Cannot find target client " + clientInterfaceTypeName);
                return;
            }

            var outputRoot = Path.Combine(CmdletsOutputPath, CurrentModel.AssemblyName);

            try
            {
                using (var sw = new StringWriter())
                {
                    // if the service has operations requiring anonymous access, we'll generate two clients
                    // one for regular authenticated calls and one using anonymous credentials
                    using (var writer = new IndentedTextWriter(sw))
                    {
                        CmdletServiceClientWriter.Write(writer,
                                                        CurrentModel,
                                                        CurrentModel.ServiceName,
                                                        GetServiceVersion(CurrentModel.ServiceNamespace, CurrentModel.ServiceClient));
                    }

                    var fileContents = sw.ToString();
                    var fileName     = CurrentModel.GetServiceCmdletClassName(false) + "Cmdlet.cs";
                    File.WriteAllText(Path.Combine(outputRoot, fileName), fileContents);
                }
            }
            catch (Exception e)
            {
                AnalysisError.ExceptionWhileWritingServiceClientCode(CurrentModel, e);
            }

            // process the methods in order to make debugging more convenient
            var methods = clientInterfaceType.GetMethods().OrderBy(m => m.Name).ToList();

            foreach (var method in methods)
            {
                if (ShouldEmitMethod(method))
                {
                    CreateCmdlet(method, CurrentModel);
                    CurrentOperation.Processed = true;
                }
                else
                {
                    if (CurrentModel.ServiceOperations.TryGetValue(method.Name, out var so))
                    {
                        so.Processed = true;
                    }
                }
            }

            foreach (var operation in CurrentModel.ServiceOperationsList.Where(operation => !operation.Processed))
            {
                AnalysisError.MissingSDKMethodForCmdletConfiguration(CurrentModel, operation);
            }

            // fuse the manually-declared custom aliases with the automatic set to go into awsaliases.ps1
            // note that this file is deprecated and likely to get yanked as no-one uses it
            foreach (var cmdletKey in CurrentModel.CustomAliases.Keys)
            {
                AliasStore.Instance.AddAliases(cmdletKey, CurrentModel.CustomAliases[cmdletKey]);
            }
        }