internal static string GetOutputFileName(CommandProcessorOptions options, IEnumerable <MetadataSection> metadataSections) { string fileName = options.OutputFile?.FullName; if (string.IsNullOrWhiteSpace(fileName)) { var wsdlDocuments = metadataSections.Where(s => s.Metadata is WsdlNS.ServiceDescription).Cast <WsdlNS.ServiceDescription>(); foreach (WsdlNS.ServiceDescription wsdl in wsdlDocuments) { if (!string.IsNullOrEmpty(wsdl.Name)) { fileName = XmlConvert.DecodeName(wsdl.Name); if (!string.IsNullOrWhiteSpace(fileName) && fileName.IndexOfAny(Path.GetInvalidFileNameChars()) == -1) { break; } } } if (string.IsNullOrWhiteSpace(fileName)) { foreach (WsdlNS.ServiceDescription wsdl in wsdlDocuments) { if (wsdl.Services.Count > 0 && !string.IsNullOrEmpty(wsdl.Services[0].Name)) { fileName = XmlConvert.DecodeName(wsdl.Services[0].Name); if (!string.IsNullOrWhiteSpace(fileName) && fileName.IndexOfAny(Path.GetInvalidFileNameChars()) == -1) { break; } } } if (string.IsNullOrWhiteSpace(fileName)) { var schemaDocuments = metadataSections.Where(s => s.Metadata is XmlSchema).Cast <XmlSchema>(); foreach (XmlSchema schema in schemaDocuments) { if (!string.IsNullOrEmpty(schema.TargetNamespace)) { fileName = OutputPathHelper.FilenameFromUri(schema.TargetNamespace); if (!string.IsNullOrWhiteSpace(fileName) && fileName.IndexOfAny(Path.GetInvalidFileNameChars()) == -1) { break; } } } if (string.IsNullOrWhiteSpace(fileName)) { fileName = CodeSerializer.s_defaultFileName; } } } } return(fileName); }
internal CodeSerializer(CommandProcessorOptions options, IEnumerable <MetadataSection> inputMetadata) { string extension = GetOutputFileExtension(options); string outputFilename = GetOutputFileName(options, inputMetadata); _outputFilePath = OutputPathHelper.BuildFilePath(s_defaultFileName, options.OutputDir.FullName, outputFilename, extension, CommandProcessorOptions.Switches.OutputFile.Name); _codeProvider = options.CodeProvider; }
private StreamWriter CreateOutputFile() { OutputPathHelper.CreateDirectoryIfNeeded(_outputFilePath); try { return(new StreamWriter(new FileStream(_outputFilePath, FileMode.Create, FileAccess.Write), s_ouputEncoding)); } catch (Exception e) { if (Utils.IsFatalOrUnexpected(e)) { throw; } throw new ToolRuntimeException(string.Format(SR.ErrCannotCreateFileFormat, _outputFilePath), e); } }
public string SaveCode(CodeCompileUnit codeCompileUnit) { string filePath = null; OutputPathHelper.CreateDirectoryIfNeeded(_outputFilePath); CodeGeneratorOptions codeGenOptions = new CodeGeneratorOptions(); codeGenOptions.BracingStyle = "C"; using (TextWriter writer = CreateOutputFile()) { try { _codeProvider.GenerateCodeFromCompileUnit(codeCompileUnit, writer, codeGenOptions); writer.Flush(); } catch (Exception e) { if (Utils.IsFatalOrUnexpected(e)) { throw; } try { if (File.Exists(_outputFilePath)) { File.Delete(_outputFilePath); } } catch (System.UnauthorizedAccessException) { } throw new ToolRuntimeException(SR.ErrCodegenError, e); } filePath = _outputFilePath.Contains(" ") ? string.Format(CultureInfo.InvariantCulture, "\"{0}\"", _outputFilePath) : _outputFilePath; } return(filePath); }