/// <summary>
        /// Generates the CodeCompileUnit for the specified service by using the CodeGenerator.
        /// </summary>
        protected static CodeCompileUnit CodegenService(IService service)
        {
            // Generate the service.
            var generator = new GoogleServiceGenerator(service, "Generated");

            return(generator.GenerateCode());
        }
Exemple #2
0
        /// <summary>
        /// Generates a service from the specified discovery uri.
        /// </summary>
        /// <param name="url">The URL or file where the discovery document can be found.</param>
        public void GenerateService(Uri url)
        {
            Console.WriteLine(" Generating: " + url);

            // Create the output directory if it does not exist yet.
            if (!Directory.Exists(OutputDir))
            {
                Directory.CreateDirectory(OutputDir);
            }

            // Discover the service.
            Console.WriteLine("  Fetching " + url);
            IDiscoveryDevice src = url.IsFile
                ? (IDiscoveryDevice)
                                   new StreamDiscoveryDevice
            {
                DiscoveryStream = File.Open(url.LocalPath, FileMode.Open, FileAccess.Read)
            }
                : new StringDiscoveryDevice {
                Document = Utils.FetchDocument(url)
            };
            var      discovery = new DiscoveryService(src);
            IService service   = discovery.GetService(DiscoveryVersion.Version_1_0);

            // Generate the formal names based upon the discovery data.
            string name    = service.Name;
            string version = service.Version;

            string formalServiceName = GeneratorUtils.UpperFirstLetter(name);
            string serviceNamespace  = GeneratorUtils.GetNamespaceName(formalServiceName);

            if ((Flags & GeneratorFlags.GoogleService) > 0) // If this is a google service, add the google prefix.
            {
                formalServiceName = GooglePrefix + formalServiceName;
                serviceNamespace  = GooglePrefix + serviceNamespace;
            }

            string baseFileName = Path.Combine(OutputDir, formalServiceName + "." + version);
            string fileName     = baseFileName + CodeFileExtension;
            string libfileName  = baseFileName + LibraryExtension;

            serviceNamespace = String.Format("{0}.{1}", serviceNamespace, GeneratorUtils.GetNamespaceName(version));

            // Produce the code.
            var             generator     = new GoogleServiceGenerator(service, serviceNamespace);
            CodeCompileUnit generatedCode = generator.GenerateCode();

            WriteCodeToFile(generatedCode, fileName);

            if ((Flags & GeneratorFlags.CompileLibrary) > 0)
            {
                CompileIntoLibrary(service, generatedCode, libfileName);
            }
        }
        public void TestCompilationWithDefaultDecorators_Buzz()
        {
            var clientNamespace = "Google.Apis.Samples.CommandLineGeneratedService.Buzz";

            var service = CreateBuzzService();

            var generator       = new GoogleServiceGenerator(service, clientNamespace);
            var codeCompileUnit = generator.GenerateCode();

            // Full Compile we should not have any warnings.
            CheckCompile(codeCompileUnit, true, "Failed To compile resultant code with default decorators.");
        }
        public void TestCompilationWithDefaultDecorators_Buzz()
        {
            var clientNamespace = "Google.Apis.Samples.CommandLineGeneratedService.Buzz";

            var service = CreateBuzzService();

            var generator = new GoogleServiceGenerator(service, clientNamespace);
            var codeCompileUnit = generator.GenerateCode();

            // Full Compile we should not have any warnings.
            CheckCompile(codeCompileUnit, true, "Failed To compile resultant code with default decorators.");
        }
        public void SystemTestCompilationWithDefaultDecorators_Discovery()
        {
            const string serviceName = "discovery";
            const string serviceVersion = "v1";
            var clientNamespace = "Google.Apis.Samples.CommandLineGeneratedService.Discovery";

            // Generate the discovery URL for that service
            string url = string.Format(GoogleServiceGenerator.GoogleDiscoveryURL, serviceName, serviceVersion);
            var discovery = new DiscoveryService(new WebDiscoveryDevice(new Uri(url)));

            // Build the service based on discovery information.
            var service = discovery.GetService(DiscoveryVersion.Version_1_0);
            Assert.AreEqual(serviceName, service.Name);
            Assert.AreEqual(serviceVersion, service.Version);

            // Generate code
            var generator = new GoogleServiceGenerator(service, clientNamespace);
            var codeCompileUnit = generator.GenerateCode();

            // Full Compile we should not have any warnings.
            CheckCompile(codeCompileUnit, true, "Failed To compile resultant code with default decorators.");
        }
        public void SystemTestCompilationWithDefaultDecorators_Discovery()
        {
            const string serviceName     = "discovery";
            const string serviceVersion  = "v1";
            var          clientNamespace = "Google.Apis.Samples.CommandLineGeneratedService.Discovery";

            // Generate the discovery URL for that service
            string url       = string.Format(GoogleServiceGenerator.GoogleDiscoveryURL, serviceName, serviceVersion);
            var    discovery = new DiscoveryService(new WebDiscoveryDevice(new Uri(url)));

            // Build the service based on discovery information.
            var service = discovery.GetService(DiscoveryVersion.Version_1_0);

            Assert.AreEqual(serviceName, service.Name);
            Assert.AreEqual(serviceVersion, service.Version);

            // Generate code
            var generator       = new GoogleServiceGenerator(service, clientNamespace);
            var codeCompileUnit = generator.GenerateCode();

            // Full Compile we should not have any warnings.
            CheckCompile(codeCompileUnit, true, "Failed To compile resultant code with default decorators.");
        }