diff --git a/subprojects/mps-gradle-plugin/CHANGELOG.md b/subprojects/mps-gradle-plugin/CHANGELOG.md index 9bfab67..63e320b 100644 --- a/subprojects/mps-gradle-plugin/CHANGELOG.md +++ b/subprojects/mps-gradle-plugin/CHANGELOG.md @@ -7,6 +7,20 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), ## Unreleased +### Changed + +- **Breaking:** MPS libraries produced by other projects of the build (project dependencies and included builds) are no + longer extracted into `mpsDefaults.mpsLibrariesDirectory` (`build/dependencies`). Instead, each project exposes the + artifacts directories of its published main builds to other projects, and the Ant tasks (`generate`, + `assemble`, `check`) receive their locations as `artifacts.` properties. Update MPS build + models and path variables that expect these libraries in `build/dependencies`. +- **Breaking:** `setup` no longer builds project dependencies. It only extracts external MPS libraries, including the + external libraries of project dependencies, and runs `setup` of each project that is a direct project dependency in + `api` or `testImplementation`. The sources of project dependencies are expected to be added to the MPS project as + project libraries. +- The Ant tasks also receive the artifacts directories of all main builds of the same project as + `artifacts.` properties. + ### Dependency updates - jbr-toolchain 1.3.0-SNAPSHOT diff --git a/subprojects/mps-gradle-plugin/README.md b/subprojects/mps-gradle-plugin/README.md index 7c6922b..3079e46 100644 --- a/subprojects/mps-gradle-plugin/README.md +++ b/subprojects/mps-gradle-plugin/README.md @@ -118,6 +118,26 @@ There is no `implementation` configuration because MPS does not support hiding i consumers at the moment. For example, the consumer's build script has to specify the location of every transitive dependency of each library it consumes. +External libraries are extracted into `mpsDefaults.mpsLibrariesDirectory` (`build/dependencies` by default). + +### Dependencies on other projects + +A dependency can also point at another project of the same Gradle build (`api(project(":other"))`) or at an included +build. The plugin treats such libraries as internal: it assumes that the sources of the other project are added to the +MPS project as a project library, so MPS can see their modules without building them. + +- `setup` does not build internal libraries. It extracts the external libraries of the whole dependency graph, including + the external libraries of other projects, and runs `setup` of each project listed directly in `api` or + `testImplementation`. Included builds are not covered; add a dependency on their `setup` task manually if needed. +- The MPS build scripts use the artifacts of the other projects directly from their artifacts directories. Each project + exposes the `buildArtifactsDirectory` of its published main builds to other projects. The Ant tasks of a project + depend on assembling these builds and receive the location of each directory as an Ant property named + `artifacts.`, which is the property that MPS-generated build scripts read the location of a dependency + build project from. For this to work, the directory name must match the name of the MPS build project, which is the + case for the usual `build/artifacts/`. + +The Ant tasks also receive `artifacts.` properties for all main builds of the same project. + ## Describing MPS build scripts Example: @@ -214,8 +234,10 @@ Version numbers are stripped from the downloaded files to reduce the number of c Based on the above configuration, the plugin creates the following tasks: - `generateBuildScripts` – runs the MPS generator to generate Ant files for all specified build scripts. -- `setup` – depends on tasks necessary to be able to open the project in MPS, such as downloading external MPS - dependencies and bundled dependencies. +- `setup` – depends on tasks necessary to be able to open the project in MPS: downloading external MPS dependencies, + 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`. - `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. @@ -228,7 +250,8 @@ For each entry in `bundledDependencies` the following task is created: For each build script in `mpsBuilds` the following tasks are created: - `generate` – runs the `generate` Ant target of a single build script. Depends on the `assemble` targets of its - build script dependencies. + build script dependencies and on the `assemble` tasks of published main builds of other projects this project + depends on (see [Dependencies on other projects](#dependencies-on-other-projects)). - `assemble` (main build scripts only) – runs the `clean` and `assemble` targets of the generated build script, in that order. - `check` (test build scripts only) – runs the `clean` and `check` targets of the generated build script, in that @@ -341,7 +364,8 @@ The following properties can be customized: means. - `javaLauncher` – uses the JVM from the `jbr` configuration by default. Override to use a different Java runtime. - `antClasspath` – uses the Ant distribution bundled with MPS by default. Override to use a different Ant version. -- `mpsLibrariesDirectory` – the directory where downloaded MPS libraries from the `api` configuration will be extracted. +- `mpsLibrariesDirectory` – the directory where downloaded external MPS libraries from the `api` and + `testImplementation` configurations will be extracted. - `pathVariables` – can be used to specify custom path variables. These are added to the MPS environment for the `generateBuildScripts` task and passed as Ant properties to all `RunAnt` tasks. diff --git a/subprojects/mps-gradle-plugin/gradle.properties b/subprojects/mps-gradle-plugin/gradle.properties index 031c771..cd92d6b 100644 --- a/subprojects/mps-gradle-plugin/gradle.properties +++ b/subprojects/mps-gradle-plugin/gradle.properties @@ -1 +1 @@ -version=2.1.2-SNAPSHOT +version=3.0.0-SNAPSHOT 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 2559bf1..c1f3939 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,8 +4,10 @@ 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.configureProjectDependencies import com.specificlanguages.mps.internal.createBundledDependenciesContainer import com.specificlanguages.mps.internal.createMpsBuildsContainer +import com.specificlanguages.mps.internal.registerArtifactDirectoriesVariant import com.specificlanguages.mpsplatformcache.MpsPlatformCache import com.specificlanguages.mpsplatformcache.MpsPlatformCachePlugin import org.gradle.api.* @@ -13,6 +15,8 @@ import org.gradle.api.artifacts.Configuration import org.gradle.api.artifacts.ConfigurationContainer import org.gradle.api.artifacts.ConsumableConfiguration import org.gradle.api.artifacts.Dependency +import org.gradle.api.artifacts.ProjectDependency +import org.gradle.api.artifacts.component.ProjectComponentIdentifier import org.gradle.api.attributes.Usage import org.gradle.api.component.SoftwareComponentContainer import org.gradle.api.component.SoftwareComponentFactory @@ -95,8 +99,14 @@ open class MpsPlugin @Inject constructor( } val resolveMpsLibraries by tasks.registering(Sync::class) { - dependsOn(mpsLibraries) - from(mpsLibraries.map { cfg -> cfg.map(project::zipTree) }) + // 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 + // MPS build scripts use their build artifacts directories instead (see configureProjectDependencies). + val externalLibraries = mpsLibraries.map { cfg -> + cfg.incoming.artifactView { componentFilter { it !is ProjectComponentIdentifier } }.files + } + dependsOn(externalLibraries) + from(externalLibraries.map { files -> files.map(project::zipTree) }) into(mpsDefaults.mpsLibrariesDirectory) group = "build setup" @@ -106,9 +116,15 @@ open class MpsPlugin @Inject constructor( val setupTask = tasks.register("setup") { dependsOn(resolveMpsLibraries) dependsOn(Callable { bundledDependencies.map(BundledDependency::resolveTask) }) + dependsOn(Callable { + listOf(apiConfiguration.get(), testImplementationConfiguration.get()) + .flatMap { it.dependencies.withType(ProjectDependency::class.java) } + .map { dependency -> if (dependency.path == ":") ":setup" else "${dependency.path}:setup" } + .distinct() + }) group = "build setup" - description = "Sets up the project so that it can be opened in MPS." + description = "Sets up the project and the projects it depends on so that it can be opened in MPS." } val executeGeneratorsConfiguration = configurations.register(ConfigurationNames.EXECUTE_GENERATORS) { @@ -125,6 +141,7 @@ open class MpsPlugin @Inject constructor( val mpsBuilds = createMpsBuildsContainer(objects, tasks, generateBuildScriptsTask) extensions.add(typeOf>(), "mpsBuilds", mpsBuilds) + configureProjectDependencies(mpsBuilds, mpsLibraries, providers) configureMbeddrIntegration(project, mpsDefaults) @@ -147,6 +164,7 @@ open class MpsPlugin @Inject constructor( val apiElementsConfiguration = registerApiElementsConfiguration(apiConfiguration, zipTask, configurations, objects) + registerArtifactDirectoriesVariant(apiElementsConfiguration, mpsBuilds, providers) configurations[Dependency.DEFAULT_CONFIGURATION].apply { extendsFrom(apiElementsConfiguration.get()) 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 new file mode 100644 index 0000000..3d26489 --- /dev/null +++ b/subprojects/mps-gradle-plugin/src/main/kotlin/com/specificlanguages/mps/internal/project_dependencies.kt @@ -0,0 +1,108 @@ +package com.specificlanguages.mps.internal + +import com.specificlanguages.mps.MainBuild +import com.specificlanguages.mps.MpsBuild +import com.specificlanguages.mps.RunAnt +import com.specificlanguages.mps.TestBuild +import org.gradle.api.DomainObjectCollection +import org.gradle.api.NamedDomainObjectProvider +import org.gradle.api.artifacts.ConsumableConfiguration +import org.gradle.api.artifacts.PublishArtifact +import org.gradle.api.artifacts.ResolvableConfiguration +import org.gradle.api.artifacts.component.ProjectComponentIdentifier +import org.gradle.api.artifacts.type.ArtifactTypeDefinition +import org.gradle.api.provider.ProviderFactory +import org.gradle.api.tasks.TaskDependency +import org.gradle.api.tasks.TaskProvider +import java.io.File +import java.util.Date + +private const val ARTIFACT_DIRECTORIES_VARIANT = "artifactDirectories" + +/** + * Adds a secondary variant to [apiElements] that exposes the artifacts directories of published main builds instead of + * the zip. Secondary variants are only visible to other projects of the same build (or included builds) and are not + * published. + */ +internal fun registerArtifactDirectoriesVariant( + apiElements: NamedDomainObjectProvider, + mpsBuilds: DomainObjectCollection, + providers: ProviderFactory +) { + apiElements.configure { + outgoing.variants.create(ARTIFACT_DIRECTORIES_VARIANT) { + attributes.attribute(ArtifactTypeDefinition.ARTIFACT_TYPE_ATTRIBUTE, ArtifactTypeDefinition.DIRECTORY_TYPE) + artifacts.addAllLater(providers.provider { + mpsBuilds.withType(MainBuild::class.java) + .filter { it.published.get() } + .map(::BuildArtifactsDirectory) + }) + } + } +} + +/** + * Passes the location of the artifacts of each main build that the Ant scripts of this project may depend on as + * an `artifacts.` Ant property. MPS-generated Ant scripts read the location of the artifacts of + * a dependency build project named `X` from the `artifacts.X` property. + * + * Covers the main builds of this project and the published main builds of other projects in [mpsLibraries]. The Ant + * tasks also depend on assembling the builds of the other projects. + */ +internal fun configureProjectDependencies( + mpsBuilds: DomainObjectCollection, + mpsLibraries: NamedDomainObjectProvider, + providers: ProviderFactory +) { + val otherProjectsArtifacts = mpsLibraries.map { cfg -> + cfg.incoming.artifactView { + componentFilter { it is ProjectComponentIdentifier } + attributes.attribute( + ArtifactTypeDefinition.ARTIFACT_TYPE_ATTRIBUTE, + ArtifactTypeDefinition.DIRECTORY_TYPE + ) + }.files + } + + val thisProjectArtifacts = providers.provider { + mpsBuilds.withType(MainBuild::class.java).mapNotNull { it.buildArtifactsDirectory.orNull?.asFile } + } + + mpsBuilds.configureEach { + antTasks(this).forEach { task -> + task.configure { + dependsOn(otherProjectsArtifacts) + pathProperties.putAll(otherProjectsArtifacts.map { files -> artifactsProperties(files.files) }) + pathProperties.putAll(thisProjectArtifacts.map(::artifactsProperties)) + } + } + } +} + +private 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) +} + +private fun artifactsProperties(directories: Iterable): Map = + directories.associateBy { "artifacts.${it.name}" } + +/** + * The artifacts directory of a main build, built by its assemble task. + * + * `ConfigurationVariant.artifact(...)` would create and configure the artifact for us, but it adds the artifact + * immediately, so it would have to be called once [MainBuild.published] is known. That is not the case in + * `mpsBuilds.all { ... }`: `create(name) { ... }` adds a build to the container before running its configuration + * block. Implementing [PublishArtifact] lets [registerArtifactDirectoriesVariant] add the artifacts lazily through + * `addAllLater`. Gradle's own plugins use the internal `LazyPublishArtifact` class for the same purpose. + */ +private class BuildArtifactsDirectory(private val build: MainBuild) : PublishArtifact { + override fun getName(): String = file.name + override fun getExtension(): String = "" + override fun getType(): String = ArtifactTypeDefinition.DIRECTORY_TYPE + override fun getClassifier(): String? = null + override fun getFile(): File = build.buildArtifactsDirectory.get().asFile + override fun getDate(): Date? = null + override fun getBuildDependencies(): TaskDependency = TaskDependency { setOf(build.assembleTask.get()) } +} 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 new file mode 100644 index 0000000..3cf61d4 --- /dev/null +++ b/subprojects/mps-gradle-plugin/src/test/kotlin/com/specificlanguages/mps/ProjectDependenciesTest.kt @@ -0,0 +1,152 @@ +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.assertNotNull +import org.junit.jupiter.api.Assertions.assertNull +import org.junit.jupiter.api.Assertions.assertTrue +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.zip.ZipEntry +import java.util.zip.ZipOutputStream + +/** + * Project `:a` depends on project `:b`, which depends on an external library `com.example:ext`. Each project has + * a main build: `main` in `:a` and `bLibs` in `:b`. + */ +class ProjectDependenciesTest { + + @TempDir + lateinit var projectDir: File + + @BeforeEach + fun setUp() { + val repoDir = projectDir.resolve("repo") + writeZip(repoDir.resolve("com/example/ext/1.0/ext-1.0.zip"), "ext/marker.txt") + + projectDir.resolve("settings.gradle.kts").writeText( + """ + include("a", "b") + """.trimIndent() + ) + + val common = """ + repositories { + maven { + url = uri("${repoDir.toURI()}") + metadataSources { artifact() } + } + } + + // Avoids resolving MPS to compute the dependencies of RunAnt tasks. + mpsDefaults.mpsHome = layout.projectDirectory.dir("fake-mps") + """.trimIndent() + + projectDir.resolve("b").mkdirs() + projectDir.resolve("b/build.gradle.kts").writeText( + """ + import com.specificlanguages.mps.MainBuild + + plugins { + id("com.specificlanguages.mps") + } + + $common + + dependencies { + api("com.example:ext:1.0@zip") + } + + mpsBuilds { + create("bLibs") { + buildSolutionDescriptor = file("b.msd") + buildArtifactsDirectory = layout.buildDirectory.dir("artifacts/bLibs") + } + } + """.trimIndent() + ) + + projectDir.resolve("a").mkdirs() + projectDir.resolve("a/build.gradle.kts").writeText( + """ + import com.specificlanguages.mps.MainBuild + import com.specificlanguages.mps.RunAnt + + plugins { + id("com.specificlanguages.mps") + } + + $common + + dependencies { + api(project(":b")) + } + + mpsBuilds { + create("main") { + buildSolutionDescriptor = file("a.msd") + buildArtifactsDirectory = layout.buildDirectory.dir("artifacts/main") + } + } + + val generateMainProperties = tasks.named("generateMain").flatMap { it.pathProperties } + tasks.register("printArtifactsProperties") { + doLast { + generateMainProperties.get() + .filterKeys { it.startsWith("artifacts.") } + .forEach { (key, value) -> println("${'$'}key=${'$'}{value.canonicalPath}") } + } + } + """.trimIndent() + ) + } + + @Test + fun `setup resolves external libraries of project dependencies without building them`() { + val result = runner(":a:setup").build() + + assertNotNull(result.task(":b:setup"), ":b:setup should run") + assertNull(result.task(":b:assembleBLibs"), ":b:assembleBLibs should not run") + assertNull(result.task(":b:zip"), ":b:zip should not run") + + assertTrue(projectDir.resolve("a/build/dependencies/ext/marker.txt").isFile, + "external library of :b should be extracted into :a") + assertThat(projectDir.resolve("a/build/dependencies").list()!!.toList(), contains("ext")) + } + + @Test + fun `Ant tasks depend on assembling builds of project dependencies`() { + val lines = runner(":a:generateMain", "--dry-run").build().output.lines() + + assertThat(lines, containsInRelativeOrder(":b:assembleBLibs SKIPPED", ":a:generateMain SKIPPED")) + assertThat(lines, not(hasItem(":b:zip SKIPPED"))) + } + + @Test + fun `Ant tasks receive artifacts locations of builds`() { + val lines = runner(":a:printArtifactsProperties").build().output.lines() + + val root = projectDir.canonicalFile + assertThat(lines, hasItems( + "artifacts.bLibs=${root.resolve("b/build/artifacts/bLibs").path}", + "artifacts.main=${root.resolve("a/build/artifacts/main").path}", + )) + } + + private fun runner(vararg arguments: String): GradleRunner = GradleRunner.create() + .withProjectDir(projectDir) + .withPluginClasspath() + .withArguments(*arguments) + + private fun writeZip(file: File, entryName: String) { + file.parentFile.mkdirs() + ZipOutputStream(file.outputStream()).use { zip -> + zip.putNextEntry(ZipEntry(entryName)) + zip.write("marker".toByteArray()) + zip.closeEntry() + } + } +}