Exemplo n.º 1
0
        void DoTypeCheck(TypeCheckChange type)
        {
            if (_typeChecker == null)
            {
                type = TypeCheckChange.Options;
            }
            if (BuildOnceOnly)
            {
                type = TypeCheckChange.Once;
            }
            switch (type)
            {
            case TypeCheckChange.None:
                return;

            case TypeCheckChange.Small:
                _typeChecker.ClearDiagnostics();
                _typeChecker.TriggerUpdate();
                break;

            case TypeCheckChange.Input:
                _typeChecker.ClearDiagnostics();
                _typeChecker.UpdateProgram(MakeSourceListArray());
                _typeChecker.TriggerUpdate();
                break;

            case TypeCheckChange.Options:
                if (_typeChecker != null)
                {
                    CompilerPool.ReleaseTs(_typeChecker);
                    _typeChecker = null;
                }
                _typeChecker = CompilerPool.GetTs(_diskCache, CompilerOptions);
                _typeChecker.ClearDiagnostics();
                _typeChecker.CreateProgram(CurrentDirectory, MakeSourceListArray());
                break;

            case TypeCheckChange.Once:
                if (_typeChecker != null)
                {
                    CompilerPool.ReleaseTs(_typeChecker);
                    _typeChecker = null;
                }
                _typeChecker = CompilerPool.GetTs(_diskCache, CompilerOptions);
                _typeChecker.ClearDiagnostics();
                _typeChecker.CheckProgram(CurrentDirectory, MakeSourceListArray());
                _lastSemantics = _typeChecker.GetDiagnostics();
                CompilerPool.ReleaseTs(_typeChecker);
                _typeChecker = null;
                return;
            }
            _lastSemantics = _typeChecker.GetDiagnostics();
        }
Exemplo n.º 2
0
        void Transpile(TsFileAdditionalInfo info)
        {
            ITSCompiler compiler = null;

            try
            {
                compiler = BuildCtx.CompilerPool.GetTs(Owner.DiskCache, BuildCtx.CompilerOptions);
                //_owner.Logger.Info("Transpiling " + info.Owner.FullPath);
                var result = compiler.Transpile(info.Owner.FullPath, info.Owner.Utf8Content);
                if (result.Diagnostics != null)
                {
                    info.ReportDiag(result.Diagnostics);
                    info.HasError = result.Diagnostics.Any(d => d.IsError);
                }
                else
                {
                    info.HasError = false;
                }

                if (info.HasError)
                {
                    info.Output  = null;
                    info.MapLink = null;
                }
                else
                {
                    info.Output  = SourceMap.RemoveLinkToSourceMap(result.JavaScript);
                    info.MapLink = SourceMap.Parse(result.SourceMap, info.Owner.Parent.FullPath);
                }
            }
            finally
            {
                if (compiler != null)
                {
                    BuildCtx.CompilerPool.ReleaseTs(compiler);
                }
            }

            if (info.HasError)
            {
                info.SourceInfo = null;
                return;
            }

            var backupCurrentlyTranspiling = _currentlyTranspiling;

            try
            {
                if (_currentlyTranspiling == null)
                {
                    _currentlyTranspiling = info;
                }

                var parser   = new Parser(new Options(), info.Output);
                var toplevel = parser.Parse();
                toplevel.FigureOutScope();
                var ctx = new ResolvingConstEvalCtx(info.Owner.FullPath, this);

                string Resolver(IConstEvalCtx myctx, string text)
                {
                    return(ResolverWithPossibleForcingResource(myctx, text, false));
                }

                string ResolverWithPossibleForcingResource(IConstEvalCtx myctx, string text, bool forceResource)
                {
                    if (text.StartsWith("project:", StringComparison.Ordinal))
                    {
                        var(pref, name) = SplitProjectAssetName(text);
                        return(pref + ResolverWithPossibleForcingResource(myctx, name, true));
                    }

                    if (text.StartsWith("resource:", StringComparison.Ordinal))
                    {
                        return("resource:" +
                               ResolverWithPossibleForcingResource(myctx, text.Substring("resource:".Length), true));
                    }

                    if (text.StartsWith("node_modules/", StringComparison.Ordinal))
                    {
                        var res2 = ResolveImport(info.Owner.FullPath, text.Substring("node_modules/".Length), false,
                                                 true, forceResource, true);
                        return(res2 == "?" ? text : res2);
                    }

                    var res = PathUtils.Join(PathUtils.Parent(myctx.SourceName), text);

                    return(res);
                }

                var sourceInfo = GatherBobrilSourceInfo.Gather(toplevel, ctx, Resolver);
                info.SourceInfo = sourceInfo;
                AddDependenciesFromSourceInfo(info);
            }
            catch (SyntaxError error)
            {
                var pos = info.MapLink?.FindPosition(error.Position.Line, error.Position.Column) ??
                          new SourceCodePosition {
                    Line = error.Position.Line, Col = error.Position.Column
                };
                info.ReportDiag(true, -16, error.Message, pos.Line, pos.Col, pos.Line, pos.Col);
                info.SourceInfo = null;
            }
            finally
            {
                _currentlyTranspiling = backupCurrentlyTranspiling;
            }
        }
Exemplo n.º 3
0
        void Transpile(TSFileAdditionalInfo info)
        {
            ITSCompiler compiler = null;

            try
            {
                compiler = BuildCtx.CompilerPool.GetTs(Owner.DiskCache, BuildCtx.CompilerOptions);
                //_owner.Logger.Info("Transpiling " + info.Owner.FullPath);
                var result = compiler.Transpile(info.Owner.FullPath, info.Owner.Utf8Content);
                if (result.Diagnostics != null)
                {
                    info.ReportDiag(result.Diagnostics);
                    info.HasError = result.Diagnostics.Any(d => d.IsError);
                }
                else
                {
                    info.HasError = false;
                }
                if (info.HasError)
                {
                    info.Output  = null;
                    info.MapLink = null;
                }
                else
                {
                    info.Output  = SourceMap.RemoveLinkToSourceMap(result.JavaScript);
                    info.MapLink = SourceMap.Parse(result.SourceMap, info.Owner.Parent.FullPath);
                }
            }
            finally
            {
                if (compiler != null)
                {
                    BuildCtx.CompilerPool.ReleaseTs(compiler);
                }
            }

            if (info.HasError)
            {
                info.SourceInfo = null;
                return;
            }
            var backupCurrentlyTranspiling = _currentlyTranspiling;

            try
            {
                if (_currentlyTranspiling == null)
                {
                    _currentlyTranspiling = info;
                }
                var parser   = new Parser(new Options(), info.Output);
                var toplevel = parser.Parse();
                toplevel.FigureOutScope();
                var ctx = new ResolvingConstEvalCtx(info.Owner.FullPath, this);

                string resolver(IConstEvalCtx myctx, string text)
                {
                    if (text.StartsWith("resource:", StringComparison.Ordinal))
                    {
                        return("resource:" + resolver(myctx, text.Substring("resource:".Length)));
                    }
                    if (text.StartsWith("node_modules/", StringComparison.Ordinal))
                    {
                        return(ResolveImport(info.Owner.FullPath, text.Substring("node_modules/".Length), false, true));
                    }
                    var res = PathUtils.Join(PathUtils.Parent(myctx.SourceName), text);

                    return(res);
                }

                var sourceInfo = GatherBobrilSourceInfo.Gather(toplevel, ctx, resolver);
                info.SourceInfo = sourceInfo;
                AddDependenciesFromSourceInfo(info);
            }
            finally
            {
                _currentlyTranspiling = backupCurrentlyTranspiling;
            }
        }
Exemplo n.º 4
0
        public void Build(BuildCtx buildCtx, BuildResult buildResult, int iterationId)
        {
            var tryDetectChanges = !buildCtx.ProjectStructureChanged;

            buildResult.HasError = false;
            if (!buildResult.Incremental || !tryDetectChanges)
            {
                buildResult.RecompiledIncrementaly.Clear();
            }
            var buildModuleCtx = new BuildModuleCtx()
            {
                BuildCtx    = buildCtx,
                Owner       = this,
                Result      = buildResult,
                ToCheck     = new OrderedHashSet <string>(),
                IterationId = iterationId
            };

            try
            {
                ProjectOptions.BuildCache.StartTransaction();
                ITSCompiler compiler = null;
                try
                {
                    if (!tryDetectChanges)
                    {
                        if (!ProjectOptions.TypeScriptVersionOverride && DevDependencies != null &&
                            DevDependencies.Contains("typescript"))
                        {
                            ProjectOptions.Tools.SetTypeScriptPath(Owner.FullPath);
                        }
                        else
                        {
                            ProjectOptions.Tools.SetTypeScriptVersion(ProjectOptions.TypeScriptVersion);
                        }
                    }
                    compiler = buildCtx.CompilerPool.GetTs(DiskCache, buildCtx.CompilerOptions);
                    var trueTSVersion = compiler.GetTSVersion();
                    buildCtx.ShowTsVersion(trueTSVersion);
                    ProjectOptions.ConfigurationBuildCacheId = ProjectOptions.BuildCache.MapConfiguration(trueTSVersion,
                                                                                                          JsonConvert.SerializeObject(buildCtx.CompilerOptions, Formatting.None, TSCompilerOptions.GetSerializerSettings()));
                }
                finally
                {
                    if (compiler != null)
                    {
                        buildCtx.CompilerPool.ReleaseTs(compiler);
                    }
                }
                if (buildModuleCtx.Result.CommonSourceDirectory == null)
                {
                    buildModuleCtx.Result.CommonSourceDirectory = Owner.FullPath;
                }
                buildResult.TaskForSemanticCheck = buildCtx.StartTypeCheck().ContinueWith((task) =>
                {
                    if (task.IsCompletedSuccessfully)
                    {
                        buildResult.SemanticResult = task.Result;
                        buildResult.NotifySemanticResult(task.Result);
                    }
                });
                if (tryDetectChanges)
                {
                    if (!buildModuleCtx.CrawlChanges())
                    {
                        buildResult.Incremental = true;
                        goto noDependencyChangeDetected;
                    }
                    buildCtx.ProjectStructureChanged = true;
                    buildResult.Incremental          = false;
                    buildModuleCtx.Result.JavaScriptAssets.Clear();
                    foreach (var info in buildModuleCtx.Result.Path2FileInfo)
                    {
                        info.Value.IterationId = iterationId - 1;
                    }
                }
                buildModuleCtx.CrawledCount = 0;
                buildModuleCtx.ToCheck.Clear();
                ProjectOptions.HtmlHeadExpanded = buildModuleCtx.ExpandHtmlHead(ProjectOptions.HtmlHead);
                if (buildCtx.MainFile != null)
                {
                    buildModuleCtx.CheckAdd(PathUtils.Join(Owner.FullPath, buildCtx.MainFile), FileCompilationType.Unknown);
                }
                if (buildCtx.ExampleSources != null)
                {
                    foreach (var src in buildCtx.ExampleSources)
                    {
                        buildModuleCtx.CheckAdd(PathUtils.Join(Owner.FullPath, src), FileCompilationType.Unknown);
                    }
                }
                if (buildCtx.TestSources != null)
                {
                    foreach (var src in buildCtx.TestSources)
                    {
                        buildModuleCtx.CheckAdd(PathUtils.Join(Owner.FullPath, src), FileCompilationType.Unknown);
                    }
                }

                if (ProjectOptions.IncludeSources != null)
                {
                    foreach (var src in ProjectOptions.IncludeSources)
                    {
                        buildModuleCtx.CheckAdd(PathUtils.Join(Owner.FullPath, src), FileCompilationType.Unknown);
                    }
                }

                buildModuleCtx.Crawl();
                noDependencyChangeDetected :;
                ProjectOptions.CommonSourceDirectory = buildModuleCtx.Result.CommonSourceDirectory;
                if (ProjectOptions.SpriteGeneration)
                {
                    ProjectOptions.SpriteGenerator.ProcessNew();
                }
                var hasError = false;
                foreach (var item in buildModuleCtx.Result.Path2FileInfo)
                {
                    if (item.Value.HasError)
                    {
                        hasError = true;
                        break;
                    }
                }
                buildModuleCtx.Result.HasError = hasError;
                if (ProjectOptions.BuildCache.IsEnabled)
                {
                    buildModuleCtx.StoreResultToBuildCache(buildModuleCtx.Result);
                }
            }
            finally
            {
                ProjectOptions.BuildCache.EndTransaction();
            }
        }