am bb78b40d: am 35059e90: Merge "Now we check at startup that the IDE does not have an old copy of builder-model jar file." into idea133 automerge: 42d9e12

* commit 'bb78b40d83b790a0e08d5587160a5138520bc402':
  Now we check at startup that the IDE does not have an old copy of builder-model jar file.
diff --git a/android/src/com/android/tools/idea/startup/AndroidStudioSpecificInitializer.java b/android/src/com/android/tools/idea/startup/AndroidStudioSpecificInitializer.java
index fb085e9..3581f5b 100755
--- a/android/src/com/android/tools/idea/startup/AndroidStudioSpecificInitializer.java
+++ b/android/src/com/android/tools/idea/startup/AndroidStudioSpecificInitializer.java
@@ -26,6 +26,7 @@
 import com.android.tools.idea.sdk.wizard.SdkQuickfixWizard;
 import com.android.tools.idea.wizard.ExperimentalActionsForTesting;
 import com.android.utils.Pair;
+import com.google.common.annotations.VisibleForTesting;
 import com.google.common.collect.Lists;
 import com.google.common.io.Closeables;
 import com.intellij.debugger.settings.NodeRendererSettings;
@@ -52,7 +53,9 @@
 import com.intellij.openapi.projectRoots.SdkAdditionalData;
 import com.intellij.openapi.projectRoots.SdkModificator;
 import com.intellij.openapi.roots.OrderRootType;
+import com.intellij.openapi.ui.Messages;
 import com.intellij.openapi.util.io.FileUtil;
+import com.intellij.openapi.util.io.FileUtilRt;
 import com.intellij.openapi.util.text.StringUtil;
 import com.intellij.openapi.vfs.VirtualFile;
 import com.intellij.psi.codeStyle.CodeStyleScheme;
@@ -114,6 +117,7 @@
 
   @Override
   public void run() {
+    checkInstallation();
     cleanUpIdePreferences();
 
     if (!Boolean.getBoolean(USE_IDEA_NEW_PROJECT_WIZARDS)) {
@@ -168,6 +172,49 @@
     checkAndSetAndroidSdkSources();
   }
 
+  private static void checkInstallation() {
+    String studioHome = PathManager.getHomePath();
+    if (StringUtil.isEmpty(studioHome)) {
+      LOG.info("Unable to find Studio home directory");
+      return;
+    }
+    File studioHomePath = new File(FileUtil.toSystemDependentName(studioHome));
+    if (!studioHomePath.isDirectory()) {
+      LOG.info(String.format("The path '%1$s' does not belong to an existing directory", studioHomePath.getPath()));
+      return;
+    }
+    File androidPluginLibFolderPath = new File(studioHomePath, FileUtil.join("plugins", "android", "lib"));
+    if (!androidPluginLibFolderPath.isDirectory()) {
+      LOG.info(String.format("The path '%1$s' does not belong to an existing directory", androidPluginLibFolderPath.getPath()));
+      return;
+    }
+
+    File[] children = FileUtil.notNullize(androidPluginLibFolderPath.listFiles());
+    if (hasMoreThanOneBuilderModelFile(children)) {
+      String msg = "Your Android Studio installation is corrupt and will not work properly. " +
+                   "(Found multiple versions of builder-model-*.jar in plugins/android/lib.)\n" +
+                   "This usually happens if Android Studio is extracted into an existing older version.\n\n" +
+                   "Please reinstall (and make sure the new installation directory is empty first.)";
+      String title = "Corrupt Installation";
+      Messages.showDialog(msg, title, new String[]{"Proceed Anyway"}, 0, Messages.getErrorIcon());
+    }
+  }
+
+  @VisibleForTesting
+  static boolean hasMoreThanOneBuilderModelFile(@NotNull File[] libraryFiles) {
+    int builderModelFileCount = 0;
+
+    for (File file : libraryFiles) {
+      String fileName = file.getName();
+      if (fileName.startsWith("builder-model-") && SdkConstants.EXT_JAR.equals(FileUtilRt.getExtension(fileName))) {
+        if (++builderModelFileCount > 1) {
+          return true;
+        }
+      }
+    }
+
+    return false;
+  }
 
   private static void cleanUpIdePreferences() {
     try {
diff --git a/android/testSrc/com/android/tools/idea/startup/AndroidStudioSpecificInitializerTest.java b/android/testSrc/com/android/tools/idea/startup/AndroidStudioSpecificInitializerTest.java
new file mode 100644
index 0000000..a5971f0
--- /dev/null
+++ b/android/testSrc/com/android/tools/idea/startup/AndroidStudioSpecificInitializerTest.java
@@ -0,0 +1,32 @@
+/*
+ * Copyright (C) 2014 The Android Open Source Project
+ *
+ * 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.
+ */
+package com.android.tools.idea.startup;
+
+import junit.framework.TestCase;
+
+import java.io.File;
+
+public class AndroidStudioSpecificInitializerTest extends TestCase {
+  public void testHasMoreThanOneBuilderModelFileWithTwoOrMoreBuilderModelFiles() {
+    File[] files = {new File("builder-model-0.11.0.jar"), new File("builder-model-0.12.0.jar")};
+    assertTrue(AndroidStudioSpecificInitializer.hasMoreThanOneBuilderModelFile(files));
+  }
+
+  public void testHasMoreThanOneBuilderModelFileWithOneBuilderModelFile() {
+    File[] files = {new File("builder-model-0.12.0.jar")};
+    assertFalse(AndroidStudioSpecificInitializer.hasMoreThanOneBuilderModelFile(files));
+  }
+}