Skip to content
This repository
Browse code

Add buildlet utility library

  • Loading branch information...
commit 8052aab8db007cc6074915a7510edd9225dd1bf7 1 parent f224f3c
Jon authored March 21, 2013

Showing 1 changed file with 57 additions and 0 deletions. Show diff stats Hide diff stats

  1. 57  buildlet_utils.ps1
57  buildlet_utils.ps1
... ...
@@ -0,0 +1,57 @@
  1
+#requires -version 2.0
  2
+
  3
+# Author: Jon Maken
  4
+# License: 3-clause BSD
  5
+# Revision: 2013-03-21 21:33:45 -0600
  6
+
  7
+function Write-Status($msg, $leader='--->', $color='Yellow') {
  8
+  Write-Host "$leader $msg" -foregroundcolor $color
  9
+}
  10
+
  11
+function Require-BuildletLibrary() {
  12
+  if (Test-Path "$PWD\buildlet_utils.ps1") { return }
  13
+
  14
+  $fetcher = New-Object System.Net.WebClient
  15
+  $fetcher.DownloadFile('https://raw.github.com/jonforums/buildlets/master/buildlet_utils.ps1',
  16
+                        "$PWD\buildlet_utils.ps1")
  17
+
  18
+  if (Test-Path "$PWD\buildlet_utils.ps1") {
  19
+    . "$PWD\buildlet_utils.ps1"
  20
+  } else {
  21
+    throw '[ERROR] unable to fetch required buildlet library'
  22
+  }
  23
+}
  24
+
  25
+function Fetch-Archive($uri, $target) {
  26
+  if (-not (Test-Path $target)) {
  27
+    Import-Module BitsTransfer
  28
+    Write-Status "downloading $uri"
  29
+    Start-BitsTransfer $uri "$PWD\$target"
  30
+  }
  31
+}
  32
+
  33
+function Validate-Archive($target, $hash_uri) {
  34
+  try {
  35
+    $fetcher = New-Object System.Net.WebClient
  36
+    $hash = ConvertFrom-StringData $fetcher.DownloadString($hash_uri)
  37
+
  38
+    $hasher = New-Object System.Security.Cryptography.SHA1Cng
  39
+    $fs = New-Object System.IO.FileStream "$PWD\$target", 'Open', 'Read'
  40
+    $test_hash = [BitConverter]::ToString($hasher.ComputeHash($fs)).Replace('-','').ToLower()
  41
+  }
  42
+  catch {
  43
+    throw "[ERROR] unable to validate $target"
  44
+  }
  45
+  finally {
  46
+    $fs.Close()
  47
+  }
  48
+
  49
+  if ($test_hash -ne $hash[$version].ToLower()) {
  50
+    throw "[ERROR] $target fingerprint different than expected value"
  51
+  }
  52
+}
  53
+
  54
+function Extract-Archive($archive) {
  55
+  $tar_file = "$($source.Substring(0, $source.LastIndexOf('-')))*.tar"
  56
+  (& "$7ZA" "x" $source) -and (& "$7ZA" "x" $tar_file) -and (rm $tar_file) | Out-Null
  57
+}

0 notes on commit 8052aab

Please sign in to comment.
Something went wrong with that request. Please try again.