Ejemplo n.º 1
0
        TsFileAdditionalInfo ReportDependency(TsFileAdditionalInfo owner, TsFileAdditionalInfo dep)
        {
            if (dep != null)
            {
                owner.ReportDependency(dep.Owner.FullPath);
            }

            return(dep);
        }
Ejemplo n.º 2
0
        bool TryToResolveFromBuildCache(TsFileAdditionalInfo itemInfo)
        {
            itemInfo.TakenFromBuildCache = false;
            var bc = Owner.ProjectOptions.BuildCache;

            if (bc.IsEnabled)
            {
                byte[] hashOfContent;
                try
                {
                    hashOfContent = itemInfo.Owner.HashOfContent;
                }
                catch
                {
                    // File was probably renamed or deleted
                    return(false);
                }

                var confId = Owner.ProjectOptions.ConfigurationBuildCacheId;
                var fbc    = bc.FindTSFileBuildCache(hashOfContent, confId);
                if (fbc != null)
                {
                    if (MatchingTranspilationDendencies(itemInfo.Owner, fbc.TranspilationDependencies))
                    {
                        if (MakeSourceInfoAbsolute(fbc.SourceInfo, itemInfo.Owner.FullPath))
                        {
                            itemInfo.Output  = fbc.Output;
                            itemInfo.MapLink = fbc.MapLink;
                            if (itemInfo.MapLink?.sources?.Count == 1)
                            {
                                itemInfo.MapLink.sources[0] = itemInfo.Owner.FullPath;
                            }

                            itemInfo.SourceInfo = fbc.SourceInfo;
                            itemInfo.TranspilationDependencies = fbc.TranspilationDependencies;
                            itemInfo.TakenFromBuildCache       = true;
                            AddDependenciesFromSourceInfo(itemInfo);
                            //_owner.Logger.Info("Loaded from cache " + itemInfo.Owner.FullPath);
                            return(true);
                        }
                    }
                }
            }

            return(false);
        }
Ejemplo n.º 3
0
        void ReportDependenciesFromCss(TsFileAdditionalInfo info)
        {
            if (info.TranspilationDependencies != null)
            {
                foreach (var dep in info.TranspilationDependencies)
                {
                    var fullJustName       = PathUtils.Join(info.Owner.Parent.FullPath, dep.Import);
                    var fileAdditionalInfo =
                        AutodetectAndAddDependency(fullJustName);
                    if (fileAdditionalInfo == null)
                    {
                        info.ReportDiag(true, -3, "Missing dependency " + dep.Import, 0, 0, 0, 0);
                    }

                    info.ReportDependency(fullJustName);
                }
            }
        }
Ejemplo n.º 4
0
        public TsFileAdditionalInfo?CheckAdd(string fullNameWithExtension, FileCompilationType compilationType)
        {
            if (!Result.Path2FileInfo.TryGetValue(fullNameWithExtension, out var info))
            {
                var fc = Owner.DiskCache.TryGetItem(fullNameWithExtension) as IFileCache;
                if (fc == null || fc.IsInvalid)
                {
                    return(null);
                }
                info      = TsFileAdditionalInfo.Create(fc, Owner.DiskCache);
                info.Type = compilationType;
                MainResult.MergeCommonSourceDirectory(fc.FullPath);
                Result.Path2FileInfo.Add(fullNameWithExtension, info);
            }
            else
            {
                if (info.Owner.IsInvalid)
                {
                    Result.Path2FileInfo.Remove(fullNameWithExtension);
                    return(null);
                }
            }

            if (!ToCheck.Contains(fullNameWithExtension))
            {
                ToCheck.Add(fullNameWithExtension);
            }
            if (info.Type == FileCompilationType.Unknown)
            {
                info.Type = compilationType;
            }

            if (info.Type == FileCompilationType.JavaScriptAsset)
            {
                if (Result.JavaScriptAssets.AddUnique(info) && _noDependencyCheck)
                {
                    _noDependencyCheck = false;
                }
            }

            return(info);
        }
Ejemplo n.º 5
0
        bool TryToResolveFromBuildCacheCss(TsFileAdditionalInfo itemInfo)
        {
            itemInfo.TakenFromBuildCache = false;
            var bc = Owner.ProjectOptions.BuildCache;

            if (bc.IsEnabled)
            {
                var hashOfContent = itemInfo.Owner.HashOfContent;
                var fbc           = bc.FindTSFileBuildCache(hashOfContent, 0);
                if (fbc != null)
                {
                    itemInfo.Output = fbc.Output;
                    itemInfo.TranspilationDependencies = fbc.TranspilationDependencies;
                    itemInfo.TakenFromBuildCache       = true;
                    //_owner.Logger.Info("Loaded from cache " + itemInfo.Owner.FullPath);
                    return(true);
                }
            }

            return(false);
        }
Ejemplo n.º 6
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;
            }
        }
Ejemplo n.º 7
0
        void CrawlInfo(TsFileAdditionalInfo info)
        {
            if (info.Owner.ChangeId != info.ChangeId)
            {
                info.ChangeId = info.Owner.ChangeId;
                Result.RecompiledIncrementaly.Add(info);
                var oldDependencies = new StructList <string>();
                if (_noDependencyCheck)
                {
                    oldDependencies.TransferFrom(ref info.Dependencies);
                }
                info.StartCompiling();
                switch (info.Type)
                {
                case FileCompilationType.Json:
                    info.Output     = null;
                    info.MapLink    = null;
                    info.SourceInfo = null;
                    break;

                case FileCompilationType.EsmJavaScript:
                case FileCompilationType.TypeScript:
                    info.HasError = false;
                    if (!TryToResolveFromBuildCache(info))
                    {
                        info.Output     = null;
                        info.MapLink    = null;
                        info.SourceInfo = null;
                        Transpile(info);
                    }

                    break;

                case FileCompilationType.JavaScriptAsset:
                case FileCompilationType.JavaScript:
                    info.Output  = info.Owner.Utf8Content;
                    info.MapLink = SourceMap.Identity(info.Output, info.Owner.FullPath);
                    break;

                case FileCompilationType.Resource:
                    break;

                case FileCompilationType.Css:
                case FileCompilationType.ImportedCss:
                    if (!TryToResolveFromBuildCacheCss(info))
                    {
                        var cssProcessor = BuildCtx.CompilerPool.GetCss();
                        try
                        {
                            info.Output = info.Owner.Utf8Content;
                            cssProcessor.ProcessCss(info.Owner.Utf8Content,
                                                    ((TsFileAdditionalInfo)info).Owner.FullPath, (string url, string from) =>
                            {
                                var urlJustName = url.Split('?', '#')[0];
                                info.ReportTranspilationDependency(null, urlJustName, null);
                                return(url);
                            }).Wait();
                        }
                        finally
                        {
                            BuildCtx.CompilerPool.ReleaseCss(cssProcessor);
                        }
                    }

                    ReportDependenciesFromCss(info);
                    break;
                }

                if (_noDependencyCheck)
                {
                    if (!info.Dependencies.SequenceEqual(oldDependencies))
                    {
                        if (BuildCtx.Verbose)
                        {
                            Owner.Logger.Info("Dependency change detected " + info.Owner.FullPath);
                        }
                        _noDependencyCheck = false;
                    }
                }
            }
        }
Ejemplo n.º 8
0
        public TsFileAdditionalInfo?CrawlFile(string fileName)
        {
            if (!Result.Path2FileInfo.TryGetValue(fileName, out var info))
            {
                if (_noDependencyCheck)
                {
                    _noDependencyCheck = false;
                }

                var fileCache = Owner.DiskCache.TryGetItem(fileName) as IFileCache;
                if (fileCache == null || fileCache.IsInvalid)
                {
                    if (BuildCtx.Verbose)
                    {
                        Owner.Logger.Warn("Crawl skipping missing file " + fileName);
                    }
                    return(null);
                }

                info      = TsFileAdditionalInfo.Create(fileCache, Owner.DiskCache);
                info.Type = FileCompilationType.Unknown;
                Result.Path2FileInfo.Add(fileName, info);
            }
            else
            {
                if (info.Owner.IsInvalid)
                {
                    if (BuildCtx.Verbose)
                    {
                        Owner.Logger.Warn("Crawl skipping missing file " + fileName);
                    }
                    return(null);
                }
            }

            if (_noDependencyCheck)
            {
                if (info.IterationId != IterationId)
                {
                    info.IterationId = IterationId;
                    CrawlInfo(info);
                }
            }
            else
            {
                if (info.Type == FileCompilationType.Unknown)
                {
                    if (IsDts(fileName))
                    {
                        info.Type = FileCompilationType.TypeScriptDefinition;
                    }
                    else if (IsTsOrTsx(fileName))
                    {
                        info.Type = FileCompilationType.TypeScript;
                    }
                    else
                    {
                        var ext = PathUtils.GetExtension(fileName);
                        if (ext.SequenceEqual("css"))
                        {
                            info.Type = FileCompilationType.Css;
                        }
                        else if (ext.SequenceEqual("js") || ext.SequenceEqual("jsx"))
                        {
                            info.Type = FileCompilationType.EsmJavaScript;
                        }
                    }
                }

                if (info.IterationId != IterationId)
                {
                    info.IterationId = IterationId;
                    CrawlInfo(info);
                }

                foreach (var dep in info.Dependencies)
                {
                    CheckAdd(dep, FileCompilationType.Unknown);
                }
            }

            return(info);
        }
Ejemplo n.º 9
0
        public void AddDependenciesFromSourceInfo(TsFileAdditionalInfo fileInfo)
        {
            var sourceInfo = fileInfo.SourceInfo;

            if (sourceInfo == null)
            {
                return;
            }
            sourceInfo.Imports?.ForEach(i =>
            {
                var resolved = ResolveImport(fileInfo.Owner.FullPath, i.Name);
                if (resolved != null && resolved != "?")
                {
                    fileInfo.ReportDependency(resolved);
                }
                else
                {
                    fileInfo.ReportDiag(true, -3, "Missing import " + i.Name, i.StartLine, i.StartCol, i.EndLine,
                                        i.EndCol);
                }
            });
            sourceInfo.Assets?.ForEach(a =>
            {
                if (a.Name == null)
                {
                    fileInfo.ReportDiag(true, -5, "First parameter of b.asset must be resolved as constant string",
                                        a.StartLine, a.StartCol, a.EndLine,
                                        a.EndCol);
                    return;
                }

                var assetName = a.Name;
                if (assetName.StartsWith("project:"))
                {
                    var(pref, name) = SplitProjectAssetName(assetName);
                    if (pref.Length == 8)
                    {
                        name += "/package.json";
                    }

                    if (!(Owner.DiskCache.TryGetItem(PathUtils.Join(Owner.Owner.FullPath, name)) is
                          IFileCache))
                    {
                        fileInfo.ReportDiag(true, -3, "Missing dependency " + assetName, a.StartLine, a.StartCol,
                                            a.EndLine, a.EndCol);
                    }
                }
                else if (assetName.StartsWith("resource:"))
                {
                    assetName = assetName.Substring(9);
                    if (ReportDependency(fileInfo, AutodetectAndAddDependency(assetName, true)) == null)
                    {
                        fileInfo.ReportDiag(true, -3, "Missing dependency " + assetName, a.StartLine, a.StartCol,
                                            a.EndLine, a.EndCol);
                    }
                }
                else
                {
                    if (ReportDependency(fileInfo, AutodetectAndAddDependency(assetName)) == null)
                    {
                        fileInfo.ReportDiag(true, -3, "Missing dependency " + assetName, a.StartLine, a.StartCol,
                                            a.EndLine, a.EndCol);
                    }
                }
            });
            if (sourceInfo.Sprites != null)
            {
                if (Owner.ProjectOptions.SpriteGeneration)
                {
                    var spriteHolder = Owner.ProjectOptions.SpriteGenerator;
                    spriteHolder.Process(sourceInfo.Sprites);
                }
                else
                {
                    sourceInfo.Sprites.ForEach(s =>
                    {
                        if (s.Name == null)
                        {
                            return;
                        }
                        var assetName = s.Name;
                        if (ReportDependency(fileInfo, AutodetectAndAddDependency(assetName)) == null)
                        {
                            fileInfo.ReportDiag(true, -3, "Missing dependency " + assetName, s.NameStartLine,
                                                s.NameStartCol, s.NameEndLine, s.NameEndCol);
                        }
                    });
                }
            }

            if (sourceInfo.VdomTranslations != null)
            {
                var trdb = Owner.ProjectOptions.TranslationDb;
                if (trdb != null)
                {
                    sourceInfo.VdomTranslations.ForEach(t =>
                    {
                        if (t.Message == null)
                        {
                            return;
                        }
                        var err = trdb.CheckMessage(t.Message, t.KnownParams);
                        if (err != null)
                        {
                            fileInfo.ReportDiag(false, -7,
                                                "Problem with translation message \"" + t.Message + "\" " + err, t.StartLine,
                                                t.StartCol, t.EndLine, t.EndCol);
                        }
                    });
                }
            }

            if (sourceInfo.Translations != null)
            {
                var trdb = Owner.ProjectOptions.TranslationDb;
                if (trdb != null)
                {
                    sourceInfo.Translations.ForEach(t =>
                    {
                        if (t.Message == null)
                        {
                            return;
                        }
                        if (t.WithParams)
                        {
                            var err = trdb.CheckMessage(t.Message, t.KnownParams);
                            if (err != null)
                            {
                                fileInfo.ReportDiag(false, -7,
                                                    "Problem with translation message \"" + t.Message + "\" " + err, t.StartLine,
                                                    t.StartCol, t.EndLine, t.EndCol);
                            }
                        }
                    });
                }
            }
        }