/*
 * Copyright 2013-2016 consulo.io
 *
 * 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 consulo.bundle.impl;

import consulo.logging.Logger;
import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.openapi.projectRoots.SdkModificator;
import com.intellij.openapi.projectRoots.SdkType;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VirtualFile;
import consulo.bundle.PredefinedBundlesProvider;

import javax.annotation.Nonnull;
import java.util.Collection;

/**
 * @author VISTALL
 * @since 10.02.15
 */
public class DefaultPredefinedBundlesProvider extends PredefinedBundlesProvider {
  private static final Logger LOG = Logger.getInstance(DefaultPredefinedBundlesProvider.class);

  @Override
  public void createBundles(@Nonnull Context context) {
    for (SdkType sdkType : SdkType.EP_NAME.getExtensionList()) {
      try {
        if (sdkType.canCreatePredefinedSdks()) {
          Collection<String> paths = sdkType.suggestHomePaths();

          for (String path : paths) {
            path = sdkType.adjustSelectedSdkHome(path);

            if (sdkType.isValidSdkHome(path)) {
              VirtualFile dirPath = LocalFileSystem.getInstance().findFileByPath(path);
              if (dirPath == null) {
                continue;
              }

              String sdkPath = sdkType.sdkPath(dirPath);

              Sdk sdk = context.createSdk(sdkType, sdkPath);
              SdkModificator sdkModificator = sdk.getSdkModificator();
              sdkModificator.setHomePath(sdkPath);
              sdkModificator.setVersionString(sdkType.getVersionString(sdkPath));
              sdkModificator.commitChanges();

              sdkType.setupSdkPaths(sdk);
            }
          }
        }
      }
      catch (Error e) {
        LOG.error(e);
      }
    }
  }
}

@@ -622,39 +622,8 @@
 l;%0A%0A
-import consulo.logging.Logger;%0A
 impo
@@ -1110,105 +1110,8 @@
 r %7B%0A
-  private static final Logger LOG = Logger.getInstance(DefaultPredefinedBundlesProvider.class);%0A%0A
   @O
@@ -1181,31 +1181,8 @@
 %0A   
- for (SdkType sdkType :
  Sdk
@@ -1194,19 +1194,23 @@
 EP_NAME.
-get
+forEach
 Extensio
@@ -1214,32 +1214,26 @@
 sion
-List()) %7B%0A      try
+Safe(sdkType -%3E
  %7B%0A
-  
     
@@ -1283,18 +1283,16 @@
         
-  
 Collecti
@@ -1335,26 +1335,24 @@
 mePaths();%0A%0A
-  
         for 
@@ -1385,18 +1385,16 @@
         
-  
 path = s
@@ -1426,26 +1426,24 @@
 ome(path);%0A%0A
-  
           if
@@ -1488,18 +1488,16 @@
         
-  
 VirtualF
@@ -1570,26 +1570,24 @@
             
-  
 if (dirPath 
@@ -1611,18 +1611,16 @@
         
-  
 continue
@@ -1621,18 +1621,16 @@
 ntinue;%0A
-  
         
@@ -1648,18 +1648,16 @@
         
-  
 String s
@@ -1688,26 +1688,24 @@
 (dirPath);%0A%0A
-  
             
@@ -1763,18 +1763,16 @@
         
-  
 SdkModif
@@ -1820,18 +1820,16 @@
 ator();%0A
-  
         
@@ -1873,34 +1873,32 @@
 h);%0A            
-  
 sdkModificator.s
@@ -1949,18 +1949,16 @@
 Path));%0A
-  
         
@@ -2006,18 +2006,16 @@
         
-  
 sdkType.
@@ -2034,18 +2034,16 @@
 s(sdk);%0A
-  
         
@@ -2050,30 +2050,26 @@
   %7D%0A        
-  %7D%0A  
+%7D%0A
       %7D%0A    
@@ -2072,71 +2072,11 @@
     
-  %7D%0A      catch (Error e) %7B%0A        LOG.error(e);%0A      %7D%0A    %7D
+%7D);
 %0A  %7D

use safe method for error less iteration