Bug 516412 Replace virgo-build-tools with Photon SDK

Use bnd-platform and eclipse-photon-sdk instead of
custom virgo-build-tools.

The *old* build tools isn't capable of building
a post Luna Equinox runtime.

Change-Id: Ie3c868d486dc5dd004795ba7340b9dc389842249
diff --git a/buildSrc/src/main/groovy/virgobuild/Config.groovy b/buildSrc/src/main/groovy/virgobuild/Config.groovy
index dddd4e7..e345254 100644
--- a/buildSrc/src/main/groovy/virgobuild/Config.groovy
+++ b/buildSrc/src/main/groovy/virgobuild/Config.groovy
@@ -27,15 +27,29 @@
         new File(virgoBuildToolsBaseDir, virgoBuildToolsBaseName)
     }
 
-    File getVirgoBuildToolsArchive() {
-        new File(virgoBuildToolsDir, 'virgo-tools.zip')
+    static File getBndPlatformDir() {
+        new File(System.getProperty('user.home'), '.gradle/bnd-platform/eclipse')
     }
 
-    File getEquinoxLauncherJar() {
-        new File(virgoBuildToolsDir.path, '/plugins').listFiles().find { it.name.startsWith('org.eclipse.equinox.launcher_') }
+    static File getEclipsePhotonDir() {
+        new File(System.getProperty('user.home'), '.gradle/eclipse-photon-sdk/eclipse')
     }
 
-    File getJarProcessorJar() {
-        new File(virgoBuildToolsDir.path, '/plugins').listFiles().find { it.name.startsWith('org.eclipse.equinox.p2.jarprocessor_') }
+    static File getEclipsePhotonSdkArchive() {
+        new File(System.getProperty('user.home'), '.gradle/eclipse-java-photon-R-linux-gtk-x86_64.tar.gz')
+    }
+
+    // used by publishContentToLocalP2Repository - old virgo-tools simply aren't able to publish OSGi framework bundle to p2 repository :(
+    static File getBndPlatformLauncherJar() {
+        new File(bndPlatformDir.path, '/plugins').listFiles().find { it.name.startsWith('org.eclipse.equinox.launcher_') }
+    }
+
+    // used by installProduct - old virgo-tools' p2.director simply isn't able to install products properly anymore :(
+    static File getEclipsePhotonLauncherJar() {
+        new File(eclipsePhotonDir.path, '/plugins').listFiles().find { it.name.startsWith('org.eclipse.equinox.launcher_') }
+    }
+
+    static File getJarProcessorJar() {
+        new File(eclipsePhotonDir.path, '/plugins').listFiles().find { it.name.startsWith('org.eclipse.equinox.p2.jarprocessor_') }
     }
 }
diff --git a/buildSrc/src/main/groovy/virgobuild/Constants.groovy b/buildSrc/src/main/groovy/virgobuild/Constants.groovy
index f798aa2..5e77a97 100644
--- a/buildSrc/src/main/groovy/virgobuild/Constants.groovy
+++ b/buildSrc/src/main/groovy/virgobuild/Constants.groovy
@@ -7,10 +7,10 @@
     }
 
     static String getVirgoBuildToolsVersion() {
-        return '1.2.1.RELEASE'
+        return '3.8.0.RELEASE'
     }
 
-    static URL getVirgoBuildToolsDownloadUrl() {
-        return new URL("https://mirror.dkm.cz/eclipse/virgo/release/VB/1.2.1/virgo-build-tools-1.2.1.RELEASE.zip")
+    static URL getEclipsePhotonSdkDownloadUrl() {
+        return new URL("https://ftp.halifax.rwth-aachen.de/eclipse/technology/epp/downloads/release/photon/R/eclipse-java-photon-R-linux-gtk-x86_64.tar.gz")
     }
 }
diff --git a/buildSrc/src/main/groovy/virgobuild/UpdateSitePlugin.groovy b/buildSrc/src/main/groovy/virgobuild/UpdateSitePlugin.groovy
index 2c1aba4..a06d8db 100644
--- a/buildSrc/src/main/groovy/virgobuild/UpdateSitePlugin.groovy
+++ b/buildSrc/src/main/groovy/virgobuild/UpdateSitePlugin.groovy
@@ -1,7 +1,5 @@
 package virgobuild
 
-import static virgobuild.VirgoToolsPlugin.DOWNLOAD_VIRGO_BUILD_TOOLS_TASK_NAME
-
 import org.gradle.api.Plugin
 import org.gradle.api.Project
 import org.gradle.api.artifacts.ProjectDependency
@@ -10,6 +8,8 @@
 
 import eclipsebuild.FeaturePlugin
 
+import static virgobuild.VirgoToolsPlugin.DOWNLOAD_ECLIPSE_PHOTON_SDK_TASK_NAME
+
 // Derived from buildship Plugins
 class UpdateSitePlugin implements Plugin<Project> {
 
@@ -171,7 +171,7 @@
     static void addTaskNormalizeBundles(Project project) {
         project.task(NORMALIZE_BUNDLES_TASK_NAME, dependsOn: [
             COPY_BUNDLES_TASK_NAME,
-            ":${DOWNLOAD_VIRGO_BUILD_TOOLS_TASK_NAME}"
+            ":${DOWNLOAD_ECLIPSE_PHOTON_SDK_TASK_NAME}"
         ]) {
             group = Constants.gradleTaskGroupName
             description = 'Repacks the bundles that make up the update site using the pack200 tool.'
@@ -226,7 +226,7 @@
         project.task(COMPRESS_BUNDLES_TASK_NAME, dependsOn: [
             NORMALIZE_BUNDLES_TASK_NAME,
             SIGN_BUNDLES_TASK_NAME,
-            ":${DOWNLOAD_VIRGO_BUILD_TOOLS_TASK_NAME}"
+            ":${DOWNLOAD_ECLIPSE_PHOTON_SDK_TASK_NAME}"
         ]) {
             group = Constants.gradleTaskGroupName
             description = 'Compresses the bundles that make up the update using the pack200 tool.'
@@ -266,7 +266,8 @@
     static void addTaskCreateP2Repository(Project project) {
         def createP2RepositoryTask = project.task(CREATE_P2_REPOSITORY_TASK_NAME, dependsOn: [
             COMPRESS_BUNDLES_TASK_NAME,
-            ":${DOWNLOAD_VIRGO_BUILD_TOOLS_TASK_NAME}"
+            ':3rd-party:checkEclipse', // download bnd-platform Eclipse, if necessary
+            ":${DOWNLOAD_ECLIPSE_PHOTON_SDK_TASK_NAME}"
         ]) {
             group = Constants.gradleTaskGroupName
             description = 'Generates the P2 repository.'
@@ -291,12 +292,6 @@
 
         // create the P2 update site
         publishContentToLocalP2Repository(project, repositoryDir)
-
-        // add custom properties to the artifacts.xml file
-        def mutateArtifactsXml = project.updateSite.mutateArtifactsXml
-        if (mutateArtifactsXml) {
-            updateArtifactsXmlFromArchive(project, repositoryDir, mutateArtifactsXml)
-        }
     }
 
     static void publishContentToLocalP2Repository(Project project, File repositoryDir) {
@@ -306,7 +301,7 @@
         project.logger.info("Publish plugins and features from '${rootDir.absolutePath}' to the update site '${repositoryDir.absolutePath}'")
         project.javaexec {
             main = 'org.eclipse.equinox.launcher.Main'
-            classpath Config.on(project).equinoxLauncherJar
+            classpath Config.on(project).bndPlatformLauncherJar
             args = [
                 '-application',
                 'org.eclipse.equinox.p2.publisher.FeaturesAndBundlesPublisher',
@@ -328,7 +323,7 @@
         project.logger.info("Publish categories defined in '${project.updateSite.siteDescriptor.absolutePath}' to the update site '${repositoryDir.absolutePath}'")
         project.javaexec {
             main = 'org.eclipse.equinox.launcher.Main'
-            classpath Config.on(project).equinoxLauncherJar
+            classpath Config.on(project).bndPlatformLauncherJar
             args = [
                 '-application',
                 'org.eclipse.equinox.p2.publisher.CategoryPublisher',
@@ -347,23 +342,6 @@
         }
     }
 
-    static void updateArtifactsXmlFromArchive(Project project, File repositoryLocation, Closure mutateArtifactsXml) {
-        //        // get the artifacts.xml file from the artifacts.jar
-        //        def artifactsJarFile = new File(repositoryLocation, "artifacts.jar")
-        //        def artifactsXmlFile = project.zipTree(artifactsJarFile).matching { 'artifacts.xml' }.singleFile
-        //
-        //        // parse the xml
-        //        def xml = new XmlParser().parse(artifactsXmlFile)
-        //
-        //        // apply artifacts.xml customization (append mirrors url, link to stat servers, etc.)
-        //        mutateArtifactsXml(xml)
-        //
-        //        // write the updated artifacts.xml back to its source
-        //        // the artifacts.xml is a temporary file hence it has to be copied back to the archive
-        //        new XmlNodePrinter(new PrintWriter(new FileWriter(artifactsXmlFile)), "  ", "'").print(xml)
-        //        project.ant.zip(update: true, filesonly: true, destfile: artifactsJarFile) { fileset(file: artifactsXmlFile) }
-    }
-
     static void validateRequiredFilesExist(Project project) {
         project.gradle.taskGraph.whenReady {
             // make sure the required descriptors exist
diff --git a/buildSrc/src/main/groovy/virgobuild/VirgoToolsPlugin.groovy b/buildSrc/src/main/groovy/virgobuild/VirgoToolsPlugin.groovy
index 9785300..8755e64 100644
--- a/buildSrc/src/main/groovy/virgobuild/VirgoToolsPlugin.groovy
+++ b/buildSrc/src/main/groovy/virgobuild/VirgoToolsPlugin.groovy
@@ -9,29 +9,12 @@
 class VirgoToolsPlugin implements Plugin<Project> {
 
     // virgo tasks names
-    static final String DOWNLOAD_VIRGO_BUILD_TOOLS_TASK_NAME = "downloadVirgoBuildTools"
-
-    static final String TASK_NAME_GENERATE_P2_INSTRUCTIONS = "generateP2Instructions"
+    static final String DOWNLOAD_ECLIPSE_PHOTON_SDK_TASK_NAME = "downloadEclipsePhotonSdk"
 
     @Override
-    public void apply(Project project) {
+    void apply(Project project) {
         Config config = Config.on(project)
-        addTaskDownloadVirgoBuildTools(project, config)
-    }
-
-    static void generateP2Instructions(Project project, File assemblyFeatureDir) {
-        project.logger.info("Generating p2 instructions for '${assemblyFeatureDir}'.")
-        project.javaexec {
-            main = 'org.eclipse.equinox.launcher.Main'
-            classpath Config.on(project).equinoxLauncherJar
-            args = [
-                '-application',
-                'org.eclipse.virgo.build.p2tools.instructions.P2InstructionGeneratorApplication',
-                '-source',
-                assemblyFeatureDir
-            ]
-        }
-        project.logger.info("Generated p2 instructions for '${assemblyFeatureDir}'.")
+        addTaskDownloadEclipsePhotonSdk(project, config)
     }
 
     static void publishProduct(Project project, File productFileLocation) {
@@ -43,7 +26,7 @@
         project.logger.info("Publishing Virgo ${productFileLocation} to '${repositoryDir}'.")
         project.javaexec {
             main = 'org.eclipse.equinox.launcher.Main'
-            classpath Config.on(project).equinoxLauncherJar
+            classpath Config.on(project).eclipsePhotonLauncherJar
             args = [
                 '-application',
                 'org.eclipse.equinox.p2.publisher.ProductPublisher',
@@ -56,8 +39,6 @@
                 '-publishArtifacts',
                 '-productFile',
                 productFileLocation,
-//                '-jreLocation',
-//                "${javaProfileLocation}",
                 '-configs',
                 'ANY.ANY.ANY',
                 '-flavor',
@@ -69,15 +50,10 @@
 
     static void installProduct(Project project, String productIu, File destinationDir) {
         File repositoryDir = project.file("${project.rootProject.projectDir}/org.eclipse.virgo.site/build/repository/")
-        installProduct(project, productIu, repositoryDir, destinationDir)
-    }
-
-    @Deprecated
-    static void installProduct(Project project, String productIu, File repositoryDir, File destinationDir) {
         project.logger.info("Installing Virgo '${productIu}' assembled from '${repositoryDir}' into '${destinationDir}'.")
         project.javaexec {
             main = 'org.eclipse.equinox.launcher.Main'
-            classpath Config.on(project).equinoxLauncherJar
+            classpath Config.on(project).eclipsePhotonLauncherJar
             args = [
                 '-application',
                 'org.eclipse.equinox.p2.director',
@@ -97,72 +73,43 @@
         project.logger.info("Installed Virgo '${productIu}' assembled from '${repositoryDir}' into '${destinationDir}'.")
     }
 
-    static void mirrorP2UpdateSite(Project project, String source) {
-        File repositoryDir = project.file("${project.rootProject.projectDir}/org.eclipse.virgo.site/build/repository/")
-        mirrorP2UpdateSite(project, source, repositoryDir)
-    }
 
-    static void mirrorP2UpdateSite(Project project, String source, File destinationDir) {
-        project.logger.info("Mirrorring update site '${source}' into '${destinationDir}'.")
-        project.javaexec {
-            main = 'org.eclipse.equinox.launcher.Main'
-            classpath Config.on(project).equinoxLauncherJar
-            args = [
-                '-application',
-                'org.eclipse.equinox.p2.metadata.repository.mirrorApplication',
-                '-writeMode',
-                '-source',
-                "${source}",
-                '-destination',
-                "${destinationDir}",
-            ]
-        }
-        project.javaexec {
-            main = 'org.eclipse.equinox.launcher.Main'
-            classpath Config.on(project).equinoxLauncherJar
-            args = [
-                '-application',
-                'org.eclipse.equinox.p2.artifact.repository.mirrorApplication',
-                '-writeMode',
-                '-source',
-                "${source}",
-                '-destination',
-                "${destinationDir}",
-            ]
-        }
-        project.logger.info("Mirrorred update site '${source}' into '${destinationDir}'.")
-    }
-
-    static void addTaskDownloadVirgoBuildTools(Project project, Config config) {
-        project.task(DOWNLOAD_VIRGO_BUILD_TOOLS_TASK_NAME) {
+    static void addTaskDownloadEclipsePhotonSdk(Project project, Config config) {
+        project.task(DOWNLOAD_ECLIPSE_PHOTON_SDK_TASK_NAME) {
             group = Constants.gradleTaskGroupName
-            description = "Downloads the Virgo Build Tools to perform P2 operations with."
-            outputs.file config.virgoBuildToolsArchive
-            doLast { downloadVirgoBuildTools(project, config) }
+            description = "Downloads the Eclipse Photon SDK to perform P2 operations with."
+            outputs.file config.eclipsePhotonSdkArchive
+            doLast { downloadEclipsePhotonSdk(project, config) }
         }
     }
 
-    static void downloadVirgoBuildTools(Project project, Config config) {
+    static void downloadEclipsePhotonSdk(Project project, Config config) {
         // if multiple builds start on the same machine (which is the case with a CI server)
         // we want to prevent them downloading the same file to the same destination
         def directoryLock = new FileSemaphore(config.virgoBuildToolsDir)
         directoryLock.lock()
         try {
-            downloadVirgoBuildToolsUnprotected(project, config)
+            downloadEclipsePhotonSdkUnprotected(project, config)
         } finally {
             directoryLock.unlock()
         }
     }
 
-    static void downloadVirgoBuildToolsUnprotected(Project project, Config config) {
+    static void downloadEclipsePhotonSdkUnprotected(Project project, Config config) {
         // download the archive
-        File virgoBuildToolsArchive = config.virgoBuildToolsArchive
-        project.logger.info("Download Virgo Build Tools from '${Constants.virgoBuildToolsDownloadUrl}' to '${virgoBuildToolsArchive.absolutePath}'")
-        project.ant.get(src: Constants.virgoBuildToolsDownloadUrl, dest: virgoBuildToolsArchive)
+        File eclipsePhotonSdkArchive = config.eclipsePhotonSdkArchive
+        project.logger.info("Download Eclipse Photon SDK from '${Constants.eclipsePhotonSdkDownloadUrl}' to '${eclipsePhotonSdkArchive.absolutePath}'")
+        project.ant.get(src: Constants.eclipsePhotonSdkDownloadUrl, dest: eclipsePhotonSdkArchive)
 
-        // extract it to the same location where it was extracted
-        project.logger.info("Extract '$virgoBuildToolsArchive' to '$virgoBuildToolsArchive.parentFile.absolutePath'")
-        project.ant.unzip(src: virgoBuildToolsArchive, dest: virgoBuildToolsArchive.parentFile, overwrite: true)
+        // extract it to the same location where it was downloaded
+        project.logger.info("Extract '$eclipsePhotonSdkArchive' to '$eclipsePhotonSdkArchive.parentFile.absolutePath'")
+
+        File tarFile = new File(eclipsePhotonSdkArchive.parentFile, 'eclipse-photon-sdk.tar')
+        project.ant.gunzip(src: eclipsePhotonSdkArchive, dest: tarFile)
+        project.copy {
+            from project.tarTree(tarFile)
+            into new File(eclipsePhotonSdkArchive.parentFile, 'eclipse-photon-sdk')
+        }
     }
 
 }
diff --git a/packaging/nano/build.gradle b/packaging/nano/build.gradle
index f6e9e4c..20c3c23 100644
--- a/packaging/nano/build.gradle
+++ b/packaging/nano/build.gradle
@@ -1,11 +1,8 @@
 import org.apache.tools.ant.filters.ReplaceTokens
 
-import static virgobuild.VirgoToolsPlugin.DOWNLOAD_VIRGO_BUILD_TOOLS_TASK_NAME
 import static virgobuild.VirgoToolsPlugin.installProduct
 import static virgobuild.VirgoToolsPlugin.publishProduct
 
-import virgobuild.Config
-
 apply plugin: 'distribution'
 
 distributions {
@@ -116,17 +113,16 @@
             def product = 'undefined'
             def shortProduct = 'undefined'
             switch (taskName.toString() - 'installProduct') {
-                case 'Nano': product = 'nano'; shortProduct = "VN"; break;
-                case 'NanoFull': product = 'nano-full'; shortProduct = "VNF"; break;
+                case 'Nano': product = 'nano'; shortProduct = "VN"; break
+                case 'NanoFull': product = 'nano-full'; shortProduct = "VNF"; break
             }
 
             description = "Installs a Virgo product to a desired destination. Default profile(roaming) is VIRGOProfile, the environment cofigurations are ANY."
 
-            File repositoryDir = project.file("${project.rootProject.projectDir}/org.eclipse.virgo.site/build/repository/")
             def destinationDir = file("${buildDir}/${shortProduct}/${product}")
             def productIu = "${product}.product"
 
-            doLast() { installProduct(project, productIu, repositoryDir, destinationDir) }
+            doLast() { installProduct(project, productIu, destinationDir) }
         }
     }
 }