Type CompileCore(IPersistentAssemblyInfo persistentAssemblyInfo, string generateCode, CompilerParameters compilerParams, System.CodeDom.Compiler.CodeDomProvider codeProvider) { CompilerResults compileAssemblyFromSource = null; Type compileCore = null; try { compileAssemblyFromSource = codeProvider.CompileAssemblyFromSource(compilerParams, generateCode); if (compilerParams.GenerateInMemory) { Assembly compiledAssembly = compileAssemblyFromSource.CompiledAssembly; _compiledAssemblies.Add(compiledAssembly); compileCore = compiledAssembly.GetTypes().Single(type => typeof(ModuleBase).IsAssignableFrom(type)); } } catch (Exception e) { Tracing.Tracer.LogError(e); } finally { if (compileAssemblyFromSource != null) { SetErrors(compileAssemblyFromSource, persistentAssemblyInfo, compilerParams); } if (string.IsNullOrEmpty(persistentAssemblyInfo.CompileErrors) && compileCore != null) { if (!ValidateBOModel(persistentAssemblyInfo, compileCore)) compileCore = null; } if (Directory.Exists(StrongKeys)) Directory.Delete(StrongKeys, true); } return compileCore; }
public static void SupportCompositeKeyPersistentObjects(this IPersistentAssemblyInfo persistentAssemblyInfo) { var keys = persistentAssemblyInfo.PersistentClassInfos.SelectMany(info => info.OwnMembers) .OfType <IPersistentReferenceMemberInfo>() .Where( memberInfo => memberInfo.ReferenceClassInfo.CodeTemplateInfo.CodeTemplate.TemplateType == TemplateType.Struct); var dictionary = new Dictionary <IPersistentClassInfo, ITemplateInfo>(); var objectSpace = XPObjectSpace.FindObjectSpaceByObject(persistentAssemblyInfo); foreach (var key in keys) { if (!dictionary.ContainsKey(key.Owner)) { var info = objectSpace.Create <ITemplateInfo>(); info.Name = SupportPersistentObjectsAsAPartOfACompositeKey; key.Owner.TemplateInfos.Add(info); dictionary.Add(key.Owner, info); } ITemplateInfo templateInfo = dictionary[key.Owner]; templateInfo.TemplateCode = @"protected override void OnLoaded() { base.OnLoaded(); " + GetCode(key) + @" }"; } }
private Assembly CompileFile(IPersistentAssemblyInfo assemblyInfo) { var compilerResults = CodeValidator.Compiler.Compile(assemblyInfo.GenerateCode(), assemblyInfo.Name, assemblyInfo.StrongKeyFileData.GetBytes()); return(Assembly.LoadFile(compilerResults.PathToAssembly)); }
void CreateMappedAssemblyInfo(XPObjectSpace objectSpace, IPersistentAssemblyInfo persistentAssemblyInfo, LogonObject logonObject, string[] selectedTables) { var assemblyInfo = objectSpace.GetObject(persistentAssemblyInfo); new AssemblyGenerator(logonObject, assemblyInfo, selectedTables).Create(); objectSpace.CommitChanges(); }
bool ValidateBOModel(IPersistentAssemblyInfo persistentAssemblyInfo, Type compileCore) { if (persistentAssemblyInfo.ValidateModelOnCompile) { var instance = XafTypesInfo.Instance; try { var typesInfo = new TypesInfoBuilder.TypesInfo(); typesInfo.AddEntityStore(new NonPersistentEntityStore(typesInfo)); typesInfo.AddEntityStore(new XpoTypeInfoSource(typesInfo)); typesInfo.AssignAsInstance(); typesInfo.LoadTypes(compileCore.Assembly); var applicationModulesManager = new ApplicationModulesManager(); applicationModulesManager.AddModule(compileCore); applicationModulesManager.Load(typesInfo, true); } catch (Exception exception) { persistentAssemblyInfo.CompileErrors = exception.ToString(); return(false); } finally { instance.AssignAsInstance(); } } return(true); }
public static string GenerateCode(IPersistentAssemblyInfo persistentAssemblyInfo) { if (persistentAssemblyInfo.Session.IsObjectMarkedDeleted(persistentAssemblyInfo)) { return(null); } var usingsDictionary = new Dictionary <string, string>(); string generateAssemblyCode = GetAssemblyAttributesCode(persistentAssemblyInfo) + Environment.NewLine + GetModuleCode(persistentAssemblyInfo.Name) + Environment.NewLine; var generatedClassCode = new StringBuilder(); var generatedUsings = new StringBuilder(); foreach (var persistentClassInfo in persistentAssemblyInfo.PersistentClassInfos) { string code = GenerateCode(persistentClassInfo); string usingsString = GetUsingsString(persistentAssemblyInfo, code); code = code.Replace(usingsString, ""); code = code.Replace("\n", "\r\n"); if (!(usingsDictionary.ContainsKey(usingsString))) { usingsDictionary.Add(usingsString, null); usingsString = usingsString.Replace("\n", "\r\n"); generatedUsings.Append(usingsString); } generatedClassCode.Append(code); } return(generatedUsings + generateAssemblyCode + generatedClassCode); }
public static Type Validate(this IPersistentAssemblyInfo assemblyInfo, string path) { return(new CompileEngine().CompileModule(assemblyInfo, parameters => { parameters.GenerateInMemory = false; parameters.OutputAssembly = parameters.OutputAssembly + "Validating"; }, path)); }
public Type CompileModule(IPersistentAssemblyInfo persistentAssemblyInfo, Action <CompilerParameters> action, string path) { Assembly loadedAssembly = AppDomain.CurrentDomain.GetAssemblies().Where(assembly => new AssemblyName(assembly.FullName + "").Name == persistentAssemblyInfo.Name).FirstOrDefault(); if (loadedAssembly != null) { return(loadedAssembly.GetTypes().Where(type => typeof(ModuleBase).IsAssignableFrom(type)).Single()); } var generateCode = CodeEngine.GenerateCode(persistentAssemblyInfo); var codeProvider = getCodeDomProvider(persistentAssemblyInfo.CodeDomProvider); var compilerParams = new CompilerParameters { CompilerOptions = @"/target:library /lib:" + GetReferenceLocations() + GetStorngKeyParams(persistentAssemblyInfo), GenerateExecutable = false, GenerateInMemory = true, IncludeDebugInformation = false, OutputAssembly = Path.Combine(path, persistentAssemblyInfo.Name + XpandExtension), }; if (action != null) { action.Invoke(compilerParams); } AddReferences(compilerParams, path); if (File.Exists(compilerParams.OutputAssembly)) { File.Delete(compilerParams.OutputAssembly); } return(CompileCore(persistentAssemblyInfo, generateCode, compilerParams, codeProvider)); }
public void Name_Is_Valid_File_name(WCTestData testData, IPersistentAssemblyInfo persistentAssemblyInfo) { persistentAssemblyInfo.Name = "file@"; throw new NotImplementedException(); // Validator.RuleSet.StateOf<RuleValidFileName>(persistentAssemblyInfo, nameof(persistentAssemblyInfo.Name)) // .Should().Be(ValidationState.Invalid); }
public AssemblyGenerator(LogonObject logonObject, IPersistentAssemblyInfo persistentAssemblyInfo, string[] tables) { _persistentAssemblyInfo = persistentAssemblyInfo; var dataStoreSchemaExplorer = ((IDataStoreSchemaExplorer)XpoDefault.GetConnectionProvider(logonObject.ConnectionString, AutoCreateOption.None)); _storageTables = dataStoreSchemaExplorer.GetStorageTables(tables).Where(table => table.PrimaryKey != null).ToArray(); _logonObject = logonObject; _objectSpace = ObjectSpace.FindObjectSpaceByObject(persistentAssemblyInfo); }
Type CompileCore(IPersistentAssemblyInfo persistentAssemblyInfo, string generateCode, CompilerParameters compilerParams, System.CodeDom.Compiler.CodeDomProvider codeProvider) { CompilerResults compileAssemblyFromSource = null; try { compileAssemblyFromSource = codeProvider.CompileAssemblyFromSource(compilerParams, generateCode); if (compilerParams.GenerateInMemory) { Assembly compiledAssembly = compileAssemblyFromSource.CompiledAssembly; CompiledAssemblies.Add(compiledAssembly); return(compiledAssembly.GetTypes().Where(type => typeof(ModuleBase).IsAssignableFrom(type)).Single()); } return(null); } catch (Exception e) { Tracing.Tracer.LogError(e); } finally { if (compileAssemblyFromSource != null) { SetErrors(compileAssemblyFromSource, persistentAssemblyInfo, compilerParams); } if (Directory.Exists(STR_StrongKeys)) { Directory.Delete(STR_StrongKeys, true); } } return(null); }
public Type CompileModule(IPersistentAssemblyInfo persistentAssemblyInfo, bool registerPersistentTypes, string path) { Type compileModule = CompileModule(persistentAssemblyInfo,path); if (registerPersistentTypes&&compileModule!= null) foreach (var type in compileModule.Assembly.GetTypes()) { XafTypesInfo.Instance.RegisterEntity(type); } return compileModule; }
public void Assmebly_File(IWCTestData testData, Compiler compiler, IPersistentAssemblyInfo assemblyInfo) { var code = assemblyInfo.GenerateCode(); var compilerResult = compiler.Compile(code, assemblyInfo.Name); compilerResult.AssemblyDefinition.Should().NotBeNull(); }
public IPersistentClassInfo Create(Table table, IPersistentAssemblyInfo persistentAssemblyInfo, IMapperInfo mapperInfo) { var persistentClassInfo = CreateCore(table, persistentAssemblyInfo,mapperInfo); foreach (ForeignKey foreignKey in table.ForeignKeys) { CreateCore(_database.Tables[foreignKey.ReferencedTable], persistentAssemblyInfo, mapperInfo); } return persistentClassInfo; }
public void Create(IPersistentAssemblyInfo persistentAssemblyInfo, IDataStoreLogonObject dataStoreLogonObject) { if (persistentAssemblyInfo.PersistentClassInfos.Any()) { var persistentAssemblyDataStoreAttributeInfo = _objectSpace.CreateWCObject <IPersistentAssemblyDataStoreAttributeInfo>(); persistentAssemblyDataStoreAttributeInfo.DataStoreLogon = dataStoreLogonObject; persistentAssemblyDataStoreAttributeInfo.PersistentClassInfo = persistentAssemblyInfo.PersistentClassInfos[0]; persistentAssemblyInfo.Attributes.Add(persistentAssemblyDataStoreAttributeInfo); } }
public Type CompileModule(IPersistentAssemblyInfo persistentAssemblyInfo, bool registerPersistentTypes, string path) { Type compileModule = CompileModule(persistentAssemblyInfo, path); if (registerPersistentTypes && compileModule != null) { RegisterPersistentTypes(compileModule); } return(compileModule); }
public AssemblyGenerator(LogonObject logonObject, IPersistentAssemblyInfo persistentAssemblyInfo, string[] tables) { _persistentAssemblyInfo = persistentAssemblyInfo; var dataStoreSchemaExplorer = ((IDataStoreSchemaExplorer)XpoDefault.GetConnectionProvider(logonObject.ConnectionString, AutoCreateOption.None)); dataStoreSchemaExplorer = GetDataStoreSchemaExplorer(dataStoreSchemaExplorer); _storageTables = dataStoreSchemaExplorer.GetStorageTables(tables).Where(table => table.PrimaryKey != null).ToArray(); _logonObject = logonObject; _objectSpace = XPObjectSpace.FindObjectSpaceByObject(persistentAssemblyInfo); }
public IPersistentClassInfo Create(Table table, IPersistentAssemblyInfo persistentAssemblyInfo, IMapperInfo mapperInfo) { var persistentClassInfo = CreateCore(table, persistentAssemblyInfo, mapperInfo); foreach (ForeignKey foreignKey in table.ForeignKeys) { CreateCore(_database.GetTable(foreignKey.ReferencedTable, foreignKey.ReferencedTableSchema), persistentAssemblyInfo, mapperInfo); } return(persistentClassInfo); }
static string GetUsingsString(IPersistentAssemblyInfo persistentAssemblyInfo, string code) { var regex = new Regex( persistentAssemblyInfo.CodeDomProvider == CodeDomProvider.CSharp ? "^using .*;" : "^Imports .*;", RegexOptions.Multiline); return(regex.Matches(code).OfType <Match>().Aggregate("", (current, match) => current + match.Value + "\n")); }
public ClassGeneratorHelper(IObjectSpace XPObjectSpace) { _persistentAssemblyInfo = XPObjectSpace.CreateObject<PersistentAssemblyInfo>(); _persistentAssemblyInfo.Name = "PersistentAssemblyInfo"; _persistentClassInfo = XPObjectSpace.CreateObject<PersistentClassInfo>(); _persistentClassInfo.Name = "MainTable"; _persistentAssemblyInfo.PersistentClassInfos.Add(_persistentClassInfo); _persistentClassInfo.SetDefaultTemplate(TemplateType.Class); _classGeneratorInfos = new Dictionary<string, ClassGeneratorInfo>(); var classGeneratorInfo = new ClassGeneratorInfo(PersistentClassInfo, DbTable); _classGeneratorInfos.Add(PersistentClassInfo.Name, classGeneratorInfo); }
private IPersistentClassInfo CreateCore(Table table, IPersistentAssemblyInfo persistentAssemblyInfo, IMapperInfo mapperInfo) { int count = table.Columns.OfType<Column>().Where(column => column.InPrimaryKey).Count(); IPersistentClassInfo persistentClassInfo = CreatePersistentClassInfo(table.Name, TemplateType.Class, persistentAssemblyInfo); persistentClassInfo.BaseType = typeof(XPLiteObject); if (count > 1) CreatePersistentClassInfo(table.Name + KeyStruct, TemplateType.Struct, persistentAssemblyInfo); _extraInfoBuilder.CreateExtraInfos(table, persistentClassInfo, mapperInfo); if (count == 0) Tracing.Tracer.LogError("No primary keys found for " + table.Name); return persistentClassInfo; }
IPersistentClassInfo CreatePersistentClassInfo(string name, TemplateType templateType, IPersistentAssemblyInfo persistentAssemblyInfo) { var findBussinessObjectType = WCTypesInfo.Instance.FindBussinessObjectType<IPersistentClassInfo>(); var info = _objectSpace.Session.FindObject(PersistentCriteriaEvaluationBehavior.InTransaction, findBussinessObjectType, CriteriaOperator.Parse("Name=?", name)) as IPersistentClassInfo; if (info != null) return info; var persistentClassInfo = CreateNew(name); persistentAssemblyInfo.PersistentClassInfos.Add(persistentClassInfo); persistentClassInfo.SetDefaultTemplate(templateType); return persistentClassInfo; }
public static Version Version(this IPersistentAssemblyInfo info) { var xpandVersion = new Version(XpandAssemblyInfo.Version); var revision = $"{xpandVersion.Revision}"; if (revision == "-1") { revision = null; } return(new Version(xpandVersion.Major, xpandVersion.Minor, int.Parse(xpandVersion.Build + revision), info.Revision)); }
public void Assembly_Should_Validate_if_version_different_to_file_version(int revision, IWCTestData testData, IPersistentAssemblyInfo assemblyInfo, IAssemblyManager assemblyManager) { assemblyInfo.Name += Guid.NewGuid().ToString("n"); assemblyInfo.Revision = 1; assemblyManager.CodeValidator.Compiler.Compile(assemblyInfo.GenerateCode(), assemblyInfo.Name, new byte[0]); assemblyInfo.Revision = revision; var validAssemblyInfos = assemblyManager.ValidateAssemblyInfos(); validAssemblyInfos.Length.Should().Be(1); }
string GetStorngKeyParams(IPersistentAssemblyInfo persistentAssemblyInfo) { if (persistentAssemblyInfo.FileData != null) { if (!Directory.Exists(StrongKeys)) Directory.CreateDirectory(StrongKeys); var newGuid = Guid.NewGuid(); using (var fileStream = new FileStream(@"StrongKeys\" + newGuid + ".snk", FileMode.Create)) { persistentAssemblyInfo.FileData.SaveToStream(fileStream); } return @" /keyfile:StrongKeys\" + newGuid + ".snk"; } return null; }
public ClassGeneratorHelper(IObjectSpace XPObjectSpace) { _persistentAssemblyInfo = XPObjectSpace.CreateObject <PersistentAssemblyInfo>(); _persistentAssemblyInfo.Name = "PersistentAssemblyInfo"; _persistentClassInfo = XPObjectSpace.CreateObject <PersistentClassInfo>(); _persistentClassInfo.Name = "MainTable"; _persistentAssemblyInfo.PersistentClassInfos.Add(_persistentClassInfo); _persistentClassInfo.SetDefaultTemplate(TemplateType.Class); _classGeneratorInfos = new Dictionary <string, ClassGeneratorInfo>(); var classGeneratorInfo = new ClassGeneratorInfo(PersistentClassInfo, DbTable); _classGeneratorInfos.Add(PersistentClassInfo.Name, classGeneratorInfo); }
public static void SupportCompositeKeyPersistentObjects(IPersistentAssemblyInfo persistentAssemblyInfo, Func <ITemplateInfo, bool> templateInfoPredicate) { var keys = persistentAssemblyInfo.PersistentClassInfos.SelectMany(info => info.OwnMembers).OfType <IPersistentReferenceMemberInfo>().Where(memberInfo => memberInfo.ReferenceClassInfo.CodeTemplateInfo.CodeTemplate.TemplateType == TemplateType.Struct); foreach (var key in keys) { var templateInfos = key.Owner.TemplateInfos.Where(templateInfoPredicate); ITemplateInfo templateInfo = templateInfos.Single(); templateInfo.TemplateCode = @"protected override void OnLoaded() { base.OnLoaded(); " + GetCode(key) + @" }"; } }
private ValidatorResult Validate(IPersistentAssemblyInfo info) { var strongKeyBytes = info.StrongKeyFileData.GetBytes(); var code = info.GeneratedCode; var validatorResult = CodeValidator.Validate(code, strongKeyBytes); info.Errors = validatorResult.Message; if (!validatorResult.Valid) { Tracing.Tracer.LogSeparator("Validation for " + info.Name); Tracing.Tracer.LogText(validatorResult.Message); } return(validatorResult); }
public static IPersistentClassInfo CreateClass(this IPersistentAssemblyInfo assemblyInfo, string className) { var objectType = XafTypesInfo.Instance.FindBussinessObjectType <IPersistentClassInfo>(); var behavior = PersistentCriteriaEvaluationBehavior.InTransaction; var persistentClassInfo = (IPersistentClassInfo)assemblyInfo.Session.FindObject(behavior, objectType, CriteriaOperator.Parse(nameof(IPersistentClassInfo.Name) + "=?", className)) ?? (IPersistentClassInfo)Activator.CreateInstance(objectType, assemblyInfo.Session); persistentClassInfo.Name = className; persistentClassInfo.PersistentAssemblyInfo = assemblyInfo; assemblyInfo.PersistentClassInfos.Add(persistentClassInfo); persistentClassInfo.SetDefaultTemplate(TemplateType.Class); return(persistentClassInfo); }
void CreateMappedAssemblyInfo(XPObjectSpace objectSpace, IPersistentAssemblyInfo persistentAssemblyInfo, ISqlMapperInfo sqlMapperInfo) { string connectionString = sqlMapperInfo.GetConnectionString(); var sqlConnection = (SqlConnection) new SimpleDataLayer(XpoDefault.GetConnectionProvider(connectionString, AutoCreateOption.None)).Connection; var server = new Server(new ServerConnection(sqlConnection)); Database database = server.Databases[sqlConnection.Database]; database.Refresh(); IPersistentAssemblyInfo assemblyInfo = objectSpace.GetObject(persistentAssemblyInfo); var dbMapper = new DbMapper(objectSpace, assemblyInfo, sqlMapperInfo); dbMapper.Map(database, sqlMapperInfo.MapperInfo); objectSpace.CommitChanges(); }
private void RaiseRevision(IObjectSpace objectSpace, IPersistentAssemblyInfo persistentAssemblyInfo) { if (persistentAssemblyInfo == null) { return; } if (!_persistentAssemblyInfos.ContainsKey(objectSpace)) { _persistentAssemblyInfos.Add(objectSpace, new List <IPersistentAssemblyInfo>()); } if (!_persistentAssemblyInfos[objectSpace].Contains(persistentAssemblyInfo)) { persistentAssemblyInfo.Revision++; } }
string GetStorngKeyParams(IPersistentAssemblyInfo persistentAssemblyInfo) { if (persistentAssemblyInfo.FileData != null) { if (!Directory.Exists(STR_StrongKeys)) { Directory.CreateDirectory(STR_StrongKeys); } var newGuid = Guid.NewGuid(); using (var fileStream = new FileStream(@"StrongKeys\" + newGuid + ".snk", FileMode.Create)) { persistentAssemblyInfo.FileData.SaveToStream(fileStream); } return(@" /keyfile:StrongKeys\" + newGuid + ".snk"); } return(null); }
public ITypeHandler <T1, T2> ManyToMany() { PersistentAssemblyBuilder persistentAssemblyBuilder = GetPersistentAssemblyBuilder(); var type1 = typeof(T1); var type2 = typeof(T2); var type1Name = getName(type1); var type2Name = getName(type2); var classHandler = persistentAssemblyBuilder.CreateClasses(new[] { type1Name, type2Name }); IPersistentAssemblyInfo persistentAssemblyInfo = persistentAssemblyBuilder.PersistentAssemblyInfo; var persistentClassInfo1 = persistentAssemblyInfo.PersistentClassInfos[0]; var persistentClassInfo2 = persistentAssemblyInfo.PersistentClassInfos[1]; classHandler.CreateCollectionMember(persistentClassInfo1, type2Name + "s", persistentClassInfo2); classHandler.CreateCollectionMember(persistentClassInfo2, type1Name + "s", persistentClassInfo1, type2Name + "s"); createTypes(type1, type2, persistentAssemblyBuilder, type1Name, type2Name); return(this); }
static void SetErrors(CompilerResults compileAssemblyFromSource, IPersistentAssemblyInfo persistentAssemblyInfo, CompilerParameters compilerParams) { persistentAssemblyInfo.CompileErrors = null; persistentAssemblyInfo.CompileErrors = compileAssemblyFromSource.Errors.Cast <CompilerError>().Aggregate( persistentAssemblyInfo.CompileErrors, (current, error) => current + Environment.NewLine + error.ToString()); if (!string.IsNullOrEmpty(persistentAssemblyInfo.CompileErrors)) { Tracing.Tracer.LogSeparator("Compilization error of " + persistentAssemblyInfo.Name); Tracing.Tracer.LogText(persistentAssemblyInfo.CompileErrors); Tracing.Tracer.LogSeparator("Referenced Assemblies:"); foreach (var reference in compilerParams.ReferencedAssemblies) { Tracing.Tracer.LogVerboseText(reference); } } }
bool NeedsCompilation(IPersistentAssemblyInfo info) { if (AppDomain.CurrentDomain.GetAssemblies().Any(assembly => assembly.GetName().Name == info.Name)) { return(false); } var assemblyFile = GetAssemblyFile(info); if (!File.Exists(assemblyFile)) { return(true); } var fileVersion = AssemblyDefinition.ReadAssembly(assemblyFile).Name.Version; var infoVersion = info.Version(); return((infoVersion > fileVersion) || (infoVersion < fileVersion)); }
private void ValidateAssembly(IPersistentAssemblyInfo persistentAssemblyInfo) { if (!_validating) { _validating = true; var validatorResult = persistentAssemblyInfo.Validate(AssemblyPathProvider.Instance.GetPath(Application)); persistentAssemblyInfo.Errors = validatorResult.Message; ObjectSpace.CommitChanges(); _validating = false; if (!validatorResult.Valid) { var messageResult = Validator.RuleSet.NewRuleSetValidationMessageResult(ObjectSpace, "Validation error! check Compile Errors Tab.", View.CurrentObject); throw new ValidationException(messageResult); } } }
private IPersistentClassInfo CreateCore(Table table, IPersistentAssemblyInfo persistentAssemblyInfo, IMapperInfo mapperInfo) { int count = table.Columns.OfType <Column>().Where(column => column.InPrimaryKey).Count(); IPersistentClassInfo persistentClassInfo = CreatePersistentClassInfo(table.Name, TemplateType.Class, persistentAssemblyInfo); persistentClassInfo.BaseType = typeof(XPLiteObject); if (count > 1) { CreatePersistentClassInfo(table.Name + KeyStruct, TemplateType.Struct, persistentAssemblyInfo); } _extraInfoBuilder.CreateExtraInfos(table, persistentClassInfo, mapperInfo); if (count == 0) { Tracing.Tracer.LogError("No primary keys found for " + table.Name); } return(persistentClassInfo); }
public Type CompileModule(IPersistentAssemblyInfo persistentAssemblyInfo,Action<CompilerParameters> action) { var generateCode = CodeEngine.GenerateCode(persistentAssemblyInfo); var codeProvider = getCodeDomProvider(persistentAssemblyInfo.CodeDomProvider); var compilerParams = new CompilerParameters { CompilerOptions = @"/target:library /lib:" + GetReferenceLocations() + GetStorngKeyParams(persistentAssemblyInfo), GenerateExecutable = false, GenerateInMemory = true, IncludeDebugInformation = false, OutputAssembly = persistentAssemblyInfo.Name }; if (action!= null) action.Invoke(compilerParams); addReferences(compilerParams); if (File.Exists(compilerParams.OutputAssembly)) File.Delete(compilerParams.OutputAssembly); return compile(persistentAssemblyInfo, generateCode, compilerParams, codeProvider); }
public Type CompileModule(IPersistentAssemblyInfo persistentAssemblyInfo, Action<CompilerParameters> action, string path) { Assembly loadedAssembly = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(assembly => new AssemblyName(assembly.FullName + "").Name == persistentAssemblyInfo.Name); if (loadedAssembly != null) return loadedAssembly.GetTypes().Single(type => typeof(ModuleBase).IsAssignableFrom(type)); var generateCode = CodeEngine.GenerateCode(persistentAssemblyInfo); var codeProvider = GetCodeDomProvider(persistentAssemblyInfo.CodeDomProvider); var compilerParams = new CompilerParameters { CompilerOptions = @"/target:library /lib:" + GetReferenceLocations() + GetStorngKeyParams(persistentAssemblyInfo), GenerateExecutable = false, GenerateInMemory = true, IncludeDebugInformation = false, OutputAssembly = Path.Combine(path, persistentAssemblyInfo.Name + XpandExtension), }; if (action != null) action.Invoke(compilerParams); AddReferences(compilerParams, path); if (File.Exists(compilerParams.OutputAssembly)) File.Delete(compilerParams.OutputAssembly); return CompileCore(persistentAssemblyInfo, generateCode, compilerParams, codeProvider); }
Type compile(IPersistentAssemblyInfo persistentAssemblyInfo, string generateCode, CompilerParameters compilerParams, System.CodeDom.Compiler.CodeDomProvider codeProvider) { CompilerResults compileAssemblyFromSource = null; try{ compileAssemblyFromSource = codeProvider.CompileAssemblyFromSource(compilerParams, generateCode); if (compilerParams.GenerateInMemory) { Assembly compiledAssembly = compileAssemblyFromSource.CompiledAssembly; CompiledAssemblies.Add(compiledAssembly); return compiledAssembly.GetTypes().Where(type => typeof(ModuleBase).IsAssignableFrom(type)).Single(); } return null; } catch (Exception){ } finally { if (compileAssemblyFromSource != null){ SetErrors(compileAssemblyFromSource, persistentAssemblyInfo); } if (Directory.Exists(STR_StrongKeys)) Directory.Delete(STR_StrongKeys,true); } return null; }
public static void CreateMissingAssociations(this IPersistentAssemblyInfo assemblyInfo) { var attributes = assemblyInfo.PersistentClassInfos.SelectMany(info => info.OwnMembers) .SelectMany(info => info.TypeAttributes) .OfType <IPersistentAssociationAttribute>() .Where(attribute => attribute.RelationType != RelationType.Undefined); foreach (var attribute in attributes) { if (attribute.RelationType == RelationType.OneToMany) { if (attribute.Owner is IPersistentReferenceMemberInfo persistentReferenceMemberInfo) { persistentReferenceMemberInfo.GetAssociatedCollection(attribute.ElementTypeFullName); } else { ((IPersistentCollectionMemberInfo)attribute.Owner).GetAssociatedReference(attribute.ElementTypeFullName); } } } }
public Type CompileModule(IPersistentAssemblyInfo persistentAssemblyInfo, bool registerPersistentTypes, string path) { Type compileModule = CompileModule(persistentAssemblyInfo, path); if (registerPersistentTypes && compileModule != null) RegisterPersistentTypes(compileModule); return compileModule; }
public Type CompileModule(IPersistentAssemblyInfo persistentAssemblyInfo, string path) { return CompileModule(persistentAssemblyInfo, parameters => { }, path); }
bool ValidateBOModel(IPersistentAssemblyInfo persistentAssemblyInfo, Type compileCore) { if (persistentAssemblyInfo.ValidateModelOnCompile) { var instance = XafTypesInfo.Instance; try{ var typesInfo = new TypesInfoBuilder.TypesInfo(); typesInfo.AddEntityStore(new NonPersistentEntityStore(typesInfo)); typesInfo.AddEntityStore(new XpoTypeInfoSource(typesInfo)); typesInfo.AssignAsInstance(); typesInfo.LoadTypes(compileCore.Assembly); var applicationModulesManager = new ApplicationModulesManager(); applicationModulesManager.AddModule(compileCore); applicationModulesManager.Load(typesInfo, true); } catch (Exception exception){ persistentAssemblyInfo.CompileErrors = exception.ToString(); return false; } finally{ instance.AssignAsInstance(); } } return true; }
static void SetErrors(CompilerResults compileAssemblyFromSource, IPersistentAssemblyInfo persistentAssemblyInfo) { persistentAssemblyInfo.CompileErrors = null; persistentAssemblyInfo.CompileErrors = compileAssemblyFromSource.Errors.Cast<CompilerError>().Aggregate( persistentAssemblyInfo.CompileErrors, (current, error) => current +Environment.NewLine+ error.ToString()); }
public static void SupportCompositeKeyPersistentObjects(IPersistentAssemblyInfo persistentAssemblyInfo, Func<ITemplateInfo, bool> templateInfoPredicate) { var keys = persistentAssemblyInfo.PersistentClassInfos.SelectMany(info => info.OwnMembers).OfType<IPersistentReferenceMemberInfo>().Where(memberInfo => memberInfo.ReferenceClassInfo.CodeTemplateInfo.CodeTemplate.TemplateType == TemplateType.Struct); foreach (var key in keys) { var templateInfo = key.Owner.TemplateInfos.Where(templateInfoPredicate).Single(); templateInfo.TemplateCode = @"protected override void OnLoaded() { base.OnLoaded(); " + GetCode(key) + @" }"; } }
static void SetErrors(CompilerResults compileAssemblyFromSource, IPersistentAssemblyInfo persistentAssemblyInfo, CompilerParameters compilerParams) { persistentAssemblyInfo.CompileErrors = null; persistentAssemblyInfo.CompileErrors = compileAssemblyFromSource.Errors.Cast<CompilerError>().Aggregate( persistentAssemblyInfo.CompileErrors, (current, error) => current + Environment.NewLine + error.ToString()); if (!string.IsNullOrEmpty(persistentAssemblyInfo.CompileErrors)) { Tracing.Tracer.LogSeparator("Compilization error of " + persistentAssemblyInfo.Name); Tracing.Tracer.LogText(persistentAssemblyInfo.CompileErrors); Tracing.Tracer.LogSeparator("Referenced Assemblies:"); foreach (var reference in compilerParams.ReferencedAssemblies) { Tracing.Tracer.LogVerboseText(reference); } } }
public void Create(IPersistentAssemblyInfo persistentAssemblyInfo, IDataStoreLogonObject dataStoreLogonObject) { if (persistentAssemblyInfo.PersistentClassInfos.Any()) { var persistentAssemblyDataStoreAttributeInfo = _objectSpace.CreateWCObject<IPersistentAssemblyDataStoreAttributeInfo>(); persistentAssemblyDataStoreAttributeInfo.DataStoreLogon = dataStoreLogonObject; persistentAssemblyDataStoreAttributeInfo.PersistentClassInfo = persistentAssemblyInfo.PersistentClassInfos[0]; persistentAssemblyInfo.Attributes.Add(persistentAssemblyDataStoreAttributeInfo); } }
public static string GenerateCode(IPersistentAssemblyInfo persistentAssemblyInfo) { string generateCode =GetVersionCode(persistentAssemblyInfo)+Environment.NewLine+getModuleCode(persistentAssemblyInfo.Name)+Environment.NewLine+ persistentAssemblyInfo.PersistentClassInfos. Aggregate<IPersistentClassInfo, string>(null, (current, persistentClassInfo) => current + (GenerateCode(persistentClassInfo) + Environment.NewLine)); return groupUsings(generateCode,persistentAssemblyInfo.CodeDomProvider); }
static string GetAssemblyAttributesCode(IPersistentAssemblyInfo persistentAssemblyInfo) { string code = persistentAssemblyInfo.Attributes.Aggregate("", (current, assemblyAttributeInfo) => current + (GenerateCode(assemblyAttributeInfo) + Environment.NewLine)); return code.TrimEnd(Environment.NewLine.ToCharArray()); }
static string GetUsingsString(IPersistentAssemblyInfo persistentAssemblyInfo, string code) { var regex = new Regex(persistentAssemblyInfo.CodeDomProvider == CodeDomProvider.CSharp ? "(using [^;]*;\r\n)" : "(Imports [^\r\n]*\r\n)", RegexOptions.IgnorePatternWhitespace); return regex.Matches(code).OfType<Match>().Aggregate("", (current, match) => current + match.Value + "\n"); }
public Type CompileModule(IPersistentAssemblyInfo persistentAssemblyInfo) { return CompileModule(persistentAssemblyInfo, null); }
public static void SupportCompositeKeyPersistentObjects(IPersistentAssemblyInfo persistentAssemblyInfo) { var keys = persistentAssemblyInfo.PersistentClassInfos.SelectMany(info => info.OwnMembers).OfType<IPersistentReferenceMemberInfo>().Where(memberInfo => memberInfo.ReferenceClassInfo.CodeTemplateInfo.CodeTemplate.TemplateType == TemplateType.Struct); var dictionary = new Dictionary<IPersistentClassInfo, ITemplateInfo>(); var objectSpace = XPObjectSpace.FindObjectSpaceByObject(persistentAssemblyInfo); foreach (var key in keys) { if (!dictionary.ContainsKey(key.Owner)) { var info = objectSpace.CreateObjectFromInterface<ITemplateInfo>(); info.Name = SupportPersistentObjectsAsAPartOfACompositeKey; key.Owner.TemplateInfos.Add(info); dictionary.Add(key.Owner, info); } ITemplateInfo templateInfo = dictionary[key.Owner]; templateInfo.TemplateCode = @"protected override void OnLoaded() { base.OnLoaded(); " + GetCode(key) + @" }"; } }
public DbMapper(XPObjectSpace objectSpace, IPersistentAssemblyInfo persistentAssemblyInfo, IDataStoreLogonObject dataStoreLogonObject) { _objectSpace = objectSpace; _persistentAssemblyInfo = persistentAssemblyInfo; _dataStoreLogonObject = dataStoreLogonObject; }
static string GetVersionCode(IPersistentAssemblyInfo persistentAssemblyInfo) { var version = persistentAssemblyInfo.Version; if (!string.IsNullOrEmpty(version)) return string.Format(@"[assembly: System.Reflection.AssemblyVersionAttribute(""{0}"")]", version) + Environment.NewLine; return null; }
public Type CompileModule(IPersistentAssemblyInfo persistentAssemblyInfo, Action<CompilerParameters> action, string path) { return CompileModule(persistentAssemblyInfo, CodeEngine.GenerateCode, action, path); }
public static string GenerateCode(IPersistentAssemblyInfo persistentAssemblyInfo) { var usingsDictionary = new Dictionary<string, string>(); string generateAssemblyCode = GetAssemblyAttributesCode(persistentAssemblyInfo) + Environment.NewLine + GetModuleCode(persistentAssemblyInfo.Name) + Environment.NewLine; var generatedClassCode = new StringBuilder(); var generatedUsings = new StringBuilder(); foreach (var persistentClassInfo in persistentAssemblyInfo.PersistentClassInfos) { string code = GenerateCode(persistentClassInfo); string usingsString = GetUsingsString(persistentAssemblyInfo, code); code = code.Replace(usingsString, ""); code = code.Replace("\n", "\r\n"); if (!(usingsDictionary.ContainsKey(usingsString))) { usingsDictionary.Add(usingsString, null); usingsString = usingsString.Replace("\n", "\r\n"); generatedUsings.Append(usingsString); } generatedClassCode.Append(code); } return generatedUsings + generateAssemblyCode + generatedClassCode; }
public ClassGenerator(IPersistentAssemblyInfo persistentAssemblyInfo, DBTable[] dbTables) { _persistentAssemblyInfo = persistentAssemblyInfo; _dbTables = dbTables; _objectSpace = XPObjectSpace.FindObjectSpaceByObject(persistentAssemblyInfo); }