src/Tools/jEdit/lib/Tools/jedit
author wenzelm
Wed, 08 Jun 2011 21:40:54 +0200
changeset 43286 a319da4fbfb0
parent 43285 966f359dc724
child 43287 acc680ab6204
permissions -rwxr-xr-x
simplified directory structure;

#!/usr/bin/env bash
#
# Author: Makarius
#
# DESCRIPTION: Isabelle/jEdit interface wrapper


## sources

declare -a SOURCE_FILES=(
  "src/dockable.scala"
  "src/document_model.scala"
  "src/document_view.scala"
  "src/html_panel.scala"
  "src/isabelle_encoding.scala"
  "src/isabelle_hyperlinks.scala"
  "src/isabelle_markup.scala"
  "src/isabelle_options.scala"
  "src/isabelle_sidekick.scala"
  "src/output_dockable.scala"
  "src/plugin.scala"
  "src/protocol_dockable.scala"
  "src/raw_output_dockable.scala"
  "src/scala_console.scala"
  "src/session_dockable.scala"
)

declare -a MORE_FILES=(
  "src/actions.xml"
  "src/dockables.xml"
  "src/Isabelle.props"
  "src/services.xml"
)


## diagnostics

PRG="$(basename "$0")"

function usage()
{
  echo
  echo "Usage: isabelle $PRG [OPTIONS] [FILES ...]"
  echo
  echo "  Options are:"
  echo "    -J OPTION    add JVM runtime option"
  echo "                 (default JEDIT_JAVA_OPTIONS=$JEDIT_JAVA_OPTIONS)"
  echo "    -b           build only"
  echo "    -d           enable debugger"
  echo "    -f           fresh build"
  echo "    -j OPTION    add jEdit runtime option"
  echo "                 (default JEDIT_OPTIONS=$JEDIT_OPTIONS)"
  echo "    -l NAME      logic image name (default ISABELLE_LOGIC=$ISABELLE_LOGIC)"
  echo "    -m MODE      add print mode for output"
  echo
  echo "Start jEdit with Isabelle plugin setup and opens theory FILES"
  echo
  exit 1
}

function fail()
{
  echo "$1" >&2
  exit 2
}

function failed()
{
  fail "Failed!"
}


## process command line

# options

BUILD_ONLY=false
BUILD_JARS="jars"
JEDIT_LOGIC="$ISABELLE_LOGIC"
JEDIT_PRINT_MODE=""

function getoptions()
{
  OPTIND=1
  while getopts "J:bdfj:l:m:" OPT
  do
    case "$OPT" in
      J)
        JAVA_ARGS["${#JAVA_ARGS[@]}"]="$OPTARG"
        ;;
      b)
        BUILD_ONLY=true
        ;;
      d)
        JAVA_ARGS["${#JAVA_ARGS[@]}"]="-Xdebug"
        JAVA_ARGS["${#JAVA_ARGS[@]}"]="-Xrunjdwp:transport=dt_socket,server=y,suspend=n"
        ;;
      f)
        BUILD_JARS="jars_fresh"
        ;;
      j)
        ARGS["${#ARGS[@]}"]="$OPTARG"
        ;;
      l)
        JEDIT_LOGIC="$OPTARG"
        ;;
      m)
        if [ -z "$JEDIT_PRINT_MODE" ]; then
          JEDIT_PRINT_MODE="$OPTARG"
        else
          JEDIT_PRINT_MODE="$JEDIT_PRINT_MODE,$OPTARG"
        fi
        ;;
      \?)
        usage
        ;;
    esac
  done
}

declare -a JAVA_ARGS; eval "JAVA_ARGS=($JEDIT_JAVA_OPTIONS $JEDIT_SYSTEM_OPTIONS)"
[ -n "$SCALA_HOME" ] && JAVA_ARGS["${#JAVA_ARGS[@]}"]="-Dscala.home=$SCALA_HOME"

declare -a ARGS; eval "ARGS=($JEDIT_OPTIONS)"

declare -a OPTIONS; eval "OPTIONS=($ISABELLE_JEDIT_OPTIONS)"
getoptions "${OPTIONS[@]}"

getoptions "$@"
shift $(($OPTIND - 1))


# args

while [ "$#" -gt 0 ]
do
  ARGS["${#ARGS[@]}"]="$(jvmpath "$1")"
  shift
done


## dependencies

pushd "$JEDIT_HOME" >/dev/null || failed

JEDIT_JAR="$ISABELLE_JEDIT_BUILD_HOME/contrib/$ISABELLE_JEDIT_BUILD_VERSION/jedit.jar"

JEDIT_JARS=(
  "$ISABELLE_JEDIT_BUILD_HOME/contrib/Console.jar"
  "$ISABELLE_JEDIT_BUILD_HOME/contrib/ErrorList.jar"
  "$ISABELLE_JEDIT_BUILD_HOME/contrib/Hyperlinks.jar"
  "$ISABELLE_JEDIT_BUILD_HOME/contrib/SideKick.jar"
  "$ISABELLE_JEDIT_BUILD_HOME/contrib/cobra.jar"
  "$ISABELLE_JEDIT_BUILD_HOME/contrib/js.jar"
)

SCALA_JARS=(
  "$SCALA_HOME/lib/scala-compiler.jar"
  "$SCALA_HOME/lib/scala-library.jar"
  "$SCALA_HOME/lib/scala-swing.jar"
)


# target

TARGET="dist/jars/Isabelle-jEdit.jar"

if [ "$BUILD_JARS" = jars_fresh ]; then
  OUTDATED=true
else
  OUTDATED=false
  for SOURCE in "${SOURCE_FILES[@]}" "${MORE_FILES[@]}" "$JEDIT_JAR" "${JEDIT_JARS[@]}"
  do
    [ ! -e "$SOURCE" ] && fail "Missing file: $SOURCE"
    [ ! -e "$TARGET" -o "$SOURCE" -nt "$TARGET" ] && OUTDATED=true
  done
fi


# build

if [ "$OUTDATED" = true ]
then
  [ -z "$SCALA_HOME" ] && fail "Unknown SCALA_HOME -- Scala unavailable"

  [ -z "$ISABELLE_JEDIT_BUILD_HOME" ] && \
    fail "Unknown ISABELLE_JEDIT_BUILD_HOME -- missing auxiliary component"

  [ -e "$ISABELLE_HOME/Admin/build" ] && \
    { "$ISABELLE_HOME/Admin/build" "$BUILD_JARS" || exit $?; }


  echo "###"
  echo "### Building Isabelle/jEdit ..."
  echo "###"

  rm -rf dist || failed
  mkdir -p dist dist/classes || failed
  cp -a "$ISABELLE_JEDIT_BUILD_HOME/contrib/$ISABELLE_JEDIT_BUILD_VERSION/." dist/.

  cp -a "${MORE_FILES[@]}" dist/classes/.
  cp -a dist-template/. dist/.

  perl -i -e 'while (<>) { if (m/NAME="javacc"/) {
    print qq,<MODE NAME="isabelle" FILE="isabelle.xml" FILE_NAME_GLOB="*.thy"/>\n\n,;
    print qq,<MODE NAME="isabelle-session" FILE="isabelle-session.xml" FILE_NAME_GLOB="session.root"/>\n\n,; }
    print; }' dist/modes/catalog

  cp -a "${JEDIT_JARS[@]}" "${SCALA_JARS[@]}" "$ISABELLE_HOME/lib/classes/Pure.jar" \
    dist/jars/. || failed

  (
    for JAR in "$JEDIT_JAR" "${JEDIT_JARS[@]}" "$SCALA_HOME/lib/scala-compiler.jar"
    do
      CLASSPATH="$CLASSPATH:$JAR"
    done
    CLASSPATH="$(jvmpath "$CLASSPATH")"

    exec "$SCALA_HOME/bin/scalac" -unchecked -deprecation \
      -d dist/classes -target:jvm-1.5 "${SOURCE_FILES[@]}"
  ) || fail "Failed to compile sources"

  cd dist/classes
  jar cf "../jars/Isabelle-jEdit.jar" * || failed
  cd ../..
  rm -rf dist/classes
fi

popd >/dev/null


## main

# perspective

mkdir -p "$JEDIT_SETTINGS/DockableWindowManager"

if [ ! -e "$JEDIT_SETTINGS/perspective.xml" ]; then
  cat > "$JEDIT_SETTINGS/DockableWindowManager/perspective-view0.xml" <<EOF
<DOCKING LEFT="" TOP="" RIGHT="" BOTTOM="isabelle-session" LEFT_POS="0" TOP_POS="0" RIGHT_POS="250" BOTTOM_POS="250" />
EOF
  cat > "$JEDIT_SETTINGS/perspective.xml" <<EOF
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE PERSPECTIVE SYSTEM "perspective.dtd">
<PERSPECTIVE>
<VIEW PLAIN="FALSE">
<GEOMETRY X="0" Y="35" WIDTH="1072" HEIGHT="787" EXT_STATE="0" />
</VIEW>
</PERSPECTIVE>
EOF
fi


# run

[ "$BUILD_ONLY" = true ] || {
  case "$JEDIT_LOGIC" in
    /*)
      ;;
    */*)
      JEDIT_LOGIC="$(pwd -P)/$JEDIT_LOGIC"
      ;;
  esac

  export JEDIT_LOGIC JEDIT_PRINT_MODE

  exec "$ISABELLE_TOOL" java "${JAVA_ARGS[@]}" \
    -jar "$(jvmpath "$JEDIT_HOME/dist/jedit.jar")" \
    "-settings=$(jvmpath "$JEDIT_SETTINGS")" "${ARGS[@]}"
}