-
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
77 changed files
with
37,395 additions
and
502 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
2 changes: 1 addition & 1 deletion
2
src/main/kotlin/com/github/chriskn/structurizrextension/api/model/Container.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
2 changes: 1 addition & 1 deletion
2
src/main/kotlin/com/github/chriskn/structurizrextension/api/model/Dependency.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
2 changes: 1 addition & 1 deletion
2
src/main/kotlin/com/github/chriskn/structurizrextension/api/model/DeploymentNode.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
2 changes: 1 addition & 1 deletion
2
src/main/kotlin/com/github/chriskn/structurizrextension/api/model/Model.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
2 changes: 1 addition & 1 deletion
2
src/main/kotlin/com/github/chriskn/structurizrextension/api/model/ModelItem.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
2 changes: 1 addition & 1 deletion
2
src/main/kotlin/com/github/chriskn/structurizrextension/api/model/SoftwareSystem.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
2 changes: 1 addition & 1 deletion
2
src/main/kotlin/com/github/chriskn/structurizrextension/api/model/StaticStructureElement.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
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.sprites.ImageSprite | ||
import com.github.chriskn.structurizrextension.api.view.sprite.sprites.PlantUmlSprite | ||
import com.github.chriskn.structurizrextension.api.view.sprite.sprites.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 | ||
} | ||
} |
6 changes: 3 additions & 3 deletions
6
...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
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.