Add support for creating an IntelliJ project.

--
MOS_MIGRATED_REVID=91492842
diff --git a/scripts/generate-classpath.sh b/scripts/generate-classpath.sh
index 6bc42f0..2ee880b 100755
--- a/scripts/generate-classpath.sh
+++ b/scripts/generate-classpath.sh
@@ -20,84 +20,38 @@
 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 $?
+source scripts/get_all_bazel_paths.sh || exit 1
 
 cat <<EOF
 <?xml version="1.0" encoding="UTF-8"?>
 <classpath>
 EOF
 
-# Find Java paths
-JAVA_PATHS="$(find src -name "*.java" | sed "s|/com/google/.*$||" | sort -u)"
-# TODO(bazel-team): Once objc_tools have buildfiles, uncomment the if below
-# if [ "$(uname -s | tr 'A-Z' 'a-z')" != "darwin" ]; then
-JAVA_PATHS="$(echo "${JAVA_PATHS}" | fgrep -v "/objc_tools/")"
-# fi
 for path in ${JAVA_PATHS}; do
     echo "    <classpathentry kind=\"src\" path=\"$path\"/>"
 done
 
 # Find third party paths
-for path in $(find third_party -name "*.jar" | sort -u); do
+for path in ${THIRD_PARTY_JAR_PATHS}; 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
+for path in ${PROTOBUF_PATHS}; 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/\{1,2\}|//|'); 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
+for path_pair in ${GENERATED_PATHS}; do
+    path_arr=(${path_pair//:/ })
+    jar=${path_arr[0]}
+    source_path=${path_arr[1]}
+    echo "    <classpathentry kind=\"lib\" path=\"${jar}\" sourcepath=\"${source_path}\"/>"
+done
 
 # Write the end of the .classpath file
-cat <<'EOF'
+cat <<EOF
     <classpathentry kind="lib" path="tools/jdk/jdk/lib/tools.jar"/>
-    <classpathentry kind="output" path="bazel-out/eclipse-classes"/>
+    <classpathentry kind="output" path="${IDE_OUTPUT_PATH}"/>
     <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
       <accessrules>
         <accessrule kind="accessible" pattern="**"/>
diff --git a/scripts/get_all_bazel_paths.sh b/scripts/get_all_bazel_paths.sh
new file mode 100755
index 0000000..0ab872f
--- /dev/null
+++ b/scripts/get_all_bazel_paths.sh
@@ -0,0 +1,96 @@
+#!/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.
+#
+# Gets all libraries needed for IDE support in 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 $?
+
+# Path IDE should put its output files in.
+IDE_OUTPUT_PATH="bazel-out/ide-classes"
+
+# Source roots.
+JAVA_PATHS="$(find src -name "*.java" | sed "s|/com/google/.*$||" | sort -u)"
+# TODO(bazel-team): Once objc_tools have buildfiles, uncomment the if below
+# if [ "$(uname -s | tr 'A-Z' 'a-z')" != "darwin" ]; then
+JAVA_PATHS="$(echo "${JAVA_PATHS}" | fgrep -v "/objc_tools/")"
+# fi
+# Android doesn't work out of the box, but should we tell users to install the
+# Android SDK?
+JAVA_PATHS="$(echo "${JAVA_PATHS}" | fgrep -v "/android/")"
+
+THIRD_PARTY_JAR_PATHS="$(find third_party -name "*.jar" | sort -u)"
+
+# Generated protobuf files have special jar files and output into .proto_output
+# directories.
+PROTOBUF_PATHS="$(find bazel-bin/ -name "*.java" | grep proto | sed "s|/com/google/.*$||" | sort -u | sed 's|//|/|')"
+
+# All other generated libraries.
+readonly 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|'
+}
+
+function collect_generated_paths() {
+  # uniq to avoid doing blaze query on duplicates.
+  for path in $(find bazel-genfiles/ -name "*.java" | sed 's|/\{0,1\}bazel-genfiles/\{1,2\}|//|' | uniq); do
+    source_path=$(echo ${path} | sed 's|//|bazel-genfiles/|' | sed 's|/com/.*$||')
+    echo "$(get_containing_library ${path}):${source_path}"
+  done | sort -u
+}
+
+# GENERATED_PATHS stores pairs of jar:source_path as a list of strings, with
+# each pair internally delimited by a colon. Use ${string//:/ } to split one.
+GENERATED_PATHS="$(collect_generated_paths)"
diff --git a/scripts/setup-intellij.sh b/scripts/setup-intellij.sh
new file mode 100755
index 0000000..a0662e9
--- /dev/null
+++ b/scripts/setup-intellij.sh
@@ -0,0 +1,116 @@
+#!/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 IntelliJ project in Bazel.
+
+set -o errexit
+cd $(dirname "$0")
+cd ..
+
+mkdir -p .idea/
+cp -R scripts/resources/idea/*.* .idea/
+source scripts/get_all_bazel_paths.sh
+
+
+readonly iml_file=bazel.iml
+# The content root output/classes is used for generated sources, specifically
+# AutoValue.
+cat > $iml_file <<EOH
+<?xml version="1.0" encoding="UTF-8"?>
+<module type="JAVA_MODULE" version="4">
+  <component name="NewModuleRootManager">
+    <output url="file://\$MODULE_DIR\$/${IDE_OUTPUT_PATH}" />
+    <exclude-output />
+    <orderEntry type="inheritedJdk" />
+    <content url="file://\$MODULE_DIR\$/output/classes">
+      <sourceFolder url="file://\$MODULE_DIR\$/output/classes" isTestSource="false" />
+      <excludeFolder url="file://\$MODULE_DIR\$/output/classes/org" />
+    </content>
+    <content url="file://\$MODULE_DIR\$/src">
+EOH
+
+for source in ${JAVA_PATHS}; do
+     if [[ $source == *"javatests" ]]; then
+       is_test_source="true"
+     elif [[ $source == *"test/java" ]]; then
+       is_test_source="true"
+     else
+       is_test_source="false"
+     fi
+     echo '      <sourceFolder url="file://$MODULE_DIR$/'"${source}\" isTestSource=\"${is_test_source}\" />" >> $iml_file
+done
+cat >> $iml_file <<'EOF'
+    </content>
+    <content url="file://$MODULE_DIR$/third_party/java">
+EOF
+
+THIRD_PARTY_JAVA_PATHS="$(ls third_party/java | sort -u | sed -e 's%$%/java%')"
+
+for third_party_java_path in ${THIRD_PARTY_JAVA_PATHS}; do
+  echo '      <sourceFolder url="file://$MODULE_DIR$/third_party/java/'${third_party_java_path}'" isTestSource="false" />' >> $iml_file
+done
+cat >> $iml_file <<'EOF'
+    </content>
+    <orderEntry type="sourceFolder" forTests="false" />
+EOF
+
+function write_jar_entry() {
+  local jar_file=$1
+  if [[ $# > 1 ]]; then
+    local source_path=$2
+  else
+    local source_path=""
+  fi
+  local readonly basename=${jar_file##*/}
+    cat >> $iml_file <<EOF
+      <orderEntry type="module-library">
+        <library name="${basename}">
+          <CLASSES>
+            <root url="jar://\$MODULE_DIR\$/${jar_file}!/" />
+          </CLASSES>
+          <JAVADOC />
+EOF
+  if [[ -z "${source_path}" ]]; then
+    echo "          <SOURCES />" >> $iml_file
+  else
+    cat >> $iml_file <<EOF
+          <SOURCES>
+            <root url="jar:/\$MODULE_DIR\$/${source_path}!/" />
+          </SOURCES>
+EOF
+  fi
+  cat >> $iml_file <<'EOF'
+      </library>
+    </orderEntry>
+EOF
+}
+
+for jar in ${THIRD_PARTY_JAR_PATHS}; do
+  write_jar_entry $jar
+done
+
+for source_path in ${PROTOBUF_PATHS}; do
+  write_jar_entry ${source_path%.proto_output} $source_path
+done
+
+for path_pair in ${GENERATED_PATHS}; do
+  write_jar_entry ${path_pair//:/ }
+done
+
+write_jar_entry tools/jdk/jdk/lib/tools.jar
+cat >> $iml_file <<'EOF'
+  </component>
+</module>
+EOF