Пример #1
0
                public void WriteHelp()
                {
                    int start = s_nameMidpoint;

                    ToolConsole.WriteLine(new string(' ', _nameStart) + _name);
                    ToolConsole.WriteLine();

                    if (_description != null)
                    {
                        s_categoryBuilder.WriteParagraph(_description);
                        ToolConsole.WriteLine();
                    }

                    if (_syntax != null)
                    {
                        s_categoryBuilder.WriteParagraph(_syntax);
                        ToolConsole.WriteLine();
                    }
                    if (_inputs != null)
                    {
                        ArgumentInfo.WriteArguments(_inputs);
                        ToolConsole.WriteLine();
                    }

                    if (_options != null)
                    {
                        ToolConsole.WriteLine(SR.Format(SR.HelpOptions));
                        ToolConsole.WriteLine();
                        ArgumentInfo.WriteArguments(_options);
                        ToolConsole.WriteLine();
                    }
                }
Пример #2
0
        private List <Type> CollectXmlSerializerTypes(Assembly assembly, List <XmlMapping> mappings)
        {
            List <Type> types = new List <Type>();

            ExportModule.ContractLoader contractLoader = new ExportModule.ContractLoader(new Assembly[] { assembly }, _isTypeExcluded);
            contractLoader.ContractLoadErrorCallback = delegate(Type contractType, string errorMessage)
            {
                ToolConsole.WriteWarning(SR.Format(SR.WrnUnableToLoadContractForSGen, contractType, errorMessage));
            };

            foreach (ContractDescription contract in contractLoader.GetContracts())
            {
                types.Add(contract.ContractType);
                foreach (OperationDescription operation in contract.Operations)
                {
                    XmlSerializerOperationBehavior behavior = operation.Behaviors.Find <XmlSerializerOperationBehavior>();
                    if (behavior != null)
                    {
                        foreach (XmlMapping map in behavior.GetXmlMappings())
                        {
                            mappings.Add(map);
                        }
                    }
                }
            }
            return(types);
        }
Пример #3
0
 public void WriteParagraph(string text)
 {
     this.Reset();
     this.AppendParagraph(text);
     ToolConsole.WriteLine(_stringBuilder.ToString());
     _stringBuilder = null;
 }
Пример #4
0
            private void CheckForBasicOptions()
            {
                _parent._noLogo = _arguments.ContainsArgument(Options.Cmd.NoLogo);
#if DEBUG
                ToolConsole.SetOptions(_arguments.ContainsArgument(Options.Cmd.Debug));
#endif
            }
Пример #5
0
        internal static void WriteInvalidDataContractError(InvalidDataContractException e)
        {
            WriteError(e);

            ToolConsole.WriteLine();
            ToolConsole.WriteLine(SR.Format(SR.HintConsiderUseXmlSerializer, Options.Cmd.DataContractOnly,
                                            Options.Cmd.ImportXmlTypes));
        }
Пример #6
0
        internal void GenerateCode(List <Assembly> assemblies)
        {
            if (!string.IsNullOrEmpty(_outFile) && assemblies.Count > 1)
            {
                ToolConsole.WriteWarning(SR.Format(SR.WrnOptionConflictsWithInput, Options.Cmd.Out));
                _outFile = null;
            }

            foreach (Assembly assembly in assemblies)
            {
                GenerateCode(assembly);
            }
        }
Пример #7
0
        internal static void Assert(bool condition, string message)
        {
            if (!condition)
            {
#if DEBUG
                ToolConsole.WriteError("Please file a bug or report the following issue with this tool:");
                StackTrace st = new StackTrace(true);
                ToolConsole.WriteLine(st.ToString());
#endif
                ToolConsole.WriteUnexpectedError(message);
                Tool.FailFast(message);
            }
        }
Пример #8
0
 internal static void WriteHelpText()
 {
     HelpGenerator.WriteUsage();
     ToolConsole.WriteLine();
     ToolConsole.WriteLine();
     HelpGenerator.WriteCommonOptionsHelp();
     ToolConsole.WriteLine();
     ToolConsole.WriteLine();
     HelpGenerator.WriteXmlSerializerTypeGenerationHelp();
     ToolConsole.WriteLine();
     ToolConsole.WriteLine();
     HelpGenerator.WriteExamples();
     ToolConsole.WriteLine();
     ToolConsole.WriteLine();
 }
Пример #9
0
                public static void WriteArguments(ArgumentInfo[] arguments)
                {
                    int    maxArgumentnLength = CalculateMaxNameLength(arguments);
                    int    helpTextIndent     = argHelpPrefix.Length + maxArgumentnLength + argHelpSeperator.Length;
                    string helpPattern        = argHelpPrefix + "{0, -" + maxArgumentnLength + "}" + argHelpSeperator + "{1}";

                    ToolStringBuilder builder = new ToolStringBuilder(helpTextIndent);

                    foreach (ArgumentInfo argument in arguments)
                    {
                        if (argument.BeginGroup)
                        {
                            ToolConsole.WriteLine();
                        }

                        string optionHelp = argument.GenerateHelp(helpPattern);
                        builder.WriteParagraph(optionHelp);
                    }
                }
Пример #10
0
        public static int Main(string[] args)
        {
            try
            {
                // ValidateUICulture() makes sure that this command-line tool can run on RightToLeft systems.
                ValidateUICulture();

                Options     options = Options.ParseArguments(args);
                ToolRuntime runtime = new ToolRuntime(options);
                return((int)runtime.Run());
            }
            catch (ToolArgumentException ae)
            {
                ToolConsole.WriteHeader();
                ToolConsole.WriteToolError(ae);
                return((int)ae.ExitCode);
            }
            catch (ToolRuntimeException re)
            {
                ToolConsole.WriteToolError(re);
                return((int)re.ExitCode);
            }
            catch (DcNS.InvalidDataContractException dce)
            {
                ToolConsole.WriteInvalidDataContractError(dce);
                return((int)ToolExitCodes.RuntimeError);
            }
#pragma warning suppress 56500 // covered by FxCOP
            catch (Exception e)
            {
                if (Tool.IsFatal(e))
                {
                    throw;
                }

                ToolConsole.WriteUnexpectedError(e);
                Tool.FailFast(e.ToString());
                return((int)ToolExitCodes.Unknown); // unreachable code;
            }
        }
Пример #11
0
        internal static void WriteError(Exception e, string prefix)
        {
#if DEBUG
            if (s_debug)
            {
                ToolConsole.WriteLine();
                WriteError(e.ToString(), prefix);
                return;
            }
#endif

            WriteError(e.Message, prefix);

            while (e.InnerException != null)
            {
                if (e.Message != e.InnerException.Message)
                {
                    WriteError(e.InnerException.Message, "    ");
                }
                e = e.InnerException;
            }
        }
Пример #12
0
        static public Type[] LoadTypes(Assembly assembly)
        {
            Type[] types;
            try
            {
                types = assembly.GetTypes();
            }
            catch (ReflectionTypeLoadException rtle)
            {
                ToolConsole.WriteWarning(SR.Format(SR.WrnCouldNotLoadTypesFromReferenceAssemblyAt, assembly.Location));
                foreach (Exception e in rtle.LoaderExceptions)
                {
                    ToolConsole.WriteLine("  " + e.Message, 2);
                }

                types = Array.FindAll <Type>(rtle.Types, delegate(Type t) { return(t != null); });
                if (types.Length == 0)
                {
                    throw new ToolInputException(SR.Format(SR.ErrCouldNotLoadTypesFromAssemblyAt, assembly.Location));
                }
            }
            return(types);
        }
Пример #13
0
        internal static void WriteToolError(ToolArgumentException ae)
        {
            WriteError(ae);

            ToolMexException me = ae as ToolMexException;

            if (me != null)
            {
                string serviceUri = me.ServiceUri.AbsoluteUri;

                ToolConsole.WriteLine();
                ToolConsole.WriteError(SR.Format(SR.WrnWSMExFailed, serviceUri), string.Empty);
                ToolConsole.WriteError(me.WSMexException, "    ");

                if (me.HttpGetException != null)
                {
                    ToolConsole.WriteLine();
                    ToolConsole.WriteError(SR.Format(SR.WrnHttpGetFailed, serviceUri), string.Empty);
                    ToolConsole.WriteError(me.HttpGetException, "    ");
                }
            }

            ToolConsole.WriteLine(SR.Format(SR.MoreHelp, Options.Abbr.Help));
        }
Пример #14
0
 internal static void WriteHeader()
 {
     // Using CommonResStrings.WcfTrademarkForCmdLine for the trademark: the proper resource for command line tools.
     ToolConsole.WriteLine(SR.Format(SR.Logo, SR.WcfTrademarkForCmdLine, ThisAssembly.InformationalVersion, SR.CopyrightForCmdLine));
 }
Пример #15
0
            }                          // beforefeildInit

            internal static void WriteUsage()
            {
                ToolConsole.WriteLine(SR.Format(SR.HelpUsage1));
                ToolConsole.WriteLine();
                ToolConsole.WriteLine(SR.Format(SR.HelpUsage6));
            }
Пример #16
0
        private void GenerateCode(Assembly assembly)
        {
            List <XmlMapping> mappings = new List <XmlMapping>();
            List <Type>       types    = CollectXmlSerializerTypes(assembly, mappings);

            if (types.Count == 0)
            {
                ToolConsole.WriteWarning(SR.Format(SR.WrnNoServiceContractTypes, assembly.GetName().CodeBase));
                return;
            }
            if (mappings.Count == 0)
            {
                ToolConsole.WriteWarning(SR.Format(SR.WrnNoXmlSerializerOperationBehavior, assembly.GetName().CodeBase));
                return;
            }

            bool success      = false;
            bool toDeleteFile = true;

            string codePath = Path.GetTempFileName();

            try
            {
                if (File.Exists(codePath))
                {
                    File.Delete(codePath);
                }

                using (FileStream fs = File.Create(codePath))
                {
                    MethodInfo method = typeof(System.Xml.Serialization.XmlSerializer).GetMethod("GenerateSerializer", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
                    if (method == null)
                    {
                        throw new ToolRuntimeException(SR.GenerateSerializerNotFound);
                    }
                    else
                    {
                        success = (bool)method.Invoke(null, new object[] { types.ToArray(), mappings.ToArray(), fs });
                    }
                }
            }
            finally
            {
                if (!success && toDeleteFile && File.Exists(codePath))
                {
                    File.Delete(codePath);
                }
            }

            string sgenSource = XmlSerializer.GetXmlSerializerAssemblyName(types[0]);

            // delete all temp files generated by CodeDom except source file
            sgenSource = BuildFilePath(sgenSource, sourceExtension, null);
            if (File.Exists(sgenSource))
            {
                File.Delete(sgenSource);
            }

            string sourceName;

            if (_outFile != null)
            {
                sourceName = FilenameHelper.UniquifyFileName(_outFile, sourceExtension);
            }
            else
            {
                sourceName = FilenameHelper.UniquifyFileName(sgenSource, sourceExtension);
            }

            string sourceFilePath = BuildFilePath(sourceName, sourceExtension, null);

            CreateDirectoryIfNeeded(sourceFilePath);
            File.Copy(codePath, sourceFilePath, true);
            ToolConsole.WriteLine(sourceFilePath);

            return;
        }
Пример #17
0
 private static void WriteExample(string syntax, string explanation)
 {
     ToolConsole.WriteLine(string.Format(CultureInfo.InvariantCulture, " {0}", syntax));
     s_exampleBuilder.WriteParagraph(string.Format(CultureInfo.InvariantCulture, "    {0}", explanation));
     ToolConsole.WriteLine();
 }