Exemple #1
0
        public async Task GetContentTypesShouldSerializeIntoAnEnumerableOfContentType()
        {
            //Arrange
            _handler.Response = GetResponseFromFile(@"ContenttypesCollection.json");

            //Act
            var res = await _client.GetContentTypes();

            //Assert
            Assert.Equal(3, res.Count());
            Assert.Equal("Brand", res.First().Name);
            Assert.Null(res.First().Description);
            Assert.Equal(DateTime.Parse("2016-11-03T10:49:52.260Z").ToUniversalTime(), res.First().SystemProperties.CreatedAt);
            Assert.Equal("n9r7gd2bwvqt", res.First().SystemProperties.Space.SystemProperties.Id);
            Assert.Equal(2, res.First().SystemProperties.Revision);
        }
Exemple #2
0
        public async Task <int> OnExecute(CommandLineApplication app, IConsole console)
        {
            var http    = new HttpClient();
            var options = new ContentfulOptions
            {
                DeliveryApiKey = ApiKey,
                SpaceId        = SpaceId,
                Environment    = Environment
            };
            var client = new ContentfulClient(http, options);

            try
            {
                _contentTypes = await client.GetContentTypes();
            }
            catch (ContentfulException ce) {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.Error.WriteLine("There was an error communicating with the Contentful API: " + ce.Message);
                Console.Error.WriteLine($"Request ID: {ce.RequestId}");
                Console.Error.WriteLine("" + ce.ErrorDetails?.Errors);
                Console.Error.WriteLine("Please verify that your api key and access token are correct");
                Console.ResetColor();
                return(Program.ERROR);
            }

            Console.WriteLine($"Found {_contentTypes.Count()} content types.");
            var path = "";

            if (string.IsNullOrEmpty(Path))
            {
                path = Directory.GetCurrentDirectory();
                Console.WriteLine($"No path specified, creating files in current working directory {path}");
            }
            else
            {
                Console.WriteLine($"Path specified. Files will be created at {Path}");
                path = Path;
            }

            var dir = new DirectoryInfo(path);

            if (dir.Exists == false)
            {
                Console.WriteLine($"Path {path} does not exist and will be created.");
                dir.Create();
            }

            foreach (var contentType in _contentTypes)
            {
                var safeFileName = GetSafeFilename(contentType.Name);

                var file = new FileInfo($"{dir.FullName}{System.IO.Path.DirectorySeparatorChar}{safeFileName}.cs");
                if (file.Exists && !Force)
                {
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    var prompt = Prompt.GetYesNo($"The folder already contains a file with the name {file.Name}. Do you want to overwrite it?", true);
                    Console.ResetColor();
                    if (prompt == false)
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine($"Skipping {file.Name}");
                        Console.ResetColor();
                    }
                }

                using (var sw = file.CreateText())
                {
                    var sb = new StringBuilder();
                    sb.AppendLine(_templateStart);
                    sb.AppendLine($"namespace {Namespace}");
                    //start namespace
                    sb.AppendLine("{");

                    sb.AppendLine($"    public class {FormatClassName(contentType.Name)}");
                    //start class
                    sb.AppendLine("    {");

                    sb.AppendLine("        public SystemProperties Sys { get; set; }");

                    foreach (var field in contentType.Fields)
                    {
                        sb.AppendLine($"        public {GetDataTypeForField(field)} {FirstLetterToUpperCase(field.Id)} {{ get; set; }}");
                    }

                    //end class
                    sb.AppendLine("    }");
                    //end namespace
                    sb.AppendLine("}");

                    sw.WriteLine(sb.ToString());
                }
            }

            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine($"Files successfully created!");
            Console.ResetColor();

            return(Program.OK);
        }