mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-09-24 04:53:45 +08:00
Update Demo
This commit is contained in:
7
Src/Asp.Net/SugarCodeGeneration/Codes/BLLParameter.cs
Normal file
7
Src/Asp.Net/SugarCodeGeneration/Codes/BLLParameter.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace SugarCodeGeneration
|
||||
{
|
||||
public class BLLParameter
|
||||
{
|
||||
public string Name { get; set; }
|
||||
}
|
||||
}
|
12
Src/Asp.Net/SugarCodeGeneration/Codes/DbContextParameter.cs
Normal file
12
Src/Asp.Net/SugarCodeGeneration/Codes/DbContextParameter.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using System.Collections.Generic;
|
||||
using SqlSugar;
|
||||
|
||||
namespace SugarCodeGeneration
|
||||
{
|
||||
public class DbContextParameter
|
||||
{
|
||||
public string ConnectionString { get; set; }
|
||||
public DbType DbType { get; set; }
|
||||
public List<string> Tables { get; set; }
|
||||
}
|
||||
}
|
70
Src/Asp.Net/SugarCodeGeneration/Codes/FileHelper.cs
Normal file
70
Src/Asp.Net/SugarCodeGeneration/Codes/FileHelper.cs
Normal file
@@ -0,0 +1,70 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace SugarCodeGeneration.Codes
|
||||
{
|
||||
internal class FileHelper
|
||||
{
|
||||
public static void CreateFile(string filePath, string text, Encoding encoding)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (IsExistFile(filePath))
|
||||
{
|
||||
DeleteFile(filePath);
|
||||
}
|
||||
if (!IsExistFile(filePath))
|
||||
{
|
||||
string directoryPath = GetDirectoryFromFilePath(filePath);
|
||||
CreateDirectory(directoryPath);
|
||||
|
||||
//Create File
|
||||
FileInfo file = new FileInfo(filePath);
|
||||
using (FileStream stream = file.Create())
|
||||
{
|
||||
using (StreamWriter writer = new StreamWriter(stream, encoding))
|
||||
{
|
||||
writer.Write(text);
|
||||
writer.Flush();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch(Exception ex)
|
||||
{
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
public static bool IsExistDirectory(string directoryPath)
|
||||
{
|
||||
return Directory.Exists(directoryPath);
|
||||
}
|
||||
public static void CreateDirectory(string directoryPath)
|
||||
{
|
||||
if (!IsExistDirectory(directoryPath))
|
||||
{
|
||||
Directory.CreateDirectory(directoryPath);
|
||||
}
|
||||
}
|
||||
public static void DeleteFile(string filePath)
|
||||
{
|
||||
if (IsExistFile(filePath))
|
||||
{
|
||||
File.Delete(filePath);
|
||||
}
|
||||
}
|
||||
public static string GetDirectoryFromFilePath(string filePath)
|
||||
{
|
||||
FileInfo file = new FileInfo(filePath);
|
||||
DirectoryInfo directory = file.Directory;
|
||||
return directory.FullName;
|
||||
}
|
||||
public static bool IsExistFile(string filePath)
|
||||
{
|
||||
return File.Exists(filePath);
|
||||
}
|
||||
}
|
||||
}
|
@@ -4,9 +4,14 @@ using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Xml.Linq;
|
||||
using RazorEngine;
|
||||
using RazorEngine.Templating;
|
||||
|
||||
namespace SugarCodeGeneration.Codes
|
||||
{
|
||||
/// <summary>
|
||||
/// 生成所需要的代码
|
||||
/// </summary>
|
||||
public class Methods
|
||||
{
|
||||
public static string GetCurrentProjectPath
|
||||
@@ -31,11 +36,14 @@ namespace SugarCodeGeneration.Codes
|
||||
public static void AddCsproj(string classPath, string projectName)
|
||||
{
|
||||
var classDirectory = Methods.GetSlnPath + "\\" +projectName+"\\"+ classPath.TrimStart('\\');
|
||||
if (FileHelper.IsExistDirectory(classDirectory) == false) {
|
||||
FileHelper.CreateDirectory(classDirectory);
|
||||
}
|
||||
var files = Directory.GetFiles(classDirectory).ToList().Select(it=>classPath+"\\"+Path.GetFileName(it));
|
||||
var xmlPath = GetSlnPath + @"\" + projectName + @"\SugarCodeGeneration.csproj";
|
||||
|
||||
var xml = File.ReadAllText(xmlPath,Encoding.UTF8);
|
||||
var firstLine = System.IO.File.ReadLines(xmlPath, Encoding.UTF8).First();
|
||||
var xml = File.ReadAllText(xmlPath, System.Text.Encoding.UTF8);
|
||||
var firstLine = System.IO.File.ReadLines(xmlPath, System.Text.Encoding.UTF8).First();
|
||||
var newXml = xml.Replace(firstLine, "").TrimStart('\r').TrimStart('\n');
|
||||
XDocument xe = XDocument.Load(xmlPath);
|
||||
var itemGroup=xe.Root.Elements().Where(it=>it.Name.LocalName== "ItemGroup"&&it.Elements().Any(y=>y.Name.LocalName== "Compile")).First();
|
||||
@@ -52,5 +60,28 @@ namespace SugarCodeGeneration.Codes
|
||||
xe = XDocument.Parse(newXml);
|
||||
xe.Save(xmlPath);
|
||||
}
|
||||
|
||||
public static void CreateBLL(string templatePath, string savePath,List<string> tables)
|
||||
{
|
||||
|
||||
string template = System.IO.File.ReadAllText(templatePath); //从文件中读出模板内容
|
||||
string templateKey = "bll"; //取个名字
|
||||
foreach (var item in tables)
|
||||
{
|
||||
BLLParameter model = new BLLParameter()
|
||||
{
|
||||
Name=item
|
||||
};
|
||||
var result = Engine.Razor.RunCompile(template, templateKey, model.GetType(), model);
|
||||
FileHelper.CreateFile(savePath+"\\"+item+ "Manager.cs", result, System.Text.Encoding.UTF8);
|
||||
}
|
||||
}
|
||||
|
||||
public static void CreateDbContext(string templatePath, string savePath, object model) {
|
||||
string template = System.IO.File.ReadAllText(templatePath); //从文件中读出模板内容
|
||||
string templateKey ="dbcontext"; //取个名字
|
||||
var result =Engine.Razor.RunCompile(template, templateKey, model.GetType(), model);
|
||||
FileHelper.CreateFile(savePath, result, System.Text.Encoding.UTF8);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user