apply plugin : CeylonBuildOsgiPlugin

tasks.remove(tasks.moduleXml)

ext.createTasksFor = { File baseFile,File jarFile, File depsDestination, Task checksum, Task parentLifecycleTask ->
    String taskNamePostfix = jarFile.name[0..-5]
    String relativePath = CeylonBuildUtil.relativeTo(jarFile.parentFile.absoluteFile,baseFile)
    File depsFullDestination = project.file("${depsDestination}/${relativePath}")

    Task fakeClasses = tasks.getByName('fakeClassesDirForExternalDependencies')

    Task moduleTask = tasks.create( "addModuleXml_${taskNamePostfix}", CeylonBuildModuleXml) {
        group "Build"
        description "Prepare module.xml for ${taskNamePostfix}"
        sourceModule new File(jarFile.parentFile,'module.xml')
        destinationDir depsFullDestination
    }

    Task osgiJar = tasks.create( "addOsgiManifest_${taskNamePostfix}", Jar ) {
        group "Build"
        description "Add OSGI data to manifest for ${taskNamePostfix}"
        archiveName jarFile.name
        destinationDir  depsFullDestination
        dependsOn moduleTask
        dependsOn fakeClasses
        setAsOsgiExternalArchive()

        from zipTree(jarFile), {
            exclude 'META-INF/MANIFEST.MF'
        }

        manifest {
            classesDir fakeClasses.outputDir
        }

        ceylon {
            seedFrom jarFile
            moduleLocation {moduleTask.destinationFile}
            forceNewOsgiManifest relativePath.matches(~/.+[\\\/]logmanager[\\\/].+/)
        }

        from moduleTask, {
            into "META-INF/jbossmodules/${relativePath}"
        }

        doFirst { mkdir manifest.classesDir }
    }

    Task osgiDistJar = tasks.create( "publishOsgiDisJar_${taskNamePostfix}") {
        group 'OSGI Distribution'
        description "Copies a ${taskNamePostfix} JAR suitable for the Ceylon OSGI Distribution"
        dependsOn osgiJar

        ext {
            outputDir = file("${osgiDistDir}/plugins")
            getOutputFileName = { osgiTask ->
                Map<String,String> attrs = [:]
                osgiTask.archivePath.withInputStream { input ->
                    java.util.jar.Attributes mainAttributes = new java.util.jar.JarInputStream(input).manifest.mainAttributes
                    mainAttributes.keySet().each { k ->
                        attrs[k.toString()] = mainAttributes.getValue(k).toString()
                    }
                }
                "${attrs['Bundle-SymbolicName']}_${attrs['Bundle-Version']}.jar"
            }
        }

        outputs.file { file("${outputDir}/${getOutputFileName(osgiJar)}") }

        doFirst {
            project.copy {
                from osgiJar
                into outputDir
                rename ~/.+/,getOutputFileName(osgiJar)
            }
        }

    }

    checksum.archive osgiJar
    checksum.dependsOn osgiJar
    parentLifecycleTask.dependsOn osgiJar, moduleTask, osgiDistJar

}

ext.createCeylonTasksFor = { File baseFile, File depsDestination, Task checksum, Task parentLifecycleTask, FileCollection jars ->
    jars.files.each {
        createTasksFor baseFile, it, depsDestination, checksum, parentLifecycleTask
    }
}

task fakeClassesDirForExternalDependencies  {
    ext {
        outputDir = file("${buildDir}/classes/fake")
    }
    outputs.dir outputDir
    doFirst {
        mkdir outputDir
    }
}
