Files
SqlSugar/Src/Asp.Net/SugarCodeGeneration/Codes/Methods.cs

139 lines
5.5 KiB
C#
Raw Normal View History

2019-01-16 18:42:05 +08:00
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Linq;
2019-01-17 00:58:05 +08:00
using RazorEngine;
using RazorEngine.Templating;
2019-01-16 18:42:05 +08:00
namespace SugarCodeGeneration.Codes
{
2019-01-17 00:58:05 +08:00
/// <summary>
/// 生成所需要的代码
/// </summary>
2019-01-16 18:42:05 +08:00
public class Methods
{
public static string GetCurrentProjectPath
{
get
{
return Environment.CurrentDirectory.Replace(@"\bin\Debug", "");
}
}
public static string GetSlnPath
{
get
{
var path = Directory.GetParent(GetCurrentProjectPath).FullName;
return path;
}
}
2019-01-16 22:03:41 +08:00
public static void AddCsproj(string classPath, string projectName)
2019-01-16 18:42:05 +08:00
{
2019-01-17 18:17:59 +08:00
CreateProject(projectName);
var classDirectory = Methods.GetSlnPath + "\\" + projectName + "\\" + classPath.TrimStart('\\');
2019-01-17 18:10:19 +08:00
if (FileHelper.IsExistDirectory(classDirectory) == false)
{
2019-01-17 00:58:05 +08:00
FileHelper.CreateDirectory(classDirectory);
}
2019-01-17 18:10:19 +08:00
var files = Directory.GetFiles(classDirectory).ToList().Select(it => classPath + "\\" + Path.GetFileName(it));
var xmlPath = GetSlnPath + @"\" + projectName + @"\" + projectName + ".csproj";
2019-01-16 18:42:05 +08:00
2019-01-17 00:58:05 +08:00
var xml = File.ReadAllText(xmlPath, System.Text.Encoding.UTF8);
var firstLine = System.IO.File.ReadLines(xmlPath, System.Text.Encoding.UTF8).First();
2019-01-16 18:42:05 +08:00
var newXml = xml.Replace(firstLine, "").TrimStart('\r').TrimStart('\n');
2019-01-16 22:03:41 +08:00
XDocument xe = XDocument.Load(xmlPath);
2019-01-17 18:10:19 +08:00
var itemGroup = xe.Root.Elements().Where(it => it.Name.LocalName == "ItemGroup" && it.Elements().Any(y => y.Name.LocalName == "Compile")).First();
var compieList = itemGroup.Elements().ToList();
2019-01-16 22:03:41 +08:00
var noAddFiles = files.Where(it => !compieList.Any(f => it.Equals(f.Attribute("Include").Value, StringComparison.CurrentCultureIgnoreCase))).ToList();
2019-01-17 18:10:19 +08:00
if (noAddFiles.Any())
{
2019-01-16 22:03:41 +08:00
foreach (var item in noAddFiles)
{
2019-01-17 18:10:19 +08:00
var addItem = new XElement("Compile", new XAttribute("Include", item));
itemGroup.AddFirst(addItem);
2019-01-16 22:03:41 +08:00
}
}
newXml = xe.ToString().Replace("xmlns=\"\"", "");
xe = XDocument.Parse(newXml);
xe.Save(xmlPath);
2019-01-16 18:42:05 +08:00
}
2019-01-17 00:58:05 +08:00
2019-01-17 18:10:19 +08:00
public static void CreateBLL(string templatePath, string savePath, List<string> tables)
2019-01-17 00:58:05 +08:00
{
string template = System.IO.File.ReadAllText(templatePath); //从文件中读出模板内容
string templateKey = "bll"; //取个名字
foreach (var item in tables)
{
BLLParameter model = new BLLParameter()
{
2019-01-17 18:10:19 +08:00
Name = item
2019-01-17 00:58:05 +08:00
};
var result = Engine.Razor.RunCompile(template, templateKey, model.GetType(), model);
2019-01-17 18:10:19 +08:00
var cp = savePath + "\\" + item + "Manager.cs";
if (FileHelper.IsExistFile(cp) == false)
FileHelper.CreateFile(cp, result, System.Text.Encoding.UTF8);
2019-01-17 00:58:05 +08:00
}
}
2019-01-17 18:10:19 +08:00
public static void CreateDbContext(string templatePath, string savePath, object model)
{
2019-01-17 00:58:05 +08:00
string template = System.IO.File.ReadAllText(templatePath); //从文件中读出模板内容
2019-01-17 18:10:19 +08:00
string templateKey = "dbcontext"; //取个名字
var result = Engine.Razor.RunCompile(template, templateKey, model.GetType(), model);
2019-01-17 00:58:05 +08:00
FileHelper.CreateFile(savePath, result, System.Text.Encoding.UTF8);
}
2019-01-17 18:10:19 +08:00
2019-01-17 18:17:59 +08:00
public static void CreateProject(string name)
{
2019-01-17 18:10:19 +08:00
var templatePath = GetCurrentProjectPath + "/Template/Project.txt";
2019-01-17 19:04:05 +08:00
string projectId = Guid.NewGuid().ToString();
string project = System.IO.File.ReadAllText(templatePath).Replace("@pid",projectId); //从文件中读出模板内容
2019-01-17 18:17:59 +08:00
var projectPath = GetSlnPath + "\\" + name + "\\" + name + ".csproj";
var projectDic = GetSlnPath + "\\" + name + "\\";
var binDic = GetSlnPath + "\\" + name + "\\bin";
if (!FileHelper.IsExistFile(projectPath))
{
2019-01-17 18:10:19 +08:00
2019-01-17 18:17:59 +08:00
if (!FileHelper.IsExistDirectory(projectDic))
{
FileHelper.CreateDirectory(projectDic);
}
if (!FileHelper.IsExistDirectory(binDic))
{
FileHelper.CreateDirectory(binDic);
}
FileHelper.CreateFile(projectPath,project,System.Text.Encoding.UTF8);
}
2019-01-17 19:04:05 +08:00
AppendProjectToSln(projectId,name);
}
public static void AppendProjectToSln(string projectId,string projectName) {
var slns = Directory.GetFiles(GetSlnPath).Where(it=> it.Contains(".sln"));
if (slns.Any()) {
var sln = slns.First();
var templatePath = GetCurrentProjectPath + "/Template/sln.txt";
string appendText = System.IO.File.ReadAllText(templatePath)
.Replace("@pid", projectId)
.Replace("@name", projectName)
.Replace("@sid", Guid.NewGuid().ToString());
FileStream fs = new FileStream(sln, FileMode.Append);
var sw = new StreamWriter(fs);
sw.WriteLine(appendText);
sw.Close();
fs.Close();
}
2019-01-17 18:10:19 +08:00
}
2019-01-16 18:42:05 +08:00
}
}