-
Notifications
You must be signed in to change notification settings - Fork 0
/
CommandFunction.cs
71 lines (57 loc) · 2.43 KB
/
CommandFunction.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
using System;
using System.IO;
using System.Linq;
namespace AbpOven
{
public class CommandFunction
{
private readonly string _projectName;
private readonly string _element;
private readonly string _folder;
private readonly string _baseNameSpace;
public CommandFunction(string cmdElementParam)
{
var result = cmdElementParam.Split('.');
if (result.Length < 2)
{
throw new Exception("Wrong Parameter: <SolutionName.[folderName.]itemName>");
}
_projectName = result[0] + @".Core";
_baseNameSpace = result[0];
_folder = result.Count() > 2 ? result[1] : string.Empty;
_element = result.Last();
}
/// <summary>
/// Template is on: Template\EntityTemplate.cs
/// </summary>
/// <returns>The file path to add in the project</returns>
public string CreateEntity()
{
var elementDir = Helper.PluralizeElement(_element);
var entityName = _element;
var projectDir = Helper.GetProjectDirectory(_projectName);
var newEntityPath = Path.Combine(projectDir, elementDir);
var newEntityFile = Path.Combine(newEntityPath, entityName + @".cs");
var templateFile = Path.Combine(Helper.GetTemplatesDirectory(), @"EntityTemplate.cs");
if (!File.Exists(templateFile))
throw new FileNotFoundException(@"The following template file does not exist: " + templateFile);
if (!Directory.Exists(newEntityPath))
{
//Create dir
Directory.CreateDirectory(newEntityPath);
}
//1- Copy to the folder
if (File.Exists(templateFile) && !File.Exists(newEntityFile))
File.Copy(templateFile, newEntityFile);
//2- Process the new entity copied to project folder
//Making replacements EntityName
string text = File.ReadAllText(newEntityFile);
text = text.Replace(@"EntityTemplate", _element);
//Setting NameSpace
text = text.Replace(@"AbpOven.Templates", $"{_baseNameSpace}.{elementDir}");
File.WriteAllText(newEntityFile, text);
//3- Add the new entity to the project file .scsproj
return $"{elementDir}\\{_element}.cs";
}
}
}