Example #1
0
        public HubCSharpFile(string file)
        {
            Name     = Path.GetFileNameWithoutExtension(file);
            FileName = file;

            FileText = Helpers.SafelyReadFromFile(file);


            Syntax = CSharpSyntaxTree.ParseText(FileText, new CSharpParseOptions(LanguageVersion.Latest, DocumentationMode.None, SourceCodeKind.Regular));

            Context = new GenerationContext();
            Root    = Syntax.GetRoot() as CompilationUnitSyntax;
            var usingStatements = Root.DescendantNodes().OfType <UsingDirectiveSyntax>().ToList();

            Regex allowedUsings;
            Regex unallowedUsings;

            if (Settings.AllowedNamespaces?.Any() ?? false)
            {
                allowedUsings = new Regex($"({string.Join("|", Settings.AllowedNamespaces)})");
            }
            else
            {
                allowedUsings = new Regex($"(.+)");
            }

            if (Settings.ExcludedNamespaces?.Any() ?? false)
            {
                unallowedUsings = new Regex($"({string.Join("|", Settings.ExcludedNamespaces)})");
            }
            else
            {
                unallowedUsings = new Regex($"(^[.]+)");
            }

            Context.UsingStatements = usingStatements.Select(x => x.WithoutLeadingTrivia().WithoutTrailingTrivia().ToFullString())
                                      .Where(x => allowedUsings.IsMatch(x) &&
                                             !unallowedUsings.IsMatch(x))
                                      .ToList();

            var namespaceDeclarations = Root.DescendantNodes().OfType <NamespaceDeclarationSyntax>().ToList();

            foreach (var nsd in namespaceDeclarations)
            {
                var classDeclarations = nsd.DescendantNodes().OfType <ClassDeclarationSyntax>().ToList();

                foreach (var cd in classDeclarations)
                {
                    Context.HubClients.Add(ClassParser.ReadClassAsHubController(cd));
                }
            }
        }