diff --git a/subprojects/mps-gradle-plugin/CHANGELOG.md b/subprojects/mps-gradle-plugin/CHANGELOG.md index 63e320b..06110da 100644 --- a/subprojects/mps-gradle-plugin/CHANGELOG.md +++ b/subprojects/mps-gradle-plugin/CHANGELOG.md @@ -7,6 +7,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), ## Unreleased +### Added + +- Ant tasks of build scripts receive an `artifacts.X` property for each top-level directory `X` of the extracted + external MPS libraries, so MPS build scripts find libraries packaged by this plugin without additional path variables. +- `RunAnt.propertyFiles`: property files passed to Ant with `-propertyfile`. + ### Changed - **Breaking:** MPS libraries produced by other projects of the build (project dependencies and included builds) are no diff --git a/subprojects/mps-gradle-plugin/README.md b/subprojects/mps-gradle-plugin/README.md index 3079e46..1bc1c6d 100644 --- a/subprojects/mps-gradle-plugin/README.md +++ b/subprojects/mps-gradle-plugin/README.md @@ -120,6 +120,13 @@ dependency of each library it consumes. External libraries are extracted into `mpsDefaults.mpsLibrariesDirectory` (`build/dependencies` by default). +For each top-level directory `X` of the extracted libraries, the MPS build scripts receive an Ant property `artifacts.X` +pointing at that directory. MPS-generated build scripts read the location of the artifacts of a dependency build project +named `X` from `artifacts.X`, so build scripts find libraries packaged by this plugin without additional path variables: +their zips contain the artifacts directory of each published build as a top-level directory, and its name usually +matches the name of the build project. Path variables set in `mpsDefaults.pathVariables` take precedence over these +properties. + ### Dependencies on other projects A dependency can also point at another project of the same Gradle build (`api(project(":other"))`) or at an included @@ -238,6 +245,8 @@ Based on the above configuration, the plugin creates the following tasks: bundled dependencies, and `setup` of other projects this project depends on (see [Dependencies on other projects](#dependencies-on-other-projects)). - `resolveMpsLibraries` – downloads external MPS libraries and extracts them into `mpsDefaults.mpsLibrariesDirectory`. + Also writes `build/mps-libraries-artifacts.properties` with an `artifacts.` property for each top-level + directory of the extracted libraries. The Ant tasks of all build scripts load this file. - `zip` – packages all artifacts of published main builds into a zip file. - `test` – runs tests in all test build scripts. `test` is a lifecycle task that does nothing on its own but depends on `check` tasks of all test build scripts. @@ -305,6 +314,7 @@ val myTask by tasks.registering(RunAnt::class) { pathProperties.put("mps_home", file("/my/mps/home")) valueProperties.put("version", provider { project.version.toString() }) + propertyFiles.from(file("my.properties")) // passed with -propertyfile, -D properties take precedence targets.set(listOf("myTarget")) // no default } diff --git a/subprojects/mps-gradle-plugin/api/mps-gradle-plugin.api b/subprojects/mps-gradle-plugin/api/mps-gradle-plugin.api index 54eb8d6..502e081 100644 --- a/subprojects/mps-gradle-plugin/api/mps-gradle-plugin.api +++ b/subprojects/mps-gradle-plugin/api/mps-gradle-plugin.api @@ -65,6 +65,7 @@ public abstract class com/specificlanguages/mps/RunAnt : org/gradle/api/DefaultT public abstract fun getOptions ()Lorg/gradle/api/provider/ListProperty; public abstract fun getPathProperties ()Lorg/gradle/api/provider/MapProperty; public abstract fun getPreserveWorkingDirectory ()Lorg/gradle/api/provider/Property; + public abstract fun getPropertyFiles ()Lorg/gradle/api/file/ConfigurableFileCollection; public abstract fun getTargets ()Lorg/gradle/api/provider/ListProperty; protected abstract fun getToolchains ()Lorg/gradle/jvm/toolchain/JavaToolchainService; public abstract fun getValueProperties ()Lorg/gradle/api/provider/MapProperty; diff --git a/subprojects/mps-gradle-plugin/src/main/kotlin/com/specificlanguages/mps/MpsPlugin.kt b/subprojects/mps-gradle-plugin/src/main/kotlin/com/specificlanguages/mps/MpsPlugin.kt index c1f3939..4bcf915 100644 --- a/subprojects/mps-gradle-plugin/src/main/kotlin/com/specificlanguages/mps/MpsPlugin.kt +++ b/subprojects/mps-gradle-plugin/src/main/kotlin/com/specificlanguages/mps/MpsPlugin.kt @@ -4,7 +4,9 @@ import com.specificlanguages.jbrtoolchain.JbrToolchainExtension import com.specificlanguages.jbrtoolchain.JbrToolchainPlugin import com.specificlanguages.mps.internal.ConfigurationNames import com.specificlanguages.mps.internal.configureMbeddrIntegration +import com.specificlanguages.mps.internal.configureLibrariesArtifactsProperties import com.specificlanguages.mps.internal.configureProjectDependencies +import com.specificlanguages.mps.internal.writeArtifactsProperties import com.specificlanguages.mps.internal.createBundledDependenciesContainer import com.specificlanguages.mps.internal.createMpsBuildsContainer import com.specificlanguages.mps.internal.registerArtifactDirectoriesVariant @@ -98,6 +100,8 @@ open class MpsPlugin @Inject constructor( extendsFrom(apiConfiguration.get(), testImplementationConfiguration.get()) } + val mpsLibrariesArtifactsProperties = layout.buildDirectory.file("mps-libraries-artifacts.properties") + val resolveMpsLibraries by tasks.registering(Sync::class) { // Libraries of other projects (in this build or in included builds) are not extracted. To open the // project in MPS, their sources are expected to be added to the MPS project as project libraries. The @@ -111,6 +115,12 @@ open class MpsPlugin @Inject constructor( into(mpsDefaults.mpsLibrariesDirectory) group = "build setup" description = "Downloads and extracts all external MPS libraries." + + val librariesDirectory = mpsDefaults.mpsLibrariesDirectory + outputs.file(mpsLibrariesArtifactsProperties) + doLast { + writeArtifactsProperties(librariesDirectory.get().asFile, mpsLibrariesArtifactsProperties.get().asFile) + } } val setupTask = tasks.register("setup") { @@ -142,6 +152,10 @@ open class MpsPlugin @Inject constructor( val mpsBuilds = createMpsBuildsContainer(objects, tasks, generateBuildScriptsTask) extensions.add(typeOf>(), "mpsBuilds", mpsBuilds) configureProjectDependencies(mpsBuilds, mpsLibraries, providers) + configureLibrariesArtifactsProperties( + mpsBuilds, + files(mpsLibrariesArtifactsProperties).builtBy(resolveMpsLibraries) + ) configureMbeddrIntegration(project, mpsDefaults) diff --git a/subprojects/mps-gradle-plugin/src/main/kotlin/com/specificlanguages/mps/RunAnt.kt b/subprojects/mps-gradle-plugin/src/main/kotlin/com/specificlanguages/mps/RunAnt.kt index acbb02b..afd0ad1 100644 --- a/subprojects/mps-gradle-plugin/src/main/kotlin/com/specificlanguages/mps/RunAnt.kt +++ b/subprojects/mps-gradle-plugin/src/main/kotlin/com/specificlanguages/mps/RunAnt.kt @@ -12,6 +12,7 @@ import org.gradle.api.provider.Property import org.gradle.api.tasks.Classpath import org.gradle.api.tasks.Input import org.gradle.api.tasks.InputFile +import org.gradle.api.tasks.InputFiles import org.gradle.api.tasks.Internal import org.gradle.api.tasks.Nested import org.gradle.api.tasks.PathSensitive @@ -62,6 +63,14 @@ abstract class RunAnt : DefaultTask() { @get:Internal("not considered inputs") abstract val pathProperties: MapProperty + /** + * Property files to load properties from, passed to Ant with `-propertyfile`. Properties given in + * [pathProperties] and [valueProperties] take precedence over properties loaded from these files. + */ + @get:InputFiles + @get:PathSensitive(PathSensitivity.NONE) + abstract val propertyFiles: ConfigurableFileCollection + /** * Value properties (e.g. build numbers, version numbers, etc.). Considered task inputs. */ @@ -145,6 +154,10 @@ abstract class RunAnt : DefaultTask() { private fun buildAntArguments(): List = buildList { addAll(pathProperties.get().map { "-D${it.key}=${it.value}" }) addAll(valueProperties.get().map { "-D${it.key}=${it.value}" }) + propertyFiles.forEach { + add("-propertyfile") + add(it.path) + } addAll(options.get()) diff --git a/subprojects/mps-gradle-plugin/src/main/kotlin/com/specificlanguages/mps/internal/libraries_artifacts.kt b/subprojects/mps-gradle-plugin/src/main/kotlin/com/specificlanguages/mps/internal/libraries_artifacts.kt new file mode 100644 index 0000000..50a6c17 --- /dev/null +++ b/subprojects/mps-gradle-plugin/src/main/kotlin/com/specificlanguages/mps/internal/libraries_artifacts.kt @@ -0,0 +1,39 @@ +package com.specificlanguages.mps.internal + +import com.specificlanguages.mps.MpsBuild +import org.gradle.api.DomainObjectCollection +import org.gradle.api.file.FileCollection +import java.io.File +import java.util.Properties + +/** + * Writes an `artifacts.` property for each top-level directory `` of the extracted MPS libraries in + * [librariesDirectory]. + * + * Zips produced by this plugin contain the artifacts directory of each published main build as a top-level directory. + * Its name usually matches the name of the MPS build project, and MPS-generated Ant scripts read the location of the + * artifacts of a dependency build project named `X` from the `artifacts.X` property. + */ +internal fun writeArtifactsProperties(librariesDirectory: File, propertiesFile: File) { + val properties = Properties() + librariesDirectory.listFiles() + ?.filter { it.isDirectory } + ?.forEach { properties["artifacts.${it.name}"] = it.absolutePath } + + propertiesFile.parentFile.mkdirs() + propertiesFile.outputStream().use { properties.store(it, null) } +} + +/** + * Passes [propertiesFile] (written by [writeArtifactsProperties]) to the Ant tasks of all [mpsBuilds]. + */ +internal fun configureLibrariesArtifactsProperties( + mpsBuilds: DomainObjectCollection, + propertiesFile: FileCollection +) { + mpsBuilds.configureEach { + antTasks(this).forEach { task -> + task.configure { propertyFiles.from(propertiesFile) } + } + } +} diff --git a/subprojects/mps-gradle-plugin/src/main/kotlin/com/specificlanguages/mps/internal/project_dependencies.kt b/subprojects/mps-gradle-plugin/src/main/kotlin/com/specificlanguages/mps/internal/project_dependencies.kt index 3d26489..137169b 100644 --- a/subprojects/mps-gradle-plugin/src/main/kotlin/com/specificlanguages/mps/internal/project_dependencies.kt +++ b/subprojects/mps-gradle-plugin/src/main/kotlin/com/specificlanguages/mps/internal/project_dependencies.kt @@ -79,7 +79,7 @@ internal fun configureProjectDependencies( } } -private fun antTasks(build: MpsBuild): List> = when (build) { +internal fun antTasks(build: MpsBuild): List> = when (build) { is MainBuild -> listOf(build.generateTask, build.assembleTask) is TestBuild -> listOf(build.generateTask, build.assembleAndCheckTask) else -> listOf(build.generateTask) diff --git a/subprojects/mps-gradle-plugin/src/test/kotlin/com/specificlanguages/mps/ProjectDependenciesTest.kt b/subprojects/mps-gradle-plugin/src/test/kotlin/com/specificlanguages/mps/ProjectDependenciesTest.kt index 3cf61d4..098c810 100644 --- a/subprojects/mps-gradle-plugin/src/test/kotlin/com/specificlanguages/mps/ProjectDependenciesTest.kt +++ b/subprojects/mps-gradle-plugin/src/test/kotlin/com/specificlanguages/mps/ProjectDependenciesTest.kt @@ -3,6 +3,7 @@ package com.specificlanguages.mps import org.gradle.testkit.runner.GradleRunner import org.hamcrest.MatcherAssert.assertThat import org.hamcrest.Matchers.* +import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.Assertions.assertNotNull import org.junit.jupiter.api.Assertions.assertNull import org.junit.jupiter.api.Assertions.assertTrue @@ -10,6 +11,7 @@ import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.Test import org.junit.jupiter.api.io.TempDir import java.io.File +import java.util.Properties import java.util.zip.ZipEntry import java.util.zip.ZipOutputStream @@ -100,6 +102,13 @@ class ProjectDependenciesTest { .forEach { (key, value) -> println("${'$'}key=${'$'}{value.canonicalPath}") } } } + + val generateMainPropertyFiles = tasks.named("generateMain").map { it.propertyFiles.files } + tasks.register("printPropertyFiles") { + doLast { + generateMainPropertyFiles.get().forEach { println("propertyFile=${'$'}{it.canonicalPath}") } + } + } """.trimIndent() ) } @@ -136,6 +145,30 @@ class ProjectDependenciesTest { )) } + @Test + fun `setup writes artifacts properties for top-level directories of external libraries`() { + runner(":a:setup").build() + + val properties = Properties() + projectDir.resolve("a/build/mps-libraries-artifacts.properties").inputStream().use { properties.load(it) } + + assertThat(properties.keys, contains("artifacts.ext")) + assertEquals( + projectDir.resolve("a/build/dependencies/ext").canonicalPath, + File(properties.getProperty("artifacts.ext")).canonicalPath + ) + } + + @Test + fun `Ant tasks load artifacts properties of external libraries`() { + val dryRunLines = runner(":a:generateMain", "--dry-run").build().output.lines() + assertThat(dryRunLines, containsInRelativeOrder(":a:resolveMpsLibraries SKIPPED", ":a:generateMain SKIPPED")) + + val lines = runner(":a:printPropertyFiles").build().output.lines() + val expected = projectDir.canonicalFile.resolve("a/build/mps-libraries-artifacts.properties").path + assertThat(lines, hasItem("propertyFile=$expected")) + } + private fun runner(vararg arguments: String): GradleRunner = GradleRunner.create() .withProjectDir(projectDir) .withPluginClasspath()