<?xml version="1.0" ?>

<project name="libphonenumber-javascript" default="compile">
  <property name="closure-compiler.dir"
            value="${basedir}/../../closure-compiler" />
  <property name="closure-compiler.jar"
            value="${closure-compiler.dir}/bazel-bin/compiler_unshaded_deploy.jar" />
  <property name="closure-library.dir"
            value="${basedir}/../../closure-library" />
  <property name="closure-linter.dir"
            value="${basedir}/../../closure-linter" />
  <property name="python-gflags.dir"
            value="${basedir}/../../python-gflags" />

  <macrodef name="closure-compile">
    <attribute name="entrypoint" />
    <attribute name="outputfile" />
    <attribute name="compilationlevel" default="ADVANCED_OPTIMIZATIONS" />
    <attribute name="outputwrapper" default="(function(){%output%})();" />
    <element name="extraflags" optional="yes" />
    <sequential>
      <exec executable="java" failonerror="true" logError="true">
        <arg value="-jar" />
        <arg value="${closure-compiler.jar}" />
        <arg line='--entry_point="@{entrypoint}"' />
        <arg line='--js_output_file="@{outputfile}"' />
        <arg line='--js="${closure-library.dir}/closure/goog/**.js"' />
        <arg line='--js="${closure-library.dir}/third_party/closure/goog/**.js"' />
        <arg line="--output_wrapper=@{outputwrapper}" />
        <arg line="--compilation_level=@{compilationlevel}" />
        <arg line="--warning_level=VERBOSE" />
        <arg line="--jscomp_error=accessControls" />
        <arg line="--jscomp_error=checkDebuggerStatement" />
        <arg line="--jscomp_error=checkRegExp" />
        <arg line="--jscomp_error=checkTypes" />
        <arg line="--jscomp_error=checkVars" />
        <arg line="--jscomp_error=const" />
        <arg line="--jscomp_error=constantProperty" />
        <arg line="--jscomp_error=duplicate" />
        <arg line="--jscomp_error=duplicateMessage" />
        <arg line="--jscomp_error=es5Strict" />
        <arg line="--jscomp_error=externsValidation" />
        <arg line="--jscomp_error=extraRequire" />
        <arg line="--jscomp_error=globalThis" />
        <arg line="--jscomp_error=invalidCasts" />
        <arg line="--jscomp_error=misplacedTypeAnnotation" />
        <arg line="--jscomp_error=missingProperties" />
        <arg line="--jscomp_error=missingRequire" />
        <arg line="--jscomp_error=nonStandardJsDocs" />
        <arg line="--jscomp_error=strictModuleDepCheck" />
        <arg line="--jscomp_error=suspiciousCode" />
        <arg line="--jscomp_error=typeInvalidation" />
        <arg line="--jscomp_error=undefinedNames" />
        <arg line="--jscomp_error=undefinedVars" />
        <arg line="--jscomp_error=unknownDefines" />
        <arg line="--jscomp_error=uselessCode" />
        <arg line="--jscomp_error=visibility" />
        <extraflags />
      </exec>
    </sequential>
  </macrodef>

  <macrodef name="gjslint">
    <attribute name="inputfile" />
    <sequential>
      <exec executable="python" failonerror="false" logError="true">
        <env key="PYTHONPATH"
             value="${closure-linter.dir}:${python-gflags.dir}"/>
        <arg value="${closure-linter.dir}/closure_linter/gjslint.py" />
        <arg line='--strict' />
        <arg line='"@{inputfile}"' />
      </exec>
    </sequential>
  </macrodef>

  <condition property="os.iswindows">
    <os family="windows" />
  </condition>
  <condition property="os.isunix">
    <os family="unix" />
  </condition>
  <target name="nul" if="os.iswindows">
    <property name="null.device" value="NUL" />
  </target>
  <target name="devnull" if="os.isunix">
    <property name="null.device" value="/dev/null" />
  </target>
  <target name="setnulldevice" depends="nul,devnull" />

  <target name="clean" description="deletes all generated files">
    <delete file="i18n/phonenumbers/demo-compiled.js" />
  </target>

  <target name="compile-demo"
          description="generates demo-compiled.js">
    <closure-compile entrypoint="goog:i18n.phonenumbers.demo"
                     outputfile="i18n/phonenumbers/demo-compiled.js">
      <extraflags>
        <arg line='--js="i18n/phonenumbers/**.js"' />
        <arg line='--js="!i18n/phonenumbers/demo-compiled.js"' />
        <arg line='--js="!i18n/phonenumbers/metadatafortesting.js"' />
        <arg line='--js="!i18n/phonenumbers/metadatalite.js"' />
        <arg line='--js="!i18n/phonenumbers/regioncodefortesting.js"' />
      </extraflags>
    </closure-compile>
  </target>

  <target name="compile-tests" depends="setnulldevice">
    <closure-compile entrypoint="goog:i18n.phonenumbers.AsYouTypeFormatterTest"
                     outputfile="${null.device}">
      <extraflags>
        <arg line='--js="i18n/phonenumbers/**.js"' />
        <arg line='--js="!i18n/phonenumbers/demo-compiled.js"' />
        <arg line='--js="!i18n/phonenumbers/metadata.js"' />
        <arg line='--js="!i18n/phonenumbers/metadatalite.js"' />
      </extraflags>
    </closure-compile>
    <closure-compile entrypoint="goog:i18n.phonenumbers.PhoneNumberUtilTest"
                     outputfile="${null.device}">
      <extraflags>
        <arg line='--js="i18n/phonenumbers/**.js"' />
        <arg line='--js="!i18n/phonenumbers/demo-compiled.js"' />
        <arg line='--js="!i18n/phonenumbers/metadata.js"' />
        <arg line='--js="!i18n/phonenumbers/metadatalite.js"' />
      </extraflags>
    </closure-compile>
    <closure-compile entrypoint="goog:i18n.phonenumbers.ShortNumberInfoTest"
                     outputfile="${null.device}">
      <extraflags>
        <arg line='--js="i18n/phonenumbers/**.js"' />
        <arg line='--js="!i18n/phonenumbers/demo-compiled.js"' />
        <arg line='--js="!i18n/phonenumbers/metadata.js"' />
        <arg line='--js="!i18n/phonenumbers/metadatalite.js"' />
      </extraflags>
    </closure-compile>
  </target>

  <target name="compile" depends="compile-demo,compile-tests" />

  <target name="lint" description="lints all javascript files">
    <gjslint inputfile="i18n/phonenumbers/asyoutypeformatter*.js" />
    <gjslint inputfile="i18n/phonenumbers/demo.js" />
    <gjslint inputfile="i18n/phonenumbers/phonenumberutil*.js" />
    <gjslint inputfile="i18n/phonenumbers/shortnumberinfo*.js" />
  </target>

</project>
