This is an automated email from the ASF dual-hosted git repository.

yiconghuang pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/texera.git


The following commit(s) were added to refs/heads/main by this push:
     new f03947ceda feat(bin): add some helper scripts (#3890)
f03947ceda is described below

commit f03947ceda915f372fb1c7afde06e8d03004cd9b
Author: Yicong Huang <[email protected]>
AuthorDate: Tue Oct 14 21:05:25 2025 -0700

    feat(bin): add some helper scripts (#3890)
    
    This PR adds two useful utility scripts:
    1. `bin/utils/resolve-texera-home.sh`, automatically resolve
    `TEXERA_HOME`.
    2. `bin/utils/texera-logging.sh`, a colorful logging helper with Texera
    line header: `Texera ▶`.
    
    To demonstrate how to use those two scripts, I have included an example
    script that uses the utility scripts above.
    - `bin/fix-format.sh`, a handy script to fix formats, can optionally
    choose which component to fix.
    
    By default, it is recommended to execute scripts from root. For example,
    use `bin/fix-format.sh` to fix format. But all the scripts in this PR
    should also be working inside `bin` directory as well.
---
 bin/fix-format.sh                | 110 +++++++++++++++++++++++++++++++++++++++
 bin/utils/resolve-texera-home.sh |  83 +++++++++++++++++++++++++++++
 bin/utils/texera-logging.sh      |  80 ++++++++++++++++++++++++++++
 3 files changed, 273 insertions(+)

diff --git a/bin/fix-format.sh b/bin/fix-format.sh
new file mode 100755
index 0000000000..f9b256a9ab
--- /dev/null
+++ b/bin/fix-format.sh
@@ -0,0 +1,110 @@
+#!/usr/bin/env bash
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+# -------------------------------------------------------------
+# fix-format.sh
+# -------------------------------------------------------------
+# Runs code formatters for the Texera repository.
+#
+# Usage:
+#   bin/fix-format.sh               # Format all (Scala, Frontend, Python)
+#   bin/fix-format.sh --scala       # Format Scala only
+#   bin/fix-format.sh --frontend    # Format Frontend only
+#   bin/fix-format.sh --python      # Format Python only
+# -------------------------------------------------------------
+
+set -euo pipefail
+
+SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
+
+
+source "$SCRIPT_DIR/utils/texera-logging.sh"
+
+TEXERA_HOME="$(/usr/bin/env bash "$SCRIPT_DIR/utils/resolve-texera-home.sh")"
+if [[ -z "${TEXERA_HOME:-}" ]]; then
+  exit 1
+fi
+
+# --- Key directories ---
+FRONTEND_DIR="$TEXERA_HOME/frontend"
+AMBER_PY_DIR="$TEXERA_HOME/amber/src/main/python"
+
+[[ -d "$FRONTEND_DIR" ]] || { tx_error "Frontend directory not found: 
$FRONTEND_DIR"; exit 1; }
+[[ -d "$AMBER_PY_DIR"  ]] || { tx_error "Amber Python directory not found: 
$AMBER_PY_DIR"; exit 1; }
+
+# --- Argument parsing ---
+TARGET="${1:-all}"
+run_scala=false
+run_frontend=false
+run_python=false
+
+case "$TARGET" in
+  --scala)    run_scala=true ;;
+  --frontend) run_frontend=true ;;
+  --python)   run_python=true ;;
+  ""|--all|all) run_scala=true; run_frontend=true; run_python=true ;;
+  *)
+    tx_error "Unknown option: $TARGET"
+    echo "Usage: bin/fix-format.sh [--scala | --frontend | --python | --all]"
+    exit 1
+    ;;
+esac
+
+# --- 1) Scala formatting ---
+if $run_scala; then
+  tx_info "Running sbt scalafmtAll at repo root..."
+  if ! command -v sbt >/dev/null 2>&1; then
+    tx_error "sbt not found. Please install sbt."
+    exit 1
+  fi
+  (
+    cd "$TEXERA_HOME"
+    sbt scalafmtAll
+  )
+  tx_success "Scala formatting completed."
+fi
+
+# --- 2) Frontend formatting ---
+if $run_frontend; then
+  tx_info "Running yarn format:fix in frontend..."
+  if ! command -v yarn >/dev/null 2>&1; then
+    tx_error "yarn not found. Please install Yarn."
+    exit 1
+  fi
+  (
+    cd "$FRONTEND_DIR"
+    yarn format:fix
+  )
+  tx_success "Frontend formatting completed."
+fi
+
+# --- 3) Python formatting ---
+if $run_python; then
+  tx_info "Running black in amber/src/main/python..."
+  if ! command -v black >/dev/null 2>&1; then
+    tx_error "black not found. Install with: pip install black"
+    exit 1
+  fi
+  (
+    cd "$AMBER_PY_DIR"
+    black .
+  )
+  tx_success "Python formatting completed."
+fi
+
+tx_success "✅ Formatting tasks completed successfully!"
\ No newline at end of file
diff --git a/bin/utils/resolve-texera-home.sh b/bin/utils/resolve-texera-home.sh
new file mode 100755
index 0000000000..ccc736c7b4
--- /dev/null
+++ b/bin/utils/resolve-texera-home.sh
@@ -0,0 +1,83 @@
+#!/usr/bin/env bash
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+# -------------------------------------------------------------
+# resolve-texera-home.sh
+# -------------------------------------------------------------
+# Determines TEXERA_HOME using the following priority:
+#   1. TEXERA_HOME environment variable (if already set)
+#   2. git repository root (if inside a git repo)
+#   3. parent directory if current dir is 'bin'
+#   4. current working directory (.)
+#
+# Prints the resolved TEXERA_HOME to stdout.
+# Logs human-readable messages via texera-logging.sh.
+#
+# Intended usage:
+#   TEXERA_HOME="$(bin/resolve-texera-home.sh)"
+#
+# Exits with code 1 if resolution fails.
+# -------------------------------------------------------------
+
+set -euo pipefail
+
+SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
+
+# --- Load logging helper ---
+# shellcheck source=bin/texera-logging.sh
+source "$SCRIPT_DIR/texera-logging.sh"
+
+resolve_texera_home() {
+  # 1. TEXERA_HOME environment variable
+  if [[ -n "${TEXERA_HOME:-}" ]]; then
+    tx_info "TEXERA_HOME found in environment: $TEXERA_HOME"
+    echo "$TEXERA_HOME"
+    return 0
+  fi
+
+  # 2. Git repository root (if any)
+  if git -C . rev-parse --show-toplevel >/dev/null 2>&1; then
+    local root
+    root="$(git rev-parse --show-toplevel)"
+    tx_info "TEXERA_HOME resolved via git repository root: $root"
+    echo "$root"
+    return 0
+  fi
+
+  # 3. Parent directory if current folder is 'bin'
+  local cwd cwd_basename
+  cwd="$(pwd)"
+  cwd_basename="$(basename "$cwd")"
+  if [[ "$cwd_basename" == "bin" ]]; then
+    local parent
+    parent="$(dirname "$cwd")"
+    tx_info "TEXERA_HOME resolved as parent of bin/: $parent"
+    echo "$parent"
+    return 0
+  fi
+
+  # 4. Fallback to current working directory
+  tx_warn "Falling back to current working directory as TEXERA_HOME: $cwd"
+  echo "$cwd"
+}
+
+# --- Main execution ---
+if ! resolve_texera_home; then
+  tx_error "Failed to resolve TEXERA_HOME."
+  exit 1
+fi
\ No newline at end of file
diff --git a/bin/utils/texera-logging.sh b/bin/utils/texera-logging.sh
new file mode 100755
index 0000000000..ad0dc9b1d7
--- /dev/null
+++ b/bin/utils/texera-logging.sh
@@ -0,0 +1,80 @@
+#!/usr/bin/env bash
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+# -------------------------------------------------------------
+# texera-logging.sh
+# -------------------------------------------------------------
+# Shared logging utilities for all Texera bash scripts.
+#
+# Features:
+#   • Colored, consistent logs prefixed with "Texera ▶"
+#   • Logs go to STDERR by default (so STDOUT stays clean)
+#   • Easy to override prefix or disable colors
+#
+# Env vars:
+#   TEXERA_LOG_PREFIX="Texera ▸"   # change prefix
+#   NO_COLOR=1                     # disable color
+#   TEXERA_LOG_TO_STDERR=0         # send to STDOUT instead
+# -------------------------------------------------------------
+
+# Prevent direct execution
+if [[ "${BASH_SOURCE[0]}" == "$0" ]]; then
+  printf 'Texera ▶ This file must be sourced, not executed.\n' >&2
+  exit 1
+fi
+
+# --- Color setup ---
+_use_color=true
+if [[ -n "${NO_COLOR:-}" ]]; then
+  _use_color=false
+fi
+
+if $_use_color; then
+  readonly TL_COLOR_BLUE=$'\033[34m'
+  readonly TL_COLOR_GREEN=$'\033[32m'
+  readonly TL_COLOR_YELLOW=$'\033[33m'
+  readonly TL_COLOR_RED=$'\033[31m'
+  readonly TL_COLOR_BOLD=$'\033[1m'
+  readonly TL_COLOR_RESET=$'\033[0m'
+else
+  readonly TL_COLOR_BLUE="" TL_COLOR_GREEN="" TL_COLOR_YELLOW="" 
TL_COLOR_RED="" TL_COLOR_BOLD="" TL_COLOR_RESET=""
+fi
+
+# --- Prefix & output stream ---
+readonly TEXERA_LOG_PREFIX="${TEXERA_LOG_PREFIX:-Texera ▶}"
+readonly TL_PREFIX="${TL_COLOR_BOLD}${TEXERA_LOG_PREFIX}${TL_COLOR_RESET}"
+readonly _TL_TO_STDERR="${TEXERA_LOG_TO_STDERR:-1}"
+
+# --- Core emitter ---
+_tx_emit() {
+  local _color="$1"; shift
+  local _level="$1"; shift
+  if [[ "$_TL_TO_STDERR" == "1" ]]; then
+    printf '%s %s[%s]%s %s\n' "$TL_PREFIX" "$_color" "$_level" 
"$TL_COLOR_RESET" "$*" >&2
+  else
+    printf '%s %s[%s]%s %s\n' "$TL_PREFIX" "$_color" "$_level" 
"$TL_COLOR_RESET" "$*"
+  fi
+}
+
+# --- Public API ---
+tx_info()    { _tx_emit "$TL_COLOR_BLUE"   "INFO" "$*"; }
+tx_success() { _tx_emit "$TL_COLOR_GREEN"  "SUCCESS" "$*"; }
+tx_warn()    { _tx_emit "$TL_COLOR_YELLOW" "WARN" "$*"; }
+tx_error()   { _tx_emit "$TL_COLOR_RED"    "ERROR" "$*"; }
+
+export -f tx_info tx_success tx_warn tx_error
\ No newline at end of file

Reply via email to