public string MinifyFiles(string[] csFiles) { CSharpParser parser = new CSharpParser(); SyntaxTree[] trees = csFiles.Select(file => parser.Parse(file, file + "_" + ParserTempFileName)).ToArray(); SyntaxTree globalTree = new SyntaxTree(); globalTree.FileName = ParserTempFileName; var usings = new List <UsingDeclaration>(); var types = new List <TypeDeclaration>(); foreach (var tree in trees) { List <UsingDeclaration> treeUsings = new List <UsingDeclaration>(); GetUsingsAndRemoveUsingsAndNamespaces(tree, treeUsings, types); usings.AddRange(treeUsings.Where(u1 => !usings.Exists(u2 => u2.Namespace == u1.Namespace))); } foreach (var u in usings) { globalTree.AddChild(u.Clone(), new Role <AstNode>("UsingDeclaration")); } foreach (var t in types) { globalTree.AddChild(t.Clone(), new Role <AstNode>("TypeDeclaration")); } SyntaxTree = globalTree; return(Minify()); }
private void SetAttributeValueOrAddAttributeIfNotDefault( SyntaxTree syntaxTree, string attributeName, object value, object defaultValue, Expression valueExpression) { if (value == null) { return; } var attribute = syntaxTree.Children.OfType <AttributeSection>().SelectMany(x => x.Attributes) .FirstOrDefault(x => x.Type is SimpleType && ((SimpleType)x.Type).Identifier == attributeName); if (attribute != null) { attribute.Arguments.Clear(); attribute.Arguments.Add(valueExpression); } else if (!value.Equals(defaultValue)) { attribute = new Attribute { Type = new SimpleType(attributeName) }; attribute.Arguments.Add(valueExpression); var attributeSection = new AttributeSection(attribute) { AttributeTarget = "assembly" }; syntaxTree.AddChild(attributeSection, new NRefactory.Role <AttributeSection>("Member")); } }
private void AddNamespaceUsingIfNotExist(SyntaxTree syntaxTree, string @namespace) { AstNode nodeToInsertAfter = null; foreach (var usingDeclaration in syntaxTree.Children.OfType <UsingDeclaration>()) { if (usingDeclaration.Namespace == @namespace) { return; } if (string.Compare(usingDeclaration.Namespace, @namespace, StringComparison.InvariantCulture) < 0) { nodeToInsertAfter = usingDeclaration; } } if (nodeToInsertAfter != null) { syntaxTree.InsertChildAfter(nodeToInsertAfter, new UsingDeclaration(@namespace), new Role <UsingDeclaration>("Using")); } else if (syntaxTree.HasChildren) { syntaxTree.InsertChildBefore(syntaxTree.FirstChild, new UsingDeclaration(@namespace), new Role <UsingDeclaration>("Using")); } else { syntaxTree.AddChild(new UsingDeclaration(@namespace), new Role <UsingDeclaration>("Using")); } }
public string MinifyFiles(string[] csFiles) { CSharpParser parser = new CSharpParser(); SyntaxTree[] trees = csFiles.Select(file => parser.Parse(file, file + "_" + ParserTempFileName)).ToArray(); SyntaxTree globalTree = new SyntaxTree(); globalTree.FileName = ParserTempFileName; var usings = new List<UsingDeclaration>(); var types = new List<TypeDeclaration>(); foreach (var tree in trees) { List<UsingDeclaration> treeUsings = new List<UsingDeclaration>(); GetUsingsAndRemoveUsingsAndNamespaces(tree, treeUsings, types); usings.AddRange(treeUsings.Where(u1 => !usings.Exists(u2 => u2.Namespace == u1.Namespace))); } foreach (var u in usings) globalTree.AddChild(u.Clone(), new Role<AstNode>("UsingDeclaration")); foreach (var t in types) globalTree.AddChild(t.Clone(), new Role<AstNode>("TypeDeclaration")); SyntaxTree = globalTree; return Minify(); }