예제 #1
0
        public void Build(BuildCtx buildCtx)
        {
            var buildModuleCtx = new BuildModuleCtx()
            {
                _buildCtx    = buildCtx,
                _owner       = this,
                _result      = new BuildResult(),
                ToCheck      = new OrderedHashSet <string>(),
                ToCompile    = new OrderedHashSet <string>(),
                ToCompileDts = new OrderedHashSet <string>()
            };
            ITSCompiler compiler = null;

            try
            {
                ProjectOptions.BuildCache.StartTransaction();
                compiler           = buildCtx.CompilerPool.GetTs();
                compiler.DiskCache = DiskCache;
                compiler.Ctx       = buildModuleCtx;
                var compOpt = buildCtx.TSCompilerOptions.Clone();
                compOpt.rootDir     = Owner.FullPath;
                compOpt.outDir      = "_virtual";
                compOpt.module      = ModuleKind.Commonjs;
                compOpt.declaration = true;
                if (!ProjectOptions.TypeScriptVersionOverride && DevDependencies != null &&
                    DevDependencies.Contains("typescript"))
                {
                    ProjectOptions.Tools.SetTypeScriptPath(Owner.FullPath);
                }
                else
                {
                    ProjectOptions.Tools.SetTypeScriptVersion(ProjectOptions.TypeScriptVersion);
                }
                compiler.MergeCompilerOptions(compOpt);
                compiler.MergeCompilerOptions(ProjectOptions.CompilerOptions);
                var positionIndependentOptions = compiler.CompilerOptions.Clone();
                positionIndependentOptions.rootDir = null;
                var trueTSVersion = compiler.GetTSVersion();
                buildCtx.ShowTsVersion(trueTSVersion);
                ProjectOptions.ConfigurationBuildCacheId = ProjectOptions.BuildCache.MapConfiguration(trueTSVersion,
                                                                                                      JsonConvert.SerializeObject(positionIndependentOptions, Formatting.None,
                                                                                                                                  new JsonSerializerSettings {
                    NullValueHandling = NullValueHandling.Ignore
                }));
                var wasSomeError = false;
                do
                {
                    buildModuleCtx.ChangedDts   = false;
                    buildModuleCtx.CrawledCount = 0;
                    buildModuleCtx.ToCheck.Clear();
                    buildModuleCtx.ToCompile.Clear();
                    buildModuleCtx.ToCompileDts.Clear();
                    buildModuleCtx.LocalResolveCache.Clear();
                    ProjectOptions.HtmlHeadExpanded = buildModuleCtx.ExpandHtmlHead(ProjectOptions.HtmlHead);
                    foreach (var src in buildCtx.Sources)
                    {
                        buildModuleCtx.CheckAdd(PathUtils.Join(compOpt.rootDir, src));
                    }

                    if (ProjectOptions.IncludeSources != null)
                    {
                        foreach (var src in ProjectOptions.IncludeSources)
                        {
                            buildModuleCtx.CheckAdd(PathUtils.Join(compOpt.rootDir, src));
                        }
                    }

                    buildModuleCtx.Crawl();
                    if (buildModuleCtx.ToCompile.Count != 0)
                    {
                        if (buildCtx.Verbose)
                        {
                            compiler.MeasurePerformance = true;
                        }
                        var start = DateTime.UtcNow;
                        buildModuleCtx.OutputedJsFiles  = 0;
                        buildModuleCtx.OutputedDtsFiles = 0;
                        compiler.CreateProgram(Owner.FullPath,
                                               buildModuleCtx.ToCompile.Concat(buildModuleCtx.ToCompileDts).ToArray());
                        if (!compiler.CompileProgram())
                        {
                            wasSomeError = true;
                            break;
                        }

                        ProjectOptions.CurrentBuildCommonSourceDirectory = compiler.CommonSourceDirectory;
                        ProjectOptions.CommonSourceDirectory             = ProjectOptions.CommonSourceDirectory == null
                            ? compiler.CommonSourceDirectory
                            : PathUtils.CommonDir(ProjectOptions.CommonSourceDirectory, compiler.CommonSourceDirectory);
                        compiler.GatherSourceInfo();
                        if (ProjectOptions.SpriteGeneration)
                        {
                            ProjectOptions.SpriteGenerator.ProcessNew();
                        }
                        if (!compiler.EmitProgram())
                        {
                            wasSomeError = true;
                            break;
                        }

                        buildModuleCtx.UpdateCacheIds();
                        Logger.Info(
                            $"Compiled Src: {buildModuleCtx.ToCompile.Count} Dts: {buildModuleCtx.ToCompileDts.Count} => Js: {buildModuleCtx.OutputedJsFiles} Dts: {buildModuleCtx.OutputedDtsFiles} in {(DateTime.UtcNow - start).TotalSeconds:F1}s");
                        buildModuleCtx.ToCompile.Clear();
                        buildModuleCtx.Crawl();
                    }
                } while (buildModuleCtx.ChangedDts || 0 < buildModuleCtx.ToCompile.Count);

                if (ProjectOptions.BuildCache.IsEnabled && !wasSomeError)
                {
                    ProjectOptions.StoreResultToBuildCache(buildModuleCtx._result);
                }
                buildCtx.BuildResult = buildModuleCtx._result;
            }
            finally
            {
                if (compiler != null)
                {
                    buildCtx.CompilerPool.ReleaseTs(compiler);
                }
                ProjectOptions.BuildCache.EndTransaction();
            }
        }
예제 #2
0
파일: TSProject.cs 프로젝트: 0papen0/bbcore
        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();
            }
        }