// 当前最新开发环境：gradle-7.5.1
plugins {
    id 'java'

    /*
    Gradle-IntelliJ-Plugin 'org.jetbrains.intellij' 的设置说明：
    1. Gradle-IntelliJ-Plugin'org.jetbrains.intellij' 版本升级说明：
       - build plugin for 2022.1 ，'org.jetbrains.intellij' 需要使用 1.5.3+ 版本（同时需要升级 gradle 版本到 7.* 以上，JDK 版本至 11）
       - build plugin for 2022.2.3 ，'org.jetbrains.intellij' 需要使用 1.13.3+ 版本（同时需要升级 gradle 版本到 7.* 以上，JDK 版本至 17）
    2. 若使用本地默认的 gradle ，请手动切换 gradle 到特定版本
    3. 当前项目默认使项目根目录下 gradle/wrapper/gradle-wrapper.properties 中指定的 gradle 版本，更改当前项目的 gradle 版本的便捷方法是：修改 gradle-wrapper.properties 中 distributionUrl（目标gradle下载地址）
       - 使用 gradle-5.2.1：distributionUrl=https\://services.gradle.org/distributions/gradle-5.2.1-all.zip
       - 使用 gradle-7.5.1：distributionUrl=https\://services.gradle.org/distributions/gradle-7.5.1-all.zip
    4. 当前项目使用的 JDK 版本设置：Preferences | Build, Execution, Deployment | Build Tools | Gradle，Gradle JVM 设置对应版本 JDK

    */
    // id 'org.jetbrains.intellij' version '0.4.16' // gradle 6.7
    // id 'org.jetbrains.intellij' version '1.12.0' // gradle 7.5.1
    // id 'org.jetbrains.intellij' version '1.13.3' // gradle 7.5.1
    id 'org.jetbrains.intellij' version '1.17.1' // gradle 7.5.1
}

// 插件需要支持的 IntelliJ IDEA Community Edition平台版本
// 平台版本使用其 branch_number 作为区分
// 关于`branch_number`的更多细节，可参考 https://www.jetbrains.org/intellij/sdk/docs/basics/getting_started/build_number_ranges.html
enum PlatformType {
    // Android Studio 3.5
    IC_191 ('191'),
    // Android Studio 3.6
    IC_192 ('192'),
    // Android Studio 4.0
    IC_193 ('193'),
    // Android Studio 4.1
    IC_201 ('201'),
    // Android Studio 4.2
    IC_202 ('202'),
    // Android Studio - Arctic Fox | 2020.3.1
    IC_203 ('203'),
    // Android Studio Bumblebee | 2021.1.1
    IC_211 ('211'),
    // Android Studio Chipmunk | 2021.2.1
    IC_212 ('212'),
    // Android Studio Dolphin | 2021.3.1
    IC_213 ('213'),
    // Android Studio Electric Eel | 2022.1.1
    IC_221 ('221'),
    // Android Studio Flamingo | 2022.2.1
    IC_222 ('222'),
    // Android Studio Giraffe | 2022.3.1
    IC_223 ('223'),
    // Android Studio Hedgehog | 2023.1.1
    IC_231 ('231'),
    // Android Studio Meerkat | 2024.3.1
    IC_243 ('243'),

    final String branchNumber

    PlatformType(String branchNumber) {
        this.branchNumber = branchNumber
    }

    String getBranchNumber() {
        return this.branchNumber
    }

    String toString(){
        'IC-' + branchNumber
    }
}

// flr-plugin-engine 的版本号
// 关于其介绍，可看文档《flr_plugin_workflow》（注：文档中使用的字段为 plugin_engine_version）
// 该值在完成一个新版本的 flr-plugin-engine 开发后，手动更新
String pluginEngineVersion = '3.4.0'

// 用于开发 flr-plugin-engine 时使用平台版本
// 一般使用最新平台版本
PlatformType pluginEnginePlatform = PlatformType.IC_243

// 用于开发 flr-plugin 时使用平台版本
// 该值在切换到 platform-master 分支后，手动更新
// 如切换到 191-master 分支后，该值更新为 PlatformType.IC_191
PlatformType pluginProductPlatform = pluginEnginePlatform

// 指定JDK版本
// JDK8: '1.8'or'8'
// JDK11: '11' （IDEA 2020.2 版本开始使用JDK11）
sourceCompatibility = '8'
switch (pluginProductPlatform) {
// Config for Android Studio 3.5
    case PlatformType.IC_191:
// Config for Android Studio 3.6
    case PlatformType.IC_192:
// Config for Android Studio 4.0
    case PlatformType.IC_193:
// Config for Android Studio 4.1
    case PlatformType.IC_201:
        sourceCompatibility = '8'
        break
// Config for Android Studio 4.2
    case PlatformType.IC_202:
// Config for Android Studio - Arctic Fox | 2020.3.1
    case PlatformType.IC_203:
// Config for Android Studio Bumblebee | 2021.1.1
    case PlatformType.IC_211:
// Config for Android Studio Chipmunk | 2021.2.1
    case PlatformType.IC_212:
// Config for Android Studio Dolphin | 2021.3.1
    case PlatformType.IC_213:
// Config for Android Studio Electric Eel | 2022.1.1
    case PlatformType.IC_221:
        sourceCompatibility = '11'
        break
// Config for Android Studio Flamingo | 2022.2.1
    case PlatformType.IC_222:
// Config for Android Studio Giraffe | 2022.3.1
    case PlatformType.IC_223:
// Config for Android Studio Hedgehog | 2023.1.1
    case PlatformType.IC_231:
// Config for Android Studio Meerkat | 2024.3.1
    case PlatformType.IC_243:
        sourceCompatibility = '17'
        break
}

println "flr-as-plugin 构建配置信息如下："
println " - PluginProductPlatform: ${pluginProductPlatform}"
println " - SourceCompatibility: JDK${sourceCompatibility}"

// flr-plugin 的版本号
// 关于其介绍，可看文档《flr_plugin_workflow》（注：文档中使用的字段为 plugin_product_version）
//
// plugin_product_version = #{IC_branch_number}.#{plugin_engine_version}
//
// example:
// IC_branch_number = 191
// plugin_engine_version = 1.0.0
// plugin_product_version = 191.1.0.0
//
String pluginProductVersion = pluginProductPlatform.branchNumber + '.' + pluginEngineVersion

// 打包文件的基本名称
// example: flr-as-plugin-191.1.0.0
archivesBaseName = rootProject.name + '-' + pluginProductVersion

repositories {
    mavenCentral()
}

dependencies {
    /*
    gradle7 适配说明：
    - compile 函数名需要改为 implementation
    - testCompile 函数名需要改为 testImplementation
    */
//    testCompile group: 'junit', name: 'junit', version: '4.12'
//    compile group: 'org.yaml', name: 'snakeyaml', version: '1.26'
    testImplementation group: 'junit', name: 'junit', version: '4.12'
    implementation group: 'org.yaml', name: 'snakeyaml', version: '1.26'
}

// See https://github.com/JetBrains/gradle-intellij-plugin#intellij-platform-properties
// Get platformVersion with Build#AI-Version of Android Studio from https://www.jetbrains.com/idea/download/other.html
intellij {

    String platformVersion = null
    List platformPlugins = null

    switch (pluginProductPlatform) {
        // Config for Android Studio 3.5
        case PlatformType.IC_191:
            platformVersion = '2019.1.4'
            platformPlugins =  ['Dart:191.8593', 'io.flutter:43.0.1']
            break
        // Config for Android Studio 3.6
        case PlatformType.IC_192:
            platformVersion = '2019.2.4'
            platformPlugins = ['Dart:192.7761', 'io.flutter:43.0.2']
            break
        // Config for Android Studio 4.0
        case PlatformType.IC_193:
            platformVersion = '2019.3.3'
            platformPlugins = ['Dart:193.7361', 'io.flutter:45.1.2']
            break
        // Config for Android Studio 4.1
        case PlatformType.IC_201:
            platformVersion = '2020.1.4'
            platformPlugins = ['Dart:201.9002', 'io.flutter:50.0.2']
            break
        // Config for Android Studio 4.2
        case PlatformType.IC_202:
            platformVersion = '2020.2.3'
            platformPlugins = ['Dart:202.8488', 'io.flutter:56.0.2']
            break
        // Config for Android Studio - Arctic Fox | 2020.3.1
        case PlatformType.IC_203:
            platformVersion = '2020.3.1'
            platformPlugins = ['Dart:203.8292', 'io.flutter:59.0.2']
            break
        // Config for Android Studio Bumblebee | 2021.1.1
        case PlatformType.IC_211:
            platformVersion = '2021.1'
            platformPlugins = ['Dart:211.7798', 'io.flutter:64.1.2']
            break
       // Config for Android Studio Chipmunk | 2021.2.1
        case PlatformType.IC_212:
            platformVersion = '2021.2.3'
            platformPlugins = ['Dart:212.5744', 'io.flutter:67.1.2']
            break
        // Config for Android Studio Dolphin | 2021.3.1
        case PlatformType.IC_213:
            platformVersion = '2021.3.2'
            platformPlugins = ['Dart:213.7371', 'io.flutter:70.0.3']
            break
        // Config for Android Studio Electric Eel | 2022.1.1
        case PlatformType.IC_221:
            platformVersion = '2022.1.4'
            platformPlugins = ['java', 'Dart:221.6096', 'io.flutter:71.2.4']
            break
        // Config for Android Studio Flamingo | 2022.2.1
        case PlatformType.IC_222:
            platformVersion = '2022.2.4'
            platformPlugins = ['java', 'Dart:222.4582', 'io.flutter:73.0.2']
            break
       // Config for Android Studio Giraffe | 2022.3.1
        case PlatformType.IC_223:
            platformVersion = '2022.3.3'
            platformPlugins = ['java', 'Dart:223.8950', 'io.flutter:73.0.3']
            break
        // Config for Android Studio Hedgehog | 2023.1.1
        case PlatformType.IC_231:
            platformVersion = '2023.1.5'
            platformPlugins = ['java', 'Dart:231.9402', 'io.flutter:77.1.2']
            break
        // Config for Android Studio Meerkat | 2024.3.1
        case PlatformType.IC_243:
            platformVersion = '2024.3.1'
            platformPlugins = ['java', 'Dart:243.23177', 'io.flutter:82.2.4']
            break
    }

    assert(platformVersion != null && platformPlugins != null)

    version = platformVersion
    plugins = platformPlugins

    /*
    * Use this property if you want to test your plugin in any non-IDEA JetBrains IDE such as WebStorm or Android Studio.
    *
    * See https://github.com/JetBrains/gradle-intellij-plugin#rundebug-ide-properties
    * */
    //alternativeIdePath '/Applications/Android Studio.app'
}

// See https://github.com/JetBrains/gradle-intellij-plugin#patching-dsl
patchPluginXml {

    // 设置插件的版本号
    version = pluginProductVersion

    // 设置插件描述
    pluginDescription = """
<p>Flr (Flutter-R): A Flutter Resource Manager Android Studio Plugin, which can help flutter developer to auto specify assets in pubspec.yaml and generate r.g.dart file.</p>
<br>
<p>You can get more details from:</p>
<ul>
  <li><a href="https://github.com/Fly-Mix/flr-as-plugin">Flr GitHub Repository</a></li>
  <li><a href="https://plugins.jetbrains.com/plugin/13789-flr/">Flr Plugin Homepage</a></li>
</ul>
    """

    /*
    * 插件兼容IDEA的最小版本号和最大版本号
    * 详细可参考：
    * - https://github.com/JetBrains/gradle-intellij-plugin#patching-dsl
    * - http://www.jetbrains.org/intellij/sdk/docs/basics/getting_started/build_number_ranges.html
    * */
    sinceBuild = pluginProductPlatform.branchNumber
    //untilBuild = '191.*'

    /*
    * 设置版本更新日志
    * 注意：最新版本号的标题样式为h3，其他版本号的标题样式为h4
    */
    changeNotes = """
<h2>Flr Plugin Change Notes</h2>

<br/>
<h2>Flr Plugin Engine Change Notes</h2>
<h3>3.4.0</h3>
<ul>
  <li>Migrate existing code from Components to modern APIs</li>
</ul>
<h3>3.3.1</h3>
<ul>
  <li>Fix bug</li>
</ul>
<h4>3.3.0</h4>
<ul>
  <li>Support for nullsafety-feature of Dart 2.12</li>
</ul>
<h4>3.2.0</h4>
<ul>
  <li>Add pubspec.yaml file tree view</li>
</ul>
<h4>3.1.0</h4>
<ul>
  <li>Support for processing(init/generate/monitor) multi projects(the main project and its sub projects in one workspace)</li>
  <li>Support for auto merging old asset specifications when specifying new assets</li>
</ul>
<h4>3.0.0</h4>
<ul>
  <li>Support for processing non-implied resource file which is outside of "lib/" directory (such as "~/path/to/flutter_r_demo/assets/images/test.png")</li>
  <li>New recommended flutter resource structure</li>
</ul>
<h4>2.0.0</h4>
<ul>
  <li>New asset generation algorithm to support all kinds of standard or nonstandard image/text resource structure</li>
  <li>New asset-id generation algorithm to support assets with the same filename but different path</li>
  <li>New recommended flutter resource structure</li>
</ul>
<h4>1.1.0</h4>
<ul>
  <li>Improve generate-capability to support nonstandard image resource structure</li>
  <li>Add recommend-capability to display the recommended flutter resource structure</li>
</ul>
<h4>1.0.0</h4>
<ul>
  <li>Automatically specify assets in pubspec.yaml and generate r.g.dart file</li>
  <li>Support for processing image assets (.png, .jpg, .jpeg, .gif, .webp, .icon, .bmp, .wbmp, .svg)</li>
  <li>Support for processing image asset variants</li>
  <li>Support for processing text assets (.txt, .json, .yaml, .xml)</li>
  <li>Support for processing font assets (.ttf, .otf, .ttc)</li>
  <li>Improve robustness</li>
</ul>
    """
}