install.ps1 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. param($installPath, $toolsPath, $package, $project)
  2. # find the App.xaml file
  3. $appxaml = $project.ProjectItems | where {$_.Name -eq "App.xaml"}
  4. $appxamlPath = ($appxaml.Properties | where {$_.Name -eq "LocalPath"}).Value
  5. # find the CSPROJ file
  6. $projectPath = ($project.Properties | where {$_.Name -eq "LocalPath"}).Value + $project.Name + ".csproj"
  7. $namespace = $project.Properties.Item("RootNamespace").Value
  8. # DEBUG ONLY
  9. #$namespace = "Univ"
  10. #$projectPath = "C:\MvvmLight\TEMPO2\AllPlatformsNoVm\Univ\Univ.Windows\Univ.Windows.csproj"
  11. # DEBUG ONLY
  12. $isUniversal = $false
  13. $sharedFolderPath = $null
  14. if ($appxamlPath -eq $null)
  15. {
  16. # Might be a universal project, try it
  17. $pathElements = $projectPath.Split('\')
  18. $appxamlPath = ""
  19. $lastElement = ""
  20. for ($i = 0; $i -le $pathElements.Length - 3; $i++)
  21. {
  22. $sharedFolderPath += $pathElements[$i] + "/"
  23. $appxamlPath += $pathElements[$i] + "/"
  24. $lastElement = $pathElements[$i]
  25. }
  26. $appxamlPath += $lastElement + ".Shared/App.xaml"
  27. # [System.Windows.MessageBox]::Show("New appxamlPath " + $appxamlPath, 'TEST', 'OK')
  28. if (Test-Path $appxamlPath)
  29. {
  30. $isUniversal = $true
  31. # [System.Windows.MessageBox]::Show("UNIVERSAL", 'TEST', 'OK')
  32. }
  33. else
  34. {
  35. $appxamlPath = $null
  36. }
  37. }
  38. if ($appxamlPath -eq $null)
  39. {
  40. # TODO Xamarin
  41. # add the required .NET assembly:
  42. Add-Type -AssemblyName PresentationFramework
  43. [System.Windows.MessageBox]::Show('Cannot find App.xaml in this project, no other changes made. If you are installing in a PCL, please use "MVVM Light Libs Only" instead.', 'Warning', 'OK')
  44. }
  45. else
  46. {
  47. $projectXml = New-Object xml
  48. $projectXml.Load($projectPath)
  49. $propertyGroups = $projectXml.SelectNodes("//*") | where { $_.Name -eq "PropertyGroup" }
  50. $found = "Nothing"
  51. foreach ($propertyGroup in $propertyGroups)
  52. {
  53. $targetFrameworkProfile = $propertyGroup.SelectNodes("//*") | where {$_.Name -eq "TargetFrameworkProfile" }
  54. if ($targetFrameworkProfile -ne $null)
  55. {
  56. if ($targetFrameworkProfile.InnerText -eq "WindowsPhone71")
  57. {
  58. $found = "wp71"
  59. break
  60. }
  61. }
  62. $targetFrameworkIdentifier = $propertyGroup.SelectNodes("//*") | where {$_.Name -eq "TargetFrameworkIdentifier" }
  63. if ($targetFrameworkIdentifier -ne $null)
  64. {
  65. if ($targetFrameworkIdentifier.InnerText -eq "WindowsPhone")
  66. {
  67. $found = "wp8"
  68. break
  69. }
  70. if ($targetFrameworkIdentifier.InnerText -eq "Silverlight")
  71. {
  72. $version = $propertyGroup.SelectNodes("//*") | where {$_.Name -eq "TargetFrameworkVersion" }
  73. if ($version -ne $null)
  74. {
  75. if ($version.InnerText -eq "v4.0")
  76. {
  77. $found = "sl4"
  78. break
  79. }
  80. if ($version.InnerText -eq "v5.0")
  81. {
  82. $found = "sl5"
  83. break
  84. }
  85. }
  86. $found = "sl"
  87. break
  88. }
  89. }
  90. $outputType = $propertyGroup.SelectNodes("//*") | where {$_.Name -eq "OutputType" }
  91. if ($outputType -ne $null)
  92. {
  93. if ($outputType.InnerText -eq "AppContainerExe")
  94. {
  95. $found = "win8"
  96. break
  97. }
  98. if ($outputType.InnerText -eq "WinExe")
  99. {
  100. $version = $propertyGroup.SelectNodes("//*") | where {$_.Name -eq "TargetFrameworkVersion" }
  101. if ($version -ne $null)
  102. {
  103. if ($version.InnerText -eq "v3.5")
  104. {
  105. $found = "wpf35"
  106. break
  107. }
  108. if ($version.InnerText -eq "v4.0")
  109. {
  110. $found = "wpf4"
  111. break
  112. }
  113. if ($version.InnerText -eq "v4.5")
  114. {
  115. $found = "wpf45"
  116. break
  117. }
  118. }
  119. $found = "wpf"
  120. break
  121. }
  122. }
  123. }
  124. # load App.xaml as XML
  125. $appXamlXml = New-Object xml
  126. $appXamlXml.Load($appxamlPath)
  127. #$comment = $appXamlXml.CreateComment($found)
  128. #$appXamlXml.AppendChild($comment)
  129. #$comment2 = $appXamlXml.CreateComment($projectPath)
  130. #$appXamlXml.AppendChild($comment2)
  131. $resources = $appXamlXml.SelectNodes("//*") | where { $_.Name -eq "Application.Resources" }
  132. if ($resources -eq $null)
  133. {
  134. $resources = $appXamlXml.CreateNode("element", "Application.Resources", "http://schemas.microsoft.com/winfx/2006/xaml/presentation")
  135. $app = $appXamlXml.SelectNodes("//*") | where { $_.Name -eq "Application" }
  136. if ($app -eq $null)
  137. {
  138. break
  139. }
  140. $app.AppendChild($resources)
  141. }
  142. $xmlnsPrefix = "clr-namespace:"
  143. if ($found -eq "win8")
  144. {
  145. $xmlnsPrefix = "using:"
  146. }
  147. $vml = $appXamlXml.CreateNode("element", "vm:ViewModelLocator", $xmlnsPrefix + $namespace + ".ViewModel")
  148. $vml.SetAttribute("Key", "http://schemas.microsoft.com/winfx/2006/xaml", "Locator")
  149. if ($found -ne "win8")
  150. {
  151. $app = $appXamlXml.ChildNodes | where { $_.Name -eq "Application" }
  152. # Check presence of design time XMLNS
  153. $dWasFound = $app.HasAttribute("xmlns:d")
  154. if (!$dWasFound)
  155. {
  156. $app.SetAttribute("xmlns:d", "http://schemas.microsoft.com/expression/blend/2008")
  157. }
  158. # Check presence of Ignorable attribute on Application element
  159. $ignorable = $app.GetAttribute("Ignorable", "http://schemas.openxmlformats.org/markup-compatibility/2006")
  160. if ($ignorable -ne "")
  161. {
  162. $allIgnorables = $ignorable.Split(' ')
  163. $dWasFound = "False"
  164. foreach ($ign in $allIgnorables)
  165. {
  166. if ($ign -eq "d")
  167. {
  168. $dWasFound = "True"
  169. }
  170. }
  171. if ($dWasFound -eq "False")
  172. {
  173. $app.SetAttribute("Ignorable", "http://schemas.openxmlformats.org/markup-compatibility/2006", $ignorable + " d")
  174. }
  175. }
  176. else
  177. {
  178. $app.SetAttribute("Ignorable", "http://schemas.openxmlformats.org/markup-compatibility/2006", "d")
  179. }
  180. $attribute = $appXamlXml.CreateAttribute("d", "IsDataSource", "http://schemas.microsoft.com/expression/blend/2008");
  181. $attribute.Value = "True";
  182. $vml.Attributes.Append($attribute)
  183. }
  184. $mergedDictionaries = $resources.SelectNodes("//*") | where { $_.Name -eq "ResourceDictionary" }
  185. if ($mergedDictionaries -eq $null)
  186. {
  187. # Add-Type -AssemblyName PresentationFramework
  188. # [System.Windows.MessageBox]::Show('Found Resources, no merged, checking Locator now', 'Warning', 'OK')
  189. $existingLocator = $resources.SelectNodes("//*") | where { $_.Name -eq "vm:ViewModelLocator" }
  190. if ($existingLocator -eq $null)
  191. {
  192. # [System.Windows.MessageBox]::Show("No locator found")
  193. $resources.AppendChild($vml)
  194. }
  195. }
  196. else
  197. {
  198. # Add-Type -AssemblyName PresentationFramework
  199. # [System.Windows.MessageBox]::Show('Found Resources, merged, checking Locator now', 'Warning', 'OK')
  200. $existingLocator = $mergedDictionaries.SelectNodes("//*") | where { $_.Name -eq "vm:ViewModelLocator" }
  201. # [System.Windows.MessageBox]::Show($existingLocator -ne $null)
  202. if ($existingLocator -eq $null)
  203. {
  204. # [System.Windows.MessageBox]::Show("No locator found")
  205. $mergedDictionaries[0].AppendChild($vml)
  206. }
  207. }
  208. $appXamlXml.Save($appxamlPath)
  209. if ($isUniversal)
  210. {
  211. # add the required .NET assembly:
  212. Add-Type -AssemblyName PresentationFramework
  213. [System.Windows.MessageBox]::Show('This is a Universal application. You need to move the ViewModel folder manually. See http://www.mvvmlight.net/nuget-univ for details')
  214. $ie = New-Object -ComObject InternetExplorer.Application
  215. $ie.Navigate("http://www.mvvmlight.net/nuget-univ")
  216. $ie.Visible = $true
  217. }
  218. }