Added script to generate an Eclipse project for Bazel

Now a developer can work on the Java sources by generating an Eclipse
project with scripts/eclipse/setup.sh. The xcode tooling are also
imported compared to the previously checked-in project resulting in
a bunch of errors.

--
MOS_MIGRATED_REVID=88548129
diff --git a/.classpath b/.classpath
deleted file mode 100644
index 1b8a76d..0000000
--- a/.classpath
+++ /dev/null
@@ -1,51 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<classpath>
-	<classpathentry kind="src" path="src/java_tools/buildjar/java"/>
-	<classpathentry kind="src" path="src/java_tools/buildjar/javatests"/>
-	<classpathentry kind="src" path="src/java_tools/singlejar/java"/>
-	<classpathentry kind="src" path="src/java_tools/singlejar/javatests"/>
-	<classpathentry kind="src" path="src/main/java"/>
-	<classpathentry kind="src" path="src/test/java"/>
-	<classpathentry kind="src" path="src/tools/xcode-common/java"/>
-	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
-	<classpathentry kind="lib" path="base_workspace/third_party/aether/aether-api-1.0.0.v20140518.jar"/>
-	<classpathentry kind="lib" path="base_workspace/third_party/aether/aether-connector-basic-1.0.0.v20140518.jar"/>
-	<classpathentry kind="lib" path="base_workspace/third_party/aether/aether-impl-1.0.0.v20140518.jar"/>
-	<classpathentry kind="lib" path="base_workspace/third_party/aether/aether-spi-1.0.0.v20140518.jar"/>
-	<classpathentry kind="lib" path="base_workspace/third_party/aether/aether-transport-classpath-1.0.0.v20140518.jar"/>
-	<classpathentry kind="lib" path="base_workspace/third_party/aether/aether-transport-file-1.0.0.v20140518.jar"/>
-	<classpathentry kind="lib" path="base_workspace/third_party/aether/aether-transport-http-1.0.0.v20140518.jar"/>
-	<classpathentry kind="lib" path="base_workspace/third_party/aether/aether-transport-wagon-1.0.0.v20140518.jar"/>
-	<classpathentry kind="lib" path="base_workspace/third_party/aether/aether-util-1.0.0.v20140518.jar"/>
-	<classpathentry kind="lib" path="base_workspace/third_party/apache_commons_compress/apache-commons-compress-1.9.jar"/>
-	<classpathentry kind="lib" path="base_workspace/third_party/apache_commons_logging/commons-logging-1.1.1.jar"/>
-	<classpathentry kind="lib" path="base_workspace/third_party/apache_httpclient/httpclient-4.2.5.jar"/>
-	<classpathentry kind="lib" path="base_workspace/third_party/apache_httpcore/httpcore-4.2.4.jar"/>
-	<classpathentry kind="lib" path="base_workspace/third_party/gson/gson-2.2.4.jar"/>
-	<classpathentry kind="lib" path="base_workspace/third_party/guava/guava-18.0.jar"/>
-	<classpathentry kind="lib" path="base_workspace/third_party/guava/guava-testlib.jar"/>
-	<classpathentry kind="lib" path="base_workspace/third_party/hamcrest/hamcrest-core-1.3.jar"/>
-	<classpathentry kind="lib" path="base_workspace/third_party/joda-time/joda-time-2.3.jar"/>
-	<classpathentry kind="lib" path="base_workspace/third_party/jsr305/jsr-305.jar"/>
-	<classpathentry kind="lib" path="base_workspace/third_party/junit/junit-4.11.jar"/>
-	<classpathentry kind="lib" path="base_workspace/third_party/maven_model/maven-aether-provider-3.2.3.jar"/>
-	<classpathentry kind="lib" path="base_workspace/third_party/maven_model/maven-model-3.2.3.jar"/>
-	<classpathentry kind="lib" path="base_workspace/third_party/maven_model/maven-model-builder-3.2.3.jar"/>
-	<classpathentry kind="lib" path="base_workspace/third_party/mockito/mockito-all-1.10.19.jar"/>
-	<classpathentry kind="lib" path="base_workspace/third_party/plexus_interpolation/plexus-interpolation-1.22.jar"/>
-	<classpathentry kind="lib" path="base_workspace/third_party/plexus_utils/plexus-utils-3.0.21.jar"/>
-	<classpathentry kind="lib" path="base_workspace/third_party/protobuf/protobuf-2.5.0.jar"/>
-	<classpathentry kind="lib" path="base_workspace/third_party/truth/truth-0.24+.jar"/>
-	<classpathentry kind="lib" path="tools/jdk/jdk/lib/tools.jar"/>
-	<classpathentry kind="lib" path="bazel-bin/src/main/protobuf/libproto_build.jar" sourcepath="bazel-bin/src/main/protobuf/libproto_build.jar.build_output"/>
-	<classpathentry kind="lib" path="bazel-bin/src/main/protobuf/libproto_bundlemerge.jar" sourcepath="bazel-bin/src/main/protobuf/libproto_bundlemerge.jar.build_output"/>
-	<classpathentry kind="lib" path="bazel-bin/src/main/protobuf/libproto_crosstool_config.jar" sourcepath="bazel-bin/src/main/protobuf/libproto_crosstool_config.jar.build_output"/>
-	<classpathentry kind="lib" path="bazel-bin/src/main/protobuf/libproto_deps.jar" sourcepath="bazel-bin/src/main/protobuf/libproto_deps.jar.build_output"/>
-	<classpathentry kind="lib" path="bazel-bin/src/main/protobuf/libproto_extra_actions_base.jar" sourcepath="bazel-bin/src/main/protobuf/libproto_extra_actions_base.jar.build_output"/>
-	<classpathentry kind="lib" path="bazel-bin/src/main/protobuf/libproto_test_status.jar" sourcepath="bazel-bin/src/main/protobuf/libproto_test_status.jar.build_output"/>
-	<classpathentry kind="lib" path="bazel-bin/src/main/protobuf/libproto_xcodegen.jar" sourcepath="bazel-bin/src/main/protobuf/libproto_xcodegen.jar.build_output"/>
-	<classpathentry kind="lib" path="bazel-bin/src/java_tools/buildjar/libjava-langtools-location-lib.jar" sourcepath="bazel-genfiles/src/java_tools/buildjar"/>
-	<classpathentry kind="lib" path="bazel-bin/src/java_tools/buildjar/libjavabuilder-javacopts-lib.jar" sourcepath="bazel-genfiles/src/java_tools/buildjar"/>
-	<classpathentry kind="lib" path="bazel-bin/src/java_tools/buildjar/libjavac-bootclasspath-locations-lib.jar" sourcepath="bazel-genfiles/src/java_tools/buildjar"/>
-	<classpathentry kind="output" path="bazel-out/eclipse-classes"/>
-</classpath>
diff --git a/.project b/.project
deleted file mode 100644
index 7e2de60..0000000
--- a/.project
+++ /dev/null
@@ -1,17 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<projectDescription>
-	<name>bazel</name>
-	<comment></comment>
-	<projects>
-	</projects>
-	<buildSpec>
-		<buildCommand>
-			<name>org.eclipse.jdt.core.javabuilder</name>
-			<arguments>
-			</arguments>
-		</buildCommand>
-	</buildSpec>
-	<natures>
-		<nature>org.eclipse.jdt.core.javanature</nature>
-	</natures>
-</projectDescription>
diff --git a/scripts/generate-classpath.sh b/scripts/generate-classpath.sh
new file mode 100755
index 0000000..31f6c5d
--- /dev/null
+++ b/scripts/generate-classpath.sh
@@ -0,0 +1,98 @@
+#!/bin/bash
+# Copyright 2015 Google Inc. All rights reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# 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.
+#
+# Generates an eclipse .classpath file for Bazel
+
+set -eu
+
+cd $(dirname "$0")
+cd ..
+
+function query() {
+    ./output/bazel query "$@"
+}
+
+# Compile bazel
+([ -f "output/bazel" ] && [ -f "tools/jdk/JavaBuilder_deploy.jar" ] && [ -f "tools/jdk/ijar" ] \
+    && [ -f "tools/jdk/SingleJar_deploy.jar" ] && [ -e "tools/jdk/jdk" ]) || ./compile.sh >&2 || exit $?
+
+# Build everything
+./output/bazel build //src/... //third_party/... >&2 || exit $?
+
+cat <<EOF
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+EOF
+
+# Find Java paths
+for path in $(find src -name "*.java" | sed "s|/com/google/.*$||" | sort -u); do
+    echo "    <classpathentry kind=\"src\" path=\"$path\"/>"
+done
+
+# Find third party paths
+for path in $(find third_party -name "*.jar" | sort -u); do
+    echo "    <classpathentry kind=\"lib\" path=\"$path\"/>"
+done
+
+# Find protobuf generation
+for path in $(find bazel-bin/ -name "*.java" | grep proto | sed "s|/com/google/.*$||" | sort -u | sed 's|//|/|'); do
+    echo "    <classpathentry kind=\"lib\" path=\"$(dirname $path)/$(basename $path .proto_output)\" sourcepath=\"$path\"/>"
+done
+
+# Find other generation
+PACKAGE_LIST=$(find src -name "BUILD" | sed "s|/BUILD||" | sed "s|^|//|")
+# Returns the package of file $1
+function get_package_of() {
+  # look for the longest matching package
+  for i in ${PACKAGE_LIST}; do
+    if [[ "$1" =~ ^$i ]]; then  # we got a match
+      echo $(echo -n $i | wc -c | xargs echo) $i
+    fi
+  done | sort -r -n | head -1 | cut -d " " -f 2
+}
+
+# returns the target corresponding to file $1
+function get_target_of() {
+    local package=$(get_package_of $1)
+    local file=$(echo $1 | sed "s|^${package}/||g")
+    echo "${package}:${file}"
+}
+
+# Returns the target that consume file $1
+function get_consuming_target() {
+    # Here to the god of bazel, I should probably offer one or two memory chips for that
+    local target=$(get_target_of $1)
+    local generating_target=$(query "deps(${target}, 1) - ${target}")
+    local java_library=$(query "rdeps(//src/..., ${generating_target}, 1) - ${generating_target}")
+    echo "${java_library}"
+}
+
+# Returns the library that contains the generated file $1
+function get_containing_library() {
+    get_consuming_target $1 | sed 's|:|/lib|' | sed 's|^//|bazel-bin/|' | sed 's|$|.jar|'
+}
+
+for path in $(find bazel-genfiles/ -name "*.java" | sed 's|/\{0,1\}bazel-genfiles/\{0,1\}|//|'); do
+    source_path=$(echo $path | sed 's|//|bazel-genfiles/|' | sed 's|/com/.*$||')
+    echo "    <classpathentry kind=\"lib\" path=\"$(get_containing_library ${path})\" sourcepath=\"${source_path}\"/>"
+done | sort -u
+
+# Write the end of the .classpath file
+cat <<'EOF'
+    <classpathentry kind="lib" path="tools/jdk/jdk/lib/tools.jar"/>
+    <classpathentry kind="output" path="bazel-out/eclipse-classes"/>
+    <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
+</classpath>
+EOF
diff --git a/scripts/setup-eclipse.sh b/scripts/setup-eclipse.sh
new file mode 100755
index 0000000..68ce918
--- /dev/null
+++ b/scripts/setup-eclipse.sh
@@ -0,0 +1,57 @@
+#!/bin/sh
+# Copyright 2015 Google Inc. All rights reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# 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.
+#
+# Generates an Eclipse project. If a .project is not present, it will creates
+# it and it will overwrite any .classpath file present
+#
+# Usage: ./setup-eclipse.sh
+#
+
+set -eu
+
+cd $(dirname "$0")
+cd ..
+
+# Simply creates a Eclipse java project
+if [ ! -f ".project" ]; then
+  cat >.project <<'EOF'
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+    <name>bazel</name>
+    <comment></comment>
+    <projects>
+    </projects>
+    <buildSpec>
+        <buildCommand>
+            <name>org.eclipse.jdt.core.javabuilder</name>
+            <arguments>
+            </arguments>
+        </buildCommand>
+    </buildSpec>
+    <natures>
+        <nature>org.eclipse.jdt.core.javanature</nature>
+    </natures>
+</projectDescription>
+EOF
+fi
+
+./scripts/generate-classpath.sh >.classpath
+
+echo
+echo '***'
+echo '*** Eclipse project generated'
+echo '***'
+echo
+echo 'You can now import the bazel project into Eclipse.'