Author: brett Date: Tue Jun 28 04:57:53 2011 New Revision: 1140436 URL: http://svn.apache.org/viewvc?rev=1140436&view=rev Log: [NPDANY-445] refactor GAC utility to be more independent and reuse from reference addition logic as well to avoid parsing paths that may not be correct
Modified: incubator/npanday/trunk/dotnet/assemblies/NPanday.ProjectImporter/Engine/src/main/csharp/NPanday/ProjectImporter/Converter/Algorithms/AbstractPomConverter.cs incubator/npanday/trunk/dotnet/assemblies/NPanday.ProjectImporter/Engine/src/main/csharp/NPanday/ProjectImporter/Digest/Algorithms/NormalProjectDigestAlgorithm.cs incubator/npanday/trunk/dotnet/assemblies/NPanday.Utils/src/main/csharp/NPanday/Utils/GacUtility.cs incubator/npanday/trunk/dotnet/assemblies/NPanday.VisualStudio.Addin/src/main/csharp/NPanday/VisualStudio/Addin/Connect.cs Modified: incubator/npanday/trunk/dotnet/assemblies/NPanday.ProjectImporter/Engine/src/main/csharp/NPanday/ProjectImporter/Converter/Algorithms/AbstractPomConverter.cs URL: http://svn.apache.org/viewvc/incubator/npanday/trunk/dotnet/assemblies/NPanday.ProjectImporter/Engine/src/main/csharp/NPanday/ProjectImporter/Converter/Algorithms/AbstractPomConverter.cs?rev=1140436&r1=1140435&r2=1140436&view=diff ============================================================================== --- incubator/npanday/trunk/dotnet/assemblies/NPanday.ProjectImporter/Engine/src/main/csharp/NPanday/ProjectImporter/Converter/Algorithms/AbstractPomConverter.cs (original) +++ incubator/npanday/trunk/dotnet/assemblies/NPanday.ProjectImporter/Engine/src/main/csharp/NPanday/ProjectImporter/Converter/Algorithms/AbstractPomConverter.cs Tue Jun 28 04:57:53 2011 @@ -39,7 +39,6 @@ namespace NPanday.ProjectImporter.Conver { public abstract class AbstractPomConverter : IPomConverter { - protected GacUtility gacUtil; protected RspUtility rspUtil; protected ArtifactContext artifactContext; protected List<Artifact.Artifact> localArtifacts; @@ -78,7 +77,6 @@ namespace NPanday.ProjectImporter.Conver this.version = parent != null ? parent.version : null; this.rspUtil = new RspUtility(); - this.gacUtil = new GacUtility(); this.model = new NPanday.Model.Pom.Model(); // Add build Tag @@ -749,7 +747,7 @@ namespace NPanday.ProjectImporter.Conver return dependency; } - List<string> refs = gacUtil.GetAssemblyInfo(reference.Name, null, projectDigest.Platform); + List<string> refs = GacUtility.GetInstance().GetAssemblyInfo(reference.Name, null, projectDigest.Platform); // resolve from GAC if (refs.Count > 0) @@ -757,65 +755,15 @@ namespace NPanday.ProjectImporter.Conver // Assembly is found at the gac //exclude ProcessArchitecture when loading assembly on a non-32 bit machine - refs = gacUtil.GetAssemblyInfo(reference.Name, reference.Version, null); + refs = GacUtility.GetInstance().GetAssemblyInfo(reference.Name, reference.Version, null); System.Reflection.Assembly a = System.Reflection.Assembly.ReflectionOnlyLoad(new System.Reflection.AssemblyName(refs[0]).FullName); Dependency refDependency = new Dependency(); refDependency.artifactId = reference.Name; refDependency.groupId = reference.Name; - - if ("MSIL".Equals(reference.ProcessorArchitecture, StringComparison.OrdinalIgnoreCase)) - { - if (a.ImageRuntimeVersion.StartsWith("v4.0")) - { - refDependency.type = "gac_msil4"; - } - else - { - refDependency.type = "gac_msil"; - } - } - else if ("x86".Equals(reference.ProcessorArchitecture, StringComparison.OrdinalIgnoreCase)) - { - if (a.ImageRuntimeVersion.StartsWith("v4.0")) - { - refDependency.type = "gac_32_4"; - } - else - { - refDependency.type = "gac_32"; - } - } - else if ("IA64".Equals(reference.ProcessorArchitecture, StringComparison.OrdinalIgnoreCase) || - "AMD64".Equals(reference.ProcessorArchitecture, StringComparison.OrdinalIgnoreCase)) - { - if (a.ImageRuntimeVersion.StartsWith("v4.0")) - { - refDependency.type = "gac_64_4"; - } - else - { - refDependency.type = "gac_64"; - } - } - //Assemblies that with null ProcessorArchitecture esp ASP.net assmblies (e.g MVC) - else if ((reference.ProcessorArchitecture == null) && ("31bf3856ad364e35".Equals(reference.PublicKeyToken.ToLower(), StringComparison.OrdinalIgnoreCase))) - { - if (a.ImageRuntimeVersion.StartsWith("v4.0")) - { - refDependency.type = "gac_msil4"; - } - else - { - refDependency.type = "gac_msil"; - } - } - else - { - refDependency.type = "gac"; - } + refDependency.type = GacUtility.GetNPandayGacType(a, reference.PublicKeyToken); refDependency.version = reference.Version ?? "1.0.0.0"; Modified: incubator/npanday/trunk/dotnet/assemblies/NPanday.ProjectImporter/Engine/src/main/csharp/NPanday/ProjectImporter/Digest/Algorithms/NormalProjectDigestAlgorithm.cs URL: http://svn.apache.org/viewvc/incubator/npanday/trunk/dotnet/assemblies/NPanday.ProjectImporter/Engine/src/main/csharp/NPanday/ProjectImporter/Digest/Algorithms/NormalProjectDigestAlgorithm.cs?rev=1140436&r1=1140435&r2=1140436&view=diff ============================================================================== --- incubator/npanday/trunk/dotnet/assemblies/NPanday.ProjectImporter/Engine/src/main/csharp/NPanday/ProjectImporter/Digest/Algorithms/NormalProjectDigestAlgorithm.cs (original) +++ incubator/npanday/trunk/dotnet/assemblies/NPanday.ProjectImporter/Engine/src/main/csharp/NPanday/ProjectImporter/Digest/Algorithms/NormalProjectDigestAlgorithm.cs Tue Jun 28 04:57:53 2011 @@ -167,7 +167,6 @@ namespace NPanday.ProjectImporter.Digest private static void DigestBuildItems(Project project, ProjectDigest projectDigest, string projectBasePath, ICollection<ProjectReference> projectReferences, ICollection<Reference> references, ICollection<Compile> compiles, ICollection<None> nones, ICollection<WebReferenceUrl> webReferenceUrls, ICollection<Content> contents, ICollection<Folder> folders, ICollection<WebReferences> webReferencesList, ICollection<EmbeddedResource> embeddedResources, ICollection<BootstrapperPackage> bootstrapperPackages, ICollection<string> globalNamespaceImports, IList<ComReference> comReferenceList) { string targetFramework = projectDigest.TargetFramework != null ? projectDigest.TargetFramework.Substring(0,3) : "2.0"; - GacUtility gac = new GacUtility(); RspUtility rsp = new RspUtility(); foreach (BuildItemGroup buildItemGroup in project.ItemGroups) { @@ -211,7 +210,7 @@ namespace NPanday.ProjectImporter.Digest else if (!rsp.IsRspIncluded(buildItem.Include,projectDigest.Language)) { // simple name needs to be resolved - List<string> refs = gac.GetAssemblyInfo(buildItem.Include, null, null); + List<string> refs = GacUtility.GetInstance().GetAssemblyInfo(buildItem.Include, null, null); if (refs.Count == 0) { Console.WriteLine("Unable to find reference '" + buildItem.Include + "' in " + string.Join("; ", refs.ToArray())); Modified: incubator/npanday/trunk/dotnet/assemblies/NPanday.Utils/src/main/csharp/NPanday/Utils/GacUtility.cs URL: http://svn.apache.org/viewvc/incubator/npanday/trunk/dotnet/assemblies/NPanday.Utils/src/main/csharp/NPanday/Utils/GacUtility.cs?rev=1140436&r1=1140435&r2=1140436&view=diff ============================================================================== --- incubator/npanday/trunk/dotnet/assemblies/NPanday.Utils/src/main/csharp/NPanday/Utils/GacUtility.cs (original) +++ incubator/npanday/trunk/dotnet/assemblies/NPanday.Utils/src/main/csharp/NPanday/Utils/GacUtility.cs Tue Jun 28 04:57:53 2011 @@ -34,15 +34,18 @@ namespace NPanday.Utils { public class GacUtility { - private string gacs = ""; + private string gacs; - public GacUtility() + private static GacUtility instance; + + private GacUtility() { // Used to determine which references exist in the GAC, used during VS project import // TODO: we need a better way to determine this by querying the GAC using .NET // rather than parsing command output // consider this: http://www.codeproject.com/KB/dotnet/undocumentedfusion.aspx // (works, but seems to be missing the processor architecture) + // Can also use LoadWithPartialName, but it is deprecated Process p = new Process(); @@ -70,6 +73,64 @@ namespace NPanday.Utils } } + public static GacUtility GetInstance() + { + if (instance == null) + { + instance = new GacUtility(); + } + return instance; + } + + public static string GetNPandayGacType(System.Reflection.Assembly a, string publicKeyToken) + { + ProcessorArchitecture architecture = a.GetName().ProcessorArchitecture; + return GetNPandayGacType(a.ImageRuntimeVersion, architecture, publicKeyToken); + } + + public static string GetNPandayGacType(string runtimeVersion, ProcessorArchitecture architecture, string publicKeyToken) + { + string type; + + if (architecture == ProcessorArchitecture.MSIL) + { + if (runtimeVersion.StartsWith("v4.0")) + { + type = "gac_msil4"; + } + else + { + type = "gac_msil"; + } + } + else if (architecture == ProcessorArchitecture.X86) + { + if (runtimeVersion.StartsWith("v4.0")) + { + type = "gac_32_4"; + } + else + { + type = "gac_32"; + } + } + else if (architecture == ProcessorArchitecture.IA64 || architecture == ProcessorArchitecture.Amd64) + { + if (runtimeVersion.StartsWith("v4.0")) + { + type = "gac_64_4"; + } + else + { + type = "gac_64"; + } + } + else + { + type = "gac"; + } + return type; + } public List<string> GetAssemblyInfo(string assemblyName, string version, string processorArchitecture) { Modified: incubator/npanday/trunk/dotnet/assemblies/NPanday.VisualStudio.Addin/src/main/csharp/NPanday/VisualStudio/Addin/Connect.cs URL: http://svn.apache.org/viewvc/incubator/npanday/trunk/dotnet/assemblies/NPanday.VisualStudio.Addin/src/main/csharp/NPanday/VisualStudio/Addin/Connect.cs?rev=1140436&r1=1140435&r2=1140436&view=diff ============================================================================== --- incubator/npanday/trunk/dotnet/assemblies/NPanday.VisualStudio.Addin/src/main/csharp/NPanday/VisualStudio/Addin/Connect.cs (original) +++ incubator/npanday/trunk/dotnet/assemblies/NPanday.VisualStudio.Addin/src/main/csharp/NPanday/VisualStudio/Addin/Connect.cs Tue Jun 28 04:57:53 2011 @@ -793,6 +793,7 @@ namespace NPanday.VisualStudio.Addin } catch { + // TODO: should this really be ignored? } //check if reference is already in pom @@ -802,11 +803,11 @@ namespace NPanday.VisualStudio.Addin return; } - //setup default dependecy values + //setup default dependency values string refType = "gac_msil"; string refName = pReference.Name; string refGroupId = pReference.Name; - string refToken = pReference.PublicKeyToken; + string refToken = pReference.PublicKeyToken.ToLower(); string refVersion = pReference.Version; string systemPath = string.Empty; string scope = string.Empty; @@ -820,34 +821,10 @@ namespace NPanday.VisualStudio.Addin refToken = pReference.Identity.Substring(0, pReference.Identity.LastIndexOf(@"\")).Replace("\\", "-"); refGroupId = refName; } - else + else if (pReference.Type == prjReferenceType.prjReferenceTypeAssembly) { //if reference is assembly - Assembly a = System.Reflection.Assembly.LoadWithPartialName(pReference.Name); - - if (a != null) - { - if (a.Location.ToLower().IndexOf(@"\gac_32\") >= 0) - refType = "gac_32"; - else if (a.Location.ToLower().IndexOf(@"\gac_64\") >= 0) - refType = "gac_64"; - else if (a.Location.ToLower().IndexOf(@"\gac\") >= 0) - refType = "gac"; - else if (a.Location.ToLower().IndexOf(@"\gac-msil4\") >= 0) - refType = "gac-msil4"; - else if (a.Location.ToLower().IndexOf(@"\gac-32-4\") >= 0) - refType = "gac-32-4"; - else if (a.Location.ToLower().IndexOf(@"\gac-64-4\") >= 0) - refType = "gac-64-4"; - else if (a.Location.ToLower().IndexOf(@"\gac-msil\") >= 0) - refType = "gac-msil"; - - if (!a.GlobalAssemblyCache) - { - refType = "dotnet-library"; - } - } - else if (pReference.SourceProject != null && pReference.ContainingProject.DTE.Solution.FullName == pReference.SourceProject.DTE.Solution.FullName) + if (pReference.SourceProject != null && pReference.ContainingProject.DTE.Solution.FullName == pReference.SourceProject.DTE.Solution.FullName) { // if intra-project reference, let's mimic Add Maven Artifact @@ -856,10 +833,10 @@ namespace NPanday.VisualStudio.Addin NPanday.Model.Pom.Model solutionPOM = NPanday.Utils.PomHelperUtility.ReadPomAsModel(CurrentSolutionPom); NPanday.Model.Pom.Model projectPOM = NPanday.Utils.PomHelperUtility.ReadPomAsModel(CurrentSelectedProjectPom); - + refGroupId = solutionPOM.groupId; - if (projectPOM.version != null && projectPOM.version!="0.0.0.0") + if (projectPOM.version != null && projectPOM.version != "0.0.0.0") { refVersion = projectPOM.version; } @@ -875,19 +852,39 @@ namespace NPanday.VisualStudio.Addin } else { - scope = "system"; - systemPath = pReference.Path; - refType = "dotnet-library"; - if (!iNPandayRepo) + Assembly a = Assembly.ReflectionOnlyLoadFrom(pReference.Path); + + // original is probably a reference assembly, so now check if it's in the GAC + // for that, we must load (runtime) instead of just reflection, or from the reference path + AppDomain appDomain = AppDomain.CreateDomain("NPandayTempDomain"); + Assembly asm = appDomain.Load(a.FullName); + if (asm.GlobalAssemblyCache) { - MessageBox.Show(string.Format("Warning: Build may not be portable if local references are used, Reference is not in Maven Repository or in GAC." - + "\nReference: {0}" - + "\nDeploying the reference to a Repository, will make the code portable to other machines", - pReference.Name - ), "Add Reference", MessageBoxButtons.OK, MessageBoxIcon.Warning); + // use the original assembly to get the GAC type so we don't get the wrong image version + // but use processor architecture from the GAC version as it is None otherwise + refType = GacUtility.GetNPandayGacType(a.ImageRuntimeVersion, asm.GetName().ProcessorArchitecture, refToken); + } + else + { + scope = "system"; + systemPath = pReference.Path; + refType = "dotnet-library"; + if (!iNPandayRepo) + { + MessageBox.Show(string.Format("Warning: Build may not be portable if local references are used, Reference is not in Maven Repository or in GAC." + + "\nReference: {0}" + + "\nDeploying the reference to a Repository, will make the code portable to other machines", + pReference.Name + ), "Add Reference", MessageBoxButtons.OK, MessageBoxIcon.Warning); + } } + AppDomain.Unload(appDomain); } } + else + { + throw new Exception("Unrecognized reference type: " + pReference.Type); + } Dependency dep = new Dependency(); dep.artifactId = refName; @@ -908,7 +905,7 @@ namespace NPanday.VisualStudio.Addin } catch (Exception e) { - //outputWindowPane.OutputString(e.Message); + MessageBox.Show("Error converting reference to artifact, not added to POM: " + e.Message, "Add Reference", MessageBoxButtons.OK, MessageBoxIcon.Warning); } }