static void Main(string[] args)
        {
            CommandLineParser.CommandLineParser parser = new CommandLineParser.CommandLineParser()
            {
                CheckMandatoryArguments     = true,
                ShowUsageOnEmptyCommandline = true
            };

            DirectoryArgument schemaDirArg = new DirectoryArgument('s', "schema", "json schema source directory")
            {
                Optional           = false,
                DirectoryMustExist = true
            };
            DirectoryArgument typeScriptDirArg = new DirectoryArgument('t', "typescript", "typescript target directory")
            {
                Optional           = true,
                DirectoryMustExist = false
            };
            DirectoryArgument cSharpDirArg = new DirectoryArgument('c', "csharp", "c# target directory")
            {
                Optional           = true,
                DirectoryMustExist = false
            };

            SwitchArgument interactive = new SwitchArgument('i', "interactive", "interactive yes/no to continue", false);
            SwitchArgument recursive   = new SwitchArgument('r', "recursive", "recursive parsing from source directory", false);

            parser.Arguments.Add(schemaDirArg);
            parser.Arguments.Add(typeScriptDirArg);
            parser.Arguments.Add(cSharpDirArg);
            //parser.Arguments.Add(interactive);
            //parser.Arguments.Add(recursive);


            try
            {
                parser.ParseCommandLine(args);
                //parser.ShowParsedArguments();
                if (!parser.ParsingSucceeded)
                {
                    return;
                }
            }
            catch (Exception e)
            {
                System.Console.WriteLine("Exception parsing arguments:");
                System.Console.WriteLine(e.Message);
                parser.PrintUsage(System.Console.Out);
                System.Console.WriteLine("press any key to continue");
                System.Console.ReadKey();
                return;
            }


            if (typeScriptDirArg.DirectoryInfo == null &&
                cSharpDirArg.DirectoryInfo == null)
            {
                System.Console.WriteLine("TypeScript and/or C# target directory missing");
                System.Console.WriteLine("press any key to continue");
                System.Console.ReadKey();
                return;
            }

            var sDirInfo = schemaDirArg.DirectoryInfo;
            var tDirInfo = typeScriptDirArg.DirectoryInfo;
            var cDirInfo = cSharpDirArg.DirectoryInfo;

            if (tDirInfo != null && !tDirInfo.Exists)
            {
                tDirInfo.Create();
            }
            if (cDirInfo != null && !cDirInfo.Exists)
            {
                cDirInfo.Create();
            }



            //get only .json files
            var schemasFiles = sDirInfo.GetFiles().ToList().Where((f) => f.Extension.ToLower().Equals(".json")).ToList();

            System.Console.WriteLine("found {0} json schema files", schemasFiles.Count());
            foreach (FileInfo schemafile in schemasFiles)
            {
                System.Console.WriteLine("generating from {0} to", schemafile.Name);
                try
                {
                    var schema = JsonSchema4.FromFile(schemafile.FullName);
                    //typescript
                    if (tDirInfo != null)
                    {
                        var generator  = new TypeScriptGenerator(schema);
                        var typeScript = generator.GenerateFile();
                        Save(sDirInfo, tDirInfo, schemafile, typeScript, ".ts");
                    }
                    //c#
                    if (cDirInfo != null)
                    {
                        var generator = new CSharpGenerator(schema);
                        var cSharp    = generator.GenerateFile();
                        Save(sDirInfo, cDirInfo, schemafile, cSharp, ".cs");
                    }
                }
                catch (Exception ex)
                {
                    System.Console.WriteLine("Exception: {0} ", ex.Message);
                    System.Console.WriteLine("Overstep and continue generating remaining?");
                    if (!GetYesOrNoUserInput())
                    {
                        return;
                    }
                }
            }
            //exit
            System.Console.WriteLine("Done!");
            System.Console.WriteLine("Press any key to exit.");
            System.Console.ReadKey();
        }
Exemple #2
0
        /// <summary>
        /// Read json file and return a JObject
        /// </summary>
        /// <param name="proc_guid"></param>
        /// <returns></returns>
        internal JObject readJsonFile(bool debug)
        {
            try
            {
                file_name = (file_name.ToLower().EndsWith(".json")) ? file_name : String.Format("{0}.json", file_name);
                canWrite  = !((file_must_exists) && (dJSONPath.GetFiles(file_name).Count() == 0));

                foreach (FileInfo fi in dJSONPath.GetFiles(file_name))
                {
                    StreamReader   str    = new StreamReader(fi.FullName);
                    JsonTextReader jsr    = new JsonTextReader(str);
                    JObject        JProc  = null;
                    bool           errors = false;

                    try
                    {
                        JsonSerializer jser = new JsonSerializer();
                        JProc = jser.Deserialize <JObject>(jsr);
                        if (JProc == null)
                        {
                            errors = true;
                        }
                    }
                    catch (Exception exc)
                    {
                        jErrors.Add(exc.Message);
                        errors = true;
                    }
                    finally
                    {
                        str.Close();
                    }
                    if ((debug) && (errors))
                    {
                        String filecontent = File.ReadAllText(fi.FullName);
                        String schema      = "";
                        if (filecontent.Contains("$schema"))
                        {
                            schema = filecontent.Substring(filecontent.ToLower().IndexOf("schemas"));
                            schema = schema.Substring(0, schema.IndexOf("\"")).Replace("\\\\", "\\");
                        }
                        if ((schema != "") && (File.Exists(Path.Combine(AppDataPath, schema))))
                        {
                            try
                            {
                                JsonSchema4 JSchema = JsonSchema4.FromFile(Path.Combine(AppDataPath, schema));
                                foreach (ValidationError ve in JSchema.Validate(filecontent))
                                {
                                    jErrors.Add(String.Format("Error in {0} > {1}: {2}", ve.Path, ve.Property, ve.Kind));
                                }

                                errors = jErrors.Count() > 0;
                            }
                            catch (Exception exc)
                            {
                                jErrors.Add(String.Format("Error in json file '{0}': {1}", file_name, exc.Message));
                                canWrite = false;
                            }
                        }
                    }
                    return((!errors)? JProc : new JObject());
                }
            }
            catch (Exception exc)
            {
                jErrors.Add(String.Format("Error in '{0}': {1}", file_name, exc.Message));
                canWrite = false;
            }

            if (canWrite)
            {
                return(new JObject());
            }
            else
            {
                jErrors.Add(String.Format("Program file not found: {0}", file_name));
                return(null);
            }
        }