Example #1
0
        static void Main(string[] args)
        {
            Parser.Default.ParseArguments <Options>(args)
            .WithParsed(o =>
            {
                ContentGenerator generator = new ContentGenerator();
                ContentSettings settings   = new ContentSettings()
                {
                    Namespace = o.Namespace,
                    ClassName = o.Class
                };

                try
                {
                    using (var zipStream = File.Open(o.Archive, FileMode.Open))
                    {
                        using (ZipArchive zip = new ZipArchive(zipStream))
                        {
                            var @namespace = generator.Create(settings, zip.Entries);

                            var code = @namespace
                                       .NormalizeWhitespace()
                                       .ToFullString();

                            File.WriteAllText(o.File, code);
                        }
                    }
                }
                catch (Exception exc)
                {
                    Console.WriteLine(exc.Message);
                }
            });
        }
        public NamespaceDeclarationSyntax CreateRoot(ContentSettings settings, ClassDeclarationSyntax rootClass)
        {
            var @namespace = SyntaxFactory.NamespaceDeclaration(SyntaxFactory.ParseName(settings.Namespace)).NormalizeWhitespace();

            @namespace = @namespace.AddUsings(SyntaxFactory.UsingDirective(SyntaxFactory.ParseName("System")));

            return(@namespace.AddMembers(rootClass));
        }
        public NamespaceDeclarationSyntax Create(ContentSettings settings, IEnumerable <ZipArchiveEntry> entries)
        {
            var rootClass = CreateClass(settings.ClassName);

            var dirs = entries
                       .Where(x => x.IsDirectory())
                       .ToDictionary(
                x => x.FullName.Trim('/', '\\'),
                x => CreateClass(StringHelper.GetDirectory(x.FullName).ToVarName()));

            foreach (var file in entries.Where(x => !x.IsDirectory()))
            {
                var varName = StringHelper.GetFileName(file.Name);

                var dir    = StringHelper.GetParent(file.FullName);
                var @class = dirs[dir];
                dirs[dir] = dirs[dir].AddMembers(CreateField(varName, file.FullName));
            }

            var keys = dirs.Keys.OrderByDescending(p => p.Length).ToArray();

            foreach (var dir in keys)
            {
                if (StringHelper.IsRoot(dir))
                {
                    rootClass = rootClass.AddMembers(dirs[dir]);
                }
                else
                {
                    var parentDir = StringHelper.GetLevelUp(dir);
                    dirs[parentDir] = dirs[parentDir].AddMembers(dirs[dir]);
                }
            }

            return(CreateRoot(settings, rootClass));
        }