Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions subprojects/mps-gradle-plugin/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<Name>`,
`assemble<Name>`, `check<Name>`) receive their locations as `artifacts.<directory name>` 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.<directory name>` properties.

### Dependency updates

- jbr-toolchain 1.3.0-SNAPSHOT
Expand Down
32 changes: 28 additions & 4 deletions subprojects/mps-gradle-plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.<directory name>`, 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/<build project name>`.

The Ant tasks also receive `artifacts.<directory name>` properties for all main builds of the same project.

## Describing MPS build scripts

Example:
Expand Down Expand Up @@ -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<Name>` tasks of all test build scripts.
Expand All @@ -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<Name>` – 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<Name>` tasks of published main builds of other projects this project
depends on (see [Dependencies on other projects](#dependencies-on-other-projects)).
- `assemble<Name>` (main build scripts only) – runs the `clean` and `assemble` targets of the generated build script, in
that order.
- `check<Name>` (test build scripts only) – runs the `clean` and `check` targets of the generated build script, in that
Expand Down Expand Up @@ -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.

Expand Down
2 changes: 1 addition & 1 deletion subprojects/mps-gradle-plugin/gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version=2.1.2-SNAPSHOT
version=3.0.0-SNAPSHOT
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@ 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.*
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
Expand Down Expand Up @@ -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"
Expand All @@ -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) {
Expand All @@ -125,6 +141,7 @@ open class MpsPlugin @Inject constructor(

val mpsBuilds = createMpsBuildsContainer(objects, tasks, generateBuildScriptsTask)
extensions.add(typeOf<PolymorphicDomainObjectContainer<MpsBuild>>(), "mpsBuilds", mpsBuilds)
configureProjectDependencies(mpsBuilds, mpsLibraries, providers)

configureMbeddrIntegration(project, mpsDefaults)

Expand All @@ -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())
Expand Down
Original file line number Diff line number Diff line change
@@ -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<ConsumableConfiguration>,
mpsBuilds: DomainObjectCollection<MpsBuild>,
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.<directory name>` 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<MpsBuild>,
mpsLibraries: NamedDomainObjectProvider<ResolvableConfiguration>,
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<TaskProvider<out RunAnt>> = 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<File>): Map<String, File> =
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()) }
}
Loading