/vsaddin/src/Util.cs
C# | 3577 lines | 3473 code | 83 blank | 21 comment | 85 complexity | c342b39434833a35c51facdd11f5ee7f MD5 | raw file
Possible License(s): BSD-3-Clause-No-Nuclear-License-2014, GPL-2.0, BSD-3-Clause
Large files files are truncated, but you can click here to view the full file
- // **********************************************************************
- //
- // Copyright (c) 2003-2013 ZeroC, Inc. All rights reserved.
- //
- // This copy of Ice is licensed to you under the terms described in the
- // ICE_LICENSE file included in this distribution.
- //
- // **********************************************************************
- using System;
- using System.Text;
- using System.Collections.Generic;
- using System.ComponentModel;
- using EnvDTE;
- using System.Windows.Forms;
- using System.Runtime.InteropServices;
- using System.IO;
- using System.Diagnostics;
- using Extensibility;
- using EnvDTE80;
- using Microsoft.VisualStudio;
- using Microsoft.VisualStudio.CommandBars;
- using Microsoft.VisualStudio.VCProjectEngine;
- using Microsoft.VisualStudio.VCProject;
- using Microsoft.VisualStudio.Shell;
- using Microsoft.VisualStudio.Shell.Interop;
- using System.Resources;
- using System.Reflection;
- using VSLangProj;
- using System.Globalization;
- using System.Collections;
- using System.Runtime.InteropServices.ComTypes;
- using Microsoft.CSharp;
- using System.Xml;
- namespace Ice.VisualStudio
- {
- public class InitializationException : System.Exception
- {
- public InitializationException(string message) : base(message)
- {
- }
- }
- public enum CPUType
- {
- x86CPUType,
- x64CPUType,
- ARMCPUType
- }
- public interface LinkerAdapter
- {
- String AdditionalDependencies
- {
- get;
- set;
- }
- String AdditionalLibraryDirectories
- {
- get;
- set;
- }
- }
- public class DynamicLinkerAdapter : LinkerAdapter
- {
- public DynamicLinkerAdapter(VCLinkerTool linkerTool)
- {
- _linkerTool = linkerTool;
- }
- public String AdditionalDependencies
- {
- get
- {
- return _linkerTool.AdditionalDependencies;
- }
- set
- {
- _linkerTool.AdditionalDependencies = value;
- }
- }
- public String AdditionalLibraryDirectories
- {
- get
- {
- return _linkerTool.AdditionalLibraryDirectories;
- }
- set
- {
- _linkerTool.AdditionalLibraryDirectories = value;
- }
- }
- private VCLinkerTool _linkerTool;
- }
- public class StaticLinkerAdapter : LinkerAdapter
- {
- public StaticLinkerAdapter(VCLibrarianTool librarianTool)
- {
- _librarianTool = librarianTool;
- }
- public String AdditionalDependencies
- {
- get
- {
- return "";
- }
- set
- {
- }
- }
- public String AdditionalLibraryDirectories
- {
- get
- {
- return "";
- }
- set
- {
- }
- }
- private VCLibrarianTool _librarianTool;
- }
- public class ComponentList : List<string>
- {
- public ComponentList()
- {
- }
- public ComponentList(string[] values)
- {
- foreach(string s in values)
- {
- Add(s);
- }
- }
- public ComponentList(string value)
- {
- if(value == null)
- {
- value = "";
- }
- init(value, ';');
- }
- public ComponentList(string value, char separator)
- {
- init(value, separator);
- }
- public new void Add(string value)
- {
- value = value.Trim();
- if(String.IsNullOrEmpty(value))
- {
- return;
- }
- if(!base.Contains(value))
- {
- base.Add(value);
- }
- }
- public new bool Contains(string value)
- {
- string v = base.Find(delegate(string s)
- {
- return s.Equals(value.Trim(), StringComparison.CurrentCultureIgnoreCase);
- });
- return v != null;
- }
- public new bool Remove(string value)
- {
- value = value.Trim();
- //
- // To do the remove case insensitive, first find the value
- // doing case insensitive comparison and then remove that.
- //
- string v = base.Find(delegate(string s)
- {
- return s.Equals(value.Trim(), StringComparison.CurrentCultureIgnoreCase);
- });
- if(v != null)
- {
- return base.Remove(v);
- }
- return false;
- }
- private void init(string value, char separator)
- {
- Array items = value.Split(separator);
- foreach(string s in items)
- {
- Add(s);
- }
- }
- public override string ToString()
- {
- return ToString(';');
- }
- public string ToString(char separator)
- {
- if(this.Count == 0)
- {
- return "";
- }
- StringBuilder sb = new StringBuilder();
- for(int cont = 0; cont < this.Count; ++cont)
- {
- sb.Append(this[cont]);
- if(cont < this.Count - 1)
- {
- sb.Append(separator);
- }
- }
- return sb.ToString();
- }
- public bool Equal(ComponentList other)
- {
- if(this.Count != other.Count)
- {
- return false;
- }
- bool equal = true;
- for(int i = 0; i < this.Count; ++i)
- {
- string val1 = this[i];
- string val2 = other[i];
- if(val1 == null && val2 == null)
- {
- continue;
- }
- if(val1 == null || val2 == null)
- {
- equal = false;
- break;
- }
- if(!val1.Equals(val2, StringComparison.CurrentCultureIgnoreCase))
- {
- equal = false;
- break;
- }
- }
- return equal;
- }
- }
- public class IncludePathList : ComponentList
- {
- public IncludePathList()
- : base()
- {
- }
- public IncludePathList(string[] values)
- : base(values)
- {
- }
- public IncludePathList(string value)
- : base(value, '|')
- {
- }
- public override string ToString()
- {
- return base.ToString('|');
- }
- public bool Contains(Project project, string value)
- {
- string path = Util.absolutePath(project, value);
- string found = base.Find(delegate(string s)
- {
- string other = Util.absolutePath(project, s);
- return path.Equals(other, StringComparison.CurrentCultureIgnoreCase);
- });
- return found != null;
- }
- }
- public static class Util
- {
- public enum msgLevel{ msgError, msgInfo, msgDebug };
- public static string MajorVersion
- {
- get
- {
- if (_majorVersion == null)
- {
- string[] tokens = Assembly.GetExecutingAssembly().GetName().Version.ToString().Split('.');
- Debug.Assert(tokens.Length > 1);
- _majorVersion = tokens[0];
- }
- return _majorVersion;
- }
- }
- private static string _majorVersion = null;
- public static string MinorVersion
- {
- get
- {
- if (_minorVersion == null)
- {
- string[] tokens = Assembly.GetExecutingAssembly().GetName().Version.ToString().Split('.');
- Debug.Assert(tokens.Length > 1);
- _minorVersion = tokens[1];
- }
- return _minorVersion;
- }
- }
- private static string _minorVersion = null;
- public const string ProjectVersion = "1";
- public const string slice2cs = "slice2cs.exe";
- public const string slice2cpp = "slice2cpp.exe";
- //
- // Property names used to persist project configuration.
- //
- public const string PropertyIce = "ZerocIce_Enabled";
- public const string PropertyIceOutputDir = "ZerocIce_OutputDir";
- public const string PropertyIceHeaderExt = "ZerocIce_HeaderExt";
- public const string PropertyIceSourceExt = "ZerocIce_SourceExt";
- public const string PropertyIceComponents = "ZerocIce_Components";
- public const string PropertyIceExtraOptions = "ZerocIce_ExtraOptions";
- public const string PropertyIceIncludePath = "ZerocIce_IncludePath";
- public const string PropertyIceStreaming = "ZerocIce_Streaming";
- public const string PropertyIceChecksum = "ZerocIce_Checksum";
- public const string PropertyIceTie = "ZerocIce_Tie";
- public const string PropertyIcePrefix = "ZerocIce_Prefix";
- public const string PropertyIceDllExport = "ZerocIce_DllExport";
- public const string PropertyVerboseLevel = "ZerocIce_VerboseLevel";
- public const string PropertyProjectVersion = "ZerocIce_ProjectVersion";
- private static readonly string[] silverlightNames =
- {
- "Glacier2", "Ice", "IceGrid", "IcePatch2",
- "IceStorm"
- };
- public static string[] getSilverlightNames()
- {
- return (string[])silverlightNames.Clone();
- }
- private static readonly string[] cppNames =
- {
- "Freeze", "Glacier2", "Ice", "IceBox", "IceGrid", "IcePatch2",
- "IceSSL", "IceStorm", "IceUtil"
- };
- public static string[] getCppNames()
- {
- return (string[])cppNames.Clone();
- }
- private static readonly string[] dotNetNames =
- {
- "Glacier2", "Ice", "IceBox", "IceGrid", "IcePatch2",
- "IceSSL", "IceStorm"
- };
- private static readonly string[] dotNetCompactNames =
- {
- "Glacier2", "Ice", "IceBox", "IceGrid", "IcePatch2",
- "IceStorm"
- };
- public static string[] getDotNetCompactNames()
- {
- return (string[])dotNetCompactNames.Clone();
- }
- public static string[] getDotNetNames()
- {
- return (string[])dotNetNames.Clone();
- }
- public static string getIceHome()
- {
- string iceSourceHome = System.Environment.GetEnvironmentVariable("IceSourceHome");
- if (iceSourceHome != null && System.IO.Directory.Exists(iceSourceHome) &&
- System.IO.File.Exists(Path.Combine(iceSourceHome, Path.Combine("cpp", Path.Combine("bin", slice2cpp)))))
- {
- return iceSourceHome;
- }
- else
- {
- string defaultIceHome = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
- if (defaultIceHome.EndsWith("\\vsaddin", StringComparison.CurrentCultureIgnoreCase))
- {
- defaultIceHome = defaultIceHome.Substring(0, defaultIceHome.Length - "\\vsaddin".Length);
- }
- else if (defaultIceHome.EndsWith("\\vsaddin\\bin", StringComparison.CurrentCultureIgnoreCase))
- {
- defaultIceHome = defaultIceHome.Substring(0, defaultIceHome.Length - "\\vsaddin\bin".Length);
- }
- return defaultIceHome;
- }
- }
- public static string getProjectOutputDirRaw(Project project)
- {
- return getProjectProperty(project, Util.PropertyIceOutputDir, "", false);
- }
- public static string getProjectOutputDir(Project project)
- {
- String path = getProjectOutputDirRaw(project);
- if(containsEnvironmentVars(path))
- {
- path = expandEnvironmentVars(path);
- }
- return path;
- }
-
- public static string getProjectAbsoluteOutputDir(Project project)
- {
- String path = getProjectOutputDir(project);
- if(!Path.IsPathRooted(path))
- {
- path = Util.absolutePath(project, path);
- }
- return path;
- }
- public static VCPropertySheet icePropertySheet(Project project)
- {
- VCPropertySheet sheet = null;
- if(isCppProject(project))
- {
- VCConfiguration configuration = getActiveVCConfiguration(project);
- if(configuration != null)
- {
- sheet = findPropertySheet(((IVCCollection)configuration.PropertySheets), "ice");
- }
- }
- return sheet;
- }
- public static VCUserMacro userMacro(Project project, String name)
- {
- VCPropertySheet sheet = icePropertySheet(project);
- VCUserMacro macro = null;
- foreach(VCUserMacro m in sheet.UserMacros)
- {
- if(m.Name.Equals(name))
- {
- macro = m;
- break;
- }
- }
- return macro;
- }
- public static VCPropertySheet findPropertySheet(IVCCollection propertySheets, string sheetName)
- {
- VCPropertySheet value = null;
- foreach(VCPropertySheet sheet in propertySheets)
- {
- if(sheet == null || String.IsNullOrEmpty(sheet.Name))
- {
- continue;
- }
- if(sheet.Name == sheetName)
- {
- value = sheet;
- break;
- }
- }
- return value;
- }
- //
- // This will add the Ice property sheet to the project.
- //
- public static void addIcePropertySheet(Project project)
- {
- VCProject vcProj = (VCProject)project.Object;
- string propSheetFileName = "$(ALLUSERSPROFILE)\\ZeroC\\Ice";
- #if VS2008
- propSheetFileName += ".vsprops";
- #endif
- #if VS2010 || VS2012
- propSheetFileName += ".props";
- #endif
- //
- // All project configurations must include ice.vsprops (vc90) or IceCommon.props (vc100 | vc110)
- //
- IVCCollection configurations = (IVCCollection)vcProj.Configurations;
- string[] cppComponents = Util.getCppNames();
- foreach(VCConfiguration vcConfig in configurations)
- {
- VCPropertySheet newSheet = findPropertySheet(vcConfig.PropertySheets as IVCCollection, "ice");
- if(newSheet == null)
- {
- #if VS2008
- string inhertiedPropertySheets = vcConfig.InheritedPropertySheets;
- if(String.IsNullOrEmpty(inhertiedPropertySheets) || !inhertiedPropertySheets.Contains(propSheetFileName))
- {
- if(!String.IsNullOrEmpty(inhertiedPropertySheets) && !inhertiedPropertySheets.EndsWith(";"))
- {
- inhertiedPropertySheets += " ; ";
- }
- inhertiedPropertySheets += propSheetFileName;
- vcConfig.InheritedPropertySheets = inhertiedPropertySheets;
- }
- #endif
- #if VS2010 || VS2012
- newSheet = vcConfig.AddPropertySheet(propSheetFileName);
- #endif
- }
- }
- }
- public static string getCsBinDir(Project project)
- {
- string binDir = "";
- if(isVBSmartDeviceProject(project) || isCSharpSmartDeviceProject(project))
- {
- binDir = _csCompactFrameworkBinDirs;
- }
- else if(isSilverlightProject(project))
- {
- binDir = _slBinDirs;
- }
- else
- {
- binDir = _csBinDirs;
- }
- return Path.Combine(Util.getIceHome(), binDir);
- }
- public static string getPathRelativeToProject(ProjectItem item)
- {
- StringBuilder path = new StringBuilder();
- if(item != null)
- {
- path.Append(Util.getPathRelativeToProject(item, item.ContainingProject.ProjectItems));
- }
- return path.ToString();
- }
- public static string getPathRelativeToProject(ProjectItem item, ProjectItems items)
- {
- StringBuilder path = new StringBuilder();
- foreach(ProjectItem i in items)
- {
- if(i == item)
- {
- if(path.Length > 0)
- {
- path.Append("\\");
- }
- path.Append(i.Name);
- break;
- }
- else if(Util.isProjectItemFilter(i) || Util.isProjectItemFolder(i))
- {
- string token = Util.getPathRelativeToProject(item, i.ProjectItems);
- if(!String.IsNullOrEmpty(token))
- {
- path.Append(i.Name);
- path.Append("\\");
- path.Append(token);
- break;
- }
- }
- }
- return path.ToString();
- }
- public static bool addCppIncludes(VCCLCompilerTool tool, Project project)
- {
- if(tool == null || project == null)
- {
- return false;
- }
- bool changed = false;
- ComponentList includes = new ComponentList(tool.AdditionalIncludeDirectories);
- string outputDir = getProjectOutputDirRaw(project);
- if(outputDir.Equals(""))
- {
- outputDir = ".";
- }
-
- if(!includes.Contains(outputDir) && !includes.Contains(quote(outputDir)))
- {
- changed = true;
- includes.Add(outputDir);
- }
-
- if(changed)
- {
- tool.AdditionalIncludeDirectories = includes.ToString();
- }
- return changed;
- }
- public static void removeCppIncludes(VCCLCompilerTool tool, string iceHome, string generatedDir)
- {
- if(tool == null || String.IsNullOrEmpty(tool.AdditionalIncludeDirectories))
- {
- return;
- }
- bool changed = false;
-
- ComponentList includes = new ComponentList(tool.AdditionalIncludeDirectories);
- if(includes.Remove(quote(iceHome + "\\include")) || includes.Remove(iceHome + "\\include"))
- {
- changed = true;
- }
- if(!generatedDir.Equals("."))
- {
- if(includes.Remove(generatedDir))
- {
- changed = true;
- }
- }
- if(changed)
- {
- tool.AdditionalIncludeDirectories = includes.ToString();
- }
- }
- public static void removeIcePropertySheet(VCConfiguration configuration)
- {
- if(configuration == null)
- {
- return;
- }
- #if VS2008
- ComponentList sheets = new ComponentList(configuration.InheritedPropertySheets);
- if(sheets.Remove("$(ALLUSERSPROFILE)\\ZeroC\\ice.vsprops"))
- {
- configuration.InheritedPropertySheets = sheets.ToString();
- }
- #endif
- #if VS2010 || VS2012
- VCPropertySheet sheet = null;
- IVCCollection sheets = (IVCCollection)configuration.PropertySheets;
- foreach(VCPropertySheet s in sheets)
- {
- if(!s.PropertySheetFile.Equals(configuration.Evaluate("$(ALLUSERSPROFILE)\\ZeroC\\Ice.props"),
- StringComparison.CurrentCultureIgnoreCase))
- {
- continue;
- }
- sheet = s;
- break;
- }
- if(sheet != null)
- {
- configuration.RemovePropertySheet(sheet);
- }
- #endif
- }
- private static readonly string _csBinDirs = "\\Assemblies\\";
- private static readonly string _csCompactFrameworkBinDirs = "\\Assemblies\\cf\\";
- private static readonly string _slBinDirs = "\\Assemblies\\sl\\";
- public static bool addDotNetReference(Project project, string component, bool development)
- {
- if(project == null || String.IsNullOrEmpty(component))
- {
- return false;
- }
- VSLangProj.VSProject vsProject = (VSLangProj.VSProject)project.Object;
- try
- {
- Reference r = vsProject.References.Add(component + ".dll");
- if (development)
- {
- r.CopyLocal = false;
- }
- return true;
- }
- catch (COMException ex)
- {
- Console.WriteLine(ex);
- }
- MessageBox.Show("Could not locate '" + component + ".dll'.",
- "Ice Visual Studio Add-in", MessageBoxButtons.OK,
- MessageBoxIcon.Error,
- MessageBoxDefaultButton.Button1,
- (MessageBoxOptions)0);
- return false;
- }
- public static bool removeDotNetReference(Project project, string component)
- {
- if(project == null || String.IsNullOrEmpty(component))
- {
- return false;
- }
- foreach(Reference r in ((VSProject)project.Object).References)
- {
- if(r.Name.Equals(component, StringComparison.OrdinalIgnoreCase))
- {
- r.Remove();
- return true;
- }
- }
- return false;
- }
- #if VS2012
- public static void addSdkReference(VCProject project, string component)
- {
- string sdkId = component + ", Version=" + Util.MajorVersion + "." + Util.MinorVersion;
- VCReference reference = (VCReference)((VCReferences)project.VCReferences).Item(sdkId);
- if(reference != null)
- {
- reference.Remove();
- }
- project.AddSdkReference(sdkId);
- }
- public static bool removeSdkReference(VCProject project, string component)
- {
- string sdkId = component + ", Version=" + Util.MajorVersion + "." + Util.MinorVersion;
- VCReference reference = (VCReference)((VCReferences)project.VCReferences).Item(sdkId);
- if (reference != null)
- {
- reference.Remove();
- return true;
- }
- return false;
- }
- #endif
- public static void addCppLib(LinkerAdapter tool, string component, bool debug)
- {
- if(tool == null || String.IsNullOrEmpty(component))
- {
- return;
- }
- if(Array.BinarySearch(Util.getCppNames(), component, StringComparer.CurrentCultureIgnoreCase) < 0)
- {
- return;
- }
- string libName = component;
- if(debug)
- {
- libName += "d";
- }
- libName += ".lib";
- libName = libName.ToLower();
- string additionalDependencies = tool.AdditionalDependencies;
- if(String.IsNullOrEmpty(additionalDependencies))
- {
- additionalDependencies = "";
- }
- ComponentList components = new ComponentList(additionalDependencies.Split(' '));
- if(!components.Contains(libName))
- {
- components.Add(libName);
- additionalDependencies = components.ToString(' ');
- tool.AdditionalDependencies = additionalDependencies;
- }
- }
- public static bool removeCppLib(LinkerAdapter tool, string component, bool debug)
- {
- if(tool == null || String.IsNullOrEmpty(tool.AdditionalDependencies))
- {
- return false;
- }
- string libName = component;
- if(debug)
- {
- libName += "d";
- }
- libName += ".lib";
- libName = libName.ToLower();
- ComponentList components = new ComponentList(tool.AdditionalDependencies.Split(' '));
- if(components.Contains(libName))
- {
- components.Remove(libName);
- tool.AdditionalDependencies = components.ToString(' ');
- return true;
- }
- return false;
- }
- //
- // Add the Ice bin path to the debug environment.
- //
- // Note: Only the last setting in the environment has effect.
- //
- public static void addIceCppEnvironment(VCDebugSettings debugSettings, Project project)
- {
- if(debugSettings == null || project == null)
- {
- return;
- }
- String value = "PATH=$(IceBin)";
- if(String.IsNullOrEmpty(debugSettings.Environment))
- {
- debugSettings.Environment = value;
- return;
- }
- if(value.Equals(debugSettings.Environment))
- {
- return;
- }
- if(String.IsNullOrEmpty(debugSettings.Environment))
- {
- debugSettings.Environment = value;
- return;
- }
- ComponentList envs = new ComponentList(debugSettings.Environment, '\n');
- string path = "";
- //
- // Find the last in the list that begins: "PATH=" accounting for case and whitespace.
- //
- int index = -1;
- for(int i = 0; i < envs.Count; ++i)
- {
- string s = envs[i].Trim();
- if(s.StartsWith("PATH", StringComparison.CurrentCultureIgnoreCase))
- {
- if(s.Substring("PATH".Length).Trim().StartsWith("=", StringComparison.CurrentCultureIgnoreCase))
- {
- path = s;
- index = i;
- }
- }
- }
- if(index == -1)
- {
- envs.Add("PATH=$(IceBin)");
- }
- else
- {
- string binDir = "$(IceBin)";
- ComponentList paths = new ComponentList(assignmentValue(path), ';');
- while(paths.Contains(binDir))
- {
- paths.Remove(binDir);
- }
- path = "PATH=" + binDir + Path.PathSeparator + paths.ToString(Path.PathSeparator);
- path = path.TrimEnd(Path.PathSeparator);
- envs[index] = path;
- }
- value = envs.ToString('\n');
- if(!debugSettings.Environment.Equals(value))
- {
- debugSettings.Environment = value;
- }
- return;
- }
- private static string removeFromPath(string path, string dir)
- {
- ComponentList list = new ComponentList(path.Split(Path.PathSeparator));
- while(list.Contains(dir))
- {
- list.Remove(dir);
- }
- return list.ToString(Path.PathSeparator);
- }
- private static string assignmentValue(string expr)
- {
- int i = expr.IndexOf('=');
- if(i < 0)
- {
- return "";
- }
- try
- {
- return expr.Substring(i).Substring(1).Trim();
- }
- catch(ArgumentOutOfRangeException)
- {
- return "";
- }
- }
- private static string prependToPath(string path, string dir)
- {
- path = removeFromPath(path, dir);
- return dir + Path.PathSeparator + path;
- }
- public static string cppBinDir(Project project, CPUType arch)
- {
- #if VS2010 || VS2012
- return isWinRTProject(project) ? "$(IceBin)\\winrt" : "$(IceBin)";
- #else
- string cppBinDir = Path.Combine("$(IceHome)", "bin");
- if(arch == CPUType.x64CPUType)
- {
- cppBinDir = Path.Combine(cppBinDir, "x64");
- }
- return cppBinDir;
- #endif
- }
- public static void removeIceCppEnvironment(VCDebugSettings debugSettings, string iceHome)
- {
- if(debugSettings == null || String.IsNullOrEmpty(debugSettings.Environment))
- {
- return;
- }
- string[] _cppBinDirs =
- {
- "bin",
- "bin\\x64",
- "bin\\vc100",
- "bin\\vc100\\x64",
- "cpp\\bin",
- };
- ComponentList envs = new ComponentList(debugSettings.Environment, '\n');
- /* Find the last in the list that begins: "PATH=" accounting for case and whitespace. */
- string path = "";
- int index = -1;
- for(int i = 0; i < envs.Count; ++i)
- {
- string s = envs[i];
- if(s.StartsWith("PATH", StringComparison.CurrentCultureIgnoreCase))
- {
- if(s.Substring("PATH".Length).Trim().StartsWith("=", StringComparison.CurrentCultureIgnoreCase))
- {
- path = s;
- index = i;
- }
- }
- }
- if(index == -1)
- {
- return;
- }
- foreach(string dir in _cppBinDirs)
- {
- path = "PATH=" + removeFromPath(assignmentValue(path).Trim(), Path.Combine(iceHome, dir));
- }
- #if VS2010 || VS2012
- path = "PATH=" + removeFromPath(assignmentValue(path).Trim(), "$(IceBin)\\winrt");
- path = "PATH=" + removeFromPath(assignmentValue(path).Trim(), "$(IceBin)");
- #endif
- if(path.Equals("PATH="))
- {
- envs.RemoveAt(index);
- }
- else
- {
- envs[index] = path;
- }
- String value = envs.ToString();
- if(!debugSettings.Environment.Equals(value))
- {
- debugSettings.Environment = value;
- }
- return;
- }
- public static void removeIceCppLibraryDir(LinkerAdapter tool, string iceHome)
- {
- if(tool == null || String.IsNullOrEmpty(tool.AdditionalLibraryDirectories))
- {
- return;
- }
- bool changed = false;
- ComponentList libs = new ComponentList(tool.AdditionalLibraryDirectories);
- //
- // Old style lib paths. New configurations use $(IceLib) instead.
- //
- string[] _cppLibDirs =
- {
- "lib",
- "lib\\x64",
- "lib\\vc100",
- "lib\\vc100\\x64",
- };
-
- foreach(string dir in _cppLibDirs)
- {
- if(libs.Remove(quote(Path.Combine(iceHome, dir))) ||
- libs.Remove(Path.Combine(iceHome, dir)))
- {
- changed = true;
- }
- }
- #if VS2010 || VS2012
- if(libs.Remove(quote("$(IceLib)")) ||
- libs.Remove("$(IceLib)") ||
- libs.Remove(quote("$(IceLib)\\winrt")) ||
- libs.Remove("$(IceLib)\\winrt"))
- {
- changed = true;
- }
- #endif
- if(changed)
- {
- tool.AdditionalLibraryDirectories = libs.ToString();
- }
- }
- public static bool isSliceFilename(string s)
- {
- return s != null && s.EndsWith(".ice", StringComparison.CurrentCultureIgnoreCase);
- }
- public static bool equalPath(string p1, string p2, string basePath)
- {
- if(p1 == p2)
- {
- return true;
- }
- if(String.IsNullOrEmpty(p1) || String.IsNullOrEmpty(p2))
- {
- return false;
- }
- //
- // Convert both paths to absolute paths if necessary
- //
- if(!Path.IsPathRooted(p1))
- {
- p1 = Path.Combine(basePath, p1);
- }
- if(!Path.IsPathRooted(p2))
- {
- p2 = Path.Combine(basePath, p2);
- }
- try
- {
- //
- // Note that in Windows white space at the beginning or end of a file are ignored.
- // When comparing the filenames "Foo ", " Foo", and "foo", all refer to the same file.
- //
- // We also need to trim / (directory separator) from the end in case it's present.
- //
- return 0 == String.Compare(
- Path.GetFullPath(p1).Trim().TrimEnd(Path.DirectorySeparatorChar),
- Path.GetFullPath(p2).Trim().TrimEnd(Path.DirectorySeparatorChar),
- StringComparison.CurrentCultureIgnoreCase);
- }
- catch(ArgumentException)
- {
- }
- catch(NotSupportedException)
- {
- }
- catch(System.Security.SecurityException)
- {
- }
- catch(PathTooLongException)
- {
- }
- return false;
- }
- public static bool isSliceBuilderEnabled(Project project)
- {
- try
- {
- if(isCppProject(project) ||
- isCSharpProject(project) ||
- isVBProject(project) ||
- isSilverlightProject(project) ||
- isCSharpSmartDeviceProject(project) ||
- isVBSmartDeviceProject(project))
- {
- return Util.getProjectPropertyAsBool(project, Util.PropertyIce);
- }
- }
- catch(System.NotImplementedException)
- {
- }
- return false;
- }
- public static bool isCSharpProject(Project project)
- {
- if(project == null)
- {
- return false;
- }
- if(String.IsNullOrEmpty(project.Kind))
- {
- return false;
- }
- return project.Kind == VSLangProj.PrjKind.prjKindCSharpProject;
- }
- public static bool isCSharpSmartDeviceProject(Project project)
- {
- return hasProjecType(project, vsSmartDeviceCSharp);
- }
- public static bool isVBSmartDeviceProject(Project project)
- {
- return hasProjecType(project, vsSmartDeviceVB);
- }
- public static bool isVBProject(Project project)
- {
- if(project == null)
- {
- return false;
- }
- if(String.IsNullOrEmpty(project.Kind))
- {
- return false;
- }
- return project.Kind == VSLangProj.PrjKind.prjKindVBProject;
- }
- public static bool isSilverlightProject(Project project)
- {
- if(!Util.isCSharpProject(project))
- {
- return false;
- }
- Array extenders = (Array)project.ExtenderNames;
- foreach(string s in extenders)
- {
- if(String.IsNullOrEmpty(s))
- {
- continue;
- }
- if(s.Equals("SilverlightProject"))
- {
- return true;
- }
- }
- return false;
- }
- public static bool isCppProject(Project project)
- {
- if(project == null)
- {
- return false;
- }
- if(String.IsNullOrEmpty(project.Kind))
- {
- return false;
- }
- return project.Kind == vcContextGuids.vcContextGuidVCProject;
- }
- public static bool isWinRTProject(Project project)
- {
- if(isCppProject(project))
- {
- ConfigurationManager configManager = project.ConfigurationManager;
- Configuration activeConfig = (Configuration)configManager.ActiveConfiguration;
- VCProject vcProject = (VCProject)project.Object;
- IVCCollection configurations = (IVCCollection)vcProject.Configurations;
- foreach(VCConfiguration conf in configurations)
- {
- if(!activeConfig.PlatformName.Equals(((VCPlatform)conf.Platform).Name))
- {
- continue;
- }
- IVCCollection sheets = (IVCCollection)conf.PropertySheets;
- foreach(VCPropertySheet s in sheets)
- {
- if(s.PropertySheetFile.EndsWith("\\Microsoft.Cpp.AppContainerApplication.props"))
- {
- return true;
- }
- }
- return false;
- }
- }
- return false;
- }
- public static bool isProjectItemFolder(ProjectItem item)
- {
- if(item == null)
- {
- return false;
- }
- if(String.IsNullOrEmpty(item.Kind))
- {
- return false;
- }
- return item.Kind == "{6BB5F8EF-4483-11D3-8BCF-00C04F8EC28C}";
- }
- public static bool isProjectItemFilter(ProjectItem item)
- {
- if(item == null)
- {
- return false;
- }
- if(String.IsNullOrEmpty(item.Kind))
- {
- return false;
- }
- return item.Kind == "{6BB5F8F0-4483-11D3-8BCF-00C04F8EC28C}";
- }
- public static bool isProjectItemFile(ProjectItem item)
- {
- if(item == null)
- {
- return false;
- }
- if(String.IsNullOrEmpty(item.Kind))
- {
- return false;
- }
- return item.Kind == "{6BB5F8EE-4483-11D3-8BCF-00C04F8EC28C}";
- }
- public static bool hasItemNamed(ProjectItems items, string name)
- {
- bool found = false;
- foreach(ProjectItem item in items)
- {
- if(item == null)
- {
- continue;
- }
- if(item.Name == null)
- {
- continue;
- }
- if(item.Name.Equals(name, StringComparison.OrdinalIgnoreCase))
- {
- found = true;
- break;
- }
- }
- return found;
- }
- public static Project findProject(VCProject vcProject)
- {
- if(vcProject == null)
- {
- return null;
- }
- Builder builder = Connect.getBuilder();
- DTE dte = builder.getCurrentDTE();
- if(dte == null)
- {
- return null;
- }
- if(dte.Solution == null)
- {
- return null;
- }
- List<Project> projects = Util.getProjects(dte.Solution);
- Project project = null;
- foreach(Project p in projects)
- {
- if(p.Object != null && p.Object.Equals(vcProject))
- {
- project = p;
- break;
- }
- }
- return project;
- }
- public static ProjectItem findItem(string path)
- {
- Builder builder = Connect.getBuilder();
- DTE dte = builder.getCurrentDTE();
- if(dte == null)
- {
- return null;
- }
- if(dte.Solution == null)
- {
- return null;
- }
- List<Project> projects = Util.getProjects(dte.Solution);
- ProjectItem item = null;
- foreach(Project project in projects)
- {
- item = findItem(path, project.ProjectItems);
- if(item != null)
- {
- break;
- }
- }
- return item;
- }
- public static ProjectItem findItem(string path, ProjectItems items)
- {
- if(String.IsNullOrEmpty(path))
- {
- return null;
- }
- ProjectItem item = null;
- foreach(ProjectItem i in items)
- {
- if(i == null)
- {
- continue;
- }
- else if(Util.isProjectItemFile(i))
- {
- string fullPath = i.Properties.Item("FullPath").Value.ToString();
- Project project = i.ContainingProject;
- if(Util.equalPath(fullPath, path, Path.GetDirectoryName(project.FileName)))
- {
- item = i;
- break;
- }
- }
- else if(Util.isProjectItemFolder(i))
- {
- string p = Path.GetDirectoryName(i.Properties.Item("FullPath").Value.ToString());
- Project project = i.ContainingProject;
- if(Util.equalPath(p, path, Path.GetDirectoryName(project.FileName)))
- {
- item = i;
- break;
- }
- item = findItem(path, i.ProjectItems);
- if(item != null)
- {
- break;
- }
- }
- else if(Util.isProjectItemFilter(i))
- {
- string p = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(i.ContainingProject.FileName),
- Util.getPathRelativeToProject(i)));
- Project project = i.ContainingProject;
- if(Util.equalPath(p, path, Path.GetDirectoryName(project.FileName)))
- {
- item = i;
- break;
- }
- item = findItem(path, i.ProjectItems);
- if(item != null)
- {
- break;
- }
- }
- }
- return item;
- }
- public static VCFile findVCFile(IVCCollection files, string name, string fullPath)
- {
- VCFile vcFile = null;
- foreach(VCFile file in files)
- {
- if(file.ItemName == name)
- {
- Project project = (Project)((VCProject) file.project).Object;
- if(!Util.equalPath(file.FullPath, fullPath, Path.GetDirectoryName(project.FileName)))
- {
- file.Remove();
- break;
- }
- vcFile = file;
- break;
- }
- }
- return vcFile;
- }
- public static string relativePath(Project project, string absoluteFilePath)
- {
- if(absoluteFilePath == null)
- {
- return "";
- }
- if(project == null)
- {
- return absoluteFilePath;
- }
- return relativePath(Path.GetDirectoryName(project.FileName), absoluteFilePath);
- }
- public static string relativePath(string mainDirPath, string absoluteFilePath)
- {
- if(absoluteFilePath == null)
- {
- return "";
- }
- if(mainDirPath == null)
- {
- return absoluteFilePath;
- }
- if(!Path.IsPathRooted(absoluteFilePath))
- {
- return absoluteFilePath;
- }
- mainDirPath = Path.GetFullPath(mainDirPath).Trim(Path.DirectorySeparatorChar);
- absoluteFilePath = Path.GetFullPath(absoluteFilePath).Trim(Path.DirectorySeparatorChar);
- string[] firstPathParts = mainDirPath.Split(Path.DirectorySeparatorChar);
- string[] secondPathParts = absoluteFilePath.Split(Path.DirectorySeparatorChar);
- int sameCounter = 0;
- while(sameCounter < Math.Min(firstPathParts.Length, secondPathParts.Length) &&
- String.Equals(firstPathParts[sameCounter], secondPathParts[sameCounter],
- StringComparison.CurrentCultureIgnoreCase))
- {
- ++sameCounter;
- }
- // Different volumes, relative path not possible.
- if(sameCounter == 0)
- {
- return absoluteFilePath;
- }
- // Pop back up to the common point.
- string newPath = "." + Path.DirectorySeparatorChar;
- for(int i = sameCounter; i < firstPathParts.Length; ++i)
- {
- newPath += ".." + Path.DirectorySeparatorChar;
- }
- // Descend to the target.
- for(int i = sameCounter; i < secondPathParts.Length; ++i)
- {
- newPath += secondPathParts[i] + Path.DirectorySeparatorChar;
- }
- return newPath.TrimEnd(Path.DirectorySeparatorChar);
- }
- // Relative paths are relative to project.
- // Inverse of Util.relativePath().
- public static string absolutePath(Project project, string path)
- {
- if(Path.IsPathRooted(path)) // If path is absolute return that path
- {
- return path;
- }
- return Path.GetFullPath(Path.Combine(Path.GetDirectoryName(project.FileName), path)).TrimEnd(
- Path.DirectorySeparatorChar);
- }
- public static Project getSelectedProject(_DTE dte)
- {
- Microsoft.VisualStudio.Shell.ServiceProvider sp = new Microsoft.VisualStudio.Shell.ServiceProvider(
- getCurrentDTE() as Microsoft.VisualStudio.OLE.Interop.IServiceProvider);
- IVsMonitorSelection selectionMonitor = sp.GetService(typeof(IVsMonitorSelection)) as IVsMonitorSelection;
- //
- // There isn't an open project.
- //
- if(selectionMonitor == null)
- {
- return null;
- }
- Project project = null;
- IntPtr ppHier;
- uint pitemid;
- IVsMultiItemSelect ppMIS;
- IntPtr ppSC;
- if(ErrorHandler.Failed(selectionMonitor.GetCurrentSelection(out ppHier, out pitemid, out ppMIS, out ppSC)))
- {
- return null;
- }
- if(ppHier != IntPtr.Zero)
- {
- IVsHierarchy hier = (IVsHierarchy)Marshal.GetObjectForIUnknown(ppHier);
- Marshal.Release(ppHier);
- object obj;
- hier.GetProperty(VSConstants.VSITEMID_ROOT, (int)__VSHPROPID.VSHPROPID_ExtObject, out obj);
- if(obj != null)
- {
- project = obj as EnvDTE.Project;
- }
- }
- if(ppSC != IntPtr.Zero)
- {
- Marshal.Release(ppSC);
- }
- return project;
- }
- public static ProjectItem getSelectedProjectItem(_DTE dte)
- {
- Microsoft.VisualStudio.Shell.ServiceProvider sp = new Microsoft.VisualStudio.Shell.ServiceProvider(
- getCurrentDTE() as Microsoft.VisualStudio.OLE.Interop.IServiceProvider);
- IVsMonitorSelection selectionMonitor = sp.GetService(typeof(IVsMonitorSelection)) as IVsMonitorSelection;
- //
- // There isn't an open project.
- //
- if(selectionMonitor == null)
- {
- return null;
- }
- ProjectItem projectItem = null;
- IntPtr ppHier;
- uint pitemid;
- IVsMultiItemSelect ppMIS;
- IntPtr ppSC;
- if(ErrorHandler.Failed(selectionMonitor.GetCurrentSelection(out ppHier, out pitemid, out ppMIS, out ppSC)))
- {
- return null;
- }
- if(ppHier != IntPtr.Zero)
- {
- IVsHierarchy hier = (IVsHierarchy)Marshal.GetObjectForIUnknown(ppHier);
- Marshal.Release(ppHier);
- object obj;
- hier.GetProperty(pitemid, (int)__VSHPROPID.VSHPROPID_ExtObject, out obj);
- if(obj != null)
- {
- projectItem = obj as EnvDTE.ProjectItem;
- }
- }
- if(ppSC != IntPtr.Zero)
- {
- Mars…
Large files files are truncated, but you can click here to view the full file