Ctrl K
ComunidadGuías…

build.sh

Rama main · réplica de lectura del repositorio original.

Archivo original ↗Preguntar a KiBot
#!/usr/bin/env bash
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0

set -euo pipefail

# Releases must use `./build.sh publish` (or stamp -> build -> verify-dist -> upload) so
# artifacts carry the display-only release version.

# 1. Build frontend (Vite outputs to dist/)
cd studio/frontend

rm -rf dist

# Tailwind v4's oxide scanner honours parent .gitignore files, and a venv's "*" .gitignore
# hides the .tsx sources; hide them during the build.
_HIDDEN_GITIGNORES=()
_dir="$(pwd)"
while [ "$_dir" != "/" ]; do
    _dir="$(dirname "$_dir")"
    if [ -f "$_dir/.gitignore" ] && grep -qx '\*' "$_dir/.gitignore" 2>/dev/null; then
        mv "$_dir/.gitignore" "$_dir/.gitignore._twbuild"
        _HIDDEN_GITIGNORES+=("$_dir/.gitignore")
    fi
done

_restore_gitignores() {
    for _gi in "${_HIDDEN_GITIGNORES[@]+"${_HIDDEN_GITIGNORES[@]}"}"; do
        mv "${_gi}._twbuild" "$_gi" 2>/dev/null || true
    done
}
trap _restore_gitignores EXIT

# UNSLOTH_NPM_REGISTRY overrides frontend/.npmrc's registry for corporate mirrors.
_NPM_REGISTRY_ARGS=()
if [ -n "${UNSLOTH_NPM_REGISTRY:-}" ]; then
    _NPM_REGISTRY_ARGS=(--registry "$UNSLOTH_NPM_REGISTRY")
fi

# package-lock.json always wins (`npm ci`); bun only without one, since bun.lock is gitignored.
_install_ok=false
if [ ! -f package-lock.json ] && [ -f bun.lock ] && command -v bun &>/dev/null; then
    if bun install --frozen-lockfile "${_NPM_REGISTRY_ARGS[@]+"${_NPM_REGISTRY_ARGS[@]}"}"; then
        _install_ok=true
    else
        echo "⚠ bun install failed, falling back to npm"
        rm -rf node_modules
    fi
fi
if [ "$_install_ok" != "true" ]; then
    _npm_install=install
    [ -f package-lock.json ] && _npm_install=ci
    if ! npm "$_npm_install" "${_NPM_REGISTRY_ARGS[@]+"${_NPM_REGISTRY_ARGS[@]}"}"; then
        echo "❌ ERROR: package install failed" >&2
        echo "   If you are behind a corporate firewall/proxy, set UNSLOTH_NPM_REGISTRY to your mirror and retry, e.g.:" >&2
        echo "   UNSLOTH_NPM_REGISTRY=https://your-mirror.example/api/npm/ ./build.sh" >&2
        exit 1
    fi
fi
npm run build

_restore_gitignores
trap - EXIT

# Catch truncated Tailwind builds before packaging.
MAX_CSS_SIZE=$(find dist/assets -name '*.css' -exec wc -c {} + 2>/dev/null | sort -n | tail -1 | awk '{print $1}')
if [ -z "$MAX_CSS_SIZE" ]; then
    echo "❌ ERROR: No CSS files were emitted into dist/assets."
    echo "   The frontend build may have failed silently."
    exit 1
fi
if [ "$MAX_CSS_SIZE" -lt 100000 ]; then
    echo "❌ ERROR: Largest CSS file is only $((MAX_CSS_SIZE / 1024))KB (expected >100KB)."
    echo "   Tailwind may not have scanned all source files."
    echo "   Check for .gitignore files blocking the Tailwind oxide scanner."
    exit 1
fi
echo "✅ Frontend CSS validated (${MAX_CSS_SIZE} bytes)"

cd ../..

# 2. Clean old artifacts
rm -rf build dist *.egg-info

# 3. Stamp display-only Unsloth release metadata for packaged builds.
_STUDIO_BUILD_INFO="studio/backend/utils/_studio_release_build.py"
_STUDIO_BUILD_INFO_BACKUP="$(mktemp)"
cp "$_STUDIO_BUILD_INFO" "$_STUDIO_BUILD_INFO_BACKUP"
_restore_studio_build_info() {
    cp "$_STUDIO_BUILD_INFO_BACKUP" "$_STUDIO_BUILD_INFO" 2>/dev/null || true
    rm -f "$_STUDIO_BUILD_INFO_BACKUP"
}
trap _restore_studio_build_info EXIT

if [ "${1:-}" = "publish" ]; then
    STUDIO_STAMPED_VERSION="$(python scripts/stamp_studio_release.py --require-release)"
else
    STUDIO_STAMPED_VERSION="$(python scripts/stamp_studio_release.py)"
fi

# 4. Build wheel/sdist.
python -m build

if [ "${1:-}" = "publish" ]; then
    python scripts/stamp_studio_release.py --verify-dist dist --expected "$STUDIO_STAMPED_VERSION"
fi

_restore_studio_build_info
trap - EXIT

# 5. Optionally publish. Wheel only: the sdist is still built for --verify-dist, but the
# py3-none-any wheel covers every installer and halves the size against PyPI's limit.
if [ "${1:-}" = "publish" ]; then
    python -m twine upload dist/*.whl
fi