mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-11-28 09:22:55 +08:00
#18696: Adding module unit tests code generation command
Work Item: 18696
This commit is contained in:
@@ -17,14 +17,18 @@ namespace Orchard.CodeGeneration.Commands {
|
||||
private readonly IExtensionManager _extensionManager;
|
||||
private readonly ISchemaCommandGenerator _schemaCommandGenerator;
|
||||
private const string SolutionDirectoryModules = "E9C9F120-07BA-4DFB-B9C3-3AFB9D44C9D5";
|
||||
private const string SolutionDirectoryTests = "74E681ED-FECC-4034-B9BD-01B0BB1BDECA";
|
||||
private const string SolutionDirectoryThemes = "74492CBC-7201-417E-BC29-28B4C25A58B0";
|
||||
|
||||
private static readonly string[] _themeDirectories = new [] {
|
||||
private static readonly string[] _themeDirectories = new[] {
|
||||
"", "Content", "Styles", "Scripts", "Views", "Zones"
|
||||
};
|
||||
private static readonly string[] _moduleDirectories = new [] {
|
||||
private static readonly string[] _moduleDirectories = new[] {
|
||||
"", "Properties", "Controllers", "Views", "Models", "Scripts", "Styles"
|
||||
};
|
||||
private static readonly string[] _moduleTestsDirectories = new[] {
|
||||
"", "Properties"
|
||||
};
|
||||
|
||||
private const string ModuleName = "CodeGeneration";
|
||||
private static readonly string _codeGenTemplatePath = HostingEnvironment.MapPath("~/Modules/Orchard." + ModuleName + "/CodeGenerationTemplates/");
|
||||
@@ -66,7 +70,7 @@ namespace Orchard.CodeGeneration.Commands {
|
||||
string dataMigrationFilePath = dataMigrationFolderPath + "Migrations.cs";
|
||||
string templatesPath = HostingEnvironment.MapPath("~/Modules/Orchard." + ModuleName + "/CodeGenerationTemplates/");
|
||||
string moduleCsProjPath = HostingEnvironment.MapPath(string.Format("~/Modules/{0}/{0}.csproj", extensionDescriptor.Id));
|
||||
|
||||
|
||||
if (!Directory.Exists(dataMigrationFolderPath)) {
|
||||
Directory.CreateDirectory(dataMigrationFolderPath);
|
||||
}
|
||||
@@ -95,7 +99,7 @@ namespace Orchard.CodeGeneration.Commands {
|
||||
string projectFileText = File.ReadAllText(moduleCsProjPath);
|
||||
|
||||
// The string searches in solution/project files can be made aware of comment lines.
|
||||
if ( projectFileText.Contains("<Compile Include") ) {
|
||||
if (projectFileText.Contains("<Compile Include")) {
|
||||
string compileReference = string.Format("<Compile Include=\"{0}\" />\r\n ", "Migrations.cs");
|
||||
projectFileText = projectFileText.Insert(projectFileText.LastIndexOf("<Compile Include"), compileReference);
|
||||
}
|
||||
@@ -115,7 +119,7 @@ namespace Orchard.CodeGeneration.Commands {
|
||||
public void CreateModule(string moduleName) {
|
||||
Context.Output.WriteLine(T("Creating Module {0}", moduleName));
|
||||
|
||||
if ( _extensionManager.AvailableExtensions().Any(extension => String.Equals(moduleName, extension.Name, StringComparison.OrdinalIgnoreCase)) ) {
|
||||
if (_extensionManager.AvailableExtensions().Any(extension => String.Equals(moduleName, extension.Name, StringComparison.OrdinalIgnoreCase))) {
|
||||
Context.Output.WriteLine(T("Creating Module {0} failed: a module of the same name already exists", moduleName));
|
||||
return;
|
||||
}
|
||||
@@ -124,6 +128,59 @@ namespace Orchard.CodeGeneration.Commands {
|
||||
Context.Output.WriteLine(T("Module {0} created successfully", moduleName));
|
||||
}
|
||||
|
||||
[CommandHelp("codegen moduletests <module-name> [/IncludeInSolution:true|false]\r\n\t" + "Creates a new test project for a module")]
|
||||
[CommandName("codegen moduletests")]
|
||||
[OrchardSwitches("IncludeInSolution")]
|
||||
public void CreateModuleTests(string moduleName) {
|
||||
var projectName = moduleName + ".Tests";
|
||||
|
||||
Context.Output.WriteLine(T("Creating module tests project {0}", projectName));
|
||||
|
||||
var testsPath = HostingEnvironment.MapPath("~/Modules/" + moduleName + "/" + projectName + "/");
|
||||
|
||||
if (Directory.Exists(testsPath)) {
|
||||
Context.Output.WriteLine(T("Creating module tests project {0} failed: a project of the same name already exists", projectName));
|
||||
return;
|
||||
}
|
||||
|
||||
var propertiesPath = testsPath + "Properties";
|
||||
var content = new HashSet<string>();
|
||||
var folders = new HashSet<string>();
|
||||
|
||||
foreach (var folder in _moduleTestsDirectories) {
|
||||
Directory.CreateDirectory(testsPath + folder);
|
||||
if (!String.IsNullOrEmpty(folder)) {
|
||||
folders.Add(testsPath + folder);
|
||||
}
|
||||
}
|
||||
|
||||
var projectGuid = Guid.NewGuid().ToString().ToUpper();
|
||||
|
||||
var templateText = File.ReadAllText(_codeGenTemplatePath + "ModuleAssemblyInfo.txt");
|
||||
templateText = templateText.Replace("$$ModuleName$$", projectName);
|
||||
templateText = templateText.Replace("$$ModuleTypeLibGuid$$", Guid.NewGuid().ToString());
|
||||
File.WriteAllText(propertiesPath + "\\AssemblyInfo.cs", templateText);
|
||||
content.Add(propertiesPath + "\\AssemblyInfo.cs");
|
||||
|
||||
var itemGroup = CreateProjectItemGroup(testsPath, content, folders);
|
||||
|
||||
var csprojText = File.ReadAllText(_codeGenTemplatePath + "\\ModuleTestsCsProj.txt");
|
||||
csprojText = csprojText.Replace("$$ProjectName$$", projectName);
|
||||
csprojText = csprojText.Replace("$$TestsProjectGuid$$", projectGuid);
|
||||
csprojText = csprojText.Replace("$$FileIncludes$$", itemGroup ?? "");
|
||||
csprojText = csprojText.Replace("$$OrchardReferences$$", GetOrchardReferences());
|
||||
|
||||
File.WriteAllText(testsPath + projectName + ".csproj", csprojText);
|
||||
|
||||
|
||||
// The string searches in solution/project files can be made aware of comment lines.
|
||||
if (IncludeInSolution) {
|
||||
AddToSolution(Context.Output, projectName, projectGuid, "Modules\\" + moduleName, SolutionDirectoryTests);
|
||||
}
|
||||
|
||||
Context.Output.WriteLine(T("Module tests project {0} created successfully", projectName));
|
||||
}
|
||||
|
||||
[CommandName("codegen theme")]
|
||||
[CommandHelp("codegen theme <theme-name> [/CreateProject:true|false][/IncludeInSolution:true|false][/BasedOn:<theme-name>]\r\n\tCreate a new Orchard theme")]
|
||||
[OrchardSwitches("IncludeInSolution,BasedOn,CreateProject")]
|
||||
@@ -217,7 +274,7 @@ namespace Orchard.CodeGeneration.Commands {
|
||||
var content = new HashSet<string>();
|
||||
var folders = new HashSet<string>();
|
||||
|
||||
foreach(var folder in _moduleDirectories) {
|
||||
foreach (var folder in _moduleDirectories) {
|
||||
Directory.CreateDirectory(modulePath + folder);
|
||||
if (!String.IsNullOrEmpty(folder)) {
|
||||
folders.Add(modulePath + folder);
|
||||
@@ -257,7 +314,7 @@ namespace Orchard.CodeGeneration.Commands {
|
||||
}
|
||||
|
||||
private static string GetOrchardReferences() {
|
||||
return IsSourceEnlistment() ?
|
||||
return IsSourceEnlistment() ?
|
||||
@"<ProjectReference Include=""..\..\..\Orchard\Orchard.Framework.csproj"">
|
||||
<Project>{2D1D92BB-4555-4CBE-8D0E-63563D6CE4C6}</Project>
|
||||
<Name>Orchard.Framework</Name>
|
||||
|
||||
Reference in New Issue
Block a user