view build.xml @ 23:92afaa27f40a

Mac app support.
author David Barts <n5jrn@me.com>
date Tue, 24 Nov 2020 16:45:37 -0800
parents 5234e4500d45
children 9bf3d8de6904
line wrap: on
line source

<?xml version="1.0" encoding="UTF-8"?>
<project name="ImagePrep" default="help" basedir=".">
  <!-- import all environment variables as env.* -->
  <property environment="env"/>

  <!-- ensure required environment variables are set -->
  <macrodef name="env-require">
    <attribute name="name"/>
    <sequential>
      <fail message="Environment variable @{name} not set!">
        <condition>
          <not><isset property="env.@{name}"/></not>
        </condition>
      </fail>
    </sequential>
  </macrodef>
  <env-require name="JRE_HOME"/>
  <env-require name="KOTLIN_HOME"/>
  <env-require name="OSDEP_HOME"/>

  <!-- cribbed from https://stackoverflow.com/questions/7129672/uppercase-lowercase-capitalize-an-ant-property -->
  <scriptdef language="javascript" name="toLowerCase">
    <attribute name="value" />
    <attribute name="target" />
    <![CDATA[
      "use strict";
      project.setProperty( attributes.get( "target" ),
                           attributes.get( "value" ).toLowerCase() );
    ]]>
  </scriptdef>

  <!-- Define the properties used by the build -->
  <property name="app.name"      value="${ant.project.name}"/>
  <toLowerCase target="lc.app.name" value="${app.name}"/>
  <property name="app.version"   value="1.00"/>
  <property name="app.domain"    value="name.blackcap.${lc.app.name}"/>
  <property name="app.entry"     value="${app.domain}.MainKt"/>
  <property name="app.copyright" value="Copyright © 2020, David W. Barts."/>
  <property name="jar.name"      value="${basedir}/${lc.app.name}.jar"/>
  <property name="work.jar"      value="${basedir}/work.jar"/>
  <property name="lib.home"      value="${basedir}/lib"/>
  <property name="src.home"      value="${basedir}/src"/>
  <property name="dist.home"     value="${basedir}/dist"/>
  <property name="pf.home"       value="${basedir}/package-files"/>
  <property name="jvm.version"   value="1.8"/>

  <!-- define the kotlin task -->
  <property name="kotlin.lib" value="${env.KOTLIN_HOME}/lib"/>
  <typedef resource="org/jetbrains/kotlin/ant/antlib.xml"
           classpath="${kotlin.lib}/kotlin-ant.jar"/>

  <!-- help message -->
  <target name="help">
    <echo>You can use the following targets:</echo>
    <echo> </echo>
    <echo>  help    : (default) Prints this message </echo>
    <echo>  all     : Cleans, compiles, and stages application</echo>
    <echo>  clean   : Deletes work directories</echo>
    <echo>  compile : Compiles servlets into class files</echo>
    <echo>  jar     : Make JAR file.</echo>
    <echo> </echo>
    <echo>For example, to clean, compile, and package all at once, run:</echo>
    <echo>prompt> ant all </echo>
  </target>

  <!-- Define the CLASSPATH -->
  <target name="classpath">
    <path id="compile.classpath">
      <pathelement location="${src.home}"/>
      <fileset dir="${lib.home}">
        <include name="*.jar"/>
      </fileset>
    </path>
  </target>

  <!-- make needed directories -->
  <target name="mkdirs">
    <mkdir dir="${dist.home}"/>
  </target>

  <!-- remove old cruft -->
  <target name="clean">
    <delete includeEmptyDirs="true" failonerror="false">
      <fileset dir="${dist.home}" includes="**/*"/>
    </delete>
  </target>

  <!-- rename files containing OS-dependant code -->
  <target name="osdep">
    <exec executable="${env.JRE_HOME}/bin/java" failonerror="true">
      <arg value="-jar"/>
      <arg file="${env.OSDEP_HOME}/osdep.jar"/>
      <arg value="${src.home}"/>
    </exec>
  </target>

  <!-- do everything but install -->
  <target name="all" depends="jar"
          description="Clean work dirs, compile, make JAR."/>

  <!-- compile *.kt to *.class -->
  <target name="compile" depends="mkdirs,osdep,classpath"
          description="Compile Java sources to ${work.home}">
    <kotlinc src="${src.home}" output="${work.jar}"
             classpathref="compile.classpath">
      <compilerarg line="-jvm-target ${jvm.version}"/>
    </kotlinc>
  </target>

  <!-- make .jar file -->
  <target name="jar" depends="compile" description="Create JAR file.">
    <jar destfile="${jar.name}">
      <manifest>
        <attribute name="Main-Class" value="${app.entry}"/>
      </manifest>
      <!-- <zipgroupfileset dir="${lib.home}" includes="*.jar"/> -->
      <zipgroupfileset dir="${lib.home}" includes="*.jar"/>
      <zipfileset src="${work.jar}"/>
      <zipfileset dir="${src.home}" includes="**/*.properties,**/*.html"/>
    </jar>
  </target>

  <target name="macapp" depends="jar" description="Create MacOS app bundle.">
    <fail message="Macintosh packages can only be built on a Mac.">
      <condition>
        <not><os family="mac"/></not>
      </condition>
    </fail>
    <sequential>
      <property name="mac.disk.image.filename"
                value="${lc.app.name}_${app.version}.dmg"/>
      <property name="app.bundle" value="${dist.home}/${app.name}.app"/>
      <mkdir dir="${app.bundle}/Contents"/>
      <copy todir="${app.bundle}/Contents" encoding="UTF-8" overwrite="true">
        <fileset file="${pf.home}/osx/Info.plist"/>
        <!-- XXX will break if any tokens contain <, >, or & -->
        <filterset>
          <filter token="app.copyright" value="${app.copyright}"/>
          <filter token="app.domain" value="${app.domain}"/>
          <filter token="app.entry" value="${app.entry}"/>
          <filter token="app.name" value="${app.name}"/>
          <filter token="app.version" value="${app.version}"/>
          <filter token="jar.filename" value="${lc.app.name}.jar"/>
          <filter token="jvm.version" value="${jvm.version}"/>
          <filter token="lc.app.name" value="${lc.app.name}"/>
        </filterset>
      </copy>
      <mkdir dir="${app.bundle}/Contents/MacOS"/>
      <copy todir="${app.bundle}/Contents/MacOS" encoding="UTF-8"
            overwrite="true">
        <fileset file="${pf.home}/osx/JavaApplicationStub"/>
        <filterset>
          <filter token="app.domain" value="${app.domain}"/>
          <filter token="app.name" value="${app.name}"/>
        </filterset>
      </copy>
      <chmod file="${app.bundle}/Contents/MacOS/JavaApplicationStub"
             perm="755"/>
      <mkdir dir="${app.bundle}/Contents/Resources"/>
      <copy file="${basedir}/${app.name}.icns"
            todir="${app.bundle}/Contents/Resources"/>
      <mkdir dir="${app.bundle}/Contents/Java"/>
      <copy file="${jar.name}" todir="${app.bundle}/Contents/Java"/>
      <echo file="${app.bundle}/Contents/PkgInfo" message="APPL????"/>
      <exec executable="hdiutil" failonerror="true">
        <arg value="create"/>
        <arg value="-volname"/>
        <arg value="${app.name}"/>
        <arg value="-srcfolder"/>
        <arg file="${app.bundle}"/>
        <arg file="${dist.home}/orig-${mac.disk.image.filename}"/>
      </exec>
      <exec executable="hdiutil" failonerror="true">
        <arg value="convert"/>
        <arg file="${dist.home}/orig-${mac.disk.image.filename}"/>
        <arg value="-format"/>
        <arg value="UDRW"/>
        <arg value="-o"/>
        <arg file="${dist.home}/udrw-${mac.disk.image.filename}"/>
      </exec>
      <exec executable="hdiutil" failonerror="true">
        <arg value="convert"/>
        <arg file="${dist.home}/udrw-${mac.disk.image.filename}"/>
        <arg value="-format"/>
        <arg value="UDZO"/>
        <arg value="-imagekey"/>
        <arg value="zlib-level=9"/>
        <arg value="-o"/>
        <arg file="${dist.home}/${mac.disk.image.filename}"/>
      </exec>
    </sequential>
  </target>

</project>