Browse Source

TOOLS: Code and jars to enable auto-generation of metadata for Java when compiling/testing.

pull/567/head
Philip Liard 15 years ago
committed by Mihaela Rosca
parent
commit
2dc010ae27
11 changed files with 211 additions and 28 deletions
  1. +50
    -2
      java/build.xml
  2. +81
    -0
      java/pom.xml
  3. +5
    -4
      java/src/com/google/i18n/phonenumbers/CountryCodeToRegionCodeMap.java
  4. +5
    -4
      java/test/com/google/i18n/phonenumbers/CountryCodeToRegionCodeMapForTesting.java
  5. +10
    -10
      tools/java/common/src/com/google/i18n/phonenumbers/tools/CopyrightNotice.java
  6. +36
    -1
      tools/java/common/src/com/google/i18n/phonenumbers/tools/FileUtils.java
  7. BIN
      tools/java/cpp-build/target/cpp-build-1.0-SNAPSHOT-jar-with-dependencies.jar
  8. +1
    -1
      tools/java/java-build/src/com/google/i18n/phonenumbers/tools/BuildMetadataJsonFromXml.java
  9. +18
    -6
      tools/java/java-build/src/com/google/i18n/phonenumbers/tools/BuildMetadataProtoFromXml.java
  10. +5
    -0
      tools/java/java-build/src/com/google/i18n/phonenumbers/tools/GenerateAreaCodeData.java
  11. BIN
      tools/java/java-build/target/java-build-1.0-SNAPSHOT-jar-with-dependencies.jar

+ 50
- 2
java/build.xml View File

@ -21,7 +21,54 @@
</fileset>
</path>
<target name="compile" description="Compile Java source.">
<target name="build-metadata">
<exec executable="java">
<arg value="-jar" />
<arg value="../tools/java/java-build/target/java-build-1.0-SNAPSHOT-jar-with-dependencies.jar"/>
<arg value="BuildMetadataProtoFromXml"/>
<arg value="../resources/PhoneNumberMetaData.xml"/>
<arg value="src"/>
<arg value="false"/> <!-- Not for testing. -->
<arg value="false"/> <!-- No lite metadata. -->
</exec>
</target>
<target name="build-test-metadata">
<exec executable="java">
<arg value="-jar" />
<arg value="../tools/java/java-build/target/java-build-1.0-SNAPSHOT-jar-with-dependencies.jar"/>
<arg value="BuildMetadataProtoFromXml"/>
<arg value="../resources/PhoneNumberMetaDataForTesting.xml"/>
<arg value="test"/>
<arg value="true"/> <!-- For testing. -->
<arg value="false"/> <!-- No lite metadata. -->
</exec>
</target>
<target name="build-geo-data">
<exec executable="java">
<arg value="-jar" />
<arg value="../tools/java/java-build/target/java-build-1.0-SNAPSHOT-jar-with-dependencies.jar"/>
<arg value="GenerateAreaCodeData"/>
<arg value="../resources/geocoding/"/>
<arg value="src/com/google/i18n/phonenumbers/geocoding/data"/>
<arg value="false"/> <!-- Not for testing. -->
</exec>
</target>
<target name="build-geo-test-data">
<exec executable="java">
<arg value="-jar" />
<arg value="../tools/java/java-build/target/java-build-1.0-SNAPSHOT-jar-with-dependencies.jar"/>
<arg value="GenerateAreaCodeData"/>
<arg value="../resources/test/geocoding/"/>
<arg value="test/com/google/i18n/phonenumbers/geocoding/testing_data"/>
<arg value="false"/> <!-- Not for testing. -->
</exec>
</target>
<target name="compile" description="Compile Java source."
depends="build-metadata,build-geo-data">
<mkdir dir="${classes.dir}"/>
<javac srcdir="${src.dir}" destdir="${classes.dir}" classpathref="classpath"/>
<javac srcdir="${test.dir}" destdir="${classes.dir}" classpathref="classpath" debug="on"/>
@ -53,7 +100,8 @@
</jar>
</target>
<target name="test-jar" depends="compile">
<target name="test-jar"
depends="compile,build-test-metadata,build-geo-test-data">
<mkdir dir="${jar.dir}"/>
<jar destfile="${jar.dir}/${ant.project.name}-test.jar">
<fileset dir="${classes.dir}">


+ 81
- 0
java/pom.xml View File

@ -94,6 +94,87 @@
</testResource>
</testResources>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2</version>
<executions>
<execution>
<id>build-metadata</id>
<phase>generate-sources</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>java</executable>
<arguments>
<argument>-jar</argument>
<argument>../tools/java/java-build/target/java-build-1.0-SNAPSHOT-jar-with-dependencies.jar</argument>
<argument>BuildMetadataProtoFromXml</argument>
<argument>../resources/PhoneNumberMetaData.xml</argument>
<argument>src</argument>
<argument>false</argument> <!-- Not for testing. -->
<argument>false</argument> <!-- No lite metadata. -->
</arguments>
</configuration>
</execution>
<execution>
<id>build-test-metadata</id>
<phase>generate-test-sources</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>java</executable>
<arguments>
<argument>-jar</argument>
<argument>../tools/java/java-build/target/java-build-1.0-SNAPSHOT-jar-with-dependencies.jar</argument>
<argument>BuildMetadataProtoFromXml</argument>
<argument>../resources/PhoneNumberMetaDataForTesting.xml</argument>
<argument>test</argument>
<argument>true</argument> <!-- For testing. -->
<argument>false</argument> <!-- No lite metadata. -->
</arguments>
</configuration>
</execution>
<execution>
<id>build-geo-data</id>
<phase>generate-sources</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>java</executable>
<arguments>
<argument>-jar</argument>
<argument>../tools/java/java-build/target/java-build-1.0-SNAPSHOT-jar-with-dependencies.jar</argument>
<argument>GenerateAreaCodeData</argument>
<argument>../resources/geocoding</argument>
<argument>src/com/google/i18n/phonenumbers/geocoding/data</argument>
<argument>false</argument> <!-- Not for testing. -->
</arguments>
</configuration>
</execution>
<execution>
<id>build-geo-test-data</id>
<phase>generate-test-sources</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>java</executable>
<arguments>
<argument>-jar</argument>
<argument>../tools/java/java-build/target/java-build-1.0-SNAPSHOT-jar-with-dependencies.jar</argument>
<argument>GenerateAreaCodeData</argument>
<argument>../resources/test/geocoding</argument>
<argument>test/com/google/i18n/phonenumbers/geocoding/testing_data</argument>
<argument>true</argument> <!-- For testing. -->
</arguments>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>


+ 5
- 4
java/src/com/google/i18n/phonenumbers/CountryCodeToRegionCodeMap.java View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2010 Google Inc.
* Copyright (C) 2011 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -12,9 +12,10 @@
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* This file is automatically generated by BuildMetadataProtoFromXml. Please
* don't modify directly.
*/
/* This file is automatically generated by {@link BuildMetadataProtoFromXml}.
* Please don't modify it directly.
*/
package com.google.i18n.phonenumbers;


+ 5
- 4
java/test/com/google/i18n/phonenumbers/CountryCodeToRegionCodeMapForTesting.java View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2010 Google Inc.
* Copyright (C) 2011 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -12,9 +12,10 @@
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* This file is automatically generated by BuildMetadataProtoFromXml. Please
* don't modify directly.
*/
/* This file is automatically generated by {@link BuildMetadataProtoFromXml}.
* Please don't modify it directly.
*/
package com.google.i18n.phonenumbers;


+ 10
- 10
tools/java/common/src/com/google/i18n/phonenumbers/tools/CopyrightNotice.java View File

@ -27,18 +27,18 @@ public class CopyrightNotice {
public static final String TEXT =
"/*\n" +
" * Copyright (C) " + Calendar.getInstance().get(Calendar.YEAR) + " Google Inc.\n" +
" * Copyright (C) " + Calendar.getInstance().get(Calendar.YEAR) + " Google Inc.\n" +
" *\n" +
" * Licensed under the Apache License, Version 2.0 (the \"License\");\n" +
" * you may not use this file except in compliance with the License.\n" +
" * You may obtain a copy of the License at\n" +
" * Licensed under the Apache License, Version 2.0 (the \"License\");\n" +
" * you may not use this file except in compliance with the License.\n" +
" * You may obtain a copy of the License at\n" +
" *\n" +
" * http://www.apache.org/licenses/LICENSE-2.0\n" +
" * http://www.apache.org/licenses/LICENSE-2.0\n" +
" *\n" +
" * Unless required by applicable law or agreed to in writing, software\n" +
" * distributed under the License is distributed on an \"AS IS\" BASIS,\n" +
" * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n" +
" * See the License for the specific language governing permissions and\n" +
" * limitations under the License.\n" +
" * Unless required by applicable law or agreed to in writing, software\n" +
" * distributed under the License is distributed on an \"AS IS\" BASIS,\n" +
" * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n" +
" * See the License for the specific language governing permissions and\n" +
" * limitations under the License.\n" +
" */\n";
}

+ 36
- 1
tools/java/common/src/com/google/i18n/phonenumbers/tools/FileUtils.java View File

@ -18,10 +18,13 @@
package com.google.i18n.phonenumbers.tools;
import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* Helper class containing methods designed to ease file manipulation.
* Helper class containing methods designed to ease file manipulation and generation.
*
* @author Philippe Liard
*/
@ -49,4 +52,36 @@ public class FileUtils {
close(closeable);
}
}
/**
* Returns true if the provided output file/directory is older than the provided input
* file/directory. The last modification time of a directory is the maximum last
* modification time of its children. It assumes the provided output directory only contains
* generated files.
*/
public static boolean isGenerationRequired(File inputFile, File outputDir) {
if (!outputDir.exists()) {
return true;
}
return getLastModificationTime(inputFile) > getLastModificationTime(outputDir);
}
/**
* Gets the modification time of the most recently modified file contained in a directory.
*/
private static long getLastModificationTime(File file) {
if (!file.isDirectory()) {
return file.lastModified();
}
long maxModificationTime = 0;
for (File child : file.listFiles()) {
long modificationTime = getLastModificationTime(child);
if (modificationTime > maxModificationTime) {
maxModificationTime = modificationTime;
}
}
return maxModificationTime;
}
}

BIN
tools/java/cpp-build/target/cpp-build-1.0-SNAPSHOT-jar-with-dependencies.jar View File


+ 1
- 1
tools/java/java-build/src/com/google/i18n/phonenumbers/tools/BuildMetadataJsonFromXml.java View File

@ -300,7 +300,7 @@ public class BuildMetadataJsonFromXml extends Command {
jsArrayBuilder.append(null);
}
// optional bool same_mobile_and_fixed_line_pattern = 18 [default=false];
if (metadata.getSameMobileAndFixedLinePattern()) {
if (metadata.isSameMobileAndFixedLinePattern()) {
jsArrayBuilder.append(1);
} else {
jsArrayBuilder.append(null);


+ 18
- 6
tools/java/java-build/src/com/google/i18n/phonenumbers/tools/BuildMetadataProtoFromXml.java View File

@ -20,6 +20,7 @@ import com.google.i18n.phonenumbers.Phonemetadata.PhoneMetadata;
import com.google.i18n.phonenumbers.Phonemetadata.PhoneMetadataCollection;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
@ -38,7 +39,7 @@ public class BuildMetadataProtoFromXml extends Command {
private static final String META_DATA_FILE_PREFIX =
"/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProto";
private static final String TEST_META_DATA_FILE_PREFIX =
"/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting";
"/com/google/i18n/phonenumbers/data/PhoneNumberMetadataProtoForTesting";
private static final String TEST_COUNTRY_CODE_TO_REGION_CODE_MAP_CLASS_NAME =
"CountryCodeToRegionCodeMapForTesting";
private static final String COUNTRY_CODE_TO_REGION_CODE_MAP_CLASS_NAME =
@ -50,7 +51,7 @@ public class BuildMetadataProtoFromXml extends Command {
"\n" +
"where:\n" +
" inputFile The input file containing phone number metadata in XML format.\n" +
" outputDir The output directory to store phone number metadata in proto\n" +
" outputDir The output source directory to store phone number metadata in proto\n" +
" format (one file per region) and the country code to region code\n" +
" mapping file.\n" +
" forTesting Flag whether to generate metadata for testing purposes or not.\n" +
@ -67,6 +68,11 @@ public class BuildMetadataProtoFromXml extends Command {
"Example command line invocation:\n" +
"BuildMetadataProtoFromXml PhoneNumberMetadata.xml src false false\n";
private static final String GENERATION_COMMENT =
"/* This file is automatically generated by {@link BuildMetadataProtoFromXml}.\n" +
" * Please don't modify it directly.\n" +
" */\n";
@Override
public String getCommandName() {
return "BuildMetadataProtoFromXml";
@ -84,6 +90,12 @@ public class BuildMetadataProtoFromXml extends Command {
boolean forTesting = args[3].equals("true");
boolean liteBuild = args.length > 4 && args[4].equals("true");
if (!FileUtils.isGenerationRequired(
new File(inputFile),
new File(outputDir, new File(META_DATA_FILE_PREFIX).getParent()))) {
System.out.println("Metadata code up-to-date.");
return true;
}
String filePrefix;
if (forTesting) {
filePrefix = outputDir + TEST_META_DATA_FILE_PREFIX;
@ -113,6 +125,7 @@ public class BuildMetadataProtoFromXml extends Command {
System.err.println(HELP_MESSAGE);
return false;
}
System.out.println("Metadata code successfully generated.");
return true;
}
@ -140,15 +153,14 @@ public class BuildMetadataProtoFromXml extends Command {
} else {
mappingClassName = COUNTRY_CODE_TO_REGION_CODE_MAP_CLASS_NAME;
}
String mappingFile =
outputDir + "/" + PACKAGE_NAME.replaceAll("\\.", "/") + "/" + mappingClassName + ".java";
String mappingFile = outputDir + "/" + PACKAGE_NAME + "/" + mappingClassName + ".java";
int capacity = (int) (countryCodeToRegionCodeMap.size() / MAPPING_LOAD_FACTOR);
BufferedWriter writer = new BufferedWriter(new FileWriter(mappingFile));
writer.write(CopyrightNotice.TEXT);
writer.write(CopyrightNotice.TEXT + "\n" + GENERATION_COMMENT + "\n");
if (PACKAGE_NAME.length() > 0) {
writer.write("package " + PACKAGE_NAME + ";\n\n");
writer.write("package " + PACKAGE_NAME.replaceAll("/", ".") + ";\n\n");
}
writer.write(MAPPING_IMPORTS);
writer.write("\n");


+ 5
- 0
tools/java/java-build/src/com/google/i18n/phonenumbers/tools/GenerateAreaCodeData.java View File

@ -236,6 +236,10 @@ public class GenerateAreaCodeData extends Command {
* @throws FileNotFoundException
*/
public void run() throws FileNotFoundException, IOException {
if (!FileUtils.isGenerationRequired(inputPath, outputPath)) {
LOGGER.log(Level.INFO, "Geocoding data up-to-date.");
return;
}
List<Pair<File, File>> inputOutputMappings = createInputOutputFileMappings();
SortedMap<Integer, Set<String>> availableDataFiles = new TreeMap<Integer, Set<String>>();
@ -268,6 +272,7 @@ public class GenerateAreaCodeData extends Command {
} finally {
closeFile(fileOutputStream);
}
LOGGER.log(Level.INFO, "Geocoding data successfully generated.");
}
@Override


BIN
tools/java/java-build/target/java-build-1.0-SNAPSHOT-jar-with-dependencies.jar View File


Loading…
Cancel
Save