Skip to content

Instantly share code, notes, and snippets.

@Muzietto
Last active September 29, 2015 12:36
Show Gist options
  • Save Muzietto/f667ce8477957e6d55c5 to your computer and use it in GitHub Desktop.
Save Muzietto/f667ce8477957e6d55c5 to your computer and use it in GitHub Desktop.
google/i18n/libphonenumber - ant project to compile single-country JS executables using the Closure compiler
<?xml version="1.0" ?>
<!--
SCLEXE - single-country libphonenumber executable
Author: Marco Faustinelli ([email protected])
Web: http://faustinelli.net/
http://faustinelli.wordpress.com/
Version: 1.0
The MIT License - Copyright (c) 2015 SCLEXE Project
-->
<project basedir="." name="libphonenumber-javascript" default="all">
<taskdef resource="net/sf/antcontrib/antlib.xml" classpath="/usr/share/java/ant-contrib.jar"></taskdef>
<!-- http://tlrobinson.net/blog/2008/11/ant-tasks-for-git/ -->
<macrodef name="git">
<attribute name="command" />
<attribute name="dir" default="" />
<element name="args" optional="true" />
<sequential>
<echo message="git @{command}" />
<exec executable="git" dir="@{dir}">
<arg value="@{command}" />
<args/>
</exec>
</sequential>
</macrodef>
<property name="google-libphonenumber-area"
value="/root/libphonenumber_area" />
<property name="closure-compiler.dir"
value="${google-libphonenumber-area}/closure-compiler" />
<property name="closure-compiler.jar"
value="${closure-compiler.dir}/build/compiler.jar" />
<property name="closure-library.dir"
value="${google-libphonenumber-area}/closure-library" />
<property name="closure-linter.dir"
value="${google-libphonenumber-area}/closure-linter" />
<property name="python-gflags.dir"
value="${google-libphonenumber-area}/python-gflags" />
<property name="libphonenumber.dir"
value=".libphonenumber" />
<property name="dist.dir" value="./.dist" />
<property name="ultimate.root.dir" value="../../" />
<property name="active-countries" value="IT,TR,ES,BR,MX,FR,AR,PT,CH,PL,UK,CL,BE,RU,ID,EC,LT,NL,DE,GR,IQ,EG,AU,MY,KW,RO,CO,DK,AE,NO,BA,TH,HU,SD,AT,CA,LV,FI,PE,TW,HR,LB,EE,MA,SI,SE,RS,BG,IE,SG,PS,ZA,KZ,LU,VN,JO,SK,UA,QA,YE,SY,HK"/>
<!--property name="active-countries" value="IT,TR,ES"/-->
<macrodef name="closure-compile">
<attribute name="inputfile" />
<attribute name="outputfile" />
<attribute name="compilationlevel" default="ADVANCED_OPTIMIZATIONS" />
<attribute name="outputmode" default="compiled" />
<attribute name="outputwrapper" default="(function(){%output%})();" />
<element name="extraflags" optional="yes" />
<sequential>
<exec executable="python" failonerror="true" logError="true">
<arg value="${closure-library.dir}/closure/bin/calcdeps.py" />
<arg line='-i "@{inputfile}"' />
<arg line='--output_file "@{outputfile}"' />
<arg line='-p "${closure-library.dir}"' />
<arg line="-o @{outputmode}" />
<arg line='-c "${closure-compiler.jar}"' />
<arg line='-f "--output_wrapper=@{outputwrapper}"' />
<arg line='-f "--compilation_level=@{compilationlevel}"' />
<arg line='-f "--warning_level=VERBOSE"' />
<arg line='-f "--jscomp_error=accessControls"' />
<arg line='-f "--jscomp_error=ambiguousFunctionDecl"' />
<arg line='-f "--jscomp_error=checkDebuggerStatement"' />
<arg line='-f "--jscomp_error=checkRegExp"' />
<arg line='-f "--jscomp_error=checkTypes"' />
<arg line='-f "--jscomp_error=checkVars"' />
<arg line='-f "--jscomp_error=const"' />
<arg line='-f "--jscomp_error=constantProperty"' />
<arg line='-f "--jscomp_error=deprecated"' />
<arg line='-f "--jscomp_error=duplicate"' />
<arg line='-f "--jscomp_error=duplicateMessage"' />
<arg line='-f "--jscomp_error=es5Strict"' />
<arg line='-f "--jscomp_error=externsValidation"' />
<arg line='-f "--jscomp_error=fileoverviewTags"' />
<arg line='-f "--jscomp_error=globalThis"' />
<arg line='-f "--jscomp_error=internetExplorerChecks"' />
<arg line='-f "--jscomp_error=invalidCasts"' />
<arg line='-f "--jscomp_error=misplacedTypeAnnotation"' />
<arg line='-f "--jscomp_error=missingProperties"' />
<arg line='-f "--jscomp_error=nonStandardJsDocs"' />
<arg line='-f "--jscomp_error=strictModuleDepCheck"' />
<arg line='-f "--jscomp_error=suspiciousCode"' />
<arg line='-f "--jscomp_error=typeInvalidation"' />
<arg line='-f "--jscomp_error=undefinedNames"' />
<arg line='-f "--jscomp_error=undefinedVars"' />
<arg line='-f "--jscomp_error=unknownDefines"' />
<arg line='-f "--jscomp_error=uselessCode"' />
<arg line='-f "--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="prepare" depends="update-dependencies">
<mkdir dir="${dist.dir}"/>
<move file="${libphonenumber.dir}/javascript/i18n/phonenumbers/metadata.js"
tofile="${libphonenumber.dir}/javascript/i18n/phonenumbers/metadata.js.PARK" failonerror="false"/>
<copy file="${libphonenumber.dir}/javascript/i18n/phonenumbers/metadata.js.PARK" tofile="${dist.dir}/metadata.js"/>
<move file="${libphonenumber.dir}/javascript/i18n/phonenumbers/metadatalite.js"
tofile="${libphonenumber.dir}/javascript/i18n/phonenumbers/metadatalite.js.PARK" failonerror="false"/>
<move file="${libphonenumber.dir}/javascript/i18n/phonenumbers/metadatafortesting.js"
tofile="${libphonenumber.dir}/javascript/i18n/phonenumbers/metadatafortesting.js.PARK" failonerror="false"/>
</target>
<target name="update-dependencies">
<git command="pull" dir=".libphonenumber" />
</target>
<target name="clean" description="deletes all generated files">
<delete dir="${dist.dir}" />
</target>
<target name="prepare-countries-metadata" >
<foreach target="-prepare-country-metadata" param="isocode" list="${active-countries}"/>
</target>
<target name="-prepare-country-metadata" >
<echo message="preparing metadata for ${isocode}"/>
<exec executable="node" failonerror="true">
<arg value="slice_country.js"/>
<arg value="${isocode}"/>
<arg value="${dist.dir}/metadata.js"/>
<arg value="${dist.dir}/metadata_${isocode}.js"/>
</exec>
</target>
<target name="compile-executable" depends="prepare, prepare-countries-metadata"
description="">
<foreach target="-compile-executable-for-country" param="isocode" list="${active-countries}"/>
</target>
<target name="-compile-executable-for-country">
<copy file="${dist.dir}/metadata_${isocode}.js" tofile="${libphonenumber.dir}/javascript/i18n/phonenumbers/metadata.js" overwrite="true"/>
<closure-compile inputfile="main.js" outputfile="${dist.dir}/libphonenumber_${isocode}.js">
<extraflags>
<arg line='-p "${libphonenumber.dir}/javascript/i18n/phonenumbers"' />
</extraflags>
</closure-compile>
</target>
<target name="copy-executable" description="">
<copy todir="${ultimate.root.dir}/public/js/libphonenumber">
<fileset dir="${dist.dir}">
<include name="libphonenumber_*.js"/>
</fileset>
</copy>
</target>
<target name="all" depends="compile-executable, copy-executable" />
</project>
@Muzietto
Copy link
Author

Starting from the original libphonenumber project and a list of active-countries, this ant project compiles a set of JavaScript executables that can format resp. validate phone numbers from a given country.
Preparation of the single-country metadata is delegated to node.js script slice_country.js.
Compilation creates global object phoneUtils according to Nathan Hammond's libphonenumber porting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment