Advertisement
Guest User

Untitled

a guest
Mar 3rd, 2015
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.53 KB | None | 0 0
  1. '------------------------------------------------------------------------------
  2. ' Visual Studio 2008 Macros
  3. '
  4. ' ProjectUtilities.vb
  5. '
  6. '------------------------------------------------------------------------------
  7. ' Copyright (C) 2007-2008 Scott Dorman (sj_dorman@hotmail.com)
  8. '
  9. ' This library is free software; you can redistribute it and/or
  10. ' modify it under the terms of the Microsoft Public License (Ms-PL).
  11. '
  12. ' This library is distributed in the hope that it will be useful,
  13. ' but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ' Microsoft Public License (Ms-PL) for more details.
  16. '------------------------------------------------------------------------------
  17. Imports System
  18. Imports EnvDTE
  19. Imports EnvDTE80
  20. Imports EnvDTE90
  21. Imports System.Diagnostics
  22.  
  23. Public Module ProjectUtilities
  24.  
  25. Private Class ProjectGuids
  26. Public Const vsWindowsCSharp As String = "{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}"
  27. Public Const vsWindowsVBNET As String = "{F184B08F-C81C-45F6-A57F-5ABD9991F28F}"
  28. Public Const vsWindowsVisualCPP As String = "{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}"
  29. Public Const vsWebApplication As String = "{349C5851-65DF-11DA-9384-00065B846F21}"
  30. Public Const vsWebSite As String = "{E24C65DC-7377-472B-9ABA-BC803B73C61A}"
  31. Public Const vsDistributedSystem As String = "{F135691A-BF7E-435D-8960-F99683D2D49C}"
  32. Public Const vsWCF As String = "{3D9AD99F-2412-4246-B90B-4EAA41C64699}"
  33. Public Const vsWPF As String = "{60DC8134-EBA5-43B8-BCC9-BB4BC16C2548}"
  34. Public Const vsVisualDatabaseTools As String = "{C252FEB5-A946-4202-B1D4-9916A0590387}"
  35. Public Const vsDatabase As String = "{A9ACE9BB-CECE-4E62-9AA4-C7E7C5BD2124}"
  36. Public Const vsDatabaseOther As String = "{4F174C21-8C12-11D0-8340-0000F80270F8}"
  37. Public Const vsTest As String = "{3AC096D0-A1C2-E12C-1390-A8335801FDAB}"
  38. Public Const vsLegacy2003SmartDeviceCSharp As String = "{20D4826A-C6FA-45DB-90F4-C717570B9F32}"
  39. Public Const vsLegacy2003SmartDeviceVBNET As String = "{CB4CE8C6-1BDB-4DC7-A4D3-65A1999772F8}"
  40. Public Const vsSmartDeviceCSharp As String = "{4D628B5B-2FBC-4AA6-8C16-197242AEB884}"
  41. Public Const vsSmartDeviceVBNET As String = "{68B1623D-7FB9-47D8-8664-7ECEA3297D4F}"
  42. Public Const vsWorkflowCSharp As String = "{14822709-B5A1-4724-98CA-57A101D1B079}"
  43. Public Const vsWorkflowVBNET As String = "{D59BE175-2ED0-4C54-BE3D-CDAA9F3214C8}"
  44. Public Const vsDeploymentMergeModule As String = "{06A35CCD-C46D-44D5-987B-CF40FF872267}"
  45. Public Const vsDeploymentCab As String = "{3EA9E505-35AC-4774-B492-AD1749C4943A}"
  46. Public Const vsDeploymentSetup As String = "{978C614F-708E-4E1A-B201-565925725DBA}"
  47. Public Const vsDeploymentSmartDeviceCab As String = "{AB322303-2255-48EF-A496-5904EB18DA55}"
  48. Public Const vsVSTA As String = "{A860303F-1F3F-4691-B57E-529FC101A107}"
  49. Public Const vsVSTO As String = "{BAA0C2D2-18E2-41B9-852F-F413020CAA33}"
  50. Public Const vsSharePointWorkflow As String = "{F8810EC1-6754-47FC-A15F-DFABD2E3FA90}"
  51. End Class
  52.  
  53. '' Defines the valid target framework values.
  54. Enum TargetFramework
  55. Fx40 = 262144
  56. Fx35 = 196613
  57. Fx30 = 196608
  58. Fx20 = 131072
  59. End Enum
  60.  
  61. '' Change the target framework for all projects in the current solution.
  62. Sub ChangeTargetFrameworkForAllProjects()
  63. Dim project As EnvDTE.Project
  64. Dim clientProfile As Boolean = False
  65.  
  66. Write("--------- CHANGING TARGET .NET FRAMEWORK VERSION -------------")
  67. Try
  68. If Not DTE.Solution.IsOpen Then
  69. Write("There is no solution open.")
  70. Else
  71. Dim targetFrameworkInput As String = InputBox("Enter the target framework version (Fx40, Fx35, Fx30, Fx20):", "Target Framework", "Fx40")
  72. Dim targetFramework As TargetFramework = [Enum].Parse(GetType(TargetFramework), targetFrameworkInput)
  73.  
  74. If targetFramework = ProjectUtilities.TargetFramework.Fx35 Or targetFramework = ProjectUtilities.TargetFramework.Fx40 Then
  75. Dim result As MsgBoxResult = MsgBox("The .NET Framework version chosen supports a Client Profile. Would you like to use that profile?", MsgBoxStyle.Question Or MsgBoxStyle.YesNo, "Target Framework Profile")
  76. If result = MsgBoxResult.Yes Then
  77. clientProfile = True
  78. End If
  79. End If
  80.  
  81. For Each project In DTE.Solution.Projects
  82. If project.Kind <> Constants.vsProjectKindSolutionItems And project.Kind <> Constants.vsProjectKindMisc Then
  83. ChangeTargetFramework(project, targetFramework, clientProfile)
  84. Else
  85. For Each projectItem In project.ProjectItems
  86. If Not (projectItem.SubProject Is Nothing) Then
  87. ChangeTargetFramework(projectItem.SubProject, targetFramework, clientProfile)
  88. End If
  89. Next
  90.  
  91. End If
  92. Next
  93. End If
  94. Catch ex As System.Exception
  95. Write(ex.Message)
  96. End Try
  97. End Sub
  98.  
  99. '' Change the target framework for a project.
  100. Function ChangeTargetFramework(ByVal project As EnvDTE.Project, ByVal targetFramework As TargetFramework, ByVal clientProfile As Boolean) As Boolean
  101. Dim changed As Boolean = True
  102.  
  103. If project.Kind = Constants.vsProjectKindSolutionItems Or project.Kind = Constants.vsProjectKindMisc Then
  104. For Each projectItem In project.ProjectItems
  105. If Not (projectItem.SubProject Is Nothing) Then
  106. ChangeTargetFramework(projectItem.SubProject, targetFramework, clientProfile)
  107. End If
  108. Next
  109. Else
  110. Try
  111. If IsLegalProjectType(project) Then
  112. SetTargetFramework(project, targetFramework, clientProfile)
  113. Else
  114. Write("Skipping project: " + project.Name + " (" + project.Kind + ")")
  115. End If
  116. Catch ex As Exception
  117. Write(ex.Message)
  118. changed = False
  119. End Try
  120. End If
  121.  
  122. Return changed
  123. End Function
  124.  
  125. '' Determines if the project is a project that actually supports changing the target framework.
  126. Function IsLegalProjectType(ByVal proejct As EnvDTE.Project) As Boolean
  127. Dim legalProjectType As Boolean = True
  128.  
  129. Select Case proejct.Kind
  130. Case ProjectGuids.vsDatabase
  131. legalProjectType = False
  132. Case ProjectGuids.vsDatabaseOther
  133. legalProjectType = False
  134. Case ProjectGuids.vsDeploymentCab
  135. legalProjectType = False
  136. Case ProjectGuids.vsDeploymentMergeModule
  137. legalProjectType = False
  138. Case ProjectGuids.vsDeploymentSetup
  139. legalProjectType = False
  140. Case ProjectGuids.vsDeploymentSmartDeviceCab
  141. legalProjectType = False
  142. Case ProjectGuids.vsDistributedSystem
  143. legalProjectType = False
  144. Case ProjectGuids.vsLegacy2003SmartDeviceCSharp
  145. legalProjectType = False
  146. Case ProjectGuids.vsLegacy2003SmartDeviceVBNET
  147. legalProjectType = False
  148. Case ProjectGuids.vsSharePointWorkflow
  149. legalProjectType = False
  150. Case ProjectGuids.vsSmartDeviceCSharp
  151. legalProjectType = True
  152. Case ProjectGuids.vsSmartDeviceVBNET
  153. legalProjectType = True
  154. Case ProjectGuids.vsTest
  155. legalProjectType = False
  156. Case ProjectGuids.vsVisualDatabaseTools
  157. legalProjectType = False
  158. Case ProjectGuids.vsVSTA
  159. legalProjectType = True
  160. Case ProjectGuids.vsVSTO
  161. legalProjectType = True
  162. Case ProjectGuids.vsWCF
  163. legalProjectType = True
  164. Case ProjectGuids.vsWebApplication
  165. legalProjectType = True
  166. Case ProjectGuids.vsWebSite
  167. legalProjectType = True
  168. Case ProjectGuids.vsWindowsCSharp
  169. legalProjectType = True
  170. Case ProjectGuids.vsWindowsVBNET
  171. legalProjectType = True
  172. Case ProjectGuids.vsWindowsVisualCPP
  173. legalProjectType = True
  174. Case ProjectGuids.vsWorkflowCSharp
  175. legalProjectType = False
  176. Case ProjectGuids.vsWorkflowVBNET
  177. legalProjectType = False
  178. Case ProjectGuids.vsWPF
  179. legalProjectType = True
  180. Case Else
  181. legalProjectType = False
  182. End Select
  183. Return legalProjectType
  184. End Function
  185.  
  186. '' Sets the target framework for the project to the specified framework.
  187. Sub SetTargetFramework(ByVal project As EnvDTE.Project, ByVal targetFramework As TargetFramework, ByVal clientProfile As Boolean)
  188. Dim currentTargetFramework As TargetFramework = CType(project.Properties.Item("TargetFramework").Value, TargetFramework)
  189. Dim targetMoniker As String = GetTargetFrameworkMoniker(targetFramework, clientProfile)
  190. Dim currentMoniker As String = project.Properties.Item("TargetFrameworkMoniker").Value
  191.  
  192. If currentMoniker <> targetMoniker Then
  193. Write("Changing project: " + project.Name + " from " + currentMoniker + " to " + targetMoniker + ".")
  194. project.Properties.Item("TargetFrameworkMoniker").Value = targetMoniker
  195. project.Properties.Item("TargetFramework").Value = targetFramework
  196. Else
  197. Write("Skipping project: " + project.Name + ", already at the correct target framework.")
  198. End If
  199. End Sub
  200.  
  201. Function GetTargetFrameworkMoniker(ByVal targetFramework As TargetFramework, ByVal clientProfile As Boolean) As String
  202. Dim moniker As String = ".NETFramework,Version=v"
  203. Select Case targetFramework
  204. Case ProjectUtilities.TargetFramework.Fx20
  205. moniker += "2.0"
  206.  
  207. Case ProjectUtilities.TargetFramework.Fx30
  208. moniker += "3.0"
  209.  
  210. Case ProjectUtilities.TargetFramework.Fx35
  211. moniker += "3.5"
  212.  
  213. Case ProjectUtilities.TargetFramework.Fx40
  214. moniker += "4.0"
  215.  
  216. End Select
  217.  
  218. If clientProfile Then
  219. moniker += ",Profile=Client"
  220. End If
  221.  
  222. Return moniker
  223. End Function
  224.  
  225. '' Writes a message to the output window
  226. Sub Write(ByVal s As String)
  227. Dim out As OutputWindowPane = GetOutputWindowPane("Change Target Framework", True)
  228. out.OutputString(s)
  229. out.OutputString(vbCrLf)
  230. End Sub
  231.  
  232. '' Gets an instance of the output window
  233. Function GetOutputWindowPane(ByVal Name As String, Optional ByVal show As Boolean = True) As OutputWindowPane
  234. Dim win As Window = DTE.Windows.Item(EnvDTE.Constants.vsWindowKindOutput)
  235. If show Then win.Visible = True
  236. Dim ow As OutputWindow = win.Object
  237. Dim owpane As OutputWindowPane
  238. Try
  239. owpane = ow.OutputWindowPanes.Item(Name)
  240. Catch e As System.Exception
  241. owpane = ow.OutputWindowPanes.Add(Name)
  242. End Try
  243. owpane.Activate()
  244. Return owpane
  245. End Function
  246.  
  247. End Module
  248.  
  249. Get-ChildItem . -Recurse -Filter *.*proj |ForEach {
  250. $content = Get-Content $_.FullName
  251. $content |ForEach {
  252. $_.Replace("<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>", "<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>")
  253. } |Set-Content $_.FullName
  254. }
  255.  
  256. using System;
  257. using System.Collections.Generic;
  258. using System.Linq;
  259. using System.Text;
  260. using System.IO;
  261. using System.Text.RegularExpressions;
  262.  
  263. namespace TextReplaceDemo
  264. {
  265. class Program
  266. {
  267. static void Main(string[] args)
  268. {
  269. try
  270. {
  271. ReplaceTargetFrameworkVersion("v4.0", "c:/projekt/2005", "*.csproj");
  272. }
  273. catch (Exception ex)
  274. {
  275. Console.WriteLine(ex.Message);
  276. }
  277. }
  278.  
  279. /// <summary>
  280. /// Inserts the denoted targetFramework into all occurrences of TargetFrameworkVersion.
  281. /// If the TargetFrameworkVersion is not present in the file, the method searches for the
  282. /// OutputType tag, which should be present, and inserts the TargetFrameworkVersion before that.
  283. /// </summary>
  284. /// <param name="targetFramework">New target framework (e.g. "v4.0")</param>
  285. /// <param name="rootDirectory">Root directory for the file search (e.g. "c:Projects2005")</param>
  286. /// <param name="fileSearchPattern">Pattern to find the project files (e.g. "*.csproj).
  287. /// Will get all files for empty parameter.</param>
  288. public static void ReplaceTargetFrameworkVersion(string targetFramework, string rootDirectory, string fileSearchPattern)
  289. {
  290. if (string.IsNullOrEmpty(targetFramework)) throw new ArgumentNullException("targetFramework");
  291. if (string.IsNullOrEmpty(rootDirectory)) throw new ArgumentNullException("rootDirectory");
  292. if (string.IsNullOrEmpty(fileSearchPattern)) fileSearchPattern = "*.*";
  293.  
  294. string regexPattern = "<TargetFrameworkVersion>.*</TargetFrameworkVersion>";
  295. string insertText = string.Format("<TargetFrameworkVersion>{0}</TargetFrameworkVersion>", targetFramework);
  296. string alternativeMarker = "<OutputType>";
  297.  
  298. // get all files
  299. List<FileInfo> files = GetAllFiles(rootDirectory, fileSearchPattern);
  300.  
  301. // iterate over found files
  302. foreach (var file in files)
  303. {
  304. string fileContent = File.ReadAllText(file.FullName);
  305. Match match = Regex.Match(fileContent, regexPattern);
  306. string newfileContent = null;
  307. if (match.Success)
  308. {
  309. // replace <TargetFrameworkVersion>
  310. newfileContent = fileContent.Replace(match.Value, insertText);
  311. }
  312. else if (fileContent.Contains(alternativeMarker))
  313. {
  314. // create <TargetFrameworkVersion>
  315. newfileContent = fileContent.Replace(alternativeMarker,
  316. insertText + Environment.NewLine + " " + alternativeMarker);
  317. }
  318.  
  319. // overwrite file
  320. if (newfileContent != null)
  321. File.WriteAllText(file.FullName, newfileContent);
  322. }
  323. }
  324.  
  325.  
  326. /// <summary>
  327. /// Recursive function to find all files in a directory by a searchPattern
  328. /// </summary>
  329. /// <param name="path">Path to the root directory</param>
  330. /// <param name="searchPattern">Pattern for the file search, e.g. "*.txt"</param>
  331. public static List<FileInfo> GetAllFiles(string path, string searchPattern)
  332. {
  333. List<FileInfo> files = new List<FileInfo>();
  334.  
  335. DirectoryInfo dir = new DirectoryInfo(path);
  336.  
  337. if (dir.Exists)
  338. {
  339. // get all files in directory
  340. files.AddRange(dir.GetFiles(searchPattern));
  341.  
  342. // get all files of subdirectories
  343. foreach (var subDir in dir.GetDirectories())
  344. {
  345. files.AddRange(GetAllFiles(subDir.FullName, searchPattern));
  346. }
  347. }
  348. return files;
  349. }
  350. }
  351. }
  352.  
  353. <TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
  354.  
  355. <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement