Beispiel #1
0
        private void GetReferencesToAddAndRemove(VSLangProj.References existingRefs, Dictionary <string, ReferenceModel> availableReplaceReferences,
                                                 Dictionary <string, ReferenceModel> referencesThanMustBeAdded, out List <VSLangProj.Reference> refsToRemove, out Dictionary <string, ReferenceModel> refsToAdd)
        {
            refsToRemove = new List <VSLangProj.Reference>();
            refsToAdd    = new Dictionary <string, ReferenceModel>();

            foreach (VSLangProj.Reference reference in existingRefs)
            {
                string name = reference.Name;

                ReferenceModel mustAddModel;
                ReferenceModel canReplaceModel;
                if (referencesThanMustBeAdded.TryGetValue(name, out mustAddModel))
                {
                    refsToRemove.Add(reference);
                    refsToAdd[name] = mustAddModel;
                }
                else if (availableReplaceReferences.TryGetValue(name, out canReplaceModel))
                {
                    refsToRemove.Add(reference);
                    refsToAdd[name] = canReplaceModel;
                }
            }

            foreach (var mustAddPair in referencesThanMustBeAdded)
            {
                if (!refsToAdd.ContainsKey(mustAddPair.Key))
                {
                    refsToAdd[mustAddPair.Key] = mustAddPair.Value;
                }
            }
        }
Beispiel #2
0
        internal static void copyReferences(AbstractTestFramework testFramework, Project parentProject, Project unitTestProject)
        {
            //Add a unit test dll like nunit.framework.dll ref
            string tvTempUnitTestFolderHolder = testFramework.BinariesDirectory;

            if (!tvTempUnitTestFolderHolder.Equals(string.Empty))
            {
                foreach (string dll in testFramework.FrameworkDlls)
                {
                    VSLangProj.Reference nunitReferance =
                        ProjectManager.AddDllReferenceToProject(unitTestProject, System.IO.Path.Combine(tvTempUnitTestFolderHolder, dll));
                }
            }

            //add original projects outputted dll
            string tvTempOriginalProjectOutputDll =
                UTGManagerAndExaminor.ProjectExaminar.GetProjectOutputFullAssemblyName(parentProject);

            ProjectManager.AddDllReferenceToProject(unitTestProject, tvTempOriginalProjectOutputDll);

            //add original projects refs
            VSLangProj.References references = ProjectManager.GetProjectReferences(parentProject);

            ProjectManager.AddReferencesToProject(references, unitTestProject);
        }
Beispiel #3
0
        private void ResolveProjectReferences(VSLangProj.VSProject project,
                                              Dictionary <string, ReferenceModel> availableReplaceReferences, Dictionary <string, ReferenceModel> referencesThanMustBeAdded)
        {
            this.Worker.ReportProgress(50, GetAction(() => { this.Info.DoOnProjectModifyingStart(project); }));

            VSLangProj.References               existingRefs = project.References;
            List <VSLangProj.Reference>         refsToRemove;
            Dictionary <string, ReferenceModel> refsToAdd;

            this.GetReferencesToAddAndRemove(existingRefs, availableReplaceReferences, referencesThanMustBeAdded, out refsToRemove, out refsToAdd);

            for (int i = 0; i < refsToRemove.Count; i++)
            {
                VSLangProj.Reference refToRemove      = refsToRemove[i];
                ReferenceModel       refModelToRemove = new ReferenceModel(refToRemove.Name, refToRemove.Path);
                refToRemove.Remove();
                this.Worker.ReportProgress(50, GetAction(() => { this.Info.DoOnReferenceRemoved(refModelToRemove); }));
            }

            foreach (KeyValuePair <string, ReferenceModel> refToAdd in refsToAdd)
            {
                existingRefs.Add(refToAdd.Value.Path);
                this.Worker.ReportProgress(50, GetAction(() => { this.Info.DoOnReferenceAdded(refToAdd.Value); }));
            }

            this.Worker.ReportProgress(50, GetAction(() => { this.Info.DoOnProjectModifyingEnd(project); }));
        }
Beispiel #4
0
        /// <summary>
        /// Adds the assembly reference.
        /// </summary>
        /// <param name="assemblyPath">The assembly path.</param>
        public void AddAssemblyReference(string assemblyPath)
        {
            Guard.ArgumentNotNullOrEmptyString(assemblyPath, "assemblyPath");

            if (VSProject != null)
            {
                if (VSProject.Object is VSLangProj.VSProject)
                {
                    VSLangProj.References references =
                        ((VSLangProj.VSProject)VSProject.Object).References;

                    if (references != null)
                    {
                        references.Add(assemblyPath);
                    }
                }
                else if (VSProject.Object is VsWebSite.VSWebSite)
                {
                    VsWebSite.AssemblyReferences references =
                        ((VsWebSite.VSWebSite)VSProject.Object).References;

                    if (references != null)
                    {
                        if (System.IO.Path.IsPathRooted(assemblyPath))
                        {
                            references.AddFromFile(assemblyPath);
                        }
                        else
                        {
                            references.AddFromGAC(assemblyPath);
                        }
                    }
                }
            }
        }
Beispiel #5
0
        /// <summary>
        /// Adds the project reference.
        /// </summary>
        /// <param name="projectId">The project id.</param>
        public void AddProjectReference(Guid projectId)
        {
            if (ProjectGuid == projectId)
            {
                return;
            }

            using (ProjectNode referencedProject = new ProjectNode(Solution, projectId))
            {
                if (VSProject != null)
                {
                    try
                    {
                        if (VSProject.Object is VSLangProj.VSProject)
                        {
                            VSLangProj.References references =
                                ((VSLangProj.VSProject)VSProject.Object).References;

                            if (references != null && referencedProject.ExtObject is EnvDTE.Project)
                            {
                                references.AddProject(referencedProject.ExtObject as EnvDTE.Project);
                            }
                        }
                        else if (VSProject.Object is VsWebSite.VSWebSite)
                        {
                            VsWebSite.AssemblyReferences references =
                                ((VsWebSite.VSWebSite)VSProject.Object).References;

                            if (references != null && referencedProject.ExtObject is EnvDTE.Project)
                            {
                                references.AddFromProject(referencedProject.ExtObject as EnvDTE.Project);
                            }
                        }
                    }
                    catch (COMException)
                    {
                        // throws exceptions if the reference already exists
                    }
                }
            }
        }
        /// <summary>
        /// Get project references with relative path.
        /// </summary>
        /// <param name="project"></param>
        /// <returns></returns>
        public static IEnumerable <string> GetProjectReferencesWithRelativePath(this Project project)
        {
            var referencePaths = new List <string>();

            VSLangProj.VSProject  vSProject  = project.Object as VSLangProj.VSProject;
            VSLangProj.References references = vSProject.References;
            foreach (VSLangProj.Reference reference in references)
            {
                if (reference.SourceProject != null)
                {
                    Project refProject   = reference.SourceProject;
                    Uri     u1           = new Uri($"{project.GetProjectRootPath()}{Path.DirectorySeparatorChar}");
                    Uri     u2           = new Uri(refProject.FullName);
                    Uri     relativeUri  = u1.MakeRelativeUri(u2);
                    string  relativePath = relativeUri.ToString();
                    relativePath.Replace('/', Path.DirectorySeparatorChar);
                    referencePaths.Add(relativePath);
                }
            }
            return(referencePaths);
        }
Beispiel #7
0
        static public void AddReferencesToProject(VSLangProj.References references, Project project)
        {
            if (project == null)
            {
                throw new Exception("Invalid  Project parameter");
            }

            VSLangProj.VSProject vsProject = (VSLangProj.VSProject)project.Object;

            foreach (VSLangProj.Reference refernce in references)
            {
                try
                {
                    vsProject.References.Add(refernce.Path);
                }
                catch (Exception ex)
                {
                    //ignore...
                    Logger.LogException(ex);
                }
            }
        }
Beispiel #8
0
        private void TestImplicitNestedProjectReload(IServiceProvider sp, ProjectNode project, int dialogAnswer)
        {
            // Save everything.
            IVsSolution solutionService = (IVsSolution)sp.GetService(typeof(IVsSolution));

            solutionService.SaveSolutionElement((uint)__VSSLNSAVEOPTIONS.SLNSAVEOPT_SaveIfDirty, project, 0);

            IVsProject3 nestedProject = Utilities.GetNestedHierarchy(project, "ANestedProject") as IVsProject3;

            if (nestedProject == null)
            {
                throw new InvalidOperationException("The nested project has not been loaded corectly");
            }

            string nestedProjectFileName = null;

            nestedProject.GetMkDocument(VSConstants.VSITEMID_ROOT, out nestedProjectFileName);

            if (nestedProjectFileName == null)
            {
                throw new InvalidOperationException("The nested project file name could not been retrieved corectly");
            }

            string resourceText = Utilities.GetResourceStringFromTheProjectAssembly("QueryReloadNestedProject");

            // Create the messageBoxListener Thread. This will bring up the reload of the nested project file.
            // In this scenario we will answer dialogAnswer. Also we rely on the exact messagebox text here.
            string message = String.Format(System.Globalization.CultureInfo.CurrentCulture, resourceText, nestedProjectFileName);

            DialogBoxPurger purger = new DialogBoxPurger(dialogAnswer, message);
            bool            result = false;

            try
            {
                purger.Start();
                this.AddReferenceExternallyToTheProjectFile(nestedProjectFileName);
            }
            finally
            {
                result = purger.WaitForDialogThreadToTerminate();
            }

            if (!result)
            {
                throw new InvalidOperationException("The messagebox for relaoding the nested project file has never popped up");
            }

            // Check to see if the nested project is there.
            EnvDTE.Project     projectDTE = Utilities.GetAutomationObject(project);
            EnvDTE.ProjectItem item       = projectDTE.ProjectItems.Item("ANestedProject");

            Assert.IsNotNull(item, "The nested project has not been loaded correctly.");
            EnvDTE.Project nestedAutomationProject = item.SubProject;

            // Now check to see if we can find the added reference
            VSLangProj.VSProject automationProject = nestedAutomationProject.Object as VSLangProj.VSProject;
            if (nestedAutomationProject == null)
            {
                throw new InvalidOperationException("The nested project is not a vs language project");
            }

            // Get references collection
            VSLangProj.References references = automationProject.References;

            IEnumerator enumerator = references.GetEnumerator();
            bool        found      = false;

            while (enumerator.MoveNext())
            {
                VSLangProj.Reference reference = enumerator.Current as VSLangProj.Reference;
                if (reference.Name == BuildEngineRef)
                {
                    found = true;
                }
            }

            if (dialogAnswer == NativeMethods.IDYES)
            {
                Assert.IsTrue(found, "The nested project file has not been reloaded correctly");
            }
            else
            {
                Assert.IsFalse(found, "The nested project file has been reloaded but was asked not to do that.");
            }
        }