using System; using System.Collections.Generic; using System.Text; using System.IO; using Microsoft.Build.BuildEngine; using NPanday.ProjectImporter.Verifiers; using NPanday.Utils; using NPanday.ProjectImporter.Digest; using NPanday.ProjectImporter.Digest.Model; using NPanday.ProjectImporter.Parser; using NPanday.ProjectImporter.Converter; using NPanday.ProjectImporter.Converter.Algorithms; using NPanday.ProjectImporter.Validator; using NPanday.ProjectImporter.ImportProjectStructureAlgorithms; using NPanday.ProjectImporter.Parser.SlnParser; using System.Windows.Forms; /// Author: Leopoldo Lee Agdeppa III namespace NPanday.ProjectImporter { public class NPandayImporter { #region Import Project Type Strategy Pattern // A strategy pattern with a twists, using c# delegates delegate string[] ImportProjectTypeDelegate(ProjectDigest[] prjDigests, string solutionFile, string groupId, string artifactId, string version, string scmTag, bool writePom); static Dictionary _importProject; /// /// Used for registering the strategies (alogrithms) for importing project type /// static NPandayImporter() { // register the algorithms here _importProject = new Dictionary(); _importProject.Add(ProjectStructureType.AbnormalProject, new AbnormalProject().ImportProjectType); _importProject.Add(ProjectStructureType.FlatMultiModuleProject, new FlatMultiModuleProject().ImportProjectType); _importProject.Add(ProjectStructureType.FlatSingleModuleProject, new FlatSingleModuleProject().ImportProjectType); _importProject.Add(ProjectStructureType.NormalMultiModuleProject, new NormalMultiModuleProject().ImportProjectType); _importProject.Add(ProjectStructureType.NormalSingleProject, new NormalSingleProject().ImportProjectType); } public static string[] ImportProjectType(ProjectStructureType structureType, ProjectDigest[] prjDigests, string solutionFile, string groupId, string artifactId, string version, string scmTag) { return _importProject[structureType](prjDigests, solutionFile, groupId, artifactId, version, scmTag, true); } #endregion #region Import Project Entry /// /// Imports a specified Visual Studio Projects in a Solution to an NPanday Pom, /// This is the Project-Importer Entry Method /// /// Path to your Visual Studio Solution File *.sln /// Project Group ID, for maven groupId /// Project Parent Pom Artifact ID, used as a maven artifact ID for the parent pom.xml /// Project version, used as a maven version for the entire pom.xmls /// An array of generated pom.xml filenames public static string[] ImportProject(string solutionFile, string groupId, string artifactId, string version, ref string warningMsg) { return ImportProject(solutionFile, groupId, artifactId, version, string.Empty, true, ref warningMsg); } /// /// Imports a specified Visual Studio Projects in a Solution to an NPanday Pom, /// This is the Project-Importer Entry Method /// /// Path to your Visual Studio Solution File *.sln /// Project Group ID, for maven groupId /// Project Parent Pom Artifact ID, used as a maven artifact ID for the parent pom.xml /// Project version, used as a maven version for the entire pom.xmls /// if true, a dialog box for verifying tests will show up and requires user interaction /// generates scm tags if txtboxfield is not empty or null /// An array of generated pom.xml filenames public static string[] ImportProject(string solutionFile, string groupId, string artifactId, string version, string scmTag,bool verifyTests, ref string warningMsg) { if (verifyTests) { return ImportProject(solutionFile, groupId, artifactId, version, scmTag, VerifyUnitTestsToUser.VerifyTests, ref warningMsg); } else { return ImportProject(solutionFile, groupId, artifactId, version, scmTag, null, ref warningMsg); } } /// /// Delegate Alogrithm for Verifying Project To Import /// /// /// /// /// /// /// public delegate void VerifyProjectToImport(ref ProjectDigest[] projectDigests, ProjectStructureType structureType, string solutionFile, ref string groupId,ref string artifactId,ref string version); private static string HasValidFolderStructure(List> projectList) { string errorProject = string.Empty; foreach (Dictionary project in projectList) { string holder; if (project.ContainsKey("ProjectFullPath")) { holder = (string)project["ProjectFullPath"]; if (holder.Contains("..\\")) { errorProject = holder; break; } } } return errorProject; } /// /// Imports a specified Visual Studio Projects in a Solution to an NPanday Pom, /// This is the Project-Importer Entry Method, /// This method accepts a delegate to use as A project Verifier algorithm /// /// Path to your Visual Studio Solution File *.sln /// Project Group ID, for maven groupId /// Project Parent Pom Artifact ID, used as a maven artifact ID for the parent pom.xml /// Project version, used as a maven version for the entire pom.xmls /// A delegate That will Accept a method for verifying Projects To Import /// adds scm tags to parent pom.xml if not string.empty or null /// An array of generated pom.xml filenames public static string[] ImportProject(string solutionFile, string groupId, string artifactId, string version, string scmTag,VerifyProjectToImport verifyProjectToImport, ref string warningMsg) { string[] result = null; FileInfo solutionFileInfo = new FileInfo(solutionFile); List> list = ParseSolution(solutionFileInfo, ref warningMsg); //Checks for Invalid folder structure if (HasValidFolderStructure(list)!=string.Empty) { throw new Exception("Project Importer failed with project " + HasValidFolderStructure(list) + " Project Directory may not be supported"); } ProjectDigest[] prjDigests = DigestProjects(list, ref warningMsg); ProjectStructureType structureType = GetProjectStructureType(solutionFile, prjDigests); // Filtering of unsupported project types. String UnsupportedProjectsMessage = string.Empty; List filteredPrjDigests = new List(); foreach (ProjectDigest pDigest in prjDigests) { if (PomConverter.__converterAlgorithms.ContainsKey(pDigest.ProjectType)) { filteredPrjDigests.Add(pDigest); } else { if (UnsupportedProjectsMessage == string.Empty) { UnsupportedProjectsMessage += pDigest.FullFileName; } else { UnsupportedProjectsMessage += ", " + pDigest.FullFileName; } } } if (!string.Empty.Equals(UnsupportedProjectsMessage)) { warningMsg = string.Format("{0}\n Unsupported Projects: {1}", warningMsg, UnsupportedProjectsMessage); } prjDigests = filteredPrjDigests.ToArray(); result =ImportProjectType(structureType, filteredPrjDigests.ToArray(), solutionFile, groupId, artifactId, version, scmTag); if (verifyProjectToImport != null && filteredPrjDigests.Count > 0) { verifyProjectToImport(ref prjDigests, structureType, solutionFile, ref groupId, ref artifactId, ref version); } return result; } #endregion #region Re-Import Project Entry public static string[] ReImportProject(string solutionFile, ref string warningMsg) { return ImportProject(solutionFile, null, null, null, null, VerifyProjectImportSyncronization.SyncronizePomValues, ref warningMsg); } #endregion /// /// Facade for Parsing A solution File to get its projects /// calls NPanday.ProjectImporter.Parser.SlnParserParser.ParseSolution(FileInfo) /// /// the full path of the *.sln (visual studio solution) file you want to parse /// public static List> ParseSolution(FileInfo solutionFile, ref string warningMsg) { return SolutionParser.ParseSolution(solutionFile, ref warningMsg); } /// /// Facade for Digesting parsed projects /// calls NPanday.ProjectImporter.Digest.ProjectDigester.DigestProjects(List>) /// /// list retured from ParseSolution /// public static ProjectDigest[] DigestProjects(List> projects, ref string warningMsg) { return ProjectDigester.DigestProjects(projects, ref warningMsg); } /// /// Facade for Getting the Project Type. /// calls NPanday.ProjectImporter.Validator.ProjectValidator.GetProjectStructureType(...) /// /// the full path of the *.sln (visual studio solution) file you want to import /// Digested Projects /// public static ProjectStructureType GetProjectStructureType(string solutionFile, ProjectDigest[] projectDigests) { return ProjectValidator.GetProjectStructureType(solutionFile, projectDigests); } } }