private IList <EdmSchemaError> GenerateCodeFor1_1Schema(string sourceEdmSchemaFilePath, LazyTextWriterCreator textWriter, IEnumerable <string> additionalEdmSchemaFilePaths) { EntityClassGenerator generator = new EntityClassGenerator(_languageOption); string target = textWriter.TargetFilePath; bool cleanUpTarget = false; try { if (textWriter.IsUserSuppliedTextWriter) { cleanUpTarget = true; target = Path.Combine(Path.GetTempPath(), Path.GetTempFileName()); } IList <EdmSchemaError> errors = generator.GenerateCode(sourceEdmSchemaFilePath, target, additionalEdmSchemaFilePaths != null ? additionalEdmSchemaFilePaths : Enumerable.Empty <string>()); if (textWriter.IsUserSuppliedTextWriter && !errors.Any(e => e.Severity == EdmSchemaErrorSeverity.Error)) { textWriter.GetOrCreateTextWriter().Write(File.ReadAllText(target)); } return(errors); } finally { if (cleanUpTarget && target != null && File.Exists(target)) { File.Delete(target); } } }
private IList <EdmSchemaError> InternalGenerateCode(string sourceEdmSchemaFilePath, Version schemaVersion, LazyTextWriterCreator textWriter, IEnumerable <string> additionalEdmSchemaFilePaths, Version targetFrameworkVersion) { List <EdmSchemaError> errors = new List <EdmSchemaError>(); try { if (targetFrameworkVersion == EntityFrameworkVersions.Version1) { errors.Add(new EdmSchemaError(Strings.EntityCodeGenTargetTooLow, (int)ModelBuilderErrorCode.TargetVersionNotSupported, EdmSchemaErrorSeverity.Error)); return(errors); } if (!MetadataItemCollectionFactory.ValidateActualVersionAgainstTarget(targetFrameworkVersion, schemaVersion, errors)) { return(errors); } if (schemaVersion == EntityFrameworkVersions.EdmVersion1_1) { return(GenerateCodeFor1_1Schema(sourceEdmSchemaFilePath, textWriter, additionalEdmSchemaFilePaths)); } ReflectionAdapter codeGenerator = ReflectionAdapter.Create(_languageOption, targetFrameworkVersion); codeGenerator.SourceCsdlPath = sourceEdmSchemaFilePath; codeGenerator.ReferenceCsdlPaths = additionalEdmSchemaFilePaths; codeGenerator.EdmToObjectNamespaceMap = _edmToObjectNamespaceMap.AsDictionary(); string code = codeGenerator.TransformText(); if (codeGenerator.Errors.Count != 0) { ModelBuilderErrorCode errorCode = ModelBuilderErrorCode.PreprocessTemplateTransformationError; errors.AddRange(codeGenerator.Errors.OfType <CompilerError>().Select(c => ConvertToEdmSchemaError(c, errorCode))); if (codeGenerator.Errors.HasErrors) { return(errors); } } using (TextWriter writer = textWriter.GetOrCreateTextWriter()) { writer.Write(code); } } catch (System.UnauthorizedAccessException ex) { errors.Add(EntityClassGenerator.CreateErrorForException(ModelBuilderErrorCode.SecurityError, ex)); } catch (System.IO.FileNotFoundException ex) { errors.Add(EntityClassGenerator.CreateErrorForException(ModelBuilderErrorCode.FileNotFound, ex)); } catch (System.Security.SecurityException ex) { errors.Add(EntityClassGenerator.CreateErrorForException(ModelBuilderErrorCode.SecurityError, ex)); } catch (System.IO.DirectoryNotFoundException ex) { errors.Add(EntityClassGenerator.CreateErrorForException(ModelBuilderErrorCode.DirectoryNotFound, ex)); } catch (System.IO.IOException ex) { errors.Add(EntityClassGenerator.CreateErrorForException(ModelBuilderErrorCode.IOException, ex)); } catch (Exception e) { if (MetadataUtil.IsCatchableExceptionType(e)) { errors.Add(EntityClassGenerator.CreateErrorForException(ModelBuilderErrorCode.PreprocessTemplateTransformationError, e, sourceEdmSchemaFilePath)); } else { throw; } } return(errors); }
private IList<EdmSchemaError> GenerateCodeFor1_1Schema(string sourceEdmSchemaFilePath, LazyTextWriterCreator textWriter, IEnumerable<string> additionalEdmSchemaFilePaths) { EntityClassGenerator generator = new EntityClassGenerator(_languageOption); string target = textWriter.TargetFilePath; bool cleanUpTarget = false; try { if (textWriter.IsUserSuppliedTextWriter) { cleanUpTarget = true; target = Path.Combine(Path.GetTempPath(), Path.GetTempFileName()); } IList<EdmSchemaError> errors = generator.GenerateCode(sourceEdmSchemaFilePath, target, additionalEdmSchemaFilePaths != null ? additionalEdmSchemaFilePaths : Enumerable.Empty<string>()); if (textWriter.IsUserSuppliedTextWriter && !errors.Any(e => e.Severity == EdmSchemaErrorSeverity.Error)) { textWriter.GetOrCreateTextWriter().Write(File.ReadAllText(target)); } return errors; } finally { if (cleanUpTarget && target != null && File.Exists(target)) { File.Delete(target); } } }
[ResourceConsumption(ResourceScope.Machine, ResourceScope.Machine)] //For Path.GetTempPath method. //We use tha path to create a temp file stream which is consistent with the resource consumption of machine. internal void GenerateCode(LazyTextWriterCreator target, string targetLocation) { Debug.Assert(target != null, "target parameter is null"); IndentedTextWriter indentedTextWriter = null; System.IO.Stream tempFileStream = null; System.IO.StreamReader reader = null; System.IO.StreamWriter writer = null; TempFileCollection tempFiles = null; try { CodeDomProvider provider = null; switch (Language) { case LanguageOption.GenerateCSharpCode: provider = new Microsoft.CSharp.CSharpCodeProvider(); break; case LanguageOption.GenerateVBCode: provider = new Microsoft.VisualBasic.VBCodeProvider(); break; } _isLanguageCaseSensitive = (provider.LanguageOptions & LanguageOptions.CaseInsensitive) == 0; new NamespaceEmitter(this, _codeNamespace, target.TargetFilePath).Emit(); // if there were errors we don't need the output file if (RealErrorsExist) { return; } if (FixUps.Count == 0 || !FixUpCollection.IsLanguageSupported(Language)) { indentedTextWriter = new IndentedTextWriter(target.GetOrCreateTextWriter(), "\t"); } else { // need to write to a temporary file so we can do fixups... tempFiles = new TempFileCollection(Path.GetTempPath()); string filename = Path.Combine(tempFiles.TempDir, "EdmCodeGenFixup-" + Guid.NewGuid().ToString() + ".tmp"); tempFiles.AddFile(filename, false); tempFileStream = new System.IO.FileStream(filename, System.IO.FileMode.CreateNew, System.IO.FileAccess.ReadWrite, System.IO.FileShare.None); indentedTextWriter = new IndentedTextWriter(new System.IO.StreamWriter(tempFileStream), "\t"); } CodeGeneratorOptions styleOptions = new CodeGeneratorOptions(); styleOptions.BracingStyle = "C"; styleOptions.BlankLinesBetweenMembers = false; styleOptions.VerbatimOrder = true; provider.GenerateCodeFromCompileUnit(CompileUnit, indentedTextWriter, styleOptions); // if we wrote to a temp file need to post process the file... if (tempFileStream != null) { indentedTextWriter.Flush(); tempFileStream.Seek(0, System.IO.SeekOrigin.Begin); reader = new System.IO.StreamReader(tempFileStream); FixUps.Do(reader, target.GetOrCreateTextWriter(), Language, SourceObjectNamespaceName != string.Empty); } } catch (System.UnauthorizedAccessException ex) { AddError(ModelBuilderErrorCode.SecurityError, EdmSchemaErrorSeverity.Error, ex); } catch (System.IO.FileNotFoundException ex) { AddError(ModelBuilderErrorCode.FileNotFound, EdmSchemaErrorSeverity.Error, ex); } catch (System.Security.SecurityException ex) { AddError(ModelBuilderErrorCode.SecurityError, EdmSchemaErrorSeverity.Error, ex); } catch (System.IO.DirectoryNotFoundException ex) { AddError(ModelBuilderErrorCode.DirectoryNotFound, EdmSchemaErrorSeverity.Error, ex); } catch (System.IO.IOException ex) { AddError(ModelBuilderErrorCode.IOException, EdmSchemaErrorSeverity.Error, ex); } finally { if (indentedTextWriter != null) { indentedTextWriter.Close(); } if (tempFileStream != null) { tempFileStream.Close(); } if (tempFiles != null) { tempFiles.Delete(); ((IDisposable)tempFiles).Dispose(); } if (reader != null) { reader.Close(); } if (writer != null) { writer.Close(); } } }
private IList<EdmSchemaError> InternalGenerateCode(string sourceEdmSchemaFilePath, Version schemaVersion, LazyTextWriterCreator textWriter, IEnumerable<string> additionalEdmSchemaFilePaths, Version targetFrameworkVersion) { List<EdmSchemaError> errors = new List<EdmSchemaError>(); try { if (targetFrameworkVersion == EntityFrameworkVersions.Version1) { errors.Add(new EdmSchemaError(Strings.EntityCodeGenTargetTooLow, (int)ModelBuilderErrorCode.TargetVersionNotSupported, EdmSchemaErrorSeverity.Error)); return errors; } if (!MetadataItemCollectionFactory.ValidateActualVersionAgainstTarget(targetFrameworkVersion, schemaVersion, errors)) { return errors; } if (schemaVersion == EntityFrameworkVersions.EdmVersion1_1) { return GenerateCodeFor1_1Schema(sourceEdmSchemaFilePath, textWriter, additionalEdmSchemaFilePaths); } ReflectionAdapter codeGenerator = ReflectionAdapter.Create(_languageOption, targetFrameworkVersion); codeGenerator.SourceCsdlPath = sourceEdmSchemaFilePath; codeGenerator.ReferenceCsdlPaths = additionalEdmSchemaFilePaths; codeGenerator.EdmToObjectNamespaceMap = _edmToObjectNamespaceMap.AsDictionary(); string code = codeGenerator.TransformText(); if (codeGenerator.Errors.Count != 0) { ModelBuilderErrorCode errorCode = ModelBuilderErrorCode.PreprocessTemplateTransformationError; errors.AddRange(codeGenerator.Errors.OfType<CompilerError>().Select(c => ConvertToEdmSchemaError(c, errorCode))); if (codeGenerator.Errors.HasErrors) return errors; } using (TextWriter writer = textWriter.GetOrCreateTextWriter()) { writer.Write(code); } } catch (System.UnauthorizedAccessException ex) { errors.Add(EntityClassGenerator.CreateErrorForException(ModelBuilderErrorCode.SecurityError, ex)); } catch (System.IO.FileNotFoundException ex) { errors.Add(EntityClassGenerator.CreateErrorForException(ModelBuilderErrorCode.FileNotFound, ex)); } catch (System.Security.SecurityException ex) { errors.Add(EntityClassGenerator.CreateErrorForException(ModelBuilderErrorCode.SecurityError, ex)); } catch (System.IO.DirectoryNotFoundException ex) { errors.Add(EntityClassGenerator.CreateErrorForException(ModelBuilderErrorCode.DirectoryNotFound, ex)); } catch (System.IO.IOException ex) { errors.Add(EntityClassGenerator.CreateErrorForException(ModelBuilderErrorCode.IOException, ex)); } catch (Exception e) { if (MetadataUtil.IsCatchableExceptionType(e)) { errors.Add(EntityClassGenerator.CreateErrorForException(ModelBuilderErrorCode.PreprocessTemplateTransformationError, e, sourceEdmSchemaFilePath)); } else { throw; } } return errors; }