|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +set -Eeuo pipefail |
| 4 | + |
| 5 | +function remove_patches() { |
| 6 | + local -i exit_code=0 |
| 7 | + quilt pop -af || exit_code=$? |
| 8 | + case $exit_code in |
| 9 | + # Sucessfully removed. |
| 10 | + 0) ;; |
| 11 | + # No more patches to remove. |
| 12 | + 2) ;; |
| 13 | + # Some error. |
| 14 | + *) return $exit_code ;; |
| 15 | + esac |
| 16 | +} |
| 17 | + |
| 18 | +function update_vscode() { |
| 19 | + pushd lib/vscode |
| 20 | + if ! git checkout "$VERSION" ; then |
| 21 | + echo "$VERSION does not exist locally, fetching..." |
| 22 | + git fetch --all --prune |
| 23 | + git checkout "$VERSION" |
| 24 | + fi |
| 25 | + popd |
| 26 | +} |
| 27 | + |
| 28 | +function refresh_patches() { |
| 29 | + local -i exit_code=0 |
| 30 | + while quilt push ; ! (( exit_code=$? )) ; do |
| 31 | + quilt refresh |
| 32 | + echo # Extra new line for separation. |
| 33 | + done |
| 34 | + case $exit_code in |
| 35 | + # No more patches to apply. |
| 36 | + 2) ;; |
| 37 | + # Some error. |
| 38 | + *) return $exit_code ;; |
| 39 | + esac |
| 40 | +} |
| 41 | + |
| 42 | +function update_node() { |
| 43 | + local node_version |
| 44 | + node_version=$(cat .node-version) |
| 45 | + if [[ $node_version == $target_node_version ]] ; then |
| 46 | + echo "$node_version already matches $target_node_version" |
| 47 | + else |
| 48 | + echo "Updating from $node_version to $target_node_version..." |
| 49 | + echo "$target_node_version" > .node-version |
| 50 | + fi |
| 51 | +} |
| 52 | + |
| 53 | +function get-webview-script-hash() { |
| 54 | + local html |
| 55 | + html=$(<$1) |
| 56 | + local start_tag='<script async type="module">' |
| 57 | + local end_tag="</script>" |
| 58 | + html=${html##*$start_tag} |
| 59 | + html=${html%%$end_tag*} |
| 60 | + echo -n "$html" | openssl sha256 -binary | openssl base64 |
| 61 | +} |
| 62 | + |
| 63 | +function update_csp() { |
| 64 | + local -i exit_code=0 |
| 65 | + # Move back to the webview patch so it can be refreshed. |
| 66 | + quilt pop webview || exit_code=$? |
| 67 | + case $exit_code in |
| 68 | + # Successfully moved. |
| 69 | + 0) ;; |
| 70 | + # Already at the patch. |
| 71 | + 2) ;; |
| 72 | + # Some error. |
| 73 | + *) return $exit_code ;; |
| 74 | + esac |
| 75 | + local file=lib/vscode/src/vs/workbench/contrib/webview/browser/pre/index.html |
| 76 | + local hash |
| 77 | + hash=$(get-webview-script-hash "$file") |
| 78 | + echo "Calculated hash as $hash" |
| 79 | + # Use octothorpe as a delimiter since the hash may contain a slash. |
| 80 | + sed -i.bak "s#script-src 'sha256-[^']\+'#script-src 'sha256-$hash'#" "$file" |
| 81 | + quilt refresh |
| 82 | + # Get patched back up. |
| 83 | + quilt push -a |
| 84 | +} |
| 85 | + |
| 86 | +function run() { |
| 87 | + local -i failed=0 |
| 88 | + rm -f .cache/checklist |
| 89 | + while (( $# )) ; do |
| 90 | + local name=$1 ; shift |
| 91 | + local fn=$1 ; shift |
| 92 | + # Only run if an earlier step has not failed. |
| 93 | + if [[ $failed == 0 ]] ; then |
| 94 | + echo "[+] $name..." |
| 95 | + if $fn ; then |
| 96 | + echo "- [X] $name" >> .cache/checklist |
| 97 | + else |
| 98 | + ((failed++)) |
| 99 | + fi |
| 100 | + fi |
| 101 | + # For all failed steps, write out an empty checkbox. |
| 102 | + if [[ $failed != 0 ]] ; then |
| 103 | + echo "- [ ] $name" >> .cache/checklist |
| 104 | + fi |
| 105 | + done |
| 106 | + if [[ $failed != 0 ]] ; then |
| 107 | + return 1 |
| 108 | + fi |
| 109 | +} |
| 110 | + |
| 111 | +function add_changelog() { |
| 112 | + local file=CHANGELOG.md |
| 113 | + if grep "Code $VERSION" "$file" ; then |
| 114 | + echo "Changelog for $VERSION already exists" |
| 115 | + else |
| 116 | + # TODO: This is not exactly robust. In particular, it needs to handle if |
| 117 | + # there is already a "changed" section. |
| 118 | + sed -i.bak "s/## Unreleased/## Unreleased\n\nCode v$VERSION\n\n### Changed\n\n- Update to Code $VERSION/" "$file" |
| 119 | + fi |
| 120 | +} |
| 121 | + |
| 122 | +function main() { |
| 123 | + cd "$(dirname "${0}")/../.." |
| 124 | + |
| 125 | + source ./ci/lib.sh |
| 126 | + |
| 127 | + local target_node_version |
| 128 | + target_node_version=$(grep target lib/vscode/remote/.npmrc | awk -F= '{print $2}' | tr -d '"') |
| 129 | + |
| 130 | + declare -a steps |
| 131 | + # Removing patches only needs to be done locally; in CI we start from a fresh |
| 132 | + # clone each time. |
| 133 | + if [[ ! ${CI-} ]] ; then |
| 134 | + steps+=("Remove patches" "remove_patches") |
| 135 | + fi |
| 136 | + |
| 137 | + steps+=( |
| 138 | + "Update VS Code to $VERSION" "update_vscode" |
| 139 | + "Refresh VS Code patches" "refresh_patches" |
| 140 | + "Set Node version to $target_node_version" "update_node" |
| 141 | + "Update CSP webview hash" "update_csp" |
| 142 | + "Add changelog note" "add_changelog" |
| 143 | + ) |
| 144 | + |
| 145 | + run "${steps[@]}" |
| 146 | + |
| 147 | + # This step is always manual. |
| 148 | + echo "- [ ] Verify changelog" >> .cache/checklist |
| 149 | +} |
| 150 | + |
| 151 | +main "$@" |
0 commit comments