Пример #1
0
 private void OutputModule(Contract.IEmitterOutput output)
 {
     foreach (var moduleOutput in output.ModuleOutput)
     {
         WriteNewLine(output.NonModuletOutput, moduleOutput.Value.ToString());
     }
 }
Пример #2
0
        private bool OutputNonModule(bool disableAsm, string fileName, Contract.IEmitterOutput output, bool isJs, StringBuilder tmp)
        {
            bool metaDataWritten = false;

            var level = this.InitialLevel;

            if (output.NonModuletOutput.Length > 0)
            {
                if (isJs)
                {
                    if (!disableAsm)
                    {
                        string asmName = this.AssemblyInfo.Assembly.FullName ?? this.Translator.AssemblyName;

                        OutputAssemblyComment(tmp);

                        tmp.Append(JS.Types.Bridge.ASSEMBLY + "(");

                        tmp.AppendFormat("\"{0}\"", asmName);

                        tmp.Append(",");

                        if (!metaDataWritten && (this.MetaDataOutputName == null || fileName == this.MetaDataOutputName))
                        {
                            var res = this.GetIncludedResources();

                            if (res != null)
                            {
                                tmp.Append(" ");
                                tmp.Append(res);
                                tmp.Append(",");
                            }

                            metaDataWritten = true;
                        }

                        tmp.Append(" ");
                        tmp.Append("function ($asm, globals) {");
                        WriteNewLine(tmp);
                        WriteIndent(tmp, level);
                        tmp.Append(this.GetOutputHeader());
                        WriteNewLine(tmp);
                        WriteNewLine(tmp);
                    }
                }

                var code = output.NonModuletOutput.ToString();

                tmp.Append(code);

                if (isJs && !disableAsm)
                {
                    tmp.Append("});");
                    WriteNewLine(tmp);
                }
            }

            return(metaDataWritten);
        }
Пример #3
0
 private void OutputBottom(Contract.IEmitterOutput output, StringBuilder tmp)
 {
     if (output.BottomOutput.Length > 0)
     {
         WriteNewLine(tmp);
         tmp.Append(output.BottomOutput.ToString());
     }
 }
Пример #4
0
 private void OutputTop(Contract.IEmitterOutput output, StringBuilder tmp)
 {
     if (output.TopOutput.Length > 0)
     {
         tmp.Append(output.TopOutput.ToString());
         WriteNewLine(tmp);
     }
 }
Пример #5
0
        private int AddDependencies(int level, Contract.IEmitterOutput output, StringBuilder tmp, StringBuilder endOutput)
        {
            var loader       = this.AssemblyInfo.Loader;
            var dependencies = output.NonModuleDependencies;

            if (dependencies != null && dependencies.Count > 0)
            {
                var disabledDependecies = dependencies.Where(d => loader.IsManual(d.DependencyName)).ToList();
                dependencies = dependencies.Where(d => !loader.IsManual(d.DependencyName)).ToList();

                if (disabledDependecies.Count > 0 && !loader.SkipManualVariables)
                {
                    this.WriteIndent(tmp, level);
                    this.Write(tmp, "var ");
                    for (int i = 0; i < disabledDependecies.Count; i++)
                    {
                        var d = disabledDependecies[i];
                        if (i != 0)
                        {
                            this.WriteIndent(tmp, level + 1);
                        }

                        this.Write(tmp, d.VariableName.IsNotEmpty() ? d.VariableName : d.DependencyName);
                        this.Write(tmp, i == (disabledDependecies.Count - 1) ? ";" : ",");
                        this.WriteNewLine(tmp);
                    }

                    this.WriteNewLine(tmp);
                }

                var type = loader.Type;
                var amd  = dependencies.Where(d => d.Type == ModuleType.AMD || ((d.Type == null || d.Type == ModuleType.UMD) && type == ModuleLoaderType.AMD)).ToList();
                var cjs  = dependencies.Where(d => d.Type == ModuleType.CommonJS || ((d.Type == null || d.Type == ModuleType.UMD) && type == ModuleLoaderType.CommonJS)).ToList();
                var es6  = dependencies.Where(d => d.Type == ModuleType.ES6 || (d.Type == null && type == ModuleLoaderType.ES6)).ToList();

                if (amd.Count > 0)
                {
                    this.WriteIndent(tmp, level);
                    tmp.Append(loader.FunctionName ?? "require");
                    tmp.Append("([");

                    amd.Each(md =>
                    {
                        tmp.Append(this.ToJavaScript(md.DependencyName));
                        tmp.Append(",");
                    });
                    tmp.Remove(tmp.Length - 1, 1); // remove trailing comma
                    tmp.Append("], function (");

                    amd.Each(md =>
                    {
                        tmp.Append(md.VariableName.IsNotEmpty() ? md.VariableName : md.DependencyName);
                        tmp.Append(",");
                    });
                    tmp.Remove(tmp.Length - 1, 1); // remove trailing comma

                    this.WriteNewLine(tmp, ") {");

                    this.WriteIndent(endOutput, level);
                    this.WriteNewLine(endOutput, JS.Types.Bridge.INIT + "();");
                    this.WriteIndent(endOutput, level);
                    this.WriteNewLine(endOutput, "});");
                    level++;
                }

                if (cjs.Count > 0)
                {
                    cjs.Each(md =>
                    {
                        this.WriteIndent(tmp, level);
                        tmp.AppendFormat("var {0} = require('{1}');", md.VariableName.IsNotEmpty() ? md.VariableName : md.DependencyName, md.DependencyName);
                        this.WriteNewLine(tmp);
                    });

                    if (es6.Count == 0)
                    {
                        this.WriteNewLine(tmp);
                    }
                }

                if (es6.Count > 0)
                {
                    es6.Each(md =>
                    {
                        this.WriteIndent(tmp, level);
                        this.WriteNewLine(tmp, "import " + (md.VariableName.IsNotEmpty() ? md.VariableName : md.DependencyName) + " from " + this.ToJavaScript(md.DependencyName) + ";");
                    });

                    this.WriteNewLine(tmp);
                }
            }

            return(level);
        }
Пример #6
0
        private bool OutputNonModule(bool disableAsm, string fileName, Contract.IEmitterOutput output, bool isJs, StringBuilder tmp)
        {
            bool metaDataWritten = false;

            var           level     = this.InitialLevel;
            StringBuilder endOutput = new StringBuilder();

            if (output.NonModuletOutput.Length > 0 || output.ModuleOutput.Count > 0)
            {
                if (isJs)
                {
                    if (!disableAsm)
                    {
                        string asmName = this.AssemblyInfo.Assembly.FullName ??
                                         this.Translator.ProjectProperties.AssemblyName;

                        OutputAssemblyComment(tmp);

                        tmp.Append(JS.Types.Bridge.ASSEMBLY + "(");

                        tmp.AppendFormat("\"{0}\"", asmName);

                        tmp.Append(",");

                        if (!metaDataWritten && (this.MetaDataOutputName == null || fileName == this.MetaDataOutputName))
                        {
                            var res = this.GetIncludedResources();

                            if (res != null)
                            {
                                tmp.Append(" ");
                                tmp.Append(res);
                                tmp.Append(",");
                            }

                            metaDataWritten = true;
                        }

                        tmp.Append(" ");
                        tmp.Append("function ($asm, globals) {");
                        WriteNewLine(tmp);
                        WriteIndent(tmp, level);
                        tmp.Append(this.GetOutputHeader());
                        WriteNewLine(tmp);
                        WriteNewLine(tmp);
                    }

                    level = this.AddDependencies(level, output, tmp, endOutput);
                }
                else
                {
                    string newLine = Bridge.Contract.XmlToJSConstants.DEFAULT_LINE_SEPARATOR.ToString();
                    tmp.Insert(0, @"/// <reference path=""./bridge.d.ts"" />" + newLine + newLine);
                }

                var code = output.NonModuletOutput.ToString();

                if (code.Length > 0)
                {
                    tmp.Append(level > this.InitialLevel ? Emitter.INDENT + code.Replace(Emitter.NEW_LINE, Emitter.NEW_LINE + Emitter.INDENT) : code);
                }

                if (endOutput.Length > 0)
                {
                    tmp.Append(endOutput.ToString());
                }

                if (output.ModuleOutput.Any())
                {
                    if (code.Length > 0)
                    {
                        this.WriteNewLine(tmp);
                    }

                    foreach (var moduleOutput in output.ModuleOutput)
                    {
                        WriteNewLine(tmp, moduleOutput.Value.ToString());
                    }
                }

                if (isJs && !disableAsm)
                {
                    tmp.Append("});");
                    WriteNewLine(tmp);
                }
            }

            return(metaDataWritten);
        }