|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +readonly PROJECT_NAME="angular-payload-size" |
| 4 | + |
| 5 | +# Calculate the size of target file uncompressed size, gzip7 size, gzip9 size |
| 6 | +# Write to global variable $payloadData, $filename |
| 7 | +calculateSize() { |
| 8 | + size["uncompressed"]=$(stat -c%s "$filename") |
| 9 | + label=$(echo "$filename" | sed "s/.*\///" | sed "s/\..*//") |
| 10 | + payloadData="$payloadData\"uncompressed/$label\": ${size["uncompressed"]}, " |
| 11 | + |
| 12 | + gzip -7 $filename -c >> "${filename}7.gz" |
| 13 | + size["gzip7"]=$(stat -c%s "${filename}7.gz") |
| 14 | + payloadData="$payloadData\"gzip7/$label\": ${size["gzip7"]}, " |
| 15 | + |
| 16 | + gzip -9 $filename -c >> "${filename}9.gz" |
| 17 | + size["gzip9"]=$(stat -c%s "${filename}9.gz") |
| 18 | + payloadData="$payloadData\"gzip9/$label\": ${size["gzip9"]}, " |
| 19 | +} |
| 20 | + |
| 21 | +# Check whether the file size is under limit |
| 22 | +# Write to global variable $failed |
| 23 | +# Read from global variables $size, $size7, $size9, $label, $limitUncompressed |
| 24 | +checkSize() { |
| 25 | + for fileType in "uncompressed" "gzip7" "gzip9"; do |
| 26 | + if [[ ${size[$fileType]} -gt ${payloadLimits[$name, $fileType, $label]} ]]; then |
| 27 | + failed=true |
| 28 | + echo "$fileType $label size is ${size[$fileType]} which is greater than ${payloadLimits[$name, $fileType, $label]}" |
| 29 | + fi |
| 30 | + done |
| 31 | +} |
| 32 | + |
| 33 | +# Write timestamp to global variable $payloadData |
| 34 | +addTimestamp() { |
| 35 | + # Add Timestamp |
| 36 | + timestamp=$(date +%s) |
| 37 | + payloadData="$payloadData\"timestamp\": $timestamp, " |
| 38 | +} |
| 39 | + |
| 40 | +# Write travis commit message to global variable $payloadData |
| 41 | +addMessage() { |
| 42 | + message=$(git log --oneline $TRAVIS_COMMIT_RANGE) |
| 43 | + message=$(echo $message | sed 's/"/\\"/g' | sed 's/\\/\\\\/g') |
| 44 | + payloadData="$payloadData\"message\": \"$message\"" |
| 45 | +} |
| 46 | + |
| 47 | +# Add change source: application, dependencies, or 'application+dependencies' |
| 48 | +# Read from global variables $parentDir |
| 49 | +# Update the change source to global variable $payloadData |
| 50 | +addChange() { |
| 51 | + yarnChanged=false |
| 52 | + allChangedFiles=$(git diff --name-only $TRAVIS_COMMIT_RANGE $parentDir | wc -l) |
| 53 | + allChangedFileNames=$(git diff --name-only $TRAVIS_COMMIT_RANGE $parentDir) |
| 54 | + |
| 55 | + if [[ $allChangedFileNames == *"yarn.lock"* ]]; then |
| 56 | + yarnChanged=true |
| 57 | + fi |
| 58 | + |
| 59 | + if [[ $allChangedFiles -eq 1 ]] && [[ "$yarnChanged" = true ]]; then |
| 60 | + # only yarn.lock changed |
| 61 | + change='dependencies' |
| 62 | + elif [[ $allChangedFiles -gt 1 ]] && [[ "$yarnChanged" = true ]]; then |
| 63 | + change='application+dependencies' |
| 64 | + elif [[ $allChangedFiles -gt 0 ]]; then |
| 65 | + change='application' |
| 66 | + else |
| 67 | + # Nothing changed in aio/ |
| 68 | + exit 0 |
| 69 | + fi |
| 70 | + payloadData="$payloadData\"change\": \"$change\", " |
| 71 | +} |
| 72 | + |
| 73 | +# Upload data to firebase database if it's commit, print out data for pull |
| 74 | +# requests |
| 75 | +uploadData() { |
| 76 | + name="$1" |
| 77 | + payloadData="{${payloadData}}" |
| 78 | + |
| 79 | + echo The data for $name is: |
| 80 | + echo $payloadData |
| 81 | + |
| 82 | + if [[ "$TRAVIS_PULL_REQUEST" == "false" ]]; then |
| 83 | + readonly safeBranchName=$(echo $TRAVIS_BRANCH | sed -e 's/\./_/g') |
| 84 | + readonly dbPath=/payload/$name/$safeBranchName/$TRAVIS_COMMIT |
| 85 | + |
| 86 | + # WARNING: FIREBASE_TOKEN should NOT be printed. |
| 87 | + set +x |
| 88 | + firebase database:update --data "$payloadData" --project $PROJECT_NAME --confirm --token "$ANGULAR_PAYLOAD_FIREBASE_TOKEN" $dbPath |
| 89 | + fi |
| 90 | +} |
| 91 | + |
| 92 | +# Track payload size, $1 is the name in database, $2 is the file path |
| 93 | +# $3 is whether we check the payload size and fail the test if the size exceeds |
| 94 | +# limit, $4 is whether record the type of changes: true | false |
| 95 | +trackPayloadSize() { |
| 96 | + name="$1" |
| 97 | + path="$2" |
| 98 | + checkSize="$3" |
| 99 | + trackChange=$4 |
| 100 | + |
| 101 | + payloadData="" |
| 102 | + failed=false |
| 103 | + for filename in $path; do |
| 104 | + declare -A size |
| 105 | + calculateSize |
| 106 | + if [[ $checkSize = true ]]; then |
| 107 | + checkSize |
| 108 | + fi |
| 109 | + done |
| 110 | + addTimestamp |
| 111 | + if [[ $trackChange = true ]]; then |
| 112 | + addChange |
| 113 | + fi |
| 114 | + addMessage |
| 115 | + uploadData $name |
| 116 | + if [[ $failed = true ]]; then |
| 117 | + echo exit 1 |
| 118 | + exit 1 |
| 119 | + fi |
| 120 | +} |
0 commit comments