-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cleaning up header writer renaming SpriteRegistry.kt to SpriteLibrary.kt
- Loading branch information
Showing
47 changed files
with
17,746 additions
and
158 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
115 changes: 115 additions & 0 deletions
115
...n/kotlin/com/github/chriskn/structurizrextension/api/view/sprite/library/SpriteLibrary.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,115 @@ | ||
package com.github.chriskn.structurizrextension.api.view.sprite.library | ||
|
||
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper | ||
import com.github.chriskn.structurizrextension.api.view.sprite.ImageSprite | ||
import com.github.chriskn.structurizrextension.api.view.sprite.PlantUmlSprite | ||
import com.github.chriskn.structurizrextension.api.view.sprite.Sprite | ||
import java.net.URI | ||
import kotlin.io.path.listDirectoryEntries | ||
import kotlin.io.path.toPath | ||
|
||
/** | ||
* Sprite library | ||
* | ||
* Library offering sprites from different [SpriteSet]s associated by their name. | ||
* | ||
* Allows to load json SpriteSets from URLs. | ||
*/ | ||
object SpriteLibrary { | ||
|
||
private const val DEFAULT_SPRITES_FOLDER = "/sprites/" | ||
|
||
private val spritesByName: MutableMap<String, Sprite> = mutableMapOf() | ||
|
||
private val defaultSpriteSetPaths = this.javaClass.getResource(DEFAULT_SPRITES_FOLDER) | ||
?.toURI() | ||
?.toPath() | ||
?.listDirectoryEntries() | ||
.orEmpty() | ||
|
||
init { | ||
defaultSpriteSetPaths.map { spriteSetPath -> | ||
loadSpriteSet(spriteSetPath.toUri()) | ||
} | ||
} | ||
|
||
/** | ||
* Get Sprite by name | ||
* | ||
* @param name the name of the sprite. | ||
* @return the sprite with the given name | ||
* | ||
* @throws IllegalArgumentException if sprite with name does not exist | ||
*/ | ||
fun spriteByName(name: String): Sprite { | ||
val lowercaseName = name.lowercase() | ||
return spritesByName[lowercaseName] | ||
?: throw IllegalArgumentException( | ||
"No sprite found for name $lowercaseName. Possible matches: ${ | ||
findSpriteByNameContaining(Regex(lowercaseName)) | ||
.map { it.name } | ||
.joinToString(", ") | ||
}" | ||
) | ||
} | ||
|
||
/** | ||
* Get Sprite by name or null | ||
* | ||
* @param name the name of the sprite. | ||
* @return the sprite with the given name or null if no sprite with name exists | ||
*/ | ||
fun spriteByNameOrNull(name: String): Sprite? { | ||
val lowercaseName = name.lowercase() | ||
return spritesByName[lowercaseName] | ||
} | ||
|
||
/** | ||
* Find Sprite by name containing regex | ||
* | ||
* @param nameRegex the regex applied to all sprite names | ||
* @return all sprites with name containing nameRegex | ||
*/ | ||
fun findSpriteByNameContaining(nameRegex: Regex): List<Sprite> = spritesByName | ||
.filter { it.key.contains(nameRegex) } | ||
.values | ||
.toList() | ||
|
||
/** | ||
* Load sprite set | ||
* | ||
* Loads a [SpriteSet] json from the given URL and adds the contained sprites to the library | ||
* | ||
* @param spriteSetJsonUri URI pointing to [SpriteSet] json file | ||
* @return the loaded SpriteSet | ||
*/ | ||
fun loadSpriteSet(spriteSetJsonUri: URI): SpriteSet { | ||
val spriteSet = jacksonObjectMapper().readValue(spriteSetJsonUri.toURL(), SpriteSet::class.java) | ||
val configuredSprites = spriteSet.sprites.map { sprite -> | ||
configureSprite(sprite, spriteSet) | ||
}.toSet() | ||
addSpritesByName(configuredSprites) | ||
return spriteSet.copy(sprites = configuredSprites) | ||
} | ||
|
||
private fun addSpritesByName(sprites: Set<Sprite>) { | ||
val spritesWithName = sprites.filter { it.name != null } | ||
spritesByName.putAll(spritesWithName.associateBy { it.name!!.lowercase() }) | ||
} | ||
|
||
private fun configureSprite( | ||
sprite: Sprite, | ||
spriteSet: SpriteSet, | ||
) = when (sprite) { | ||
is PlantUmlSprite -> sprite.copy( | ||
additionalIncludes = spriteSet.additionalIncludes.orEmpty() + sprite.additionalIncludes.orEmpty(), | ||
additionalDefinitions = spriteSet.additionalDefinitions.orEmpty() + sprite.additionalDefinitions.orEmpty() | ||
) | ||
|
||
is ImageSprite -> sprite.copy( | ||
additionalDefinitions = spriteSet.additionalDefinitions.orEmpty() + sprite.additionalDefinitions.orEmpty() | ||
) | ||
|
||
else -> sprite | ||
} | ||
} |
4 changes: 2 additions & 2 deletions
4
...ion/api/view/sprite/registry/SpriteSet.kt → ...sion/api/view/sprite/library/SpriteSet.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
75 changes: 0 additions & 75 deletions
75
...kotlin/com/github/chriskn/structurizrextension/api/view/sprite/registry/SpriteRegistry.kt
This file was deleted.
Oops, something went wrong.
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
10,306 changes: 10,305 additions & 1 deletion
10,306
src/main/resources/sprites/aws_stdlib_sprites.json
Large diffs are not rendered by default.
Oops, something went wrong.
7,010 changes: 7,009 additions & 1 deletion
7,010
src/main/resources/sprites/gilbarbara_image_sprites.json
Large diffs are not rendered by default.
Oops, something went wrong.
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
Oops, something went wrong.