예제 #1
0
        public async Task <IEnumerable <VisualTypeGroup> > GetTypes(string key, string spaceId, string groupNameSeparator)
        {
            var ctfClient = new ContentfulManagementClient(_httpClient, new ContentfulOptions {
                ManagementApiKey = key, SpaceId = spaceId
            });
            var ctfTypes = (await ctfClient.GetContentTypes()).OrderBy(c => c.Name).ToList();

            return(GroupTypes(ctfTypes.Select(VisualTypeFactory.CreateType), groupNameSeparator));
        }
        public async Task <int> OnExecute(CommandLineApplication app, IConsole console)
        {
            var http   = new HttpClient();
            var client = new ContentfulManagementClient(http, new ContentfulOptions
            {
                ManagementApiKey = ManagementToken,
                SpaceId          = SpaceId,
                Environment      = Environment
            });

            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.SystemProperties.Id);

                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.SystemProperties.Id)}");
                    //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);
        }
 public IEnumerable <Core.Models.ContentType> GetcontentTypes()
 {
     return(_apiClient.GetContentTypes(Space).Result);
 }