/// <summary>
        /// Copy the source code of a package in the library and add it to the current solution as a new project
        /// </summary>
        public void AddOrUpdatePackageInSolution()
        {
            if (SelectedLibraryPackage == null)
            {
                return;
            }

            string solutionDirectoryPath, solutionFilePath, solutionUserOptionsFilePath;
            var    solutionInfo = _solutionService.GetSolutionInfo(out solutionDirectoryPath, out solutionFilePath, out solutionUserOptionsFilePath);

            if (solutionFilePath == null)
            {
                MessageBox.Show("No solution found. First open a solution or create a new one");
            }
            else
            {
                IntPtr proj;
                Guid   projectType;
                Guid   iidProject      = Guid.Empty;
                string projectLocation = _LibraryPackagesExplorer.GetProjectLocation(SelectedLibraryPackage.Name);

                _solutionService.GetProjectTypeGuid(0, projectLocation, out projectType);
                int result = _solutionService.CreateProject(projectType, projectLocation, null, null, (uint)__VSCREATEPROJFLAGS.CPF_OPENFILE, ref iidProject, out proj);

                ErrorHandler.ThrowOnFailure(result);
            }

            Refresh();
        }
Example #2
0
        public static void AddSolutionItem(IVsSolution solution, string fileName)
        {
            uint itemId = DteHelper2.FindItemByName(
                solution as IVsHierarchy, "Solution Items");

            IntPtr ptr = IntPtr.Zero;
            Guid   solutionFolderGuid = new Guid("2150E333-8FDC-42a3-9474-1A3956D46DE8");
            Guid   iidProject         = typeof(IVsHierarchy).GUID;

            int res = solution.CreateProject(
                ref solutionFolderGuid,
                null,
                null,
                "Solution Items",
                0,
                ref iidProject,
                out ptr);

            if (ptr != IntPtr.Zero)
            {
                IVsHierarchy hierarchy = (IVsHierarchy)Marshal.GetObjectForIUnknown(ptr);

                Guid projGuid;

                hierarchy.GetGuidProperty(
                    VSConstants.VSITEMID_ROOT,
                    (int)__VSHPROPID.VSHPROPID_ProjectIDGuid,
                    out projGuid);

                ProjectNode node = new ProjectNode(solution, projGuid);

                node.AddItem(fileName);
            }
        }
Example #3
0
        private void AddProjectToSolution(IVsSolution solution, string projectName, string projectReference)
        {
            ThreadHelper.ThrowIfNotOnUIThread();

            var projectType = Guid.Empty;
            var projectId   = Guid.Empty;
            var result      = solution.CreateProject(ref projectType, projectReference, projectReference, projectName,
                                                     (uint)(__VSCREATEPROJFLAGS.CPF_OPENFILE | __VSCREATEPROJFLAGS.CPF_SILENT), ref projectId, out _);

            if (result != VSConstants.S_OK)
            {
                MessageBox.Show($"Failed to add project {projectReference} to solution. (CreateProject)",
                                "Fail", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Example #4
0
        public void OpenOrAddProjectFile(string projectFile, bool add)
        {
            ThreadHelper.ThrowIfNotOnUIThread();

            if (string.IsNullOrEmpty(projectFile))
            {
                throw new ArgumentNullException("projectFile");
            }

            string ext        = Path.GetExtension(projectFile);
            bool   isSolution = false;

            foreach (string x in SolutionFilter.Split(';'))
            {
                if (string.Equals(ext, Path.GetExtension(x), StringComparison.OrdinalIgnoreCase))
                {
                    isSolution = true;
                    break;
                }
            }

            IVsSolution solution = GetService <IVsSolution>(typeof(SVsSolution));

            int hr;

            if (isSolution && !add)
            {
                hr = solution.OpenSolutionFile(0, projectFile);
            }
            else
            {
                Guid   gnull      = Guid.Empty;
                Guid   gInterface = Guid.Empty;
                IntPtr pProj      = IntPtr.Zero;

                hr = solution.CreateProject(ref gnull, projectFile, null, null, (uint)__VSCREATEPROJFLAGS.CPF_OPENFILE, ref gInterface, out pProj);
            }

            if (!VSErr.Succeeded(hr))
            {
                new AnkhMessageBox(this).Show(Marshal.GetExceptionForHR(hr).Message, "", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
            }
        }
Example #5
0
        public void TestLoadingOfProjectWithDuplicateItems()
        {
            UIThreadInvoker.Invoke((ThreadInvoker) delegate()
            {
                //Get the global service provider and the dte
                IServiceProvider sp = VsIdeTestHostContext.ServiceProvider;
                DTE dte             = (DTE)sp.GetService(typeof(DTE));

                string destination  = Path.Combine(TestContext.TestDir, TestContext.TestName);
                ProjectNode project = Utilities.CreateMyNestedProject(sp, dte, TestContext.TestName, destination, true);

                string path = project.GetMkDocument();

                // It would be nice if we could just copy the embedded resource to the opened project file, but implicit reload of nested projects crashes on non SP1 versions.
                // For now we are going to close the project and reopen it with the embedded project.
                IVsSolution solutionService = (IVsSolution)sp.GetService(typeof(IVsSolution));

                solutionService.CloseSolutionElement((uint)__VSSLNSAVEOPTIONS.SLNSAVEOPT_ForceSave, project, 0);

                TestUtils.WriteEmbeddedResourceToBinaryFile(typeof(TestProject).Assembly, EmbeddedResourceProjectLocation, path);

                IntPtr projectPtr = IntPtr.Zero;
                Guid guid         = Guid.Empty;
                Guid iid          = new Guid("{00000000-0000-0000-C000-000000000046}");        // IID_IUnknown;

                try
                {
                    ErrorHandler.ThrowOnFailure(solutionService.CreateProject(ref guid, path, null, null, (uint)__VSCREATEPROJFLAGS.CPF_OPENFILE, ref iid, out projectPtr));

                    // Now see that we have only unique items. We do not care much about canonicalization issues in this test.
                    this.CheckForUniqueCaptions(project);
                }
                finally
                {
                    if (projectPtr != IntPtr.Zero)
                    {
                        Marshal.Release(projectPtr);
                    }
                }
            });
        }
Example #6
0
        public HierarchyNode CreateSolutionFolder(string folderName)
        {
            Guard.NotNullOrEmpty(() => folderName, folderName);
            IntPtr ptr = IntPtr.Zero;
            Guid   solutionFolderGuid = new Guid(Constants.SolutionFolderType);
            Guid   iidProject         = typeof(IVsHierarchy).GUID;
            int    hr = solution.CreateProject(
                ref solutionFolderGuid,
                null,
                null,
                folderName,
                0,
                ref iidProject,
                out ptr);

            if (hr == VSConstants.S_OK && ptr != IntPtr.Zero)
            {
                IVsHierarchy vsHierarchy = (IVsHierarchy)Marshal.GetObjectForIUnknown(ptr);
                Debug.Assert(vsHierarchy != null);
                return(new HierarchyNode(solution, vsHierarchy));
            }
            return(null);
        }
Example #7
0
        private void okButton_Click(object sender, EventArgs e)
        {
            ThreadHelper.ThrowIfNotOnUIThread();

            try
            {
                StringBuilder errors;

                this.BeginAction();

                _settings.Save(_settingsFileName);

                errors = new StringBuilder();

                foreach (ListViewItem item in projectsListView.CheckedItems)
                {
                    string fileName;

                    fileName = item.Name;

                    if (!_loadedProjects.Contains(fileName))
                    {
                        if (!File.Exists(fileName))
                        {
                            errors.AppendLine($"Project file '{fileName}' not found, unable to add to solution.").AppendLine();
                        }
                        else
                        {
                            Guid   projectType;
                            Guid   projectId;
                            int    result;
                            IntPtr project;

                            projectType = Guid.Empty;
                            projectId   = Guid.Empty;

                            result = _currentSolution.CreateProject(ref projectType, fileName, null, null, (uint)(__VSCREATEPROJFLAGS.CPF_OPENFILE | __VSCREATEPROJFLAGS.CPF_SILENT), ref projectId, out project);

                            if (result != VSConstants.S_OK)
                            {
                                errors.AppendLine($"Failed to add project: {fileName}").AppendLine();
                            }
                            else
                            {
                                _loadedProjects.Add(fileName);
                                this.MarkProjectAsLoaded(item);
                            }
                        }
                    }
                }

                this.EndAction();

                if (errors.Length != 0)
                {
                    MessageBox.Show(errors.ToString(), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            catch (Exception ex)
            {
                Utilities.ShowExceptionMessage(ex);
            }
        }
Example #8
0
 public int CreateProject(ref Guid rguidProjectType, string lpszMoniker, string lpszLocation, string lpszName, uint grfCreateFlags, ref Guid iidProject, out IntPtr ppProject)
 {
     return(_solution.CreateProject(ref rguidProjectType, lpszMoniker, lpszLocation, lpszName, grfCreateFlags, ref iidProject, out ppProject));
 }
Example #9
-1
		public static void AddSolutionItem(IVsSolution solution, string fileName)
		{
			uint itemId = DteHelper2.FindItemByName(
				solution as IVsHierarchy, "Solution Items");

			IntPtr ptr = IntPtr.Zero;
			Guid solutionFolderGuid = new Guid("2150E333-8FDC-42a3-9474-1A3956D46DE8");
			Guid iidProject = typeof(IVsHierarchy).GUID;

			int res = solution.CreateProject(
				ref solutionFolderGuid,
				null,
				null,
				"Solution Items",
				0,
				ref iidProject,
				out ptr);

			if(ptr != IntPtr.Zero)
			{
				IVsHierarchy hierarchy = (IVsHierarchy)Marshal.GetObjectForIUnknown(ptr);

				Guid projGuid;

				hierarchy.GetGuidProperty(
					VSConstants.VSITEMID_ROOT,
					(int)__VSHPROPID.VSHPROPID_ProjectIDGuid,
					out projGuid);

				ProjectNode node = new ProjectNode(solution, projGuid);

				node.AddItem(fileName);
			}
		}
        public void RunFinished()
        {

            sln = ((IVsSolution)Package.GetGlobalService(typeof(SVsSolution)));
            ServiceProvider serviceProvider = new ServiceProvider(dte as Microsoft.VisualStudio.OLE.Interop.IServiceProvider);
            string projectGuid = null;
            using (XmlReader projectReader = XmlReader.Create(fileName))
            {
                projectReader.MoveToContent();
                object nodeName = projectReader.NameTable.Add("ProjectGuid");
                while (projectReader.Read())
                {
                    if (Object.Equals(projectReader.LocalName, nodeName))
                    {
                        projectGuid = projectReader.ReadElementContentAsString();
                        break;
                    }
                }
            }
            IVsHierarchy hier = VsShellUtilities.GetHierarchy(serviceProvider, new Guid(projectGuid));
            sln.CloseSolutionElement((uint)__VSSLNCLOSEOPTIONS.SLNCLOSEOPT_UnloadProject, hier, 0);
            String bistroPath = (String)Registry.LocalMachine.CreateSubKey(@"Software\Hill30\Bistro").GetValue("InstallDir");
#if VS2008
            String ndjangoPath = (String)Registry.LocalMachine.CreateSubKey(@"Software\Hill30\NDjango").GetValue("InstallDir2008");
#elif VS2010
            String ndjangoPath = (String)Registry.LocalMachine.CreateSubKey(@"Software\Hill30\NDjango").GetValue("InstallDir2010");
#endif
            StreamReader reader = new StreamReader(fileName);
            string content = reader.ReadToEnd();
            reader.Close();
            content = content.Replace(BISTRODIR, (bistroPath == null) ? BISTRODIR : Path.GetFullPath(bistroPath)).
                Replace(NDJANGODIR, (ndjangoPath == null) ? NDJANGODIR : Path.GetFullPath(ndjangoPath));
            StreamWriter sw = new StreamWriter(fileName);
            sw.Write(content);
            sw.Close();
            IntPtr ppProject = IntPtr.Zero;
            Guid guid1 = new Guid();
            Guid guid2 = new Guid();
            sln.CreateProject(ref guid1, fileName, null, null, (uint)__VSCREATEPROJFLAGS.CPF_OPENFILE, ref guid2, out ppProject);


        }