Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 110 additions & 0 deletions bin/fix-format.sh
Original file line number Diff line number Diff line change
@@ -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!"
83 changes: 83 additions & 0 deletions bin/utils/resolve-texera-home.sh
Original file line number Diff line number Diff line change
@@ -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
80 changes: 80 additions & 0 deletions bin/utils/texera-logging.sh
Original file line number Diff line number Diff line change
@@ -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
Loading