projectfactory.cs :  » Installers-Generators » WiX » Microsoft » VisualStudio » Package » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Installers Generators » WiX 
WiX » Microsoft » VisualStudio » Package » projectfactory.cs
/***************************************************************************

Copyright (c) Microsoft Corporation. All rights reserved.
This code is licensed under the Visual Studio SDK license terms.
THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.

***************************************************************************/

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using MSBuildMicrosoft.Build.BuildEngine;
using Microsoft.VisualStudio.OLE.Interop;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.Shell;
using System.Runtime.InteropServices;
using System.Reflection;
using System.Windows.Forms;
using System.Globalization;

namespace Microsoft.VisualStudio.Package{
  /// <summary>
  /// Creates projects within the solution
  /// </summary>
  [CLSCompliant(false)]
  public abstract class ProjectFactory : Microsoft.VisualStudio.Shell.Flavor.FlavoredProjectFactoryBase
  {
    #region constants
    /// <summary>
    /// The registry key holding the value for the dont show again dialog box.
    /// </summary>
    internal const string DontShowProjectSecurityWarningAgain = "DontShowProjectSecurityWarningAgain";

    /// <summary>
    /// Security warning when opening/creating client project on UNC share.
    /// </summary>
    private const string ProjectFolderFotSecureHelp = "vs.projectfolder_not_secure";
    #endregion

    #region fields
    private Microsoft.VisualStudio.Shell.Package package;
    private System.IServiceProvider site;

    /// <summary>
    /// The msbuild engine that we are going to use.
    /// </summary>
    private MSBuild.Engine buildEngine;

    /// <summary>
    /// The msbuild project for the project file.
    /// </summary>
    private MSBuild.Project buildProject;
    #endregion

    #region properties
    protected Microsoft.VisualStudio.Shell.Package Package
    {
      get
      {
        return this.package;
      }
    }

    protected System.IServiceProvider Site
    {
      get
      {
        return this.site;
      }
    }

    /// <summary>
    /// The msbuild engine that we are going to use.
    /// </summary>
    protected MSBuild.Engine BuildEngine
    {
      get
      {
        return this.buildEngine;
      }
    }

    /// <summary>
    /// The msbuild project for the temporary project file.
    /// </summary>
    protected MSBuild.Project BuildProject
    {
      get
      {
        return this.buildProject;
      }
      set
      {
        this.buildProject = value;
      }
    }
    #endregion

    #region ctor
    protected ProjectFactory(Microsoft.VisualStudio.Shell.Package package)
    {
      this.package = package;
      this.site = package;

      // Please be aware that this methods needs that ServiceProvider is valid, thus the ordering of calls in the ctor matters.
      this.buildEngine = Utilities.InitializeMsBuildEngine(this.buildEngine, this.site);
    }
    #endregion

    #region abstract methods
    protected abstract ProjectNode CreateProject();
    #endregion

    #region overriden methods
    /// <summary>
    /// Rather than directly creating the project, ask VS to initate the process of
    /// creating an aggregated project in case we are flavored. We will be called
    /// on the IVsAggregatableProjectFactory to do the real project creation.
    /// </summary>
    /// <param name="fileName">Project file</param>
    /// <param name="location">Path of the project</param>
    /// <param name="name">Project Name</param>
    /// <param name="flags">Creation flags</param>
    /// <param name="projectGuid">Guid of the project</param>
    /// <param name="project">Project that end up being created by this method</param>
    /// <param name="canceled">Was the project creation canceled</param>
    protected override void CreateProject(string fileName, string location, string name, uint flags, ref Guid projectGuid, out IntPtr project, out int canceled)
    {
      project = IntPtr.Zero;
      canceled = 0;

      // Get the list of GUIDs from the project/template
      string guidsList = this.ProjectTypeGuids(fileName);
#if !VS2005
      string projectDirectory = String.IsNullOrEmpty(location) ? Path.GetDirectoryName(fileName) : location;
      if (!this.IsProjectLocationSecure(projectDirectory))
      {
        canceled = 1;
        ErrorHandler.ThrowOnFailure(VSConstants.VS_E_WIZARDBACKBUTTONPRESS);
      }
#endif

      // Launch the aggregate creation process (we should be called back on our IVsAggregatableProjectFactoryCorrected implementation)
      IVsCreateAggregateProject aggregateProjectFactory = (IVsCreateAggregateProject)this.Site.GetService(typeof(SVsCreateAggregateProject));
      int hr = aggregateProjectFactory.CreateAggregateProject(guidsList, fileName, location, name, flags, ref projectGuid, out project);
      if (hr == VSConstants.E_ABORT)
        canceled = 1;
      ErrorHandler.ThrowOnFailure(hr);

      // This needs to be done after the aggregation is completed (to avoid creating a non-aggregated CCW) and as a result we have to go through the interface
      IProjectEventsProvider eventsProvider = (IProjectEventsProvider)Marshal.GetTypedObjectForIUnknown(project, typeof(IProjectEventsProvider));
      eventsProvider.ProjectEventsProvider = this.GetProjectEventsProvider();

      this.buildProject = null;
    }


    /// <summary>
    /// Instantiate the project class, but do not proceed with the
    /// initialization just yet.
    /// Delegate to CreateProject implemented by the derived class.
    /// </summary>
    protected override object PreCreateForOuter(IntPtr outerProjectIUnknown)
    {
      Debug.Assert(this.buildProject != null, "The build project should have been initialized before calling PreCreateForOuter.");
      GlobalPropertyHandler globalPropertyHandler = new GlobalPropertyHandler(this.buildProject);
      globalPropertyHandler.InitializeGlobalProperties();

      // Please be very carefull what is initialized here on the ProjectNode. Normally this should only instantiate and return a project node.
      // The reason why one should very carefully add state to the project node here is that at this point the aggregation has not yet been created and anything that would cause a CCW for the project to be created would cause the aggregation to fail
      // Our reasoning is that there is no other place where state on the project node can be set that is known by the Factory and has to execute before the Load method.
      ProjectNode node = this.CreateProject();
      Debug.Assert(node != null, "The project failed to be created");
      node.BuildEngine = this.buildEngine;
      node.BuildProject = this.buildProject;
      node.GlobalPropetyHandler = globalPropertyHandler;
      globalPropertyHandler.RegisterConfigurationChangeListener(node, this.site);
      node.Package = this.package as ProjectPackage;
      return node;
    }

    /// <summary>
    /// Retrives the list of project guids from the project file.
    /// If you don't want your project to be flavorable, override
    /// to only return your project factory Guid:
    ///      return this.GetType().GUID.ToString("B");
    /// </summary>
    /// <param name="file">Project file to look into to find the Guid list</param>
    /// <returns>List of semi-colon separated GUIDs</returns>
    protected override string ProjectTypeGuids(string file)
    {
      // Load the project so we can extract the list of GUIDs

      this.buildProject = Utilities.ReinitializeMsBuildProject(this.buildEngine, file, this.buildProject);

      // Retrieve the list of GUIDs, if it is not specify, make it our GUID
      string guids = buildProject.GetEvaluatedProperty(ProjectFileConstants.ProjectTypeGuids);
      if (String.IsNullOrEmpty(guids))
        guids = this.GetType().GUID.ToString("B");

      return guids;
    }
    #endregion

    #region helpers
    private IProjectEvents GetProjectEventsProvider()
    {
      ProjectPackage projectPackage = this.package as ProjectPackage;
      Debug.Assert(projectPackage != null, "Package not inherited from framework");
      if (projectPackage != null)
      {
        foreach (SolutionListener listener in projectPackage.SolutionListeners)
        {
          IProjectEvents projectEvents = listener as IProjectEvents;
          if (projectEvents != null)
          {
            return projectEvents;
          }
        }
      }

      return null;
    }

#if !VS2005
    /// <summary>
    /// Checks if the project location is secure. If it is not then it will launch a dialog box prompting the user to acknowledge that the project location is not secure.
    /// </summary>
    /// <param name="location">The location to check</param>
    /// <returns>true if it is secure, false otherwise.</returns>
    private bool IsProjectLocationSecure(string location)
    {
      if (!Utilities.IsProjectLocationSecure(location))
      {
        if (Utilities.IsShellInCommandLineMode(this.Site) || Utilities.IsInAutomationFunction(this.Site))
        {
          return false;
        }

        string errorMessage = String.Format(CultureInfo.CurrentCulture, SR.GetString(SR.ProjectLocationNotTrusted, CultureInfo.CurrentUICulture), Environment.NewLine, Path.GetDirectoryName(location));
        return (DontShowAgainDialog.LaunchDontShowAgainDialog(this.Site, errorMessage, ProjectFolderFotSecureHelp, DontShowAgainDialog.DefaultButton.OK, DontShowProjectSecurityWarningAgain) == DialogResult.OK);
      }

      return true;
    }
#endif
    #endregion
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.