PageRenderTime 56ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/vsaddin/src/Util.cs

https://bitbucket.org/cleto/zeroc-ice-package
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

  1. // **********************************************************************
  2. //
  3. // Copyright (c) 2003-2013 ZeroC, Inc. All rights reserved.
  4. //
  5. // This copy of Ice is licensed to you under the terms described in the
  6. // ICE_LICENSE file included in this distribution.
  7. //
  8. // **********************************************************************
  9. using System;
  10. using System.Text;
  11. using System.Collections.Generic;
  12. using System.ComponentModel;
  13. using EnvDTE;
  14. using System.Windows.Forms;
  15. using System.Runtime.InteropServices;
  16. using System.IO;
  17. using System.Diagnostics;
  18. using Extensibility;
  19. using EnvDTE80;
  20. using Microsoft.VisualStudio;
  21. using Microsoft.VisualStudio.CommandBars;
  22. using Microsoft.VisualStudio.VCProjectEngine;
  23. using Microsoft.VisualStudio.VCProject;
  24. using Microsoft.VisualStudio.Shell;
  25. using Microsoft.VisualStudio.Shell.Interop;
  26. using System.Resources;
  27. using System.Reflection;
  28. using VSLangProj;
  29. using System.Globalization;
  30. using System.Collections;
  31. using System.Runtime.InteropServices.ComTypes;
  32. using Microsoft.CSharp;
  33. using System.Xml;
  34. namespace Ice.VisualStudio
  35. {
  36. public class InitializationException : System.Exception
  37. {
  38. public InitializationException(string message) : base(message)
  39. {
  40. }
  41. }
  42. public enum CPUType
  43. {
  44. x86CPUType,
  45. x64CPUType,
  46. ARMCPUType
  47. }
  48. public interface LinkerAdapter
  49. {
  50. String AdditionalDependencies
  51. {
  52. get;
  53. set;
  54. }
  55. String AdditionalLibraryDirectories
  56. {
  57. get;
  58. set;
  59. }
  60. }
  61. public class DynamicLinkerAdapter : LinkerAdapter
  62. {
  63. public DynamicLinkerAdapter(VCLinkerTool linkerTool)
  64. {
  65. _linkerTool = linkerTool;
  66. }
  67. public String AdditionalDependencies
  68. {
  69. get
  70. {
  71. return _linkerTool.AdditionalDependencies;
  72. }
  73. set
  74. {
  75. _linkerTool.AdditionalDependencies = value;
  76. }
  77. }
  78. public String AdditionalLibraryDirectories
  79. {
  80. get
  81. {
  82. return _linkerTool.AdditionalLibraryDirectories;
  83. }
  84. set
  85. {
  86. _linkerTool.AdditionalLibraryDirectories = value;
  87. }
  88. }
  89. private VCLinkerTool _linkerTool;
  90. }
  91. public class StaticLinkerAdapter : LinkerAdapter
  92. {
  93. public StaticLinkerAdapter(VCLibrarianTool librarianTool)
  94. {
  95. _librarianTool = librarianTool;
  96. }
  97. public String AdditionalDependencies
  98. {
  99. get
  100. {
  101. return "";
  102. }
  103. set
  104. {
  105. }
  106. }
  107. public String AdditionalLibraryDirectories
  108. {
  109. get
  110. {
  111. return "";
  112. }
  113. set
  114. {
  115. }
  116. }
  117. private VCLibrarianTool _librarianTool;
  118. }
  119. public class ComponentList : List<string>
  120. {
  121. public ComponentList()
  122. {
  123. }
  124. public ComponentList(string[] values)
  125. {
  126. foreach(string s in values)
  127. {
  128. Add(s);
  129. }
  130. }
  131. public ComponentList(string value)
  132. {
  133. if(value == null)
  134. {
  135. value = "";
  136. }
  137. init(value, ';');
  138. }
  139. public ComponentList(string value, char separator)
  140. {
  141. init(value, separator);
  142. }
  143. public new void Add(string value)
  144. {
  145. value = value.Trim();
  146. if(String.IsNullOrEmpty(value))
  147. {
  148. return;
  149. }
  150. if(!base.Contains(value))
  151. {
  152. base.Add(value);
  153. }
  154. }
  155. public new bool Contains(string value)
  156. {
  157. string v = base.Find(delegate(string s)
  158. {
  159. return s.Equals(value.Trim(), StringComparison.CurrentCultureIgnoreCase);
  160. });
  161. return v != null;
  162. }
  163. public new bool Remove(string value)
  164. {
  165. value = value.Trim();
  166. //
  167. // To do the remove case insensitive, first find the value
  168. // doing case insensitive comparison and then remove that.
  169. //
  170. string v = base.Find(delegate(string s)
  171. {
  172. return s.Equals(value.Trim(), StringComparison.CurrentCultureIgnoreCase);
  173. });
  174. if(v != null)
  175. {
  176. return base.Remove(v);
  177. }
  178. return false;
  179. }
  180. private void init(string value, char separator)
  181. {
  182. Array items = value.Split(separator);
  183. foreach(string s in items)
  184. {
  185. Add(s);
  186. }
  187. }
  188. public override string ToString()
  189. {
  190. return ToString(';');
  191. }
  192. public string ToString(char separator)
  193. {
  194. if(this.Count == 0)
  195. {
  196. return "";
  197. }
  198. StringBuilder sb = new StringBuilder();
  199. for(int cont = 0; cont < this.Count; ++cont)
  200. {
  201. sb.Append(this[cont]);
  202. if(cont < this.Count - 1)
  203. {
  204. sb.Append(separator);
  205. }
  206. }
  207. return sb.ToString();
  208. }
  209. public bool Equal(ComponentList other)
  210. {
  211. if(this.Count != other.Count)
  212. {
  213. return false;
  214. }
  215. bool equal = true;
  216. for(int i = 0; i < this.Count; ++i)
  217. {
  218. string val1 = this[i];
  219. string val2 = other[i];
  220. if(val1 == null && val2 == null)
  221. {
  222. continue;
  223. }
  224. if(val1 == null || val2 == null)
  225. {
  226. equal = false;
  227. break;
  228. }
  229. if(!val1.Equals(val2, StringComparison.CurrentCultureIgnoreCase))
  230. {
  231. equal = false;
  232. break;
  233. }
  234. }
  235. return equal;
  236. }
  237. }
  238. public class IncludePathList : ComponentList
  239. {
  240. public IncludePathList()
  241. : base()
  242. {
  243. }
  244. public IncludePathList(string[] values)
  245. : base(values)
  246. {
  247. }
  248. public IncludePathList(string value)
  249. : base(value, '|')
  250. {
  251. }
  252. public override string ToString()
  253. {
  254. return base.ToString('|');
  255. }
  256. public bool Contains(Project project, string value)
  257. {
  258. string path = Util.absolutePath(project, value);
  259. string found = base.Find(delegate(string s)
  260. {
  261. string other = Util.absolutePath(project, s);
  262. return path.Equals(other, StringComparison.CurrentCultureIgnoreCase);
  263. });
  264. return found != null;
  265. }
  266. }
  267. public static class Util
  268. {
  269. public enum msgLevel{ msgError, msgInfo, msgDebug };
  270. public static string MajorVersion
  271. {
  272. get
  273. {
  274. if (_majorVersion == null)
  275. {
  276. string[] tokens = Assembly.GetExecutingAssembly().GetName().Version.ToString().Split('.');
  277. Debug.Assert(tokens.Length > 1);
  278. _majorVersion = tokens[0];
  279. }
  280. return _majorVersion;
  281. }
  282. }
  283. private static string _majorVersion = null;
  284. public static string MinorVersion
  285. {
  286. get
  287. {
  288. if (_minorVersion == null)
  289. {
  290. string[] tokens = Assembly.GetExecutingAssembly().GetName().Version.ToString().Split('.');
  291. Debug.Assert(tokens.Length > 1);
  292. _minorVersion = tokens[1];
  293. }
  294. return _minorVersion;
  295. }
  296. }
  297. private static string _minorVersion = null;
  298. public const string ProjectVersion = "1";
  299. public const string slice2cs = "slice2cs.exe";
  300. public const string slice2cpp = "slice2cpp.exe";
  301. //
  302. // Property names used to persist project configuration.
  303. //
  304. public const string PropertyIce = "ZerocIce_Enabled";
  305. public const string PropertyIceOutputDir = "ZerocIce_OutputDir";
  306. public const string PropertyIceHeaderExt = "ZerocIce_HeaderExt";
  307. public const string PropertyIceSourceExt = "ZerocIce_SourceExt";
  308. public const string PropertyIceComponents = "ZerocIce_Components";
  309. public const string PropertyIceExtraOptions = "ZerocIce_ExtraOptions";
  310. public const string PropertyIceIncludePath = "ZerocIce_IncludePath";
  311. public const string PropertyIceStreaming = "ZerocIce_Streaming";
  312. public const string PropertyIceChecksum = "ZerocIce_Checksum";
  313. public const string PropertyIceTie = "ZerocIce_Tie";
  314. public const string PropertyIcePrefix = "ZerocIce_Prefix";
  315. public const string PropertyIceDllExport = "ZerocIce_DllExport";
  316. public const string PropertyVerboseLevel = "ZerocIce_VerboseLevel";
  317. public const string PropertyProjectVersion = "ZerocIce_ProjectVersion";
  318. private static readonly string[] silverlightNames =
  319. {
  320. "Glacier2", "Ice", "IceGrid", "IcePatch2",
  321. "IceStorm"
  322. };
  323. public static string[] getSilverlightNames()
  324. {
  325. return (string[])silverlightNames.Clone();
  326. }
  327. private static readonly string[] cppNames =
  328. {
  329. "Freeze", "Glacier2", "Ice", "IceBox", "IceGrid", "IcePatch2",
  330. "IceSSL", "IceStorm", "IceUtil"
  331. };
  332. public static string[] getCppNames()
  333. {
  334. return (string[])cppNames.Clone();
  335. }
  336. private static readonly string[] dotNetNames =
  337. {
  338. "Glacier2", "Ice", "IceBox", "IceGrid", "IcePatch2",
  339. "IceSSL", "IceStorm"
  340. };
  341. private static readonly string[] dotNetCompactNames =
  342. {
  343. "Glacier2", "Ice", "IceBox", "IceGrid", "IcePatch2",
  344. "IceStorm"
  345. };
  346. public static string[] getDotNetCompactNames()
  347. {
  348. return (string[])dotNetCompactNames.Clone();
  349. }
  350. public static string[] getDotNetNames()
  351. {
  352. return (string[])dotNetNames.Clone();
  353. }
  354. public static string getIceHome()
  355. {
  356. string iceSourceHome = System.Environment.GetEnvironmentVariable("IceSourceHome");
  357. if (iceSourceHome != null && System.IO.Directory.Exists(iceSourceHome) &&
  358. System.IO.File.Exists(Path.Combine(iceSourceHome, Path.Combine("cpp", Path.Combine("bin", slice2cpp)))))
  359. {
  360. return iceSourceHome;
  361. }
  362. else
  363. {
  364. string defaultIceHome = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
  365. if (defaultIceHome.EndsWith("\\vsaddin", StringComparison.CurrentCultureIgnoreCase))
  366. {
  367. defaultIceHome = defaultIceHome.Substring(0, defaultIceHome.Length - "\\vsaddin".Length);
  368. }
  369. else if (defaultIceHome.EndsWith("\\vsaddin\\bin", StringComparison.CurrentCultureIgnoreCase))
  370. {
  371. defaultIceHome = defaultIceHome.Substring(0, defaultIceHome.Length - "\\vsaddin\bin".Length);
  372. }
  373. return defaultIceHome;
  374. }
  375. }
  376. public static string getProjectOutputDirRaw(Project project)
  377. {
  378. return getProjectProperty(project, Util.PropertyIceOutputDir, "", false);
  379. }
  380. public static string getProjectOutputDir(Project project)
  381. {
  382. String path = getProjectOutputDirRaw(project);
  383. if(containsEnvironmentVars(path))
  384. {
  385. path = expandEnvironmentVars(path);
  386. }
  387. return path;
  388. }
  389. public static string getProjectAbsoluteOutputDir(Project project)
  390. {
  391. String path = getProjectOutputDir(project);
  392. if(!Path.IsPathRooted(path))
  393. {
  394. path = Util.absolutePath(project, path);
  395. }
  396. return path;
  397. }
  398. public static VCPropertySheet icePropertySheet(Project project)
  399. {
  400. VCPropertySheet sheet = null;
  401. if(isCppProject(project))
  402. {
  403. VCConfiguration configuration = getActiveVCConfiguration(project);
  404. if(configuration != null)
  405. {
  406. sheet = findPropertySheet(((IVCCollection)configuration.PropertySheets), "ice");
  407. }
  408. }
  409. return sheet;
  410. }
  411. public static VCUserMacro userMacro(Project project, String name)
  412. {
  413. VCPropertySheet sheet = icePropertySheet(project);
  414. VCUserMacro macro = null;
  415. foreach(VCUserMacro m in sheet.UserMacros)
  416. {
  417. if(m.Name.Equals(name))
  418. {
  419. macro = m;
  420. break;
  421. }
  422. }
  423. return macro;
  424. }
  425. public static VCPropertySheet findPropertySheet(IVCCollection propertySheets, string sheetName)
  426. {
  427. VCPropertySheet value = null;
  428. foreach(VCPropertySheet sheet in propertySheets)
  429. {
  430. if(sheet == null || String.IsNullOrEmpty(sheet.Name))
  431. {
  432. continue;
  433. }
  434. if(sheet.Name == sheetName)
  435. {
  436. value = sheet;
  437. break;
  438. }
  439. }
  440. return value;
  441. }
  442. //
  443. // This will add the Ice property sheet to the project.
  444. //
  445. public static void addIcePropertySheet(Project project)
  446. {
  447. VCProject vcProj = (VCProject)project.Object;
  448. string propSheetFileName = "$(ALLUSERSPROFILE)\\ZeroC\\Ice";
  449. #if VS2008
  450. propSheetFileName += ".vsprops";
  451. #endif
  452. #if VS2010 || VS2012
  453. propSheetFileName += ".props";
  454. #endif
  455. //
  456. // All project configurations must include ice.vsprops (vc90) or IceCommon.props (vc100 | vc110)
  457. //
  458. IVCCollection configurations = (IVCCollection)vcProj.Configurations;
  459. string[] cppComponents = Util.getCppNames();
  460. foreach(VCConfiguration vcConfig in configurations)
  461. {
  462. VCPropertySheet newSheet = findPropertySheet(vcConfig.PropertySheets as IVCCollection, "ice");
  463. if(newSheet == null)
  464. {
  465. #if VS2008
  466. string inhertiedPropertySheets = vcConfig.InheritedPropertySheets;
  467. if(String.IsNullOrEmpty(inhertiedPropertySheets) || !inhertiedPropertySheets.Contains(propSheetFileName))
  468. {
  469. if(!String.IsNullOrEmpty(inhertiedPropertySheets) && !inhertiedPropertySheets.EndsWith(";"))
  470. {
  471. inhertiedPropertySheets += " ; ";
  472. }
  473. inhertiedPropertySheets += propSheetFileName;
  474. vcConfig.InheritedPropertySheets = inhertiedPropertySheets;
  475. }
  476. #endif
  477. #if VS2010 || VS2012
  478. newSheet = vcConfig.AddPropertySheet(propSheetFileName);
  479. #endif
  480. }
  481. }
  482. }
  483. public static string getCsBinDir(Project project)
  484. {
  485. string binDir = "";
  486. if(isVBSmartDeviceProject(project) || isCSharpSmartDeviceProject(project))
  487. {
  488. binDir = _csCompactFrameworkBinDirs;
  489. }
  490. else if(isSilverlightProject(project))
  491. {
  492. binDir = _slBinDirs;
  493. }
  494. else
  495. {
  496. binDir = _csBinDirs;
  497. }
  498. return Path.Combine(Util.getIceHome(), binDir);
  499. }
  500. public static string getPathRelativeToProject(ProjectItem item)
  501. {
  502. StringBuilder path = new StringBuilder();
  503. if(item != null)
  504. {
  505. path.Append(Util.getPathRelativeToProject(item, item.ContainingProject.ProjectItems));
  506. }
  507. return path.ToString();
  508. }
  509. public static string getPathRelativeToProject(ProjectItem item, ProjectItems items)
  510. {
  511. StringBuilder path = new StringBuilder();
  512. foreach(ProjectItem i in items)
  513. {
  514. if(i == item)
  515. {
  516. if(path.Length > 0)
  517. {
  518. path.Append("\\");
  519. }
  520. path.Append(i.Name);
  521. break;
  522. }
  523. else if(Util.isProjectItemFilter(i) || Util.isProjectItemFolder(i))
  524. {
  525. string token = Util.getPathRelativeToProject(item, i.ProjectItems);
  526. if(!String.IsNullOrEmpty(token))
  527. {
  528. path.Append(i.Name);
  529. path.Append("\\");
  530. path.Append(token);
  531. break;
  532. }
  533. }
  534. }
  535. return path.ToString();
  536. }
  537. public static bool addCppIncludes(VCCLCompilerTool tool, Project project)
  538. {
  539. if(tool == null || project == null)
  540. {
  541. return false;
  542. }
  543. bool changed = false;
  544. ComponentList includes = new ComponentList(tool.AdditionalIncludeDirectories);
  545. string outputDir = getProjectOutputDirRaw(project);
  546. if(outputDir.Equals(""))
  547. {
  548. outputDir = ".";
  549. }
  550. if(!includes.Contains(outputDir) && !includes.Contains(quote(outputDir)))
  551. {
  552. changed = true;
  553. includes.Add(outputDir);
  554. }
  555. if(changed)
  556. {
  557. tool.AdditionalIncludeDirectories = includes.ToString();
  558. }
  559. return changed;
  560. }
  561. public static void removeCppIncludes(VCCLCompilerTool tool, string iceHome, string generatedDir)
  562. {
  563. if(tool == null || String.IsNullOrEmpty(tool.AdditionalIncludeDirectories))
  564. {
  565. return;
  566. }
  567. bool changed = false;
  568. ComponentList includes = new ComponentList(tool.AdditionalIncludeDirectories);
  569. if(includes.Remove(quote(iceHome + "\\include")) || includes.Remove(iceHome + "\\include"))
  570. {
  571. changed = true;
  572. }
  573. if(!generatedDir.Equals("."))
  574. {
  575. if(includes.Remove(generatedDir))
  576. {
  577. changed = true;
  578. }
  579. }
  580. if(changed)
  581. {
  582. tool.AdditionalIncludeDirectories = includes.ToString();
  583. }
  584. }
  585. public static void removeIcePropertySheet(VCConfiguration configuration)
  586. {
  587. if(configuration == null)
  588. {
  589. return;
  590. }
  591. #if VS2008
  592. ComponentList sheets = new ComponentList(configuration.InheritedPropertySheets);
  593. if(sheets.Remove("$(ALLUSERSPROFILE)\\ZeroC\\ice.vsprops"))
  594. {
  595. configuration.InheritedPropertySheets = sheets.ToString();
  596. }
  597. #endif
  598. #if VS2010 || VS2012
  599. VCPropertySheet sheet = null;
  600. IVCCollection sheets = (IVCCollection)configuration.PropertySheets;
  601. foreach(VCPropertySheet s in sheets)
  602. {
  603. if(!s.PropertySheetFile.Equals(configuration.Evaluate("$(ALLUSERSPROFILE)\\ZeroC\\Ice.props"),
  604. StringComparison.CurrentCultureIgnoreCase))
  605. {
  606. continue;
  607. }
  608. sheet = s;
  609. break;
  610. }
  611. if(sheet != null)
  612. {
  613. configuration.RemovePropertySheet(sheet);
  614. }
  615. #endif
  616. }
  617. private static readonly string _csBinDirs = "\\Assemblies\\";
  618. private static readonly string _csCompactFrameworkBinDirs = "\\Assemblies\\cf\\";
  619. private static readonly string _slBinDirs = "\\Assemblies\\sl\\";
  620. public static bool addDotNetReference(Project project, string component, bool development)
  621. {
  622. if(project == null || String.IsNullOrEmpty(component))
  623. {
  624. return false;
  625. }
  626. VSLangProj.VSProject vsProject = (VSLangProj.VSProject)project.Object;
  627. try
  628. {
  629. Reference r = vsProject.References.Add(component + ".dll");
  630. if (development)
  631. {
  632. r.CopyLocal = false;
  633. }
  634. return true;
  635. }
  636. catch (COMException ex)
  637. {
  638. Console.WriteLine(ex);
  639. }
  640. MessageBox.Show("Could not locate '" + component + ".dll'.",
  641. "Ice Visual Studio Add-in", MessageBoxButtons.OK,
  642. MessageBoxIcon.Error,
  643. MessageBoxDefaultButton.Button1,
  644. (MessageBoxOptions)0);
  645. return false;
  646. }
  647. public static bool removeDotNetReference(Project project, string component)
  648. {
  649. if(project == null || String.IsNullOrEmpty(component))
  650. {
  651. return false;
  652. }
  653. foreach(Reference r in ((VSProject)project.Object).References)
  654. {
  655. if(r.Name.Equals(component, StringComparison.OrdinalIgnoreCase))
  656. {
  657. r.Remove();
  658. return true;
  659. }
  660. }
  661. return false;
  662. }
  663. #if VS2012
  664. public static void addSdkReference(VCProject project, string component)
  665. {
  666. string sdkId = component + ", Version=" + Util.MajorVersion + "." + Util.MinorVersion;
  667. VCReference reference = (VCReference)((VCReferences)project.VCReferences).Item(sdkId);
  668. if(reference != null)
  669. {
  670. reference.Remove();
  671. }
  672. project.AddSdkReference(sdkId);
  673. }
  674. public static bool removeSdkReference(VCProject project, string component)
  675. {
  676. string sdkId = component + ", Version=" + Util.MajorVersion + "." + Util.MinorVersion;
  677. VCReference reference = (VCReference)((VCReferences)project.VCReferences).Item(sdkId);
  678. if (reference != null)
  679. {
  680. reference.Remove();
  681. return true;
  682. }
  683. return false;
  684. }
  685. #endif
  686. public static void addCppLib(LinkerAdapter tool, string component, bool debug)
  687. {
  688. if(tool == null || String.IsNullOrEmpty(component))
  689. {
  690. return;
  691. }
  692. if(Array.BinarySearch(Util.getCppNames(), component, StringComparer.CurrentCultureIgnoreCase) < 0)
  693. {
  694. return;
  695. }
  696. string libName = component;
  697. if(debug)
  698. {
  699. libName += "d";
  700. }
  701. libName += ".lib";
  702. libName = libName.ToLower();
  703. string additionalDependencies = tool.AdditionalDependencies;
  704. if(String.IsNullOrEmpty(additionalDependencies))
  705. {
  706. additionalDependencies = "";
  707. }
  708. ComponentList components = new ComponentList(additionalDependencies.Split(' '));
  709. if(!components.Contains(libName))
  710. {
  711. components.Add(libName);
  712. additionalDependencies = components.ToString(' ');
  713. tool.AdditionalDependencies = additionalDependencies;
  714. }
  715. }
  716. public static bool removeCppLib(LinkerAdapter tool, string component, bool debug)
  717. {
  718. if(tool == null || String.IsNullOrEmpty(tool.AdditionalDependencies))
  719. {
  720. return false;
  721. }
  722. string libName = component;
  723. if(debug)
  724. {
  725. libName += "d";
  726. }
  727. libName += ".lib";
  728. libName = libName.ToLower();
  729. ComponentList components = new ComponentList(tool.AdditionalDependencies.Split(' '));
  730. if(components.Contains(libName))
  731. {
  732. components.Remove(libName);
  733. tool.AdditionalDependencies = components.ToString(' ');
  734. return true;
  735. }
  736. return false;
  737. }
  738. //
  739. // Add the Ice bin path to the debug environment.
  740. //
  741. // Note: Only the last setting in the environment has effect.
  742. //
  743. public static void addIceCppEnvironment(VCDebugSettings debugSettings, Project project)
  744. {
  745. if(debugSettings == null || project == null)
  746. {
  747. return;
  748. }
  749. String value = "PATH=$(IceBin)";
  750. if(String.IsNullOrEmpty(debugSettings.Environment))
  751. {
  752. debugSettings.Environment = value;
  753. return;
  754. }
  755. if(value.Equals(debugSettings.Environment))
  756. {
  757. return;
  758. }
  759. if(String.IsNullOrEmpty(debugSettings.Environment))
  760. {
  761. debugSettings.Environment = value;
  762. return;
  763. }
  764. ComponentList envs = new ComponentList(debugSettings.Environment, '\n');
  765. string path = "";
  766. //
  767. // Find the last in the list that begins: "PATH=" accounting for case and whitespace.
  768. //
  769. int index = -1;
  770. for(int i = 0; i < envs.Count; ++i)
  771. {
  772. string s = envs[i].Trim();
  773. if(s.StartsWith("PATH", StringComparison.CurrentCultureIgnoreCase))
  774. {
  775. if(s.Substring("PATH".Length).Trim().StartsWith("=", StringComparison.CurrentCultureIgnoreCase))
  776. {
  777. path = s;
  778. index = i;
  779. }
  780. }
  781. }
  782. if(index == -1)
  783. {
  784. envs.Add("PATH=$(IceBin)");
  785. }
  786. else
  787. {
  788. string binDir = "$(IceBin)";
  789. ComponentList paths = new ComponentList(assignmentValue(path), ';');
  790. while(paths.Contains(binDir))
  791. {
  792. paths.Remove(binDir);
  793. }
  794. path = "PATH=" + binDir + Path.PathSeparator + paths.ToString(Path.PathSeparator);
  795. path = path.TrimEnd(Path.PathSeparator);
  796. envs[index] = path;
  797. }
  798. value = envs.ToString('\n');
  799. if(!debugSettings.Environment.Equals(value))
  800. {
  801. debugSettings.Environment = value;
  802. }
  803. return;
  804. }
  805. private static string removeFromPath(string path, string dir)
  806. {
  807. ComponentList list = new ComponentList(path.Split(Path.PathSeparator));
  808. while(list.Contains(dir))
  809. {
  810. list.Remove(dir);
  811. }
  812. return list.ToString(Path.PathSeparator);
  813. }
  814. private static string assignmentValue(string expr)
  815. {
  816. int i = expr.IndexOf('=');
  817. if(i < 0)
  818. {
  819. return "";
  820. }
  821. try
  822. {
  823. return expr.Substring(i).Substring(1).Trim();
  824. }
  825. catch(ArgumentOutOfRangeException)
  826. {
  827. return "";
  828. }
  829. }
  830. private static string prependToPath(string path, string dir)
  831. {
  832. path = removeFromPath(path, dir);
  833. return dir + Path.PathSeparator + path;
  834. }
  835. public static string cppBinDir(Project project, CPUType arch)
  836. {
  837. #if VS2010 || VS2012
  838. return isWinRTProject(project) ? "$(IceBin)\\winrt" : "$(IceBin)";
  839. #else
  840. string cppBinDir = Path.Combine("$(IceHome)", "bin");
  841. if(arch == CPUType.x64CPUType)
  842. {
  843. cppBinDir = Path.Combine(cppBinDir, "x64");
  844. }
  845. return cppBinDir;
  846. #endif
  847. }
  848. public static void removeIceCppEnvironment(VCDebugSettings debugSettings, string iceHome)
  849. {
  850. if(debugSettings == null || String.IsNullOrEmpty(debugSettings.Environment))
  851. {
  852. return;
  853. }
  854. string[] _cppBinDirs =
  855. {
  856. "bin",
  857. "bin\\x64",
  858. "bin\\vc100",
  859. "bin\\vc100\\x64",
  860. "cpp\\bin",
  861. };
  862. ComponentList envs = new ComponentList(debugSettings.Environment, '\n');
  863. /* Find the last in the list that begins: "PATH=" accounting for case and whitespace. */
  864. string path = "";
  865. int index = -1;
  866. for(int i = 0; i < envs.Count; ++i)
  867. {
  868. string s = envs[i];
  869. if(s.StartsWith("PATH", StringComparison.CurrentCultureIgnoreCase))
  870. {
  871. if(s.Substring("PATH".Length).Trim().StartsWith("=", StringComparison.CurrentCultureIgnoreCase))
  872. {
  873. path = s;
  874. index = i;
  875. }
  876. }
  877. }
  878. if(index == -1)
  879. {
  880. return;
  881. }
  882. foreach(string dir in _cppBinDirs)
  883. {
  884. path = "PATH=" + removeFromPath(assignmentValue(path).Trim(), Path.Combine(iceHome, dir));
  885. }
  886. #if VS2010 || VS2012
  887. path = "PATH=" + removeFromPath(assignmentValue(path).Trim(), "$(IceBin)\\winrt");
  888. path = "PATH=" + removeFromPath(assignmentValue(path).Trim(), "$(IceBin)");
  889. #endif
  890. if(path.Equals("PATH="))
  891. {
  892. envs.RemoveAt(index);
  893. }
  894. else
  895. {
  896. envs[index] = path;
  897. }
  898. String value = envs.ToString();
  899. if(!debugSettings.Environment.Equals(value))
  900. {
  901. debugSettings.Environment = value;
  902. }
  903. return;
  904. }
  905. public static void removeIceCppLibraryDir(LinkerAdapter tool, string iceHome)
  906. {
  907. if(tool == null || String.IsNullOrEmpty(tool.AdditionalLibraryDirectories))
  908. {
  909. return;
  910. }
  911. bool changed = false;
  912. ComponentList libs = new ComponentList(tool.AdditionalLibraryDirectories);
  913. //
  914. // Old style lib paths. New configurations use $(IceLib) instead.
  915. //
  916. string[] _cppLibDirs =
  917. {
  918. "lib",
  919. "lib\\x64",
  920. "lib\\vc100",
  921. "lib\\vc100\\x64",
  922. };
  923. foreach(string dir in _cppLibDirs)
  924. {
  925. if(libs.Remove(quote(Path.Combine(iceHome, dir))) ||
  926. libs.Remove(Path.Combine(iceHome, dir)))
  927. {
  928. changed = true;
  929. }
  930. }
  931. #if VS2010 || VS2012
  932. if(libs.Remove(quote("$(IceLib)")) ||
  933. libs.Remove("$(IceLib)") ||
  934. libs.Remove(quote("$(IceLib)\\winrt")) ||
  935. libs.Remove("$(IceLib)\\winrt"))
  936. {
  937. changed = true;
  938. }
  939. #endif
  940. if(changed)
  941. {
  942. tool.AdditionalLibraryDirectories = libs.ToString();
  943. }
  944. }
  945. public static bool isSliceFilename(string s)
  946. {
  947. return s != null && s.EndsWith(".ice", StringComparison.CurrentCultureIgnoreCase);
  948. }
  949. public static bool equalPath(string p1, string p2, string basePath)
  950. {
  951. if(p1 == p2)
  952. {
  953. return true;
  954. }
  955. if(String.IsNullOrEmpty(p1) || String.IsNullOrEmpty(p2))
  956. {
  957. return false;
  958. }
  959. //
  960. // Convert both paths to absolute paths if necessary
  961. //
  962. if(!Path.IsPathRooted(p1))
  963. {
  964. p1 = Path.Combine(basePath, p1);
  965. }
  966. if(!Path.IsPathRooted(p2))
  967. {
  968. p2 = Path.Combine(basePath, p2);
  969. }
  970. try
  971. {
  972. //
  973. // Note that in Windows white space at the beginning or end of a file are ignored.
  974. // When comparing the filenames "Foo ", " Foo", and "foo", all refer to the same file.
  975. //
  976. // We also need to trim / (directory separator) from the end in case it's present.
  977. //
  978. return 0 == String.Compare(
  979. Path.GetFullPath(p1).Trim().TrimEnd(Path.DirectorySeparatorChar),
  980. Path.GetFullPath(p2).Trim().TrimEnd(Path.DirectorySeparatorChar),
  981. StringComparison.CurrentCultureIgnoreCase);
  982. }
  983. catch(ArgumentException)
  984. {
  985. }
  986. catch(NotSupportedException)
  987. {
  988. }
  989. catch(System.Security.SecurityException)
  990. {
  991. }
  992. catch(PathTooLongException)
  993. {
  994. }
  995. return false;
  996. }
  997. public static bool isSliceBuilderEnabled(Project project)
  998. {
  999. try
  1000. {
  1001. if(isCppProject(project) ||
  1002. isCSharpProject(project) ||
  1003. isVBProject(project) ||
  1004. isSilverlightProject(project) ||
  1005. isCSharpSmartDeviceProject(project) ||
  1006. isVBSmartDeviceProject(project))
  1007. {
  1008. return Util.getProjectPropertyAsBool(project, Util.PropertyIce);
  1009. }
  1010. }
  1011. catch(System.NotImplementedException)
  1012. {
  1013. }
  1014. return false;
  1015. }
  1016. public static bool isCSharpProject(Project project)
  1017. {
  1018. if(project == null)
  1019. {
  1020. return false;
  1021. }
  1022. if(String.IsNullOrEmpty(project.Kind))
  1023. {
  1024. return false;
  1025. }
  1026. return project.Kind == VSLangProj.PrjKind.prjKindCSharpProject;
  1027. }
  1028. public static bool isCSharpSmartDeviceProject(Project project)
  1029. {
  1030. return hasProjecType(project, vsSmartDeviceCSharp);
  1031. }
  1032. public static bool isVBSmartDeviceProject(Project project)
  1033. {
  1034. return hasProjecType(project, vsSmartDeviceVB);
  1035. }
  1036. public static bool isVBProject(Project project)
  1037. {
  1038. if(project == null)
  1039. {
  1040. return false;
  1041. }
  1042. if(String.IsNullOrEmpty(project.Kind))
  1043. {
  1044. return false;
  1045. }
  1046. return project.Kind == VSLangProj.PrjKind.prjKindVBProject;
  1047. }
  1048. public static bool isSilverlightProject(Project project)
  1049. {
  1050. if(!Util.isCSharpProject(project))
  1051. {
  1052. return false;
  1053. }
  1054. Array extenders = (Array)project.ExtenderNames;
  1055. foreach(string s in extenders)
  1056. {
  1057. if(String.IsNullOrEmpty(s))
  1058. {
  1059. continue;
  1060. }
  1061. if(s.Equals("SilverlightProject"))
  1062. {
  1063. return true;
  1064. }
  1065. }
  1066. return false;
  1067. }
  1068. public static bool isCppProject(Project project)
  1069. {
  1070. if(project == null)
  1071. {
  1072. return false;
  1073. }
  1074. if(String.IsNullOrEmpty(project.Kind))
  1075. {
  1076. return false;
  1077. }
  1078. return project.Kind == vcContextGuids.vcContextGuidVCProject;
  1079. }
  1080. public static bool isWinRTProject(Project project)
  1081. {
  1082. if(isCppProject(project))
  1083. {
  1084. ConfigurationManager configManager = project.ConfigurationManager;
  1085. Configuration activeConfig = (Configuration)configManager.ActiveConfiguration;
  1086. VCProject vcProject = (VCProject)project.Object;
  1087. IVCCollection configurations = (IVCCollection)vcProject.Configurations;
  1088. foreach(VCConfiguration conf in configurations)
  1089. {
  1090. if(!activeConfig.PlatformName.Equals(((VCPlatform)conf.Platform).Name))
  1091. {
  1092. continue;
  1093. }
  1094. IVCCollection sheets = (IVCCollection)conf.PropertySheets;
  1095. foreach(VCPropertySheet s in sheets)
  1096. {
  1097. if(s.PropertySheetFile.EndsWith("\\Microsoft.Cpp.AppContainerApplication.props"))
  1098. {
  1099. return true;
  1100. }
  1101. }
  1102. return false;
  1103. }
  1104. }
  1105. return false;
  1106. }
  1107. public static bool isProjectItemFolder(ProjectItem item)
  1108. {
  1109. if(item == null)
  1110. {
  1111. return false;
  1112. }
  1113. if(String.IsNullOrEmpty(item.Kind))
  1114. {
  1115. return false;
  1116. }
  1117. return item.Kind == "{6BB5F8EF-4483-11D3-8BCF-00C04F8EC28C}";
  1118. }
  1119. public static bool isProjectItemFilter(ProjectItem item)
  1120. {
  1121. if(item == null)
  1122. {
  1123. return false;
  1124. }
  1125. if(String.IsNullOrEmpty(item.Kind))
  1126. {
  1127. return false;
  1128. }
  1129. return item.Kind == "{6BB5F8F0-4483-11D3-8BCF-00C04F8EC28C}";
  1130. }
  1131. public static bool isProjectItemFile(ProjectItem item)
  1132. {
  1133. if(item == null)
  1134. {
  1135. return false;
  1136. }
  1137. if(String.IsNullOrEmpty(item.Kind))
  1138. {
  1139. return false;
  1140. }
  1141. return item.Kind == "{6BB5F8EE-4483-11D3-8BCF-00C04F8EC28C}";
  1142. }
  1143. public static bool hasItemNamed(ProjectItems items, string name)
  1144. {
  1145. bool found = false;
  1146. foreach(ProjectItem item in items)
  1147. {
  1148. if(item == null)
  1149. {
  1150. continue;
  1151. }
  1152. if(item.Name == null)
  1153. {
  1154. continue;
  1155. }
  1156. if(item.Name.Equals(name, StringComparison.OrdinalIgnoreCase))
  1157. {
  1158. found = true;
  1159. break;
  1160. }
  1161. }
  1162. return found;
  1163. }
  1164. public static Project findProject(VCProject vcProject)
  1165. {
  1166. if(vcProject == null)
  1167. {
  1168. return null;
  1169. }
  1170. Builder builder = Connect.getBuilder();
  1171. DTE dte = builder.getCurrentDTE();
  1172. if(dte == null)
  1173. {
  1174. return null;
  1175. }
  1176. if(dte.Solution == null)
  1177. {
  1178. return null;
  1179. }
  1180. List<Project> projects = Util.getProjects(dte.Solution);
  1181. Project project = null;
  1182. foreach(Project p in projects)
  1183. {
  1184. if(p.Object != null && p.Object.Equals(vcProject))
  1185. {
  1186. project = p;
  1187. break;
  1188. }
  1189. }
  1190. return project;
  1191. }
  1192. public static ProjectItem findItem(string path)
  1193. {
  1194. Builder builder = Connect.getBuilder();
  1195. DTE dte = builder.getCurrentDTE();
  1196. if(dte == null)
  1197. {
  1198. return null;
  1199. }
  1200. if(dte.Solution == null)
  1201. {
  1202. return null;
  1203. }
  1204. List<Project> projects = Util.getProjects(dte.Solution);
  1205. ProjectItem item = null;
  1206. foreach(Project project in projects)
  1207. {
  1208. item = findItem(path, project.ProjectItems);
  1209. if(item != null)
  1210. {
  1211. break;
  1212. }
  1213. }
  1214. return item;
  1215. }
  1216. public static ProjectItem findItem(string path, ProjectItems items)
  1217. {
  1218. if(String.IsNullOrEmpty(path))
  1219. {
  1220. return null;
  1221. }
  1222. ProjectItem item = null;
  1223. foreach(ProjectItem i in items)
  1224. {
  1225. if(i == null)
  1226. {
  1227. continue;
  1228. }
  1229. else if(Util.isProjectItemFile(i))
  1230. {
  1231. string fullPath = i.Properties.Item("FullPath").Value.ToString();
  1232. Project project = i.ContainingProject;
  1233. if(Util.equalPath(fullPath, path, Path.GetDirectoryName(project.FileName)))
  1234. {
  1235. item = i;
  1236. break;
  1237. }
  1238. }
  1239. else if(Util.isProjectItemFolder(i))
  1240. {
  1241. string p = Path.GetDirectoryName(i.Properties.Item("FullPath").Value.ToString());
  1242. Project project = i.ContainingProject;
  1243. if(Util.equalPath(p, path, Path.GetDirectoryName(project.FileName)))
  1244. {
  1245. item = i;
  1246. break;
  1247. }
  1248. item = findItem(path, i.ProjectItems);
  1249. if(item != null)
  1250. {
  1251. break;
  1252. }
  1253. }
  1254. else if(Util.isProjectItemFilter(i))
  1255. {
  1256. string p = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(i.ContainingProject.FileName),
  1257. Util.getPathRelativeToProject(i)));
  1258. Project project = i.ContainingProject;
  1259. if(Util.equalPath(p, path, Path.GetDirectoryName(project.FileName)))
  1260. {
  1261. item = i;
  1262. break;
  1263. }
  1264. item = findItem(path, i.ProjectItems);
  1265. if(item != null)
  1266. {
  1267. break;
  1268. }
  1269. }
  1270. }
  1271. return item;
  1272. }
  1273. public static VCFile findVCFile(IVCCollection files, string name, string fullPath)
  1274. {
  1275. VCFile vcFile = null;
  1276. foreach(VCFile file in files)
  1277. {
  1278. if(file.ItemName == name)
  1279. {
  1280. Project project = (Project)((VCProject) file.project).Object;
  1281. if(!Util.equalPath(file.FullPath, fullPath, Path.GetDirectoryName(project.FileName)))
  1282. {
  1283. file.Remove();
  1284. break;
  1285. }
  1286. vcFile = file;
  1287. break;
  1288. }
  1289. }
  1290. return vcFile;
  1291. }
  1292. public static string relativePath(Project project, string absoluteFilePath)
  1293. {
  1294. if(absoluteFilePath == null)
  1295. {
  1296. return "";
  1297. }
  1298. if(project == null)
  1299. {
  1300. return absoluteFilePath;
  1301. }
  1302. return relativePath(Path.GetDirectoryName(project.FileName), absoluteFilePath);
  1303. }
  1304. public static string relativePath(string mainDirPath, string absoluteFilePath)
  1305. {
  1306. if(absoluteFilePath == null)
  1307. {
  1308. return "";
  1309. }
  1310. if(mainDirPath == null)
  1311. {
  1312. return absoluteFilePath;
  1313. }
  1314. if(!Path.IsPathRooted(absoluteFilePath))
  1315. {
  1316. return absoluteFilePath;
  1317. }
  1318. mainDirPath = Path.GetFullPath(mainDirPath).Trim(Path.DirectorySeparatorChar);
  1319. absoluteFilePath = Path.GetFullPath(absoluteFilePath).Trim(Path.DirectorySeparatorChar);
  1320. string[] firstPathParts = mainDirPath.Split(Path.DirectorySeparatorChar);
  1321. string[] secondPathParts = absoluteFilePath.Split(Path.DirectorySeparatorChar);
  1322. int sameCounter = 0;
  1323. while(sameCounter < Math.Min(firstPathParts.Length, secondPathParts.Length) &&
  1324. String.Equals(firstPathParts[sameCounter], secondPathParts[sameCounter],
  1325. StringComparison.CurrentCultureIgnoreCase))
  1326. {
  1327. ++sameCounter;
  1328. }
  1329. // Different volumes, relative path not possible.
  1330. if(sameCounter == 0)
  1331. {
  1332. return absoluteFilePath;
  1333. }
  1334. // Pop back up to the common point.
  1335. string newPath = "." + Path.DirectorySeparatorChar;
  1336. for(int i = sameCounter; i < firstPathParts.Length; ++i)
  1337. {
  1338. newPath += ".." + Path.DirectorySeparatorChar;
  1339. }
  1340. // Descend to the target.
  1341. for(int i = sameCounter; i < secondPathParts.Length; ++i)
  1342. {
  1343. newPath += secondPathParts[i] + Path.DirectorySeparatorChar;
  1344. }
  1345. return newPath.TrimEnd(Path.DirectorySeparatorChar);
  1346. }
  1347. // Relative paths are relative to project.
  1348. // Inverse of Util.relativePath().
  1349. public static string absolutePath(Project project, string path)
  1350. {
  1351. if(Path.IsPathRooted(path)) // If path is absolute return that path
  1352. {
  1353. return path;
  1354. }
  1355. return Path.GetFullPath(Path.Combine(Path.GetDirectoryName(project.FileName), path)).TrimEnd(
  1356. Path.DirectorySeparatorChar);
  1357. }
  1358. public static Project getSelectedProject(_DTE dte)
  1359. {
  1360. Microsoft.VisualStudio.Shell.ServiceProvider sp = new Microsoft.VisualStudio.Shell.ServiceProvider(
  1361. getCurrentDTE() as Microsoft.VisualStudio.OLE.Interop.IServiceProvider);
  1362. IVsMonitorSelection selectionMonitor = sp.GetService(typeof(IVsMonitorSelection)) as IVsMonitorSelection;
  1363. //
  1364. // There isn't an open project.
  1365. //
  1366. if(selectionMonitor == null)
  1367. {
  1368. return null;
  1369. }
  1370. Project project = null;
  1371. IntPtr ppHier;
  1372. uint pitemid;
  1373. IVsMultiItemSelect ppMIS;
  1374. IntPtr ppSC;
  1375. if(ErrorHandler.Failed(selectionMonitor.GetCurrentSelection(out ppHier, out pitemid, out ppMIS, out ppSC)))
  1376. {
  1377. return null;
  1378. }
  1379. if(ppHier != IntPtr.Zero)
  1380. {
  1381. IVsHierarchy hier = (IVsHierarchy)Marshal.GetObjectForIUnknown(ppHier);
  1382. Marshal.Release(ppHier);
  1383. object obj;
  1384. hier.GetProperty(VSConstants.VSITEMID_ROOT, (int)__VSHPROPID.VSHPROPID_ExtObject, out obj);
  1385. if(obj != null)
  1386. {
  1387. project = obj as EnvDTE.Project;
  1388. }
  1389. }
  1390. if(ppSC != IntPtr.Zero)
  1391. {
  1392. Marshal.Release(ppSC);
  1393. }
  1394. return project;
  1395. }
  1396. public static ProjectItem getSelectedProjectItem(_DTE dte)
  1397. {
  1398. Microsoft.VisualStudio.Shell.ServiceProvider sp = new Microsoft.VisualStudio.Shell.ServiceProvider(
  1399. getCurrentDTE() as Microsoft.VisualStudio.OLE.Interop.IServiceProvider);
  1400. IVsMonitorSelection selectionMonitor = sp.GetService(typeof(IVsMonitorSelection)) as IVsMonitorSelection;
  1401. //
  1402. // There isn't an open project.
  1403. //
  1404. if(selectionMonitor == null)
  1405. {
  1406. return null;
  1407. }
  1408. ProjectItem projectItem = null;
  1409. IntPtr ppHier;
  1410. uint pitemid;
  1411. IVsMultiItemSelect ppMIS;
  1412. IntPtr ppSC;
  1413. if(ErrorHandler.Failed(selectionMonitor.GetCurrentSelection(out ppHier, out pitemid, out ppMIS, out ppSC)))
  1414. {
  1415. return null;
  1416. }
  1417. if(ppHier != IntPtr.Zero)
  1418. {
  1419. IVsHierarchy hier = (IVsHierarchy)Marshal.GetObjectForIUnknown(ppHier);
  1420. Marshal.Release(ppHier);
  1421. object obj;
  1422. hier.GetProperty(pitemid, (int)__VSHPROPID.VSHPROPID_ExtObject, out obj);
  1423. if(obj != null)
  1424. {
  1425. projectItem = obj as EnvDTE.ProjectItem;
  1426. }
  1427. }
  1428. if(ppSC != IntPtr.Zero)
  1429. {
  1430. Mars

Large files files are truncated, but you can click here to view the full file