Esempio n. 1
0
        private AttributeLine CreateAttribute(string attributeName, object attributeValue = null)
        {
            _cmdlet.WriteDebug("CreateAttribute");
            _cmdlet.WriteDebug($"attributeName: {attributeName}");
            _cmdlet.WriteDebug($"attributeValue: {attributeValue}");
            if (attributeValue == null)
            {
                _cmdlet.WriteDebug("no attribute value");
                attributeValue = CreateAttributeValue(attributeName);
            }

            _lines.Add(attributeValue);
            var lineNumber = _lines.Count - 1;

            var attributeLine = new AttributeLine
            {
                Format     = CreateAttributeFormat(attributeName, attributeValue),
                LineNumber = lineNumber,
                Value      = attributeValue
            };

            _cmdlet.WriteDebug($"attributeLine.Format: {attributeLine.Format}");
            _cmdlet.WriteDebug($"attributeLine.LineNumber: {attributeLine.LineNumber}");
            _cmdlet.WriteDebug($"attributeLine.Value: {attributeLine.Value}");

            _attributes[attributeName] = attributeLine;

            return(_attributes[attributeName]);
        }
Esempio n. 2
0
        /// <summary>
        ///     Initializes a new instance of the <see cref="AssemblyInfoFile" /> class with the specified AssemblyInfo file.
        /// </summary>
        /// <param name="path">
        ///     The AssemblyInfo file to parse.
        /// </param>
        /// <param name="ensureAttribute"></param>
        public AssemblyInfoFile(UpdateAssemblyInfo cmdlet, string path, bool?ensureAttribute)
        {
            _cmdlet = cmdlet;
            _cmdlet.WriteDebug("AssemblyInfoFile");
            _cmdlet.WriteDebug("path: " + path);
            _ensureAttribute = ensureAttribute;
            _cmdlet.WriteDebug("ensureAttribute: " + ensureAttribute);
            _language = DetermineFileLanguage(path);

            using (var sr = File.OpenText(path))
            {
                string line;
                var    lineNumber = 0;
                var    isComment  = false;

                _cmdlet.WriteDebug("file begin");
                while ((line = sr.ReadLine()) != null)
                {
                    _cmdlet.WriteDebug(line);
                    _lines.Add(line);

                    if (LineCommentParser.IsMatch(line))
                    {
                        // line comment
                        ++lineNumber;

                        continue;
                    }

                    if (MultilineCommentStartParser.IsMatch(line))
                    {
                        // multiline comment starts
                        ++lineNumber;
                        isComment = true;

                        continue;
                    }

                    if (MultilineCommentEndParser.IsMatch(line) && isComment)
                    {
                        // multiline comment ends
                        ++lineNumber;
                        isComment = false;

                        continue;
                    }

                    if (isComment)
                    {
                        // inside multiline comment
                        ++lineNumber;

                        continue;
                    }

                    var matches = AssemblyAttributeParser.Match(line);
                    if (matches.Success)
                    {
                        // line contains assembly attribute, save result
                        var attributeName = matches.Groups["shortname"].Value;

                        if (!_attributes.ContainsKey(attributeName))
                        {
                            _attributes[attributeName] = new AttributeLine
                            {
                                Format = matches.Groups["start"].Value + matches.Groups["longname"].Value +
                                         matches.Groups["middle"].Value + "{0}" + matches.Groups["end"].Value,
                                LineNumber = lineNumber,
                                Value      = matches.Groups["value"].Value
                            };
                        }
                    }

                    ++lineNumber;
                }

                _cmdlet.WriteDebug("file end");
            }
        }