/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.
 */
import org.gradle.plugins.ide.eclipse.model.Container

plugins {
  id 'idea'
  id 'eclipse'
}

eclipse {
  classpath {
    defaultOutputDir = file('build-eclipse')
    downloadSources = true
    file {
      whenMerged { classpath ->
        // Remove the gradle output directories from the eclipse classpath.
        // Unfortunately, using minusConfigurations does not work here, because
        // it removes the entire geode-core project.
        classpath.entries.removeAll { entry ->
          (entry.path.contains('geode-core/build') ||
            entry.path.contains('geode-web/build') ||
            entry.path.contains('geode-assembly/build') ||
            entry.path.contains('geode-dependencies.jar')) &&
            !entry.path.contains('generated-resources') &&
            !entry.path.contains('generated-src')
        }

        //By default, gradle adds the java 1.8 *execution environment*, which has access restrictions on
        //things like Unsafe. Change it to a direct dependency on the workspace JDK
        classpath.entries = classpath.entries.collect { entry ->
          entry.path.contains('org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE') ?
            new Container('org.eclipse.jdt.launching.JRE_CONTAINER') :
            entry
        }
      }
    }
  }
  // Several files have UTF-8 encoding and Eclipse running on Windows
  // will have trouble unless we tell it to use UTF-8 encoding.
  // This setting needs to go into the core.resources.prefs file,
  // which the JDT script isn't set up to configure
  eclipseJdt {
    doLast {
      File f = file('.settings/org.eclipse.core.resources.prefs')
      f.write('eclipse.preferences.version=1\n')
      f.append('encoding/<project>=utf-8')
    }
  }
}

cleanEclipse {
  doLast {
    delete '.settings/org.eclipse.core.resources.prefs'
  }
}

tasks.eclipse.dependsOn(cleanEclipse)

idea {
  module {
    inheritOutputDirs = true
    downloadSources = true
  }
}
