import groovy.xml.MarkupBuilder
import java.util.regex.*
import java.util.zip.*
import java.io.*
import com.jetbrains.plugin.blockmap.core.BlockMap
import com.jetbrains.plugin.blockmap.core.FileHash
import com.fasterxml.jackson.databind.ObjectMapper

buildscript {
    repositories {
       mavenCentral()
    }
    dependencies {
       classpath group: 'org.jetbrains.intellij', name: 'blockmap', version: '1.0.5'
       // https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind
       classpath group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.12.3'
    }
}

// In case gradle build fails to execute any of the tasks imported from
// buid.xml, try the following steps: 
//
// 1. Uncomment the following section importing the task-tree plugin
// 2. Run `./gradlew assemble taskTree --no-repeat`
//
// If the above command generates suspiciously-looking output and never
// finishes, the most likely cause is that the target definitions in Ant
// build.xml file have cyclic dependencies. This needs to be fixed in build
// script, and build.xml regenerated.

// plugins {
//  id "com.dorongold.task-tree" version "1.5"
// }

defaultTasks 'assemble'

allprojects {
  apply plugin: 'java'

  repositories {
    mavenLocal()
    mavenCentral()
  }

  // please no jars, no manifests, no build folders 
  jar { onlyIf { false } }
}

repositories {
  ivy {
      url 'https://teamcity.jetbrains.com/guestAuth/repository/download'
      patternLayout {
        ivy '[module]/[revision]/teamcity-ivy.xml'
        artifact '[module]/[revision]/[revision].[ext]'
      }
  }
}

configurations {
    junitAnt
}

dependencies {
  // initialize JUnit optional ant task
  junitAnt 'junit:junit:4.8.2'
  junitAnt('org.apache.ant:ant-junit:1.9.2') {
      transitive = false
  }
}

def allInitDependencies = [
  'coderules:solutions:jetbrains.mps.logic.reactor:copyDependencies',
  'coderules:solutions:jetbrains.mps.logic.test:copyDependencies',
  'coderules:solutions:jetbrains.mps.coderules.typechecking:copyDependencies',
  'coderules:solutions:jetbrains.mps.coderules:copyDependencies' ]


task antProperties {
  onlyIf{ 
    buildNumber().find()
  } 
  doLast {
    ant.properties.'ci.build.num' = "${buildNumber().find() ? buildNumber()[0][4] : '9999'}".toString()
    ant.properties.'ci.build.tag' = "${buildNumber().find() ? buildNumber()[0][4] : 'SNAPSHOT'}".toString()
    ant.properties.'ci.mps.release' = "${buildNumber().find() ? buildNumber()[0][1] : '232'}".toString()
    ant.properties.'mps.ant.log' = "debug"
  }
}

task setup (dependsOn: [allInitDependencies, antProperties]) {
  doLast {
    println 'Initialized all dependencies.'
  }
}

def buildNumber() {
  System.env.BUILD_NUMBER =~ /MPS-([0-9]+)\.([0-9]+).([0-9]+)-([0-9]+)/
}

ant.importBuild('build.xml') { antTarget ->
    'coderules-'+antTarget
}

// ensure 'setup' runs before 'fetchDependencies' and 'declare-mps-tasks'
'coderules-fetchDependencies' {
  dependsOn {
    'setup'
  }
}

'coderules-declare-mps-tasks' {
  dependsOn {
    'setup'
  }
}

// ensure 'generate' runs before 'classes' ('generate' is NOT optional)
'coderules-classes' {
  dependsOn {
    'coderules-generate'
  }
  mustRunAfter {
    'coderules-generate'
  }
}

'coderules-assemble' {

}

assemble {
  dependsOn {
    [
      ':coderules-assemble',
      ':samples:mpscore:mpscore-assemble',
      ':samples:lambdacalc:lambdacalc-assemble',
      ':samples:fitch:fitch-assemble'
    ]
  }
}

clean {
  dependsOn {
    [
      ':coderules-clean', ':coderules-cleanSources',
      ':samples:mpscore:mpscore-clean', ':samples:mpscore:mpscore-cleanSources',
      ':samples:lambdacalc:lambdacalc-clean', ':samples:lambdacalc:lambdacalc-cleanSources',
      ':samples:fitch:fitch-clean', ':samples:fitch:fitch-cleanSources'
    ]
  }
  finalizedBy {
     deleteMps
  }
}

check {
  dependsOn {
    [
      ':samples:mpscore:mpscore-check',
      ':samples:lambdacalc:lambdacalc-check',
      ':samples:fitch:fitch-check'
    ]
  }
  // tests from this target also include model checker invocation, which requires everything to be built
  finalizedBy {
    ':coderules-check'
  }
}
