Exemple #1
0
        public static Tuple <string, string> GetNamespaceFilename(ITypeInfo typeInfo, IEmitter emitter)
        {
            var ns       = typeInfo.GetNamespace(emitter, true);
            var fileName = ns ?? typeInfo.GetNamespace(emitter);

            switch (emitter.AssemblyInfo.FileNameCasing)
            {
            case FileNameCaseConvert.Lowercase:
                fileName = fileName.ToLower();
                break;

            case FileNameCaseConvert.CamelCase:
                var sepList = new string[] { ".", System.IO.Path.DirectorySeparatorChar.ToString(), "\\", "/" };

                // Populate list only with needed separators, as usually we will never have all four of them
                var neededSepList = new List <string>();

                foreach (var separator in sepList)
                {
                    if (fileName.Contains(separator.ToString()) && !neededSepList.Contains(separator))
                    {
                        neededSepList.Add(separator);
                    }
                }

                // now, separating the filename string only by the used separators, apply lowerCamelCase
                if (neededSepList.Count > 0)
                {
                    foreach (var separator in neededSepList)
                    {
                        var stringList = new List <string>();

                        foreach (var str in fileName.Split(separator[0]))
                        {
                            stringList.Add(str.ToLowerCamelCase());
                        }

                        fileName = stringList.Join(separator);
                    }
                }
                else
                {
                    fileName = fileName.ToLowerCamelCase();
                }
                break;
            }

            return(new Tuple <string, string>(ns, fileName));
        }
Exemple #2
0
        public static Tuple <string, string, Module> GetNamespaceFilename(ITypeInfo typeInfo, IEmitter emitter)
        {
            var    ns         = typeInfo.GetNamespace(emitter, true);
            var    fileName   = ns ?? typeInfo.GetNamespace(emitter);
            var    module     = typeInfo.Module;
            string moduleName = null;

            if (module != null && module.Type == ModuleType.UMD)
            {
                if (!module.PreventModuleName)
                {
                    moduleName = module.ExportAsNamespace;
                }

                if (!String.IsNullOrEmpty(moduleName))
                {
                    ns = string.IsNullOrWhiteSpace(ns) ? moduleName : (moduleName + "." + ns);
                }
                else
                {
                    module = null;
                }
            }

            switch (emitter.AssemblyInfo.FileNameCasing)
            {
            case FileNameCaseConvert.Lowercase:
                fileName = fileName.ToLower();
                break;

            case FileNameCaseConvert.CamelCase:
                var sepList = new string[] { ".", System.IO.Path.DirectorySeparatorChar.ToString(), "\\", "/" };

                // Populate list only with needed separators, as usually we will never have all four of them
                var neededSepList = new List <string>();

                foreach (var separator in sepList)
                {
                    if (fileName.Contains(separator.ToString()) && !neededSepList.Contains(separator))
                    {
                        neededSepList.Add(separator);
                    }
                }

                // now, separating the filename string only by the used separators, apply lowerCamelCase
                if (neededSepList.Count > 0)
                {
                    foreach (var separator in neededSepList)
                    {
                        var stringList = new List <string>();

                        foreach (var str in fileName.Split(separator[0]))
                        {
                            stringList.Add(str.ToLowerCamelCase());
                        }

                        fileName = stringList.Join(separator);
                    }
                }
                else
                {
                    fileName = fileName.ToLowerCamelCase();
                }
                break;
            }

            return(new Tuple <string, string, Module>(ns, fileName, module));
        }
Exemple #3
0
        protected virtual StringBuilder GetOutputForType(ITypeInfo typeInfo, string name, bool isMeta = false)
        {
            Module module = null;

            if (typeInfo != null && typeInfo.Module != null)
            {
                module = typeInfo.Module;
            }
            else if (this.Emitter.AssemblyInfo.Module != null)
            {
                module = this.Emitter.AssemblyInfo.Module;
            }

            var fileName = typeInfo != null ? typeInfo.FileName : name;

            if (fileName.IsEmpty() && this.Emitter.AssemblyInfo.OutputBy != OutputBy.Project)
            {
                switch (this.Emitter.AssemblyInfo.OutputBy)
                {
                case OutputBy.ClassPath:
                    fileName = typeInfo.Type.FullName;
                    break;

                case OutputBy.Class:
                    fileName = this.GetIteractiveClassPath(typeInfo);
                    break;

                case OutputBy.Module:
                    fileName = module != null ? module.Name : null;
                    break;

                case OutputBy.NamespacePath:
                case OutputBy.Namespace:
                    fileName = typeInfo.GetNamespace(this.Emitter);
                    break;

                default:
                    break;
                }

                var isPathRelated = this.Emitter.AssemblyInfo.OutputBy == OutputBy.ClassPath ||
                                    this.Emitter.AssemblyInfo.OutputBy == OutputBy.NamespacePath;

                if (fileName.IsNotEmpty() && isPathRelated)
                {
                    fileName = fileName.Replace('.', System.IO.Path.DirectorySeparatorChar);

                    if (this.Emitter.AssemblyInfo.StartIndexInName > 0)
                    {
                        fileName = fileName.Substring(this.Emitter.AssemblyInfo.StartIndexInName);
                    }
                }
            }

            if (fileName.IsEmpty() && this.Emitter.AssemblyInfo.FileName != null)
            {
                fileName = this.Emitter.AssemblyInfo.FileName;
            }

            if (fileName.IsEmpty() && this.Emitter.Translator.ProjectProperties.AssemblyName != null)
            {
                fileName = this.Emitter.Translator.ProjectProperties.AssemblyName;
            }

            if (fileName.IsEmpty())
            {
                fileName = AssemblyInfo.DEFAULT_FILENAME;
            }

            // Apply lowerCamelCase to filename if set up in bridge.json (or left default)
            if (this.Emitter.AssemblyInfo.FileNameCasing == FileNameCaseConvert.CamelCase)
            {
                var sepList = new string[] { ".", System.IO.Path.DirectorySeparatorChar.ToString(), "\\", "/" };

                // Populate list only with needed separators, as usually we will never have all four of them
                var neededSepList = new List <string>();

                foreach (var separator in sepList)
                {
                    if (fileName.Contains(separator.ToString()) && !neededSepList.Contains(separator))
                    {
                        neededSepList.Add(separator);
                    }
                }

                // now, separating the filename string only by the used separators, apply lowerCamelCase
                if (neededSepList.Count > 0)
                {
                    foreach (var separator in neededSepList)
                    {
                        var stringList = new List <string>();

                        foreach (var str in fileName.Split(separator[0]))
                        {
                            stringList.Add(str.ToLowerCamelCase());
                        }

                        fileName = stringList.Join(separator);
                    }
                }
                else
                {
                    fileName = fileName.ToLowerCamelCase();
                }
            }

            // Append '.js' extension to file name at translator.Outputs level: this aids in code grouping on files
            // when filesystem is not case sensitive.
            if (!FileHelper.IsJS(fileName))
            {
                fileName += Contract.Constants.Files.Extensions.JS;
            }

            switch (this.Emitter.AssemblyInfo.FileNameCasing)
            {
            case FileNameCaseConvert.Lowercase:
                fileName = fileName.ToLower();
                break;

            default:
                var lcFileName = fileName.ToLower();

                // Find a file name that matches (case-insensitive) and use it as file name (if found)
                // The used file name will use the same casing of the existing one.
                foreach (var existingFile in this.Emitter.Outputs.Keys)
                {
                    if (lcFileName == existingFile.ToLower())
                    {
                        fileName = existingFile;
                    }
                }

                break;
            }

            IEmitterOutput output = null;

            if (this.Emitter.Outputs.ContainsKey(fileName))
            {
                output = this.Emitter.Outputs[fileName];
            }
            else
            {
                output = new EmitterOutput(fileName)
                {
                    IsMetadata = isMeta
                };
                this.Emitter.Outputs.Add(fileName, output);
            }

            this.Emitter.EmitterOutput = output;

            if (module == null)
            {
                if (output.NonModuleDependencies == null)
                {
                    output.NonModuleDependencies = new List <IPluginDependency>();
                }
                this.Emitter.CurrentDependencies = output.NonModuleDependencies;
                return(output.NonModuletOutput);
            }

            if (module.Name == "")
            {
                module.Name = Bridge.Translator.AssemblyInfo.DEFAULT_FILENAME;
            }

            if (output.ModuleOutput.ContainsKey(module))
            {
                this.Emitter.CurrentDependencies = output.ModuleDependencies[module.Name];
                return(output.ModuleOutput[module]);
            }

            StringBuilder moduleOutput = new StringBuilder();

            output.ModuleOutput.Add(module, moduleOutput);
            var dependencies = new List <IPluginDependency>();

            output.ModuleDependencies.Add(module.Name, dependencies);

            if (typeInfo != null && typeInfo.Dependencies.Count > 0)
            {
                dependencies.AddRange(typeInfo.Dependencies);
            }

            this.Emitter.CurrentDependencies = dependencies;

            return(moduleOutput);
        }
        public void AfterTypeEmit(IEmitter emitter, ITypeInfo typeInfo)
        {
            Logger.Trace(String.Format("check should export {0} == {1}", Path.GetFileNameWithoutExtension(emitter.SourceFileName), typeInfo.Name));

            if (!isBindingClass(typeInfo))
            {
                Logger.Trace(String.Format("{0} is not binding class", typeInfo.Name));
                return;
            }

            var filePath = "";

            switch (emitter.AssemblyInfo.OutputBy)
            {
            case OutputBy.ClassPath:
                filePath = typeInfo.Type.FullName + ".binding.js";
                break;

//                case OutputBy.Class:
//                    filePath = this.GetIteractiveClassPath(typeInfo);
//                    break;
//                case OutputBy.Module:
//                    filePath = module != null ? module.Name : null;
//                    break;
            case OutputBy.NamespacePath:
            case OutputBy.Namespace:
                filePath = typeInfo.GetNamespace(emitter);
                break;

            case OutputBy.FilePath:
                filePath = Path.ChangeExtension(emitter.SourceFileName, ".binding.js");
                break;

            default:
                break;
            }

            if (String.IsNullOrEmpty(filePath))
            {
                return;
            }

            List <BindingType> bindingTypeList;

            if (!bindingTypes.ContainsKey(filePath))
            {
                bindingTypeList = new List <BindingType>();
                bindingTypes.Add(filePath, bindingTypeList);
            }
            else
            {
                bindingTypeList = bindingTypes[filePath];
            }

            var bindingType = new BindingType();

            bindingType.typeInfo = typeInfo;

            bindingType.shouldExport = Path.GetFileNameWithoutExtension(emitter.SourceFileName).Equals(typeInfo.Name);
            bindingTypeList.Add(bindingType);
        }