示例#1
0
        public static void Generate(CliArgs cliArgs)
        {
            var settings     = new SettingsImpl();
            var template     = new Template(cliArgs.TemplatePath);
            var provider     = new RoslynMetadataProvider();
            var indexBuilder = new StringBuilder();

//detect whether its a directory or file
            foreach (var path in GetFiles(cliArgs.SourcePath, cliArgs.Recursive, cliArgs.Regex))
            {
                var file       = new FileImpl(provider.GetFile(path, settings, null));
                var outputPath = template.RenderFile(file);
                if (outputPath != null)
                {
                    indexBuilder.Append(ExportStatement(outputPath));
                }
            }

            if (cliArgs.GenerateIndex)
            {
                var @join = Path.Join(Path.GetDirectoryName(cliArgs.TemplatePath), "index.ts");
                Console.WriteLine($"Outputting to {@join}");
                File.WriteAllText(@join, indexBuilder.ToString(), new UTF8Encoding(true));
            }
        }
示例#2
0
        static void Main(string[] args)
        {
            //var stopwatch = Stopwatch.StartNew();
            var showHelp = args == null || args.Length == 0;

            string templatePath = null;
            string sourcePath   = null;

            var p = new OptionSet  {
                { "t|template=", "full path to template (*.tst) file.", v => templatePath = v },
                { "s|source=", "full path to source (*.cs) file.", v => sourcePath = v },
                { "h|help", "show this message and exit", v => showHelp = v != null }
            };

            try
            {
                p.Parse(args);
            }
            catch (OptionException e) {
                Console.Write("TypewriterCli: ");
                Console.WriteLine(e.Message);
                Console.WriteLine("Try `dotnet TypewriterCli.dll --help' for more information.");
                return;
            }

            try
            {
                if (showHelp)
                {
                    ShowHelp(p);
                    return;
                }

                if (templatePath == null)
                {
                    throw new InvalidOperationException("Missing required option -t|template");
                }

                if (sourcePath == null)
                {
                    throw new InvalidOperationException("Missing required option -s|source");
                }

                var settings = new SettingsImpl();
                var template = new Template(templatePath);
                var provider = new RoslynMetadataProvider();
                var file     = new FileImpl(provider.GetFile(sourcePath, settings, null));

                template.RenderFile(file);
                //Console.WriteLine("Convert {0} ms", stopwatch.ElapsedMilliseconds);
            }
            catch (Exception e)
            {
                Console.WriteLine($"Error: {e.Message}");
            }
        }