-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce ModuleFeaturesConfig (#1101)
This allows us to support client-side configuration and overrides of the module feature rules in module topography <!-- ⬆ Put your description above this! ⬆ Please be descriptive and detailed. Please read our [Contributing Guidelines](https://github.com/tinyspeck/foundry/blob/main/.github/CONTRIBUTING.md) and [Code of Conduct](https://slackhq.github.io/code-of-conduct). Don't worry about deleting this, it's not visible in the PR! -->
- Loading branch information
Showing
9 changed files
with
261 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
...s/gradle/foundry-gradle-plugin/src/main/kotlin/foundry/gradle/topography/ModuleFeature.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Copyright (C) 2024 Slack Technologies, LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package foundry.gradle.topography | ||
|
||
import com.squareup.moshi.JsonClass | ||
|
||
@JsonClass(generateAdapter = true) | ||
public data class ModuleFeature( | ||
val name: String, | ||
val explanation: String, | ||
val advice: String, | ||
val removalPatterns: Set<Regex>?, | ||
/** | ||
* Generated sources root dir relative to the project dir, if any. Files are checked recursively. | ||
*/ | ||
val generatedSourcesDir: String? = null, | ||
val generatedSourcesExtensions: Set<String> = emptySet(), | ||
val matchingText: Set<String> = emptySet(), | ||
val matchingTextFileExtensions: Set<String> = emptySet(), | ||
/** | ||
* If specified, looks for any sources in this dir relative to the project dir. Files are checked | ||
* recursively. | ||
*/ | ||
val matchingSourcesDir: String? = null, | ||
val matchingPlugin: String? = null, | ||
) |
75 changes: 75 additions & 0 deletions
75
...e/foundry-gradle-plugin/src/main/kotlin/foundry/gradle/topography/ModuleFeaturesConfig.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* Copyright (C) 2024 Slack Technologies, LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package foundry.gradle.topography | ||
|
||
import com.squareup.moshi.Json | ||
import com.squareup.moshi.JsonClass | ||
import foundry.common.json.JsonTools | ||
import foundry.common.json.JsonTools.readJsonValueMap | ||
import java.nio.file.Path | ||
|
||
/** | ||
* Represents a configuration for module features that can be JSON-encoded. | ||
* | ||
* @property _features the set of user-defined [features][ModuleFeature]. | ||
* @property _buildUponDefaults indicates whether these should build upon the [DefaultFeatures] set. | ||
* @property _defaultFeatureOverrides adhoc overrides of default feature values. These should be a | ||
* subset of [ModuleFeature] properties and will be overlaid onto them | ||
*/ | ||
@Suppress("PropertyName") | ||
@JsonClass(generateAdapter = true) | ||
internal data class ModuleFeaturesConfig( | ||
@Json(name = "features") val _features: Set<ModuleFeature> = emptySet(), | ||
@Json(name = "buildUponDefaults") val _buildUponDefaults: Boolean = true, | ||
@Json(name = "defaultFeatureOverrides") | ||
val _defaultFeatureOverrides: List<Map<String, Any>> = emptyList(), | ||
) { | ||
|
||
fun loadFeatures(): Map<String, ModuleFeature> { | ||
val inputFeatures = _features.associateBy { it.name } | ||
val defaultFeatures: Map<String, ModuleFeature> = | ||
if (_buildUponDefaults) { | ||
val defaults = DefaultFeatures.load() | ||
buildMap { | ||
putAll(defaults) | ||
for (override in _defaultFeatureOverrides) { | ||
val overrideName = | ||
override["name"] as? String? | ||
?: error("No feature name defined in override '$override'") | ||
val defaultToOverride = | ||
defaults[overrideName] ?: error("No default feature found for '$overrideName'") | ||
// To simply do this, we just finagle the default to a JSON map and then overlay the new | ||
// one onto it | ||
val defaultJsonValueMap = JsonTools.toJsonBuffer(defaultToOverride).readJsonValueMap() | ||
val newJsonValueMap = defaultJsonValueMap + override | ||
val newFeature = JsonTools.fromJsonValue<ModuleFeature>(newJsonValueMap) | ||
put(overrideName, newFeature) | ||
} | ||
} | ||
} else { | ||
emptyMap() | ||
} | ||
return defaultFeatures + inputFeatures | ||
} | ||
|
||
companion object { | ||
val DEFAULT = ModuleFeaturesConfig() | ||
|
||
fun load(path: Path): ModuleFeaturesConfig { | ||
return JsonTools.fromJson(path) | ||
} | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
...radle/foundry-gradle-plugin/src/main/kotlin/foundry/gradle/topography/ModuleTopography.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Copyright (C) 2024 Slack Technologies, LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package foundry.gradle.topography | ||
|
||
import com.squareup.moshi.JsonClass | ||
import foundry.common.json.JsonTools | ||
import java.nio.file.Path | ||
import org.gradle.api.file.FileSystemLocation | ||
import org.gradle.api.provider.Provider | ||
|
||
@JsonClass(generateAdapter = true) | ||
public data class ModuleTopography( | ||
val name: String, | ||
val gradlePath: String, | ||
val features: Set<String>, | ||
val plugins: Set<String>, | ||
) { | ||
public fun writeJsonTo(property: Provider<out FileSystemLocation>, prettyPrint: Boolean = false) { | ||
writeJsonTo(property.get().asFile.toPath(), prettyPrint) | ||
} | ||
|
||
public fun writeJsonTo(path: Path, prettyPrint: Boolean = false) { | ||
JsonTools.toJson(path, this, prettyPrint) | ||
} | ||
|
||
public companion object { | ||
public fun from(provider: Provider<out FileSystemLocation>): ModuleTopography = | ||
from(provider.get().asFile.toPath()) | ||
|
||
public fun from(path: Path): ModuleTopography = JsonTools.fromJson<ModuleTopography>(path) | ||
} | ||
} |
Oops, something went wrong.