wh1ter0se/PowerUp-2018

View on GitHub
wpilib18/java/current/ant/build.xml

Summary

Maintainability
Test Coverage
<?xml version="1.0" encoding="utf-8"?>
<project name="athena-project-build"
         default="deploy">
  <property file="${user.home}/wpilib/java/${version}/ant/ni_image.properties" />
  <!-- Load Tasks -->
  <taskdef resource="net/sf/antcontrib/antlib.xml">
    <classpath>
      <pathelement location="${wpilib.ant.dir}/ant-contrib.jar" />
    </classpath>
  </taskdef>
  <taskdef resource="net/jtools/classloadertask/antlib.xml"
           classpath="${classloadertask.jar}" />
  <classloader loader="system"
               classpath="${jsch.jar}" />
  <taskdef name="webdav"
           classname="com.github.sardine.ant.SardineTask">
    <classpath>
      <pathelement location="${wpilib.ant.dir}/sardine-5.8-SNAPSHOT.jar" />
      <pathelement location="${wpilib.ant.dir}/httpclient-4.5.3.jar" />
      <pathelement location="${wpilib.ant.dir}/httpcore-4.4.6.jar" />
      <pathelement location="${wpilib.ant.dir}/commons-codec-1.10.jar" />
      <pathelement location="${wpilib.ant.dir}/commons-logging-1.2.jar" />
    </classpath>
  </taskdef>
  <typedef resource="edu/wpi/first/ant/roboriotasks.xml">
    <classpath>
      <pathelement location="${wpilib.ant.dir}/roborio-ant-tasks-1.1.jar" />
      <pathelement location="${wpilib.ant.dir}/httpclient-4.5.3.jar" />
      <pathelement location="${wpilib.ant.dir}/httpcore-4.4.6.jar" />
      <pathelement location="${wpilib.ant.dir}/commons-codec-1.10.jar" />
      <pathelement location="${wpilib.ant.dir}/commons-logging-1.2.jar" />
    </classpath>
  </typedef>
  <target name="clean"
          description="Clean up all build and distribution artifacts.">

    <delete dir="${build.dir}" />
    <delete dir="${dist.dir}" />
  </target>
  <!-- Targets -->
  <target name="get-target-ip">
    <property name="ant.enable.asserts"
              value="true" />
    <assert name="team-number"
            exists="true"
            message="Team number not set. Go to Window-&gt;Preferences-&gt;WPILib Preferences to set it." />
    <echo message="Finding roboRIO..." />
    <var name="target"
         unset="true" />
    <find-roborio team="${team-number}"
                  username="${adminUsername}"
                  password="${adminPassword}"
                  targetProperty="target"
                  imageYearProperty="roboRIOImageYear"
                  imageProperty="roboRIOImage" />
    <if>
      <isset property="target" />
      <then>
        <echo message="roboRIO found at ${target}, image FRC_roboRIO_${roboRIOImageYear}_v${roboRIOImage}" />
      </then>
      <else>
        <assert name="roboRIOFound"
                message="roboRIO not found, please check that the roboRIO is connected, imaged and that the team number is set properly in Eclipse" />
      </else>
    </if>
  </target>
  <target name="compile"
          description="Compile the source code.">
    <mkdir dir="${build.dir}" />
    <path id="classpath.path">
      <fileset dir="${userLibs.dir}"
               includes="*.jar"
               excludes="*-sources.jar,*-javadoc.jar" />
      <fileset file="${wpilib.jar}" />
      <fileset file="${ntcore.jar}" />
      <fileset file="${opencv.jar}" />
      <fileset file="${cscore.jar}" />
      <fileset file="${wpiutil.jar}" />
      <fileset dir="${userLibs}"
               erroronmissingdir="false" />
    </path>
    <pathconvert property="classpathProp"
                 refid="classpath.path" />
    <echo message="[athena-compile] Compiling ${src.dir} with classpath=${classpathProp} to ${build.dir}" />
    <javac srcdir="${src.dir}"
           destdir="${build.dir}"
           includeAntRuntime="no"
           includeJavaRuntime="no"
           classpathref="classpath.path"
           target="${ant.java.version}"
           source="${ant.java.version}"
           compiler="javac${ant.java.version}"
           debug="true"></javac>
  </target>
  <target name="jar"
          depends="compile">
    <echo message="[athena-jar] Making jar ${dist.jar}." />
    <mkdir dir="${dist.dir}" />
    <mkdir dir="${build.jars}" />
    <echo message="[athena-jar] Copying jars to ${build.jars}." />
    <copy todir="${build.jars}"
          flatten="true">
      <path refid="classpath.path" />
    </copy>
    <jar destfile="${dist.jar}"
         update="false">
      <manifest>
        <attribute name="Main-Class"
                   value="edu.wpi.first.wpilibj.RobotBase" />
        <attribute name="Robot-Class"
                   value="${robot.class}" />
        <attribute name="Class-Path"
                   value="." />
      </manifest>
      <fileset dir="${build.dir}"
               includes="**/*.class" />
      <fileset dir="${src.dir}"
               includes="**/*.java" />
      <zipgroupfileset dir="${build.jars}" />
    </jar>
  </target>
  <!-- We're running a clean here to get around a known ant issue where it does not detected changed constant variables.
     To get around this, we're recompiling the entire project, which is not an issue for most teams. If this is an issue
     for you, you can remove the clean here, just be sure to do a full rebuild after you've changed any constants.
     Reference: http://stackoverflow.com/questions/6430001/ant-doesnt-recompile-constants -->
  <target name="deploy"
          depends="clean,jar,get-target-ip,dependencies"
          description="Deploy the jar and start the program running.">

    <deploy-libs libs.name="WPI_Native_Libraries"
                 libs.basedir="${wpilib.native.lib}"
                 libs.deployDir="${libDeploy.dir}">
      <libs.local>
        <fileset id="wpiNativeLibs.local"
                 dir="${wpilib.native.lib}">
          <include name="libwpiHal.so" />
          <include name="libntcore.so" />
          <include name="libwpiutil.so" />
          <include name="libopencv*.so.3.2" />
          <include name="libcscore.so" />
          <include name="libopencv_java320.so" />
          <include name="libwpilibJNI.so" />
        </fileset>
      </libs.local>
    </deploy-libs>
    <deploy-libs libs.name="User_Libraries"
                 libs.basedir="${userLibs.dir}"
                 libs.deployDir="${libDeploy.dir}">
      <libs.local>
        <fileset dir="${userLibs.dir}">
          <include name="**/*.so" />
        </fileset>
      </libs.local>
    </deploy-libs>
    <echo message="[athena-deploy] Copying code over." />
    <webdav username="${adminUsername}"
            password="${adminPassword}">
      <delete url="http://${target}/files${deploy.dir}/${jar}" />
      <put url="http://${target}/files${deploy.dir}/">
        <fileset file="${dist.jar}" />
      </put>
      <delete url="http://${target}/files${command.dir}robotCommand" />
      <put url="http://${target}/files${command.dir}">
        <fileset file="${wpilib.ant.dir}/robotCommand" />
      </put>
    </webdav>
    <echo message="[athena-deploy] Starting program." />
    <sshexec host="${target}"
             username="${username}"
             password="${password}"
             trust="true"
             failonerror="false"
             command="${deploy.kill.command}; sync" />
  </target>
  <target name="debug-deploy"
          depends="clean,jar,get-target-ip,dependencies"
          description="Deploy the jar and start the program running.">

    <deploy-libs libs.name="WPI_Native_Libraries"
                 libs.basedir="${wpilib.native.lib}"
                 libs.deployDir="${libDeploy.dir}">
      <libs.local>
        <fileset id="wpiNativeLibs.local"
                 dir="${wpilib.native.lib}">
          <include name="libwpiHal.so" />
          <include name="libntcore.so" />
          <include name="libwpiutil.so" />
          <include name="libopencv*.so.3.2" />
          <include name="libcscore.so" />
          <include name="libopencv_java320.so" />
          <include name="libwpilibJNI.so" />
        </fileset>
      </libs.local>
    </deploy-libs>
    <deploy-libs libs.name="User_Libraries"
                 libs.basedir="${userLibs.dir}"
                 libs.deployDir="${libDeploy.dir}">
      <libs.local>
        <fileset dir="${userLibs.dir}">
          <include name="**/*.so" />
        </fileset>
      </libs.local>
    </deploy-libs>
    <echo message="[athena-deploy] Copying code over." />
    <webdav username="${adminUsername}"
            password="${adminPassword}">
      <delete url="http://${target}/files${deploy.dir}/${jar}" />
      <put url="http://${target}/files${deploy.dir}/">
        <fileset file="${dist.jar}" />
      </put>
      <!-- The remoteDebugCommand file is used by /usr/local/frc/bin/frcRunRobot.sh on the roboRIO  -->
      <delete url="http://${target}/files${command.dir}/robotDebugCommand" />
      <put url="http://${target}/files${command.dir}/">
        <fileset file="${wpilib.ant.dir}/robotDebugCommand" />
      </put>
      <!-- The frcdebug file is used as a flag for /usr/local/frc/bin/frcRunRobot.sh to run the robot program in debug mode -->
      <delete url="http://${target}/files${debug.flag.dir}/frcdebug" />
      <put url="http://${target}/files${debug.flag.dir}/">
        <fileset file="${wpilib.ant.dir}/frcdebug" />
      </put>
    </webdav>
    <sshexec host="${target}"
             username="${adminUsername}"
             password="${adminPassword}"
             trust="true"
             command="${debug.flag.command}" />
    <echo message="[athena-deploy] Starting Debug program." />
    <sshexec host="${target}"
             username="${username}"
             password="${password}"
             trust="true"
             failonerror="false"
             command="${deploy.kill.command}" />
  </target>
  <!-- Simulate -->
  <target name="jar-for-simulation"
          depends="compile">
    <echo message="[jar-for-simulation] Building jar." />
    <jar destfile="${simulation.dist.jar}">
      <manifest>
        <attribute name="Built-By"
                   value="${user.name}" />
        <attribute name="Robot-Class"
                   value="${robot.class}" />
        <attribute name="Main-Class"
                   value="edu.wpi.first.wpilibj.RobotBase" />
      </manifest>
      <fileset dir="${build.dir}" />
      <zipgroupfileset dir="${wpilib.sim.lib}">
        <include name="**/*.jar" />
      </zipgroupfileset>
    </jar>
  </target>
  <target name="simulate"
          depends="jar-for-simulation">
    <sequential>
      <echo message="[simulate] You may now run Gazebo and your DriverStation" />
      <echo message="[simulate] Running Code." />
      <java jar="${simulation.dist.jar}"
            fork="true">
        <jvmarg value="-Djava.library.path=${wpilib.sim.lib}" />
      </java>
    </sequential>
  </target>
  <target name="debug-simulate"
          depends="jar-for-simulation">
    <sequential>
      <echo message="[simulate] You may now run Gazebo and your DriverStation" />
      <echo message="[simulate] Running Code In Debug Mode." />
      <java jar="${simulation.dist.jar}"
            fork="true">
        <jvmarg value="-Xdebug" />
        <jvmarg value="-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8348" />
        <jvmarg value="-Djava.library.path=${wpilib.sim.lib}" />
      </java>
    </sequential>
  </target>
  <target name="dependencies"
          depends="get-target-ip">
    <property name="ant.enable.asserts"
              value="true" />
    <assert message="Image of roboRIO does not match plugin. ${line.separator}Allowed image year: ${roboRIOAllowedYear} version: ${roboRIOAllowedImages}. ${line.separator}Actual image year:  ${roboRIOImageYear} version: ${roboRIOImage}. ${line.separator}RoboRIO needs to be re-imaged or plugins updated.">

      <bool>
        <and>
          <contains string="${roboRIOAllowedImages}"
                    substring="${roboRIOImage}" />
          <contains string="${roboRIOAllowedYear}"
                    substring="${roboRIOImageYear}" />
        </and>
      </bool>
    </assert>
    <echo message="roboRIO image version validated" />
    <webdav username="${adminUsername}"
            password="${adminPassword}">
      <exists url="http://${target}/files${roboRIOJRE.dir}/bin/java"
              property="jreExists" />
    </webdav>
    <if>
      <not>
        <equals arg1="${jreExists}" arg2="true" />
      </not>
      <then>
        <echo message="Installing Azul Zulu JRE, this may take a couple of minutes..." />
        <webdav username="${adminUsername}"
                password="${adminPassword}">
          <delete url="http://${target}/files/tmp/${roboRIOJRE.ipk}" />
          <put url="http://${target}/files/tmp/">
            <fileset file="${wpilib.ant.dir}/${roboRIOJRE.ipk}" />
          </put>
        </webdav>
        <!-- the sed command is a workaround for an issue in the 2018_v4 image -->
        <sshexec host="${target}"
                 username="${adminUsername}"
                 password="${adminPassword}"
                 trust="true"
                 command="sed -i -e 's/^StartupDLLs/;StartupDLLs/' /etc/natinst/share/ni-rt.ini; opkg remove zulu-jre\*; opkg install /tmp/${roboRIOJRE.ipk}; rm /tmp/${roboRIOJRE.ipk}" />
      </then>
      <else>
        <echo message="JRE installation validated" />
      </else>
    </if>
  </target>
  <!-- libs.name should not contain spaces as it is used to name a file -->
  <macrodef name="deploy-libs">
    <attribute name="libs.name" />
    <attribute name="libs.basedir" />
    <attribute name="libs.deployDir" />
    <element name="libs.local" />
    <sequential>
      <local name="libs.local.notEmpty" />
      <local name="libs.local.checksum" />
      <local name="libs.deployed.checksum" />
      <local name="libs.local.modified.property" />
      <local name="libs.remote.modified.property" />
      <trycatch>
        <try>
          <webdav username="${adminUsername}"
                  password="${adminPassword}">
            <get url="http://${target}/files@{libs.deployDir}/@{libs.name}.properties"
                 file="@{libs.basedir}/@{libs.name}.properties"
                 overwrite="true" />
          </webdav>
        </try>
        <catch>
          <delete file="@{libs.basedir}/@{libs.name}.properties"
                  failonerror="false" />
        </catch>
      </trycatch>
      <restrict id="libs.local.modified">
        <libs.local />
        <modified update="true"
                  seldirs="true"
                  cache="propertyfile"
                  algorithm="digest"
                  comparator="equal">
          <param name="cache.cachefile"
                 value="@{libs.basedir}/@{libs.name}.properties" />
          <param name="algorithm.algorithm"
                 value="MD5" />
        </modified>
      </restrict>
      <pathconvert refid="libs.local.modified"
                   property="libs.local.modified.property"
                   pathsep=","
                   setonempty="false">
        <globmapper from="@{libs.basedir}/*"
                    to="*"
                    handledirsep="true" />
      </pathconvert>
      <if>
        <isset property="libs.local.modified.property" />
        <then>
          <echo message="Deploying libraries ${line.separator} ${libs.local.modified.property}" />
          <webdav username="${adminUsername}"
                  password="${adminPassword}">
            <put url="http://${target}/files/tmp/">
              <fileset dir="@{libs.basedir}"
                       includes="${libs.local.modified.property}" />
              <fileset file="@{libs.basedir}/@{libs.name}.properties" />
            </put>
          </webdav>
          <pathconvert refid="libs.local.modified"
                       property="libs.remote.modified.property"
                       pathsep=" "
                       setonempty="true">
            <globmapper from="@{libs.basedir}/*"
                        to="/tmp/*"
                        handledirsep="true" />
          </pathconvert>
          <sshexec host="${target}"
                   username="${adminUsername}"
                   password="${adminPassword}"
                   trust="true"
                   command="chmod +x ${libs.remote.modified.property} &amp;&amp; mv ${libs.remote.modified.property} /tmp/@{libs.name}.properties @{libs.deployDir} &amp;&amp; ldconfig" />
        </then>
      </if>
    </sequential>
  </macrodef>
</project>