Update setup-intellij to get it working again.

This is just based on what I had to do to get IntelliJ working with
Bazel at head, but I think it makes things simpler as well.

Main changes:
* External jars are supported (I think);
* Protobufs are imported en masse via a "directory" module library;
* We now include all Android files unless it looks like we won't be
able to compile them.

--
Change-Id: Ib2ce59f6636f0d262b05f89d57837d7018a0fbfc
Reviewed-on: https://bazel-review.googlesource.com/1611
MOS_MIGRATED_REVID=98118522
diff --git a/scripts/get_all_bazel_paths.sh b/scripts/get_all_bazel_paths.sh
index b64ff40..4a0cbfe 100755
--- a/scripts/get_all_bazel_paths.sh
+++ b/scripts/get_all_bazel_paths.sh
@@ -38,23 +38,16 @@
 ./output/bazel build -- //src/... //third_party/... \
   -//third_party/ijar/test/... -//src/tools/{xcode,android}/... >&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)"
 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|//|/|')"
+# Android-SDK-dependent files may need to be excluded from compilation.
+ANDROID_IMPORTING_FILES="$(grep "^import android\." -R -l --include "*.java" src | sort)"
 
 # All other generated libraries.
 readonly package_list=$(find src -name "BUILD" | sed "s|/BUILD||" | sed "s|^|//|")
@@ -94,6 +87,12 @@
   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 &&
+  # Add in "external" jars which don't have source paths.
+  for jardir in "jar/" ""; do
+    for path in $(find bazel-genfiles/${jardir}_ijar -name "*.jar" | sed 's|^/+||' | uniq); do
+      echo "${path}:"
+    done
   done | sort -u
 }
 
diff --git a/scripts/resources/idea/compiler.xml b/scripts/resources/idea/compiler.xml
deleted file mode 100644
index 96cc43e..0000000
--- a/scripts/resources/idea/compiler.xml
+++ /dev/null
@@ -1,22 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<project version="4">
-  <component name="CompilerConfiguration">
-    <resourceExtensions />
-    <wildcardResourcePatterns>
-      <entry name="!?*.java" />
-      <entry name="!?*.form" />
-      <entry name="!?*.class" />
-      <entry name="!?*.groovy" />
-      <entry name="!?*.scala" />
-      <entry name="!?*.flex" />
-      <entry name="!?*.kt" />
-      <entry name="!?*.clj" />
-      <entry name="!?*.aj" />
-    </wildcardResourcePatterns>
-    <annotationProcessing>
-      <profile default="true" name="Default" enabled="false">
-        <processorPath useClasspath="true" />
-      </profile>
-    </annotationProcessing>
-  </component>
-</project>
\ No newline at end of file
diff --git a/scripts/setup-intellij.sh b/scripts/setup-intellij.sh
index a0662e9..dc0949b 100755
--- a/scripts/setup-intellij.sh
+++ b/scripts/setup-intellij.sh
@@ -23,6 +23,38 @@
 cp -R scripts/resources/idea/*.* .idea/
 source scripts/get_all_bazel_paths.sh
 
+readonly compiler_file=.idea/compiler.xml
+cat >$compiler_file <<'EOH'
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="CompilerConfiguration">
+    <excludeFromCompile>
+EOH
+for android_file in $ANDROID_IMPORTING_FILES; do
+  echo "      <file url=\"\$PROJECT_DIR\$/${android_file}\" />" >> $compiler_file
+done
+cat >>$compiler_file <<'EOF'
+    </excludeFromCompile>
+    <resourceExtensions />
+    <wildcardResourcePatterns>
+      <entry name="!?*.java" />
+      <entry name="!?*.form" />
+      <entry name="!?*.class" />
+      <entry name="!?*.groovy" />
+      <entry name="!?*.scala" />
+      <entry name="!?*.flex" />
+      <entry name="!?*.kt" />
+      <entry name="!?*.clj" />
+      <entry name="!?*.aj" />
+    </wildcardResourcePatterns>
+    <annotationProcessing>
+      <profile default="true" name="Default" enabled="true">
+        <processorPath useClasspath="true" />
+      </profile>
+    </annotationProcessing>
+  </component>
+</project>
+EOF
 
 readonly iml_file=bazel.iml
 # The content root output/classes is used for generated sources, specifically
@@ -31,13 +63,8 @@
 <?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 />
+    <output url="file://\$MODULE_DIR\$/out" />
     <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
 
@@ -66,19 +93,26 @@
     <orderEntry type="sourceFolder" forTests="false" />
 EOF
 
+# Write a module-library entry, usually a jar file but occasionally a directory.
 function write_jar_entry() {
-  local jar_file=$1
+  local root_file=$1
   if [[ $# > 1 ]]; then
     local source_path=$2
   else
     local source_path=""
   fi
-  local readonly basename=${jar_file##*/}
+  local protocol="file"
+  local file_end=""
+  if [[ $root_file == *.jar ]]; then
+    protocol="jar"
+    file_end="!"
+  fi
+  local readonly basename=${root_file##*/}
     cat >> $iml_file <<EOF
       <orderEntry type="module-library">
         <library name="${basename}">
           <CLASSES>
-            <root url="jar://\$MODULE_DIR\$/${jar_file}!/" />
+            <root url="${protocol}://\$MODULE_DIR\$/${root_file}${file_end}/" />
           </CLASSES>
           <JAVADOC />
 EOF
@@ -91,6 +125,11 @@
           </SOURCES>
 EOF
   fi
+  if [[ $protocol == "file" ]]; then
+    cat >> $iml_file <<EOF
+          <jarDirectory url="file://\$MODULE_DIR\$/${root_file}" recursive="false" />
+EOF
+  fi
   cat >> $iml_file <<'EOF'
       </library>
     </orderEntry>
@@ -101,15 +140,12 @@
   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
+write_jar_entry "bazel-bin/src/main/protobuf"
+
 cat >> $iml_file <<'EOF'
   </component>
 </module>