Skip to content

Commit

Permalink
MonoDevelop 2.8 likes to remove the TargetFrameworkVersion attribute …
Browse files Browse the repository at this point in the history
…from the project. This will make sure the "v1.0" tag sticks when it gets loaded into visual studio.
  • Loading branch information
Lunat1k committed Oct 16, 2011
1 parent 8cf19b0 commit 3a22ec0
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 47 deletions.
23 changes: 12 additions & 11 deletions VSMonoTouch/ProjectUtils.cs
@@ -1,5 +1,6 @@
using System;
using System.Runtime.InteropServices;
using Microsoft.VisualStudio.Shell.Flavor;
using Microsoft.VisualStudio.Shell.Interop;
using EnvDTE;
using IServiceProvider = Microsoft.VisualStudio.OLE.Interop.IServiceProvider;
Expand All @@ -10,20 +11,21 @@ public class ProjectUtils
{
public static string GetProjectTypeGuids(Project proj)
{
string projectTypeGuids = "";
var projectTypeGuids = "";
IVsHierarchy hierarchy;
IVsAggregatableProject aggregatableProject;
int result;

object service = GetService(proj.DTE, typeof(IVsSolution));
var service = GetService(proj.DTE, typeof(IVsSolution));
var solution = (IVsSolution)service;

result = solution.GetProjectOfUniqueName(proj.UniqueName, out hierarchy);
var result = solution.GetProjectOfUniqueName(proj.UniqueName, out hierarchy);

if (result == 0)
{
aggregatableProject = (IVsAggregatableProject)hierarchy;
aggregatableProject.GetAggregateProjectTypeGuids(out projectTypeGuids);
if (hierarchy is IVsAggregatableProjectCorrected)
{
var aggregatableProject = (IVsAggregatableProjectCorrected)hierarchy;
aggregatableProject.GetAggregateProjectTypeGuids(out projectTypeGuids);
}
}

return projectTypeGuids;
Expand All @@ -40,12 +42,11 @@ private static object GetService(object serviceProviderObject, Guid guid)

object service = null;
IntPtr serviceIntPtr;
int hr;

Guid SIDGuid = guid;
Guid IIDGuid = SIDGuid;
var sidGuid = guid;
var iidGuid = sidGuid;
var serviceProvider = (IServiceProvider)serviceProviderObject;
hr = serviceProvider.QueryService(ref SIDGuid, ref IIDGuid, out serviceIntPtr);
var hr = serviceProvider.QueryService(ref sidGuid, ref iidGuid, out serviceIntPtr);

if (hr != 0)
{
Expand Down
20 changes: 19 additions & 1 deletion VSMonoTouch/VSMonoTouch.csproj
Expand Up @@ -22,6 +22,7 @@
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<RegisterForComInterop>false</RegisterForComInterop>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
Expand Down Expand Up @@ -52,7 +53,24 @@
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml" />
<Reference Include="VSLangProj, Version=7.0.3300.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<EmbedInteropTypes>True</EmbedInteropTypes>
<EmbedInteropTypes>False</EmbedInteropTypes>
<HintPath>C:\Program Files (x86)\Common Files\Microsoft Shared\MSEnv\PublicAssemblies\VSLangProj.dll</HintPath>
</Reference>
<Reference Include="VSLangProj100, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<EmbedInteropTypes>False</EmbedInteropTypes>
<HintPath>C:\Program Files (x86)\Common Files\Microsoft Shared\MSEnv\PublicAssemblies\VSLangProj100.dll</HintPath>
</Reference>
<Reference Include="VSLangProj2, Version=7.0.5000.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<EmbedInteropTypes>False</EmbedInteropTypes>
<HintPath>C:\Program Files (x86)\Common Files\Microsoft Shared\MSEnv\PublicAssemblies\VSLangProj2.dll</HintPath>
</Reference>
<Reference Include="VSLangProj80, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<EmbedInteropTypes>False</EmbedInteropTypes>
<HintPath>C:\Program Files (x86)\Common Files\Microsoft Shared\MSEnv\PublicAssemblies\VSLangProj80.dll</HintPath>
</Reference>
<Reference Include="VSLangProj90, Version=9.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<EmbedInteropTypes>False</EmbedInteropTypes>
<HintPath>C:\Program Files (x86)\Common Files\Microsoft Shared\MSEnv\PublicAssemblies\VSLangProj90.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
Expand Down
146 changes: 112 additions & 34 deletions VSMonoTouch/VSMonoTouchPackage.cs
@@ -1,10 +1,11 @@
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;

using System.Runtime.Versioning;
using EnvDTE;
using EnvDTE80;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Flavor;
using Microsoft.VisualStudio.Shell.Interop;
Expand All @@ -22,37 +23,46 @@ namespace Follesoe.VSMonoTouch
/// IVsPackage interface and uses the registration attributes defined in the framework to
/// register itself and its components with the shell.
/// </summary>
[ProvideProjectFactory(typeof(MonoTouch26FlavorProjectFactory), "MonoTouch Flavor", "Mono Files (*.csproj);*.csproj", null, null, null)]
[ProvideProjectFactory(typeof(MonoTouch28FlavorProjectFactory), "MonoTouch Flavor", "Mono Files (*.csproj);*.csproj", null, null, null)]
[ProvideProjectFactory(typeof(MonoTouch26FlavorProjectFactory), "MonoTouch Flavor", "Mono Files (*.csproj);*.csproj", "csproj", "csproj", null)]
[ProvideProjectFactory(typeof(MonoTouch28FlavorProjectFactory), "MonoTouch Flavor", "Mono Files (*.csproj);*.csproj", "csproj", "csproj", null)]
[PackageRegistration(UseManagedResourcesOnly = true)]
[InstalledProductRegistration("#110", "#112", "1.0", IconResourceID = 400)]
[Guid(GuidList.guidVSMonoTouchPkgString)]
public sealed class VSMonoTouchPackage : Package
{
private DTE _dte;
private BuildEvents _BuildEvents;
private BuildEvents _buildEvents;
private readonly SolutionEvents _solutionEvents = new SolutionEvents();
private uint _solutionEventsCookie;
private IVsSolution _solution;

protected override void Initialize()
{
RegisterProjectFactory(new MonoTouch26FlavorProjectFactory(this));
RegisterProjectFactory(new MonoTouch28FlavorProjectFactory(this));

_dte = GetGlobalService(typeof(SDTE)) as DTE;
RegisterProjectFactory(new MonoTouch28FlavorProjectFactory(this));

if (_dte != null)
{
_BuildEvents = _dte.Events.BuildEvents;
_BuildEvents.OnBuildBegin += MakeXibsNone;
_BuildEvents.OnBuildDone += MakeXibsPage;
}
else
{
throw new Exception();
}
_dte = GetService(typeof(SDTE)) as DTE;
if (_dte == null) throw new Exception("DTE Reference Not Found");

_buildEvents = _dte.Events.BuildEvents;
_buildEvents.OnBuildBegin += MakeXibsNone;
_buildEvents.OnBuildDone += MakeXibsPage;

_solution = (IVsSolution)GetService(typeof(SVsSolution));
if (_solution == null) throw new Exception("IVSSolution Reference Not Found.");
_solution.AdviseSolutionEvents(_solutionEvents, out _solutionEventsCookie);

base.Initialize();
}

protected override int QueryClose(out bool canClose)
{
if (_solutionEventsCookie != 0 && _solution != null)
_solution.UnadviseSolutionEvents(_solutionEventsCookie);

return base.QueryClose(out canClose);
}

private void MakeXibsNone(vsBuildScope scope, vsBuildAction action)
{
var xibs = FindAllXibsInSolution();
Expand Down Expand Up @@ -85,11 +95,7 @@ private void FindXibs(Project project, List<ProjectItem> xibs)
{
if (project.ConfigurationManager != null)
{
if (IsMonoTouchProject(project))
{
var vsProject = (VSLangProj.VSProject)project.Object;
FindXibs(project.ProjectItems, xibs);
}
if (IsMonoTouchProject(project)) FindXibs(project.ProjectItems, xibs);
}
else
{
Expand All @@ -99,13 +105,11 @@ private void FindXibs(Project project, List<ProjectItem> xibs)

private void NavigateProjectItems(ProjectItems items, List<ProjectItem> xibs)
{
foreach (ProjectItem item in items)
if (items == null) return;
items.Cast<ProjectItem>().ToList().ForEach(pi =>
{
if (item.SubProject != null)
{
FindXibs(item.SubProject, xibs);
}
}
if (pi.SubProject != null) FindXibs(pi.SubProject, xibs);
});
}

private static void FindXibs(ProjectItems items, List<ProjectItem> xibs)
Expand All @@ -131,27 +135,101 @@ private static void FindXibs(ProjectItems items, List<ProjectItem> xibs)
}
}

private static bool IsMonoTouchProject(Project project)
internal static bool IsMonoTouchProject(Project project)
{
string projectTypeGuids = ProjectUtils.GetProjectTypeGuids(project);
var projectTypeGuids = ProjectUtils.GetProjectTypeGuids(project);

if (projectTypeGuids.Contains(GuidList.guidMonoTouchProjectFactory26)) return true;
if (projectTypeGuids.Contains(GuidList.guidMonoTouchProjectFactory28)) return true;

return false;
}
}

public sealed class SolutionEvents : IVsSolutionEvents
{
public int OnAfterOpenProject(IVsHierarchy pHierarchy, int fAdded)
{
const string targetFrameworkMoniker = "TargetFrameworkMoniker";

object projectObj;
pHierarchy.GetProperty(VSConstants.VSITEMID_ROOT, (int)__VSHPROPID.VSHPROPID_ExtObject, out projectObj);
var project = (Project) projectObj;

if (VSMonoTouchPackage.IsMonoTouchProject(project))
{
var v10FrameworkName = (new FrameworkName(".NETFramework", new Version(1, 0))).FullName;
var item = project.Properties.Item(targetFrameworkMoniker);
if (item != null)
{
if (item.Value == null || (string)item.Value != v10FrameworkName) item.Value = v10FrameworkName;
}
else
{
project.Properties.Item(targetFrameworkMoniker).Value = v10FrameworkName;
}
}
return VSConstants.S_OK;
}

public int OnQueryCloseProject(IVsHierarchy pHierarchy, int fRemoving, ref int pfCancel)
{
return VSConstants.S_OK;
}

public int OnBeforeCloseProject(IVsHierarchy pHierarchy, int fRemoved)
{
return VSConstants.S_OK;
}

public int OnAfterLoadProject(IVsHierarchy pStubHierarchy, IVsHierarchy pRealHierarchy)
{
return VSConstants.S_OK;
}

public int OnQueryUnloadProject(IVsHierarchy pRealHierarchy, ref int pfCancel)
{
return VSConstants.S_OK;
}

public int OnBeforeUnloadProject(IVsHierarchy pRealHierarchy, IVsHierarchy pStubHierarchy)
{
return VSConstants.S_OK;
}

public int OnAfterOpenSolution(object pUnkReserved, int fNewSolution)
{
return VSConstants.S_OK;
}

public int OnQueryCloseSolution(object pUnkReserved, ref int pfCancel)
{
return VSConstants.S_OK;
}

public int OnBeforeCloseSolution(object pUnkReserved)
{
return VSConstants.S_OK;
}

public int OnAfterCloseSolution(object pUnkReserved)
{
return VSConstants.S_OK;
}
}

public abstract class MonoTouchFlavorProjectFactory : FlavoredProjectFactoryBase
{
protected readonly VSMonoTouchPackage _package;
protected readonly VSMonoTouchPackage Package;

protected MonoTouchFlavorProjectFactory(VSMonoTouchPackage package)
{
_package = package;
Package = package;
}

protected override object PreCreateForOuter(IntPtr outerProjectIUnknown)
{
return new MonoTouchFlavePackageProject(_package);
return new MonoTouchFlavePackageProject(Package);
}
}

Expand Down
2 changes: 1 addition & 1 deletion VSMonoTouch/source.extension.vsixmanifest
Expand Up @@ -3,7 +3,7 @@
<Identifier Id="4e51e215-eb16-4614-b6d2-92e6e1d8c204">
<Name>MonoTouch for Visual Studio 2010</Name>
<Author>Follesoe</Author>
<Version>1.0</Version>
<Version>1.2</Version>
<Description xml:space="preserve">Load and build MonoTouch projects in Visual Studio 2010</Description>
<Locale>1033</Locale>
<MoreInfoUrl>https://github.com/follesoe/VSMonoTouch</MoreInfoUrl>
Expand Down

0 comments on commit 3a22ec0

Please sign in to comment.