예제 #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
        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;
                ProjectOptions.Tools.SetTypeScriptVersion(ProjectOptions.TypeScriptVersion);
                compiler.MergeCompilerOptions(compOpt);
                compiler.MergeCompilerOptions(ProjectOptions.CompilerOptions);
                ProjectOptions.ConfigurationBuildCacheId = ProjectOptions.BuildCache.MapConfiguration(ProjectOptions.TypeScriptVersion, JsonConvert.SerializeObject(compiler.CompilerOptions, Formatting.None, new JsonSerializerSettings {
                    NullValueHandling = NullValueHandling.Ignore
                }));
                do
                {
                    buildModuleCtx.ChangedDts          = false;
                    buildModuleCtx.CrawledCount        = 0;
                    buildModuleCtx.TrullyCompiledCount = 0;
                    buildModuleCtx.ToCheck.Clear();
                    buildModuleCtx.ToCompile.Clear();
                    buildModuleCtx.ToCompileDts.Clear();
                    ProjectOptions.HtmlHeadExpanded = buildModuleCtx.ExpandHtmlHead(ProjectOptions.HtmlHead);
                    foreach (var src in buildCtx.Sources)
                    {
                        buildModuleCtx.CheckAdd(PathUtils.Join(compOpt.rootDir, src));
                    }
                    buildModuleCtx.Crawl();
                    if (buildModuleCtx.ToCompile.Count != 0)
                    {
                        if (buildCtx.Verbose)
                        {
                            compiler.MeasurePerformance = true;
                        }
                        compiler.CreateProgram(Owner.FullPath, buildModuleCtx.ToCompile.Concat(buildModuleCtx.ToCompileDts).ToArray());
                        if (!compiler.CompileProgram())
                        {
                            break;
                        }
                        ProjectOptions.CommonSourceDirectory = compiler.CommonSourceDirectory;
                        compiler.GatherSourceInfo();
                        if (ProjectOptions.SpriteGeneration)
                        {
                            ProjectOptions.SpriteGenerator.ProcessNew();
                        }
                        if (!compiler.EmitProgram())
                        {
                            break;
                        }
                        buildModuleCtx.UpdateCacheIds();
                        buildModuleCtx.Crawl();
                    }
                } while (buildModuleCtx.ChangedDts || buildModuleCtx.TrullyCompiledCount < buildModuleCtx.ToCompile.Count);
                if (ProjectOptions.BuildCache.IsEnabled)
                {
                    StoreResultToBuildCache(buildModuleCtx._result);
                }
                buildCtx.BuildResult = buildModuleCtx._result;
            }
            finally
            {
                if (compiler != null)
                {
                    buildCtx.CompilerPool.ReleaseTs(compiler);
                }
                ProjectOptions.BuildCache.EndTransaction();
            }
        }