public static void CopyDtsFile(DefinitionMapData definitionMapData, ProjectItem projectItem, string dts, bool isEnumDefinition)
        {
            // There might be paths where this file should be copied to
            foreach (string copyPath in definitionMapData.CopyPaths)
            {
                // Ignore empty paths
                if (string.IsNullOrWhiteSpace(copyPath))
                {
                    continue;
                }

                // Get the path from our project item and combine it with the target path and target name
                string filePath = Path.GetFullPath(Path.Combine(
                                                       Path.GetDirectoryName(projectItem.FileNames[1]),
                                                       copyPath,
                                                       GenerationService.GetCopyDtsFileName(definitionMapData, projectItem, isEnumDefinition)));

                // Try to write our definition file to the new path too
                try
                {
                    File.WriteAllText(filePath, dts);
                    VSHelpers.WriteOnOutputWindow($"File written to \"{filePath}\"");
                }
                catch (Exception ex)
                {
                    VSHelpers.WriteOnOutputWindow($"Could not write file to \"{filePath}\"{Environment.NewLine}" +
                                                  $"Reason: {ex.Message}");
                }
            }
        }
예제 #2
0
        public static string WriteTypeScriptWithoutEnums(IEnumerable <IntellisenseObject> objects, ProjectItem sourceItem, out bool isEmpty)
        {
            isEmpty = true;

            var sb = new StringBuilder();

            foreach (var ns in objects.GroupBy(o => o.Namespace))
            {
                List <string> references = GetReferences(objects, sourceItem);

                if (references.Count > 0)
                {
                    foreach (string referencePath in references.OrderBy(p => Path.GetFileName(p)))
                    {
                        string path = Path.GetFileName(referencePath);

                        ProjectItem definitionMapProjectItem = sourceItem.DTE.Solution.FindProjectItem(referencePath);

                        if (definitionMapProjectItem != null)
                        {
                            DefinitionMapData definitionMapData = VSHelpers.GetDefinitionMapData(definitionMapProjectItem.Collection.Parent as ProjectItem);

                            if (definitionMapData != null)
                            {
                                if (string.IsNullOrWhiteSpace(definitionMapData.CustomName) == false)
                                {
                                    path = GenerationService.GetCopyDtsFileName(definitionMapData, definitionMapProjectItem, false);
                                }
                            }
                        }

                        sb.AppendFormat("/// <reference path=\"{0}\" />\r\n", path);
                    }

                    sb.AppendLine();
                }

                if (!Options.GlobalScope)
                {
                    sb.AppendFormat("declare module {0} {{\r\n", ns.Key);
                }

                foreach (IntellisenseObject io in ns)
                {
                    if (io.IsEnum)
                    {
                        continue;
                    }

                    isEmpty = false;

                    if (!string.IsNullOrEmpty(io.Summary))
                    {
                        sb.AppendLine("\t/** " + _whitespaceTrimmer.Replace(io.Summary, "") + " */");
                    }

                    string type = Options.ClassInsteadOfInterface ? "\tclass " : "\tinterface ";
                    sb.Append(type).Append(Utility.CamelCaseClassName(io.Name)).Append(" ");

                    if (!string.IsNullOrEmpty(io.BaseName))
                    {
                        sb.Append("extends ");

                        if (Options.GlobalScope == false)
                        {
                            sb.Append(ns.Key).Append(".");
                        }

                        if (!string.IsNullOrEmpty(io.BaseNamespace) && io.BaseNamespace != io.Namespace)
                        {
                            sb.Append(io.BaseNamespace).Append(".");
                        }

                        sb.Append(Utility.CamelCaseClassName(io.BaseName)).Append(" ");
                    }

                    WriteTSInterfaceDefinition(sb, "\t", io.Properties);
                    sb.AppendLine();
                }

                if (!Options.GlobalScope)
                {
                    sb.AppendLine("}");
                }
            }

            return(sb.ToString());
        }
예제 #3
0
        public static string WriteTypeScriptEnumsOnly(IEnumerable <IntellisenseObject> objects, ProjectItem sourceItem, out bool isEmpty)
        {
            isEmpty = true;

            var sb = new StringBuilder();

            foreach (var ns in objects.GroupBy(o => o.Namespace))
            {
                List <string> references = GetReferences(objects, sourceItem);

                if (references.Count > 0)
                {
                    foreach (string referencePath in references.OrderBy(p => Path.GetFileName(p)))
                    {
                        string path = Path.GetFileName(referencePath);

                        ProjectItem definitionMapProjectItem = sourceItem.DTE.Solution.FindProjectItem(referencePath);

                        if (definitionMapProjectItem != null)
                        {
                            DefinitionMapData definitionMapData = VSHelpers.GetDefinitionMapData(definitionMapProjectItem.Collection.Parent as ProjectItem);

                            if (definitionMapData != null)
                            {
                                if (string.IsNullOrWhiteSpace(definitionMapData.CustomName) == false)
                                {
                                    path = GenerationService.GetCopyDtsFileName(definitionMapData, definitionMapProjectItem, true);
                                }
                            }
                        }

                        sb.AppendFormat("/// <reference path=\"{0}\" />\r\n", path);
                    }

                    sb.AppendLine();
                }

                if (!Options.GlobalScope)
                {
                    sb.AppendFormat("declare module {0} {{\r\n", ns.Key);
                }

                foreach (IntellisenseObject io in ns)
                {
                    if (io.IsEnum == false)
                    {
                        continue;
                    }

                    isEmpty = false;

                    if (!string.IsNullOrEmpty(io.Summary))
                    {
                        sb.AppendLine("\t/** " + _whitespaceTrimmer.Replace(io.Summary, "") + " */");
                    }

                    sb.AppendLine("\tenum " + Utility.CamelCaseClassName(io.Name) + " {");

                    foreach (var p in io.Properties)
                    {
                        WriteTypeScriptComment(p, sb);

                        if (p.InitExpression != null)
                        {
                            sb.AppendLine("\t\t" + Utility.CamelCaseEnumValue(p.Name) + " = " + CleanEnumInitValue(p.InitExpression) + ",");
                        }
                        else
                        {
                            sb.AppendLine("\t\t" + Utility.CamelCaseEnumValue(p.Name) + ",");
                        }
                    }

                    sb.AppendLine("\t}");
                }

                if (!Options.GlobalScope)
                {
                    sb.AppendLine("}");
                }
            }

            return(sb.ToString());
        }