You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

116 lines
3.9 KiB

10 years ago
  1. function Get-Checksum($file) {
  2. $cryptoProvider = New-Object "System.Security.Cryptography.MD5CryptoServiceProvider"
  3. $fileInfo = Get-Item $file
  4. trap { ;
  5. continue } $stream = $fileInfo.OpenRead()
  6. if ($? -eq $false) {
  7. # Couldn't open file for reading
  8. return $null
  9. }
  10. $bytes = $cryptoProvider.ComputeHash($stream)
  11. $checksum = ''
  12. foreach ($byte in $bytes) {
  13. $checksum += $byte.ToString('x2')
  14. }
  15. $stream.Close() | Out-Null
  16. return $checksum
  17. }
  18. function AddOrUpdate-Reference($scriptsFolderProjectItem, $fileNamePattern, $newFileName) {
  19. try {
  20. $referencesFileProjectItem = $scriptsFolderProjectItem.ProjectItems.Item("_references.js")
  21. }
  22. catch {
  23. # _references.js file not found
  24. return
  25. }
  26. if ($referencesFileProjectItem -eq $null) {
  27. # _references.js file not found
  28. return
  29. }
  30. $referencesFilePath = $referencesFileProjectItem.FileNames(1)
  31. $referencesTempFilePath = Join-Path $env:TEMP "_references.tmp.js"
  32. if ((Select-String $referencesFilePath -pattern $fileNamePattern).Length -eq 0) {
  33. # File has no existing matching reference line
  34. # Add the full reference line to the beginning of the file
  35. "/// <reference path=""$newFileName"" />" | Add-Content $referencesTempFilePath -Encoding UTF8
  36. Get-Content $referencesFilePath | Add-Content $referencesTempFilePath
  37. }
  38. else {
  39. # Loop through file and replace old file name with new file name
  40. Get-Content $referencesFilePath | ForEach-Object { $_ -replace $fileNamePattern, $newFileName } > $referencesTempFilePath
  41. }
  42. # Copy over the new _references.js file
  43. Copy-Item $referencesTempFilePath $referencesFilePath -Force
  44. Remove-Item $referencesTempFilePath -Force
  45. }
  46. function Remove-Reference($scriptsFolderProjectItem, $fileNamePattern) {
  47. try {
  48. $referencesFileProjectItem = $scriptsFolderProjectItem.ProjectItems.Item("_references.js")
  49. }
  50. catch {
  51. # _references.js file not found
  52. return
  53. }
  54. if ($referencesFileProjectItem -eq $null) {
  55. return
  56. }
  57. $referencesFilePath = $referencesFileProjectItem.FileNames(1)
  58. $referencesTempFilePath = Join-Path $env:TEMP "_references.tmp.js"
  59. if ((Select-String $referencesFilePath -pattern $fileNamePattern).Length -eq 1) {
  60. # Delete the line referencing the file
  61. Get-Content $referencesFilePath | ForEach-Object { if (-not ($_ -match $fileNamePattern)) { $_ } } > $referencesTempFilePath
  62. # Copy over the new _references.js file
  63. Copy-Item $referencesTempFilePath $referencesFilePath -Force
  64. Remove-Item $referencesTempFilePath -Force
  65. }
  66. }
  67. function Delete-ProjectItem($item) {
  68. $itemDeleted = $false
  69. for ($1=1; $i -le 5; $i++) {
  70. try {
  71. $item.Delete()
  72. $itemDeleted = $true
  73. break
  74. }
  75. catch {
  76. # Try again in 200ms
  77. [System.Threading.Thread]::Sleep(200)
  78. }
  79. }
  80. if ($itemDeleted -eq $false) {
  81. throw "Unable to delete project item after five attempts."
  82. }
  83. }
  84. # Extract the version number from the jquery file in the package's content\scripts folder
  85. $packageScriptsFolder = Join-Path $installPath Content\Scripts
  86. $jqueryFileName = Join-Path $packageScriptsFolder "jquery-*.js" | Get-ChildItem -Exclude "*.min.js","*-vsdoc.js" | Split-Path -Leaf
  87. $jqueryFileNameRegEx = "jquery-((?:\d+\.)?(?:\d+\.)?(?:\d+\.)?(?:\d+)).js"
  88. $jqueryFileName -match $jqueryFileNameRegEx
  89. $ver = $matches[1]
  90. $intelliSenseFileName = "jquery-$ver.intellisense.js"
  91. # Get the project item for the scripts folder
  92. try {
  93. $scriptsFolderProjectItem = $project.ProjectItems.Item("Scripts")
  94. $projectScriptsFolderPath = $scriptsFolderProjectItem.FileNames(1)
  95. }
  96. catch {
  97. # No Scripts folder
  98. Write-Host "No scripts folder found"
  99. }