private async void Generate() { try { // Reset content FileItems = new ObservableCollection <string>(); SelectedFile = string.Empty; ShowBuildrogress = true; var request = new GenerateCodeRequest(); DbServerType serverType = (DbServerType)Enum.Parse(typeof(DbServerType), SelectedDatabaseTypeItem); request.ServerType = serverType; request.FolderLocation = FolderLocation; request.IncludeRelationship = IncludeRelationship; request.ConnectionString = GetConnectionString(serverType); if (string.IsNullOrEmpty(request.ConnectionString)) { throw new Exception("Invalid database info provided!"); } var codeResponse = await GenerateCodeProcessor.Generate(request); if (codeResponse != null && codeResponse.Filenames != null) { FileItems = new ObservableCollection <string>(codeResponse.Filenames); SelectedFile = string.Empty; if (codeResponse.Filenames.Count > 0) { SelectedFile = codeResponse.Filenames[0]; FileSelectionChanged(); } } } finally { ShowBuildrogress = false; } }
public static GenerateCodeResponse GenerateCode <T>(Tables tables, GenerateCodeRequest request, GenerateCodeResponse response , string subFolder, string fileNameTemplate) where T : ITemplate, new() { foreach (var table in tables) { try { var model = new T { IncludeRelationships = request.IncludeRelationship, Table = table, Tables = tables }; string pageContent = model.TransformText(); var folderLocation = Path.Combine(request.FolderLocation, subFolder); if (!Directory.Exists(folderLocation)) { Directory.CreateDirectory(folderLocation); } string fileName = string.Format(fileNameTemplate, table.ClassName); File.WriteAllText(Path.Combine(folderLocation, fileName), pageContent, Encoding.UTF8); fileName = subFolder + "/" + fileName; if (response.Filenames.IndexOf(fileName) < 0) { response.Filenames.Add(fileName); } } catch (Exception e) { MessageBox.Show(string.Format("{0} {1} Failed!", table.ClassName, subFolder)); } } return(response); }
public static Task <GenerateCodeResponse> Generate(GenerateCodeRequest request) { return(Task.Run(() => { using (var schemaReader = SchemaReaderProvider.GetReader(request.ServerType)) { var connectionString = request.ConnectionString; if (schemaReader == null || string.IsNullOrEmpty(connectionString)) { return new GenerateCodeResponse(); } var tables = schemaReader.ReadSchema(connectionString); if (tables == null || tables.Count <= 0) { throw new Exception(string.Format("Empty database or invalid connection string: {0}", connectionString)); } var response = new GenerateCodeResponse { Filenames = new List <string>() }; var types = AppDomain.CurrentDomain.GetAssemblies().SelectMany(s => s.GetTypes()).Where(p => typeof(ITemplate).IsAssignableFrom(p)); foreach (var type in types) { var attr = (OutputFileNameAttribute)type.GetCustomAttributes(typeof(OutputFileNameAttribute), true).FirstOrDefault(); if (attr != null) { typeof(GenerateCodeProcessor).GetMethod("GenerateCode") .MakeGenericMethod(type) .Invoke(null, new object[] { tables, request, response, type.Name, attr.FileName }); } } return response; } })); }