public CodeGenerator(ITextTemplatingEngineHost host, ITextTemplatingSessionHost textTemplatingSessionHost, ITextTemplating textTemplating, ISolutionManager solutionManager, string rosMessagesProjectName, string rosMessageTypeAttributeName, string rosMessageTypeAttributeNamespace)
        {
            if (null == host)
            {
                throw new ArgumentNullException(nameof(host));
            }

            if (null == textTemplatingSessionHost)
            {
                throw new ArgumentNullException(nameof(textTemplatingSessionHost));
            }

            if (null == textTemplating)
            {
                throw new ArgumentNullException(nameof(textTemplating));
            }

            if (null == solutionManager)
            {
                throw new ArgumentNullException(nameof(solutionManager));
            }

            if (string.IsNullOrWhiteSpace(rosMessagesProjectName))
            {
                throw new ArgumentException("Parameter cannot be empty!", nameof(rosMessagesProjectName));
            }

            if (string.IsNullOrWhiteSpace(rosMessageTypeAttributeName))
            {
                throw new ArgumentException("Parameter cannot be empty!", nameof(rosMessageTypeAttributeName));
            }

            if (string.IsNullOrWhiteSpace(rosMessageTypeAttributeNamespace))
            {
                throw new ArgumentException("Parameter cannot be empty!", nameof(rosMessageTypeAttributeNamespace));
            }

            _textTemplatingEngineHost         = host;
            _textTemplating                   = textTemplating;
            _textTemplatingSessionHost        = textTemplatingSessionHost;
            _solutionManager                  = solutionManager;
            _rosMessageTypeAttributeName      = rosMessageTypeAttributeName;
            _rosMessageTypeAttributeNamespace = rosMessageTypeAttributeNamespace;

            _defaultNamespace = rosMessagesProjectName;
            _rosMessageCodeGenerationTemplatePath    = _textTemplatingEngineHost.ResolvePath(ROS_MESSAGE_CODE_GENERATION_TEMPLATE_RELATIVE_PATH);
            _rosMessageCodeGenerationTemplateContent = ReadAllTextFromFile(_rosMessageCodeGenerationTemplatePath);
            _customTimeDataTemplatePath = _textTemplatingEngineHost.ResolvePath(CUSTOM_TIME_DATA_TEMPLATE_RELATIVE_PATH);
            _solutionManager.Initialize();
        }
Exemplo n.º 2
0
        public AstDefinition GetDefinition()
        {
            var fullPath = _host.ResolvePath(_file);

            var lines = System.IO.File.ReadLines(fullPath);

            var definition = new AstDefinition();

            AbstractSyntaxTree currentAst = null; // the root AST node type, which is generated as an abstract class, and all other node types inherit from.

            foreach (var line in lines)
            {
                // check if the line we're dealing with is an AST definition, or an AST node-type definition.
                if (!line.Contains(":"))
                {
                    var isFirst = currentAst == null;
                    currentAst = new AbstractSyntaxTree();
                    var name = line.Trim();
                    if (line.EndsWith("<>"))
                    {
                        currentAst.Name           = line.Substring(0, line.Length - 2);
                        currentAst.HasVisitResult = true;
                    }
                    else
                    {
                        currentAst.Name = line.Trim();
                    }
                    currentAst.IsFirst = isFirst;
                    definition.Trees.Add(currentAst);
                }
                else
                {
                    // extract node type information: name and fields (type and name of each).
                    var segments  = line.Split(new[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
                    var className = segments[0].Trim();
                    var fields    = segments[1].Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
                                    .Select(fieldsSegment =>
                    {
                        var fieldSegments = fieldsSegment.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                        // key: field name, value: field type
                        return(new KeyValuePair <FieldName, string>(new FieldName(fieldSegments[1].Trim()), fieldSegments[0].Trim()));
                    })
                                    .ToList();

                    currentAst.NodeTypes.Add(new NodeType
                    {
                        Name   = className,
                        Fields = fields
                    });
                }
            }

            return(definition);
        }
Exemplo n.º 3
0
        public void ProcessTemplate(string templateFileName, string outputFileName)
        {
            string templateDirectory = Path.GetDirectoryName(_host.TemplateFile);
            string outputFilePath    = Path.Combine(templateDirectory, outputFileName);

            string template = File.ReadAllText(_host.ResolvePath(templateFileName));
            string output   = _engine.ProcessTemplate(template, _host);

            File.WriteAllText(outputFilePath, output);

            ProjectItem templateProjectItem = _getTemplateProjectItem();

            templateProjectItem.ProjectItems.AddFromFile(outputFilePath);

            _savedOutputs.Add(outputFileName);
        }