Пример #1
0
        public Rewriter(SemanticModel semanticModel, CodeTemplate template, ArtifactOutput output, ErrorAggregator aggregator)
        {
            this._semanticModel = semanticModel;
            this._output        = output;
            this._template      = template;
            this._aggregator    = aggregator;

            this._indentType   = _template.TypeIndent;
            this._indentMember = _template.MemberIndent;
        }
Пример #2
0
        public Rewriter(SemanticModel semanticModel, CodeTemplate template, ArtifactOutput output, ErrorAggregator aggregator)
        {
            this._semanticModel = semanticModel;
            this._output = output;
            this._template = template;
            this._aggregator = aggregator;

            this._indentType = _template.TypeIndent;
            this._indentMember = _template.MemberIndent;
        }
Пример #3
0
        public static IList<SyntaxTree> Sort(Compilation c, ErrorAggregator aggregator)
        {
            List<SyntaxTreeRelationship> rawList = new List<SyntaxTreeRelationship>();

            foreach (var tree in c.SyntaxTrees)
            {
                SyntaxTreeRelationship relation = new SyntaxTreeRelationship(tree);
                SemanticModel model = c.GetSemanticModel(tree);
                TypeWalker w = new TypeWalker(model, relation);
                w.Visit(tree.GetRoot());
                rawList.Add(relation);
            }

            int max = rawList.Count;

            List<SyntaxTree> sortedList = new List<SyntaxTree>();

            for (int times = 0; times < max; times++)
            {
                for (var i = 0; i < rawList.Count; i++)
                {
                    if (IsIndependent(rawList, i))
                    {
                        sortedList.Add(rawList[i].Tree);
                        rawList.RemoveAt(i);
                    }
                }
            }

            if (rawList.Count > 0)
            {
                CompileIssue issue = CompileIssue.CreateNoLocationIssue(IssueType.Error, IssueId.CrossRef);
                aggregator.AppendIssue(issue);

                //Note:use original order. use sortedList will miss cross ref class.
                return rawList.ConvertAll(r => r.Tree);
            }

            return sortedList;
        }
Пример #4
0
        public static IList <SyntaxTree> Sort(Compilation c, ErrorAggregator aggregator)
        {
            List <SyntaxTreeRelationship> rawList = new List <SyntaxTreeRelationship>();

            foreach (var tree in c.SyntaxTrees)
            {
                SyntaxTreeRelationship relation = new SyntaxTreeRelationship(tree);
                SemanticModel          model    = c.GetSemanticModel(tree);
                TypeWalker             w        = new TypeWalker(model, relation);
                w.Visit(tree.GetRoot());
                rawList.Add(relation);
            }

            int max = rawList.Count;

            List <SyntaxTree> sortedList = new List <SyntaxTree>();

            for (int times = 0; times < max; times++)
            {
                for (var i = 0; i < rawList.Count; i++)
                {
                    if (IsIndependent(rawList, i))
                    {
                        sortedList.Add(rawList[i].Tree);
                        rawList.RemoveAt(i);
                    }
                }
            }

            if (rawList.Count > 0)
            {
                CompileIssue issue = CompileIssue.CreateNoLocationIssue(IssueType.Error, IssueId.CrossRef);
                aggregator.AppendIssue(issue);

                //Note:use original order. use sortedList will miss cross ref class.
                return(rawList.ConvertAll(r => r.Tree));
            }

            return(sortedList);
        }
Пример #5
0
        public void InitializeCompiler(string csprojPath, CompileOptions options = null, string supressWarnings = "")
        {
            _options = (options != null) ? options : CompileOptions.DEFAULT;

            _options.StdError.Information($"{Constants.SharpJs} compile: project: {Path.GetFileNameWithoutExtension(csprojPath)}, configuration: {options.ProjectConfig}", true);

            string[] numbers = supressWarnings.Split(',', StringSplitOptions.RemoveEmptyEntries);
            var      q       = from n in numbers
                               select int.Parse(n);

            _aggregator = new ErrorAggregator(q);


            #region Parse C# project file
            _startPath = Path.GetDirectoryName(csprojPath);
            XDoc doc = XDoc.LoadFromFile(csprojPath);
            doc.AddNamespace("x", "http://schemas.microsoft.com/developer/msbuild/2003");

            _outputScript = doc.GetStringValue(@"x:PropertyGroup/x:AssemblyName") + ArtifactsFactory.JavaScriptFileExtension;


            var  cfgList     = doc.NavigateToList(@"x:PropertyGroup[@Condition]");
            bool configExist = false;
            foreach (var cfg in cfgList)
            {
                string condition = cfg.GetStringValue("@Condition");
                if (condition.Contains(options.ProjectConfig))
                {
                    var outpath = cfg.GetStringValue("x:OutputPath");
                    _outputDir  = Path.Combine(_startPath, outpath);
                    configExist = true;
                    break;
                }
            }
            if (!configExist)
            {
                _aggregator.AppendIssue(CompileIssue.CreateNoLocationIssue(IssueType.Error, IssueId.BuildConfigError, options.ProjectConfig));
                return;
            }

            //Note:Project ref, get the assembly name and should be able to found under output
            var rl = doc.NavigateToList(@"x:ItemGroup/x:ProjectReference");
            foreach (var r in rl)
            {
                var  refPrjPath = r.GetStringValue(@"@Include");
                XDoc xref       = XDoc.LoadFromFile(Path.Combine(_startPath, refPrjPath));
                xref.AddNamespace("x", "http://schemas.microsoft.com/developer/msbuild/2003");
                var libName = xref.GetStringValue(@"x:PropertyGroup/x:AssemblyName");

                string assemblyPath = Path.GetFullPath(Path.Combine(_outputDir, libName + ".dll"));

                _refPathList.Add(assemblyPath);
                CheckAssembly(assemblyPath);
            }

            rl = doc.NavigateToList(@"x:ItemGroup/x:Reference");
            foreach (var r in rl)
            {
                string hint = r.GetStringValue(@"x:HintPath");
                if (!string.IsNullOrEmpty(hint))//Note: Add external assembly.
                {
                    string assemblyPath = Path.GetFullPath(Path.Combine(_startPath, hint));
                    _refPathList.Add(assemblyPath);
                    if (!CheckAssembly(assemblyPath))
                    {
                        var issue = CompileIssue.CreateNoLocationIssue(IssueType.Warning, IssueId.InvalidAssembly, assemblyPath);
                        _aggregator.AppendIssue(issue);
                    }
                }
            }

            //Note:Item to be compiled.
            var cl = doc.NavigateToList(@"x:ItemGroup/x:Compile");
            foreach (var c in cl)
            {
                string path = c.GetStringValue("@Include");
                if (path == @"Properties\AssemblyInfo.cs")
                {
                    continue;
                }
                _compilePathList.Add(Path.Combine(_startPath, path));
            }

            _template = CodeTemplate.Parse(Path.Combine(_startPath, @"Properties\template.xml"));
            #endregion
        }
Пример #6
0
        public void InitializeCompiler(string csprojPath, CompileOptions options = null, string supressWarnings = "")
        {
            _options = (options != null) ? options : CompileOptions.DEFAULT;

            _options.StdError.Information($"{Constants.SharpJs} compile: project: {Path.GetFileNameWithoutExtension(csprojPath)}, configuration: {options.ProjectConfig}", true);

            string[] numbers = supressWarnings.Split(',', StringSplitOptions.RemoveEmptyEntries);
            var q = from n in numbers
                    select int.Parse(n);
            _aggregator = new ErrorAggregator(q);


            #region Parse C# project file
            _startPath = Path.GetDirectoryName(csprojPath);
            XDoc doc = XDoc.LoadFromFile(csprojPath);
            doc.AddNamespace("x", "http://schemas.microsoft.com/developer/msbuild/2003");

            _outputScript = doc.GetStringValue(@"x:PropertyGroup/x:AssemblyName") + ArtifactsFactory.JavaScriptFileExtension;


            var cfgList = doc.NavigateToList(@"x:PropertyGroup[@Condition]");
            bool configExist = false;
            foreach (var cfg in cfgList)
            {
                string condition = cfg.GetStringValue("@Condition");
                if (condition.Contains(options.ProjectConfig))
                {
                    var outpath = cfg.GetStringValue("x:OutputPath");
                    _outputDir = Path.Combine(_startPath, outpath);
                    configExist = true;
                    break;
                }
            }
            if (!configExist)
            {
                _aggregator.AppendIssue(CompileIssue.CreateNoLocationIssue(IssueType.Error, IssueId.BuildConfigError, options.ProjectConfig));
                return;
            }

            //Note:Project ref, get the assembly name and should be able to found under output
            var rl = doc.NavigateToList(@"x:ItemGroup/x:ProjectReference");
            foreach (var r in rl)
            {
                var refPrjPath = r.GetStringValue(@"@Include");
                XDoc xref = XDoc.LoadFromFile(Path.Combine(_startPath, refPrjPath));
                xref.AddNamespace("x", "http://schemas.microsoft.com/developer/msbuild/2003");
                var libName = xref.GetStringValue(@"x:PropertyGroup/x:AssemblyName");

                string assemblyPath = Path.GetFullPath(Path.Combine(_outputDir, libName + ".dll"));

                _refPathList.Add(assemblyPath);
                CheckAssembly(assemblyPath);
            }

            rl = doc.NavigateToList(@"x:ItemGroup/x:Reference");
            foreach (var r in rl)
            {
                string hint = r.GetStringValue(@"x:HintPath");
                if (!string.IsNullOrEmpty(hint))//Note: Add external assembly.
                {
                    string assemblyPath = Path.GetFullPath(Path.Combine(_startPath, hint));
                    _refPathList.Add(assemblyPath);
                    if (!CheckAssembly(assemblyPath))
                    {
                        var issue = CompileIssue.CreateNoLocationIssue(IssueType.Warning, IssueId.InvalidAssembly, assemblyPath);
                        _aggregator.AppendIssue(issue);
                    }
                }
            }

            //Note:Item to be compiled.
            var cl = doc.NavigateToList(@"x:ItemGroup/x:Compile");
            foreach (var c in cl)
            {
                string path = c.GetStringValue("@Include");
                if (path == @"Properties\AssemblyInfo.cs")
                    continue;
                _compilePathList.Add(Path.Combine(_startPath, path));
            }

            _template = CodeTemplate.Parse(Path.Combine(_startPath, @"Properties\template.xml"));
            #endregion
        }