|
| 1 | +#!/usr/bin/bash |
| 2 | + |
| 3 | +# Copyright 2025 Adobe. All rights reserved. |
| 4 | +# This file is licensed to you under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. You may obtain a copy |
| 6 | +# of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + |
| 8 | +# Unless required by applicable law or agreed to in writing, software distributed under |
| 9 | +# the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS |
| 10 | +# OF ANY KIND, either express or implied. See the License for the specific language |
| 11 | +# governing permissions and limitations under the License. |
| 12 | + |
| 13 | +set -euo pipefail |
| 14 | + |
| 15 | +usage() { |
| 16 | + echo "Usage: tasks/remove-modifiers.sh <component> [username]" 1>&2 |
| 17 | + echo "" 1>&2 |
| 18 | + echo "Arguments:" 1>&2 |
| 19 | + echo " component The component folder name under components/ (e.g. breadcrumb)" 1>&2 |
| 20 | + echo " username Optional. Overrides the username segment used for the branch name." 1>&2 |
| 21 | + echo "" 1>&2 |
| 22 | + echo "This script performs:" 1>&2 |
| 23 | + echo " 1) git checkout spectrum-two" 1>&2 |
| 24 | + echo " 2) git checkout -b <username>/feat-<component>-modifier-removal" 1>&2 |
| 25 | + echo " 3) git checkout origin/castastrophe/feat-remove-all-modifiers -- components/<component>" 1>&2 |
| 26 | + echo " 4) Prompts you to manually review components/<component>/index.css" 1>&2 |
| 27 | + echo " 5) Updates .changeset/weak-colts-divide.md to include the component package" 1>&2 |
| 28 | + echo " 6) Commits the changes with the appropriate message" 1>&2 |
| 29 | +} |
| 30 | + |
| 31 | +if [[ ${1:-} == "-h" || ${1:-} == "--help" || $# -lt 1 ]]; then |
| 32 | + usage |
| 33 | + exit 1 |
| 34 | +fi |
| 35 | + |
| 36 | +component="$1" |
| 37 | +username_override="${2:-}" |
| 38 | + |
| 39 | +if [[ -z "$component" ]]; then |
| 40 | + # Prompt for component if not provided |
| 41 | + read -p "Enter the component name: " component |
| 42 | + |
| 43 | + if [[ -z "$component" ]]; then |
| 44 | + echo "Error: component is required." 1>&2 |
| 45 | + usage |
| 46 | + exit 1 |
| 47 | + fi |
| 48 | +fi |
| 49 | + |
| 50 | +# Ensure working tree is clean to avoid accidental data loss |
| 51 | +if ! git diff-index --quiet HEAD --; then |
| 52 | + echo "Error: working tree has uncommitted changes. Please commit or stash before running." 1>&2 |
| 53 | + exit 1 |
| 54 | +fi |
| 55 | + |
| 56 | +# Helper function to return to the return branch if something goes wrong |
| 57 | +return_to_branch() { |
| 58 | + git checkout castastrophe/feat-remove-all-modifiers |
| 59 | + exit 1 |
| 60 | +} |
| 61 | + |
| 62 | +derive_username() { |
| 63 | + local raw |
| 64 | + if raw=$(git config --get user.name 2>/dev/null); then |
| 65 | + : |
| 66 | + else |
| 67 | + raw="" |
| 68 | + fi |
| 69 | + if [[ -z "$raw" ]]; then |
| 70 | + if raw=$(git config --get user.email 2>/dev/null); then |
| 71 | + raw="${raw%@*}" |
| 72 | + fi |
| 73 | + fi |
| 74 | + # Lowercase and remove anything that's not a-z or 0-9 to satisfy branch naming guidance |
| 75 | + raw=$(printf "%s" "$raw" | tr '[:upper:]' '[:lower:]' | tr -cd 'a-z0-9') |
| 76 | + if [[ -z "$raw" ]]; then |
| 77 | + raw="user" |
| 78 | + fi |
| 79 | + printf "%s" "$raw" |
| 80 | +} |
| 81 | + |
| 82 | +username_segment="${username_override}" |
| 83 | +if [[ -z "$username_segment" ]]; then |
| 84 | + username_segment="$(derive_username)" |
| 85 | +fi |
| 86 | + |
| 87 | +branch_name="${username_segment}/feat-${component}-modifier-removal" |
| 88 | + |
| 89 | +echo "→ Checking out base branch: spectrum-two" |
| 90 | +git checkout spectrum-two || return_to_branch |
| 91 | +git pull origin spectrum-two || return_to_branch |
| 92 | + |
| 93 | +echo "→ Creating feature branch: ${branch_name}" |
| 94 | +git checkout -b "$branch_name" || return_to_branch |
| 95 | + |
| 96 | +echo "→ Checking out component from remote reference: components/${component}" |
| 97 | +git checkout origin/castastrophe/feat-remove-all-modifiers -- "components/${component}" || return_to_branch |
| 98 | + |
| 99 | +component_css="components/${component}/index.css" |
| 100 | +if [[ ! -f "$component_css" ]]; then |
| 101 | + echo "Warning: ${component_css} does not exist yet. Continuing." 1>&2 |
| 102 | +else |
| 103 | + echo "→ Please review ${component_css} and perform the following manual cleanup:" |
| 104 | + echo " - Remove unnecessary custom property mappings (especially --mod-* hooks)." |
| 105 | + echo " - Remove any high contrast styles that match default WHCM behavior." |
| 106 | + echo " - Keep existing class selectors and variants unchanged." |
| 107 | + echo " When finished, save your changes and return to this terminal." |
| 108 | + read -r -p "Press Enter to continue once manual edits are complete..." _ |
| 109 | +fi |
| 110 | + |
| 111 | +# Once updated, refresh the metadata.json file by running: |
| 112 | +echo "→ Refreshing metadata.json file" |
| 113 | +yarn reporter "$component" --skip-nx-cache || true |
| 114 | + |
| 115 | +changeset_file=".changeset/weak-colts-divide.md" |
| 116 | +package_entry_name="\"@spectrum-css/${component}\"" |
| 117 | +package_entry_line="${package_entry_name}: major" |
| 118 | + |
| 119 | +if [[ -f "$changeset_file" ]]; then |
| 120 | + echo "→ Ensuring ${package_entry_name} is included in ${changeset_file}" |
| 121 | + tmp_file="${changeset_file}.tmp" |
| 122 | + awk -v pkgline="$package_entry_line" -v pkgname="$package_entry_name" ' |
| 123 | + BEGIN { fm=0; found=0 } |
| 124 | + /^---$/ { |
| 125 | + fm++ |
| 126 | + if (fm==2 && !found) { print pkgline } |
| 127 | + print |
| 128 | + next |
| 129 | + } |
| 130 | + { |
| 131 | + if (fm==1 && index($0, pkgname) != 0) { found=1 } |
| 132 | + print |
| 133 | + } |
| 134 | + ' "$changeset_file" > "$tmp_file" |
| 135 | + mv "$tmp_file" "$changeset_file" |
| 136 | +else |
| 137 | + echo "Warning: ${changeset_file} not found. Skipping changeset update." 1>&2 |
| 138 | +fi |
| 139 | + |
| 140 | +echo "→ Staging files" |
| 141 | +git add "components/${component}" || true |
| 142 | +test -f "$changeset_file" && git add "$changeset_file" || true |
| 143 | + |
| 144 | +echo "→ Committing changes" |
| 145 | +git commit -m "feat(${component}): remove modifiers from the API [SWC-1264]" |
| 146 | + |
| 147 | +echo "Done. Branch ${branch_name} is ready with changes for ${component}." |
| 148 | + |
| 149 | +# If the user has the gh cli installed, open the PR |
| 150 | +if command -v gh &> /dev/null; then |
| 151 | + echo "→ Opening PR" |
| 152 | + gh pr create --base spectrum-two --fill --label "ready-for-review,S2,run_vrt" --title "feat(${component}): remove modifiers from the API [SWC-1264]" |
| 153 | +fi |
| 154 | + |
| 155 | +echo "→ Returning to base branch" |
| 156 | +return_to_branch |
0 commit comments