|
| 1 | +/* |
| 2 | + * Copyright 2022 Typelevel |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.typelevel.sbt |
| 18 | + |
| 19 | +import sbt.Keys._ |
| 20 | +import sbt._ |
| 21 | +import sbtcrossproject.CrossPlugin |
| 22 | +import sbtcrossproject.CrossPlugin.autoImport._ |
| 23 | +import sbtcrossproject.Platform |
| 24 | + |
| 25 | +object BspControlPlugin extends AutoPlugin { |
| 26 | + override def trigger = allRequirements |
| 27 | + override def requires: Plugins = CrossPlugin |
| 28 | + |
| 29 | + object autoImport { |
| 30 | + lazy val tlBspCrossProjectPlatforms: SettingKey[Set[Platform]] = |
| 31 | + settingKey[Set[Platform]]( |
| 32 | + "set of platforms for which BSP should be enabled (default: not initialized)") |
| 33 | + } |
| 34 | + |
| 35 | + import autoImport._ |
| 36 | + |
| 37 | + override def projectSettings: Seq[Setting[?]] = Seq( |
| 38 | + bspEnabled := { |
| 39 | + val oldValue = bspEnabled.value |
| 40 | + |
| 41 | + // We have to check if `crossProjectCrossType` and `crossProjectPlatform` |
| 42 | + // are initialized. Otherwise, the `sbt-typelevel` won't load itself. |
| 43 | + ( |
| 44 | + crossProjectCrossType.?.value, |
| 45 | + crossProjectPlatform.?.value, |
| 46 | + tlBspCrossProjectPlatforms.?.value |
| 47 | + ) match { |
| 48 | + // `tlBspCrossProjectPlatforms` is set by a user explicitly. |
| 49 | + case (_, Some(projectPlatform), Some(bspPlatforms)) => |
| 50 | + bspPlatforms.contains(projectPlatform) |
| 51 | + // If `CrossType` is `Pure` then enable BSP for the JVM platform only. |
| 52 | + case (Some(CrossType.Pure), Some(projectPlatform), None) => |
| 53 | + projectPlatform == JVMPlatform |
| 54 | + // Otherwise simply return the old value. |
| 55 | + case _ => oldValue |
| 56 | + } |
| 57 | + } |
| 58 | + ) |
| 59 | +} |
0 commit comments