Esempio n. 1
0
        private static void Run(List <string> messageDirs, List <string> assemblies = null, string outputdir = null, bool interactive = false, string configuration = "Debug", string projectName = "Messages")
        {
            var loggerFactory = new LoggerFactory();

            loggerFactory.AddProvider(
                new ConsoleLoggerProvider(
                    (string text, LogLevel logLevel) => { return(logLevel >= LogLevel.Debug); }, true)
                );
            ApplicationLogging.LoggerFactory = loggerFactory;
            Logger = ApplicationLogging.CreateLogger("Program");

            string        yamlparser_parent = "";
            DirectoryInfo di = new DirectoryInfo(Directory.GetCurrentDirectory());

            while (di != null && di.Name != "YAMLParser")
            {
                di = Directory.GetParent(di.FullName);
            }
            if (di == null)
            {
                throw new InvalidOperationException("Not started from within YAMLParser directory.");
            }
            di = Directory.GetParent(di.FullName);
            yamlparser_parent = di.FullName;

            if (outputdir == null)
            {
                outputdir = yamlparser_parent;
                outputdir = Path.Combine(outputdir, DEFAULT_OUTPUT_FOLDERNAME);
            }

            Templates.LoadTemplateStrings(Path.Combine(yamlparser_parent, "YAMLParser", "TemplateProject"));

            MessageTypeRegistry.Default.ParseAssemblyAndRegisterRosMessages(MessageTypeRegistry.Default.GetType().GetTypeInfo().Assembly);

            if (assemblies != null)
            {
                string hints = "";
                foreach (var assembly in assemblies)
                {
                    var rosNetMessages = Assembly.LoadFile(Path.GetFullPath(assembly));
                    MessageTypeRegistry.Default.ParseAssemblyAndRegisterRosMessages(rosNetMessages);
                    hints += $@"
  <ItemGroup>
    <Reference Include=""Messages"">
      <HintPath>{assembly}</HintPath>
  
      </Reference>
  
    </ItemGroup>

  ";
                }

                Templates.MessagesProj = Templates.MessagesProj.Replace("$$HINTS$$", hints);
            }
            else
            {
                Templates.MessagesProj = Templates.MessagesProj.Replace("$$HINTS$$", "");
            }

            var paths               = new List <MsgFileLocation>();
            var pathssrv            = new List <MsgFileLocation>();
            var actionFileLocations = new List <MsgFileLocation>();

            Console.WriteLine("Generatinc C# classes for ROS Messages...\n");
            foreach (var messageDir in messageDirs)
            {
                string d = new DirectoryInfo(Path.GetFullPath(messageDir)).FullName;
                Console.WriteLine("Looking in " + d);
                MsgFileLocator.findMessages(paths, pathssrv, actionFileLocations, d);
            }

            // first pass: create all msg files (and register them in static resolver dictionary)
            var baseTypes = MessageTypeRegistry.Default.GetTypeNames().ToList();

            foreach (MsgFileLocation path in paths)
            {
                var typeName = $"{path.package}/{path.basename}";
                if (baseTypes.Contains(typeName))
                {
                    Logger.LogInformation($"Skip file {path} because MessageBase already contains this message");
                }
                else
                {
                    msgsFiles.Add(new MsgFile(path));
                }
            }
            Logger.LogDebug($"Added {msgsFiles.Count} message files");

            foreach (MsgFileLocation path in pathssrv)
            {
                srvFiles.Add(new SrvFile(path));
            }

            // secend pass: parse and resolve types
            foreach (var msg in msgsFiles)
            {
                msg.ParseAndResolveTypes();
            }
            foreach (var srv in srvFiles)
            {
                srv.ParseAndResolveTypes();
            }

            var actionFileParser = new ActionFileParser(actionFileLocations);

            actionFiles = actionFileParser.GenerateRosMessageClasses();
            //var actionFiles = new List<ActionFile>();

            if (paths.Count + pathssrv.Count > 0)
            {
                MakeTempDir(outputdir);
                GenerateFiles(msgsFiles, srvFiles, actionFiles, outputdir);
                GenerateProject(msgsFiles, srvFiles, projectName, outputdir);
                BuildProject(configuration, projectName, outputdir);
            }
            else
            {
                Console.WriteLine("Usage:         YAMLParser.exe <SolutionFolder> [... other directories to search]\n      The Messages dll will be output to <SolutionFolder>/Messages/Messages.dll");
                if (interactive)
                {
                    Console.ReadLine();
                }
                Environment.Exit(1);
            }
            if (interactive)
            {
                Console.WriteLine("Finished. Press enter.");
                Console.ReadLine();
            }
        }