LoadFromJson() public static method

public static LoadFromJson ( string json ) : GeneratorConfig
json string
return GeneratorConfig
Ejemplo n.º 1
0
        public void Run(string projectJson, List <ExternalType> tsTypes)
        {
            var root       = Path.GetDirectoryName(projectJson);
            var configFile = Path.Combine(root, "sergen.json");

            if (!File.Exists(configFile))
            {
                System.Console.Error.WriteLine("Can't find sergen.json in current directory!");
                Environment.Exit(1);
            }

            var config = GeneratorConfig.LoadFromJson(File.ReadAllText(configFile));

            if (config.ServerTypings == null)
            {
                System.Console.Error.WriteLine("ServerTypings is not configured in sergen.json file!");
                Environment.Exit(1);
            }

            if (config.ServerTypings.Assemblies.IsEmptyOrNull())
            {
                System.Console.Error.WriteLine("ServerTypings has no assemblies configured in sergen.json file!");
                Environment.Exit(1);
            }

            if (config.RootNamespace.IsEmptyOrNull())
            {
                System.Console.Error.WriteLine("Please set RootNamespace option in sergen.json file!");
                Environment.Exit(1);
            }

            var outDir = Path.Combine(root, (config.ServerTypings.OutDir.TrimToNull() ?? "Imports/ServerTypings")
                                      .Replace('/', Path.DirectorySeparatorChar));

            Console.ForegroundColor = ConsoleColor.Cyan;
            Console.Write("Transforming ServerTypings at: ");
            Console.ResetColor();
            Console.WriteLine(outDir);

            List <Assembly> assemblies = new List <Assembly>();

            foreach (var assembly in config.ServerTypings.Assemblies)
            {
                assemblies.Add(AssemblyLoadContext.Default.LoadFromAssemblyPath(Path.Combine(root, assembly)));
            }

            var generator = new ServerTypingsGenerator(assemblies.ToArray());

            generator.RootNamespaces.Add(config.RootNamespace);

            foreach (var type in tsTypes)
            {
                generator.AddTSType(type);
            }

            var codeByFilename = generator.Run();

            new MultipleOutputHelper().WriteFiles(outDir, codeByFilename, "*.ts");
        }
Ejemplo n.º 2
0
        public void Run(string projectJson, List <ExternalType> tsTypes)
        {
            var root       = Path.GetDirectoryName(projectJson);
            var configFile = Path.Combine(root, "sergen.json");

            if (!File.Exists(configFile))
            {
                System.Console.Error.WriteLine("Can't find sergen.json in current directory!");
                Environment.Exit(1);
            }

            var config = GeneratorConfig.LoadFromJson(File.ReadAllText(configFile));

            config.ClientTypes = config.ClientTypes ?? new GeneratorConfig.ClientTypesConfig();

            if (config.RootNamespace.IsEmptyOrNull())
            {
                System.Console.Error.WriteLine("Please set RootNamespace option in sergen.json file!");
                Environment.Exit(1);
            }

            var outDir = Path.Combine(root, (config.ClientTypes.OutDir.TrimToNull() ?? "Imports/ClientTypes")
                                      .Replace('/', Path.DirectorySeparatorChar));

            Console.ForegroundColor = ConsoleColor.Cyan;
            Console.Write("Transforming ClientTypes at: ");
            Console.ResetColor();
            Console.WriteLine(outDir);

            var generator = new ClientTypesGenerator();

            generator.RootNamespaces.Add(config.RootNamespace);

            foreach (var type in tsTypes)
            {
                generator.AddTSType(type);
            }

            var codeByFilename = generator.Run();

            new MultipleOutputHelper().WriteFiles(outDir, codeByFilename, "*.ts");
        }
Ejemplo n.º 3
0
        public void Run(string projectJson)
        {
            var root       = Path.GetDirectoryName(projectJson);
            var configFile = Path.Combine(root, "sergen.json");

            if (!File.Exists(configFile))
            {
                System.Console.Error.WriteLine("Can't find sergen.json in current directory!");
                Environment.Exit(1);
            }

            var config = GeneratorConfig.LoadFromJson(File.ReadAllText(configFile));

            config.MVC = config.MVC ?? new GeneratorConfig.MVCConfig();

            var outDir = Path.Combine(root, (config.MVC.OutDir.TrimToNull() ?? "Imports/MVC")
                                      .Replace('/', Path.DirectorySeparatorChar));

            Console.ForegroundColor = ConsoleColor.Cyan;
            Console.Write("Transforming MVC at: ");
            Console.ResetColor();
            Console.WriteLine(outDir);

            string[] stripViewPaths = config.MVC.StripViewPaths ?? new string[] {
                "Modules/",
                "Views/"
            };

            var rootDir         = Path.GetDirectoryName(projectJson) + Path.DirectorySeparatorChar;
            var searchViewPaths = (config.MVC.SearchViewPaths ?? new string[] { "Modules/", "Views/" })
                                  .Select(x => Path.Combine(rootDir, x.Replace('/', Path.DirectorySeparatorChar)));

            IEnumerable <string> files = new List <string>();

            foreach (var path in searchViewPaths)
            {
                files = files.Concat(Directory.GetFiles(path, "*.cshtml", SearchOption.AllDirectories));
            }

            Func <string, string> getName = s => {
                var path = s.Substring(rootDir.Length);
                var name = Path.ChangeExtension(path, null).Replace('\\', '/');
                foreach (var strip in stripViewPaths)
                {
                    if (name.StartsWith(strip, StringComparison.OrdinalIgnoreCase))
                    {
                        name = name.Substring(strip.Length);
                    }
                }

                return(name);
            };

            files = files.OrderBy(x => getName(x));

            StringBuilder sb = new StringBuilder();

            sb.AppendLine("using System;");
            sb.AppendLine("");
            sb.AppendLine("namespace MVC");
            sb.AppendLine("{");
            sb.AppendLine("    public static class Views");
            sb.AppendLine("    {");

            var last      = new string[0];
            var processed = new HashSet <string>();

            foreach (var file in files)
            {
                var path = file.Substring(rootDir.Length);
                var name = getName(file);
                if (name.StartsWith("App_Code/", StringComparison.OrdinalIgnoreCase) ||
                    name.EndsWith("_ViewStart", StringComparison.OrdinalIgnoreCase) ||
                    name.EndsWith("_ViewImports", StringComparison.OrdinalIgnoreCase) ||
                    processed.Contains(name))
                {
                    continue;
                }

                processed.Add(name);
                var parts = name.Split(new char[] { '/' });

                if (parts.Length <= 1)
                {
                    continue;
                }

                for (var i = last.Length; i > parts.Length; i--)
                {
                    var close = (new String(' ', (i * 4)) + "}");
                    sb.AppendLine(close);
                }

                var x = Math.Min(last.Length, parts.Length) - 2;
                while (x >= 0 && last[x] != parts[x])
                {
                    var close = (new String(' ', ((x + 2) * 4)) + "}");
                    x--;
                    sb.AppendLine(close);
                    sb.AppendLine();
                }

                for (var i = Math.Max(x + 1, 0); i < parts.Length - 1; i++)
                {
                    var indent = new String(' ', (i + 2) * 4);
                    var u      = parts[i];
                    if (i > 0 && parts[i - 1] == u)
                    {
                        u = u + "_";
                    }
                    sb.AppendLine(indent + "public static class " + u);
                    sb.AppendLine(indent + "{");
                }

                var n = parts[parts.Length - 1].Replace(".", "_");
                if (parts.Length - 1 > 0 && parts[parts.Length - 2] == n)
                {
                    n += "_";
                }

                sb.Append(new String(' ', (parts.Length + 1) * 4));
                sb.AppendLine("public const string " + n + " = \"~/" + path.Replace(@"\", "/") + "\";");

                last = parts;
            }

            for (var i = last.Length - 1; i > 0; i--)
            {
                sb.AppendLine(new String(' ', ((i + 1) * 4)) + "}\r\n");
            }

            sb.AppendLine("    }");
            sb.AppendLine("}");

            new MultipleOutputHelper().WriteFiles(outDir, new SortedDictionary <string, string>
            {
                { "MVC.cs", sb.ToString() }
            });
        }