Beispiel #1
0
        protected override void ExecuteImpl(string[] args)
        {
            string id = args[0];

            var catalog = ApiCatalog.Load();

            if (catalog.Apis.Any(api => api.Id == id))
            {
                throw new UserErrorException($"API {id} already exists in the API catalog.");
            }
            var serviceDirectory = ServiceDirectory.LoadFromGoogleapis();

            var service = serviceDirectory.Services.FirstOrDefault(service => service.CSharpNamespaceFromProtos == id);

            if (service is null)
            {
                var lowerWithoutCloud = id.Replace(".Cloud", "").ToLowerInvariant();
                var possibilities     = serviceDirectory.Services
                                        .Select(svc => svc.CSharpNamespaceFromProtos)
                                        .Where(ns => ns.Replace(".Cloud", "").ToLowerInvariant() == lowerWithoutCloud);
                throw new UserErrorException(
                          $"No service found for '{id}'.{Environment.NewLine}Similar possibilities (check options?): {string.Join(", ", possibilities)}");
            }

            var api = new ApiMetadata
            {
                Id          = id,
                ProtoPath   = service.ServiceDirectory,
                ProductName = service.Title.EndsWith(" API") ? service.Title[..^ 4] : service.Title,
        protected override void ExecuteImpl(string[] args)
        {
            var googleapisRoot   = GetGoogleapisRoot();
            var serviceDirectory = ServiceDirectory.LoadFromGoogleapis(googleapisRoot);
            var outputDirectory  = DirectoryLayout.ForApi("ServiceDirectory").SourceDirectory;

            string directoryJson = JsonConvert.SerializeObject(serviceDirectory, Formatting.Indented);

            File.WriteAllText(Path.Combine(outputDirectory, "directory.json"), directoryJson);

            var gitCommit     = GetCommit(googleapisRoot);
            var synthMetadata = new
            {
                sources = new[] {
                    new {
                        git = new
                        {
                            name   = "googleapis",
                            remote = "https://github.com/googleapis/googleapis.git",
                            sha    = gitCommit
                        }
                    }
                }
            };
            var synthMetadataJson = JsonConvert.SerializeObject(synthMetadata, Formatting.Indented);

            File.WriteAllText(Path.Combine(outputDirectory, "synth.metadata"), synthMetadataJson);
        }
Beispiel #3
0
        protected override void ExecuteImpl(string[] args)
        {
            var directory = ServiceDirectory.LoadFromGoogleapis();

            foreach (var api in directory.Services)
            {
                ReportAnomalies(api);
            }
        }
        protected override void ExecuteImpl(string[] args)
        {
            var catalog = ApiCatalog.Load();
            // Note: for now, we could actually load it from apis/ServiceDirectory, but hey...
            var directory       = ServiceDirectory.LoadFromGoogleapis();
            var stabilityFilter = BuildStabilityFilter(args[0]);

            var ignoredOrGeneratedPaths = new HashSet <string>(catalog.IgnoredPaths.Keys);

            ignoredOrGeneratedPaths.UnionWith(catalog.Apis.Select(api => api.ProtoPath));

            var missingServices = directory.Services
                                  .Where(stabilityFilter)
                                  .Where(svc => !ignoredOrGeneratedPaths.Contains(svc.ServiceDirectory))
                                  .ToList();

            Console.WriteLine($"Missing services: {missingServices.Count}");
            foreach (var service in missingServices)
            {
                Console.WriteLine(service.ServiceDirectory);
            }
        }
        protected override void ExecuteImpl(string[] args)
        {
            string id = args[0];

            var catalog = ApiCatalog.Load();

            if (catalog.Apis.Any(api => api.Id == id))
            {
                throw new UserErrorException($"API {id} already exists in the API catalog.");
            }
            var serviceDirectory = ServiceDirectory.LoadFromGoogleapis();

            var service = serviceDirectory.Services.FirstOrDefault(service => service.CSharpNamespaceFromProtos == id);

            if (service is null)
            {
                var lowerWithoutCloud = id.Replace(".Cloud", "").ToLowerInvariant();
                var possibilities     = serviceDirectory.Services
                                        .Select(svc => svc.CSharpNamespaceFromProtos)
                                        .Where(ns => ns.Replace(".Cloud", "").ToLowerInvariant() == lowerWithoutCloud);
                throw new UserErrorException(
                          $"No service found for '{id}'.{Environment.NewLine}Similar possibilities (check options?): {string.Join(", ", possibilities)}");
            }

            var api = new ApiMetadata
            {
                Id          = id,
                ProtoPath   = service.ServiceDirectory,
                ProductName = service.Title,
                Description = service.Description,
                Version     = "1.0.0-beta00",
                Type        = ApiType.Grpc,
                Generator   = GeneratorType.Micro,
                // Let's not include test dependencies, which are rarely useful.
                TestDependencies = null
            };

            // Add dependencies discovered via the proto imports.
            // This doesn't fail on any dependencies that aren't found - we could tighten this up later
            // by knowing about common protos, for example.
            var apisByProtoPath = catalog.Apis.Where(api => api.ProtoPath is object).ToDictionary(api => api.ProtoPath);

            foreach (var import in service.ImportDirectories)
            {
                if (apisByProtoPath.TryGetValue(import, out var dependency))
                {
                    api.Dependencies.Add(dependency.Id, dependency.Version);
                }
            }

            // Now work out what the new API metadata looks like in JSON.
            var serializer = new JsonSerializer
            {
                NullValueHandling = NullValueHandling.Ignore,
                Converters        = { new StringEnumConverter(new CamelCaseNamingStrategy()) },
                ContractResolver  = new DefaultContractResolver {
                    NamingStrategy = new CamelCaseNamingStrategy()
                }
            };

            api.Json = JToken.FromObject(api, serializer);

            var followingApi = catalog.Apis.FirstOrDefault(api => string.Compare(api.Id, id, StringComparison.Ordinal) > 0);

            if (followingApi is object)
            {
                followingApi.Json.AddBeforeSelf(api.Json);
            }
            else
            {
                // Looks like this API will be last in the list.
                catalog.Apis.Last().Json.AddAfterSelf(api.Json);
            }

            // Done. Let's write out the catalog and display what we've done.
            File.WriteAllText(ApiCatalog.CatalogPath, catalog.FormatJson());
            Console.WriteLine($"Added {id} to the API catalog with the following configuration:");
            Console.WriteLine(api.Json.ToString(Formatting.Indented));
        }