Created
April 26, 2024 23:25
-
-
Save yveszoundi/cddecaca70b0b3a97d69224d7a29d2c1 to your computer and use it in GitHub Desktop.
#jArchi script to import specializations from properties file
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
/* | |
Author: Yves Zoundi | |
Description: Import specializations and applicable icons | |
- Assumptions | |
- Folder with containing your specialization icons and a mapping file 'mapping.properties' | |
- Icon files are derived automatically from your specialization names (in lowercase) | |
- Supported icon file names: .png, .jpg, .jpeg, .gif, .tif, .tiff, .bmp, .ico | |
- The icon file name is derived automatically via the following approach: | |
- For a specialization 'Custom Location' will attempt loading icons with the name 'custom-location' (spaces to dashes) | |
- Then, all the supported file extensions will be tried (file existence), until a match is found | |
- Sample mappings.properties entries | |
Location=location,false | |
Resource=resource | |
In the above mapping file, 2 specializations are created 'Location' and 'Resource' | |
- 'Location' doesn't have an icon | |
- 'Resource' will have an icon by default, no need to explicitly add ",true" at the end | |
- All supported file extensions will be tried (specialization in lowercase): resource.png, resource.jpg, etc. | |
- If no existing icon file is found, an error will be thrown with a bit of contextual information | |
*/ | |
console.clear() | |
console.show() | |
console.log("START: ImportSpecializations") | |
let dirPath = window.promptOpenDirectory({ title: "Select folder with icons and mappings.properties" }) | |
if (dirPath) { | |
let JFile = Java.type("java.io.File") | |
let JProperties = Java.type("java.util.Properties") | |
let JFileInputStream = Java.type("java.io.FileInputStream") | |
let mappingsFile = new JFile(dirPath, "mappings.properties") | |
let iconFileExtensions = [".png", ".jpg", ".jpeg", ".gif", ".tif", ".tiff", ".bmp", ".ico"] | |
if (!mappingsFile.exists()) { | |
console.log("No mappings.properties file was not found in folder:" + dirPath + "!") | |
console.log("\nSample mappings.properties contents below.\n\n") | |
console.log("# Syntax Specialization=element-type,<optional-applyIconFileNameConventions-flag>") | |
console.log("# You can obtain element types with this script : https://gist.github.com/yveszoundi/2f3dd1e97b616540d86931f55662bed3") | |
console.log("# 'Cloud Provider' specialization for 'location' element type with no image") | |
console.log("Cloud\\ Provider=location,false") | |
console.log("# 'Resource' specialization for 'resource' element type with default image filename conventions: e.g., resource.png") | |
console.log("Resource=resource\n\n") | |
} else { | |
let fis = new JFileInputStream(mappingsFile) | |
try { | |
let props = new JProperties() | |
props.load(fis) | |
let entries = props.entrySet() | |
for (let entry of entries) { | |
let name = entry.getKey() | |
let providedConceptType = entry.getValue().toString().toLowerCase().trim() | |
let shouldUseCustomIcon = true | |
let conceptTypeEntries = providedConceptType.split(",") | |
if (conceptTypeEntries.length > 1) { | |
providedConceptType = conceptTypeEntries[0] | |
let shouldUseCustomIconAsText = conceptTypeEntries[1].toLowerCase().trim() | |
if (shouldUseCustomIconAsText == "false" || shouldUseCustomIconAsText == "0" || shouldUseCustomIconAsText == "no") | |
shouldUseCustomIcon = false | |
} | |
if (providedConceptType.endsWith("-relationship")) | |
shouldUseCustomIcon = false | |
let conceptMapping = { | |
conceptType: providedConceptType, | |
useCustomIcon: shouldUseCustomIcon | |
} | |
if (!model.findSpecialization(name, conceptMapping.conceptType)) { | |
if (!conceptMapping.useCustomIcon) { | |
model.createSpecialization(name, conceptMapping.conceptType) | |
} else { | |
let iconFileNameNoExtension = name.replaceAll(" ", "-").toLowerCase() | |
let iconFileFound = false; | |
let filePath = null | |
for (let i = 0; i < iconFileExtensions.length && !iconFileFound; i++) { | |
filePath = new JFile(dirPath, iconFileNameNoExtension + iconFileExtensions[i]) | |
if (filePath.exists()) | |
iconFileFound = true | |
} | |
if (!iconFileFound) { | |
let msg = "Could not create specialization '" + name + "'. Could not find icon in folder:'" + dirPath + "'." | |
msg = msg + "\n Tried the following combinations: " + " iconFileNameNoExtension:" + iconFileNameNoExtension + ", iconFileExtensions: [" + iconFileExtensions + "]." | |
throw new Error(msg) | |
} | |
model.createSpecialization(name, conceptMapping.conceptType, model.createImage(filePath)) | |
} | |
console.log("Created specialization '" + name + "' for concept '" + conceptMapping.conceptType + "', using icons:" + conceptMapping.useCustomIcon + ".") | |
} | |
} | |
} finally { | |
fis.close() | |
} | |
} | |
} | |
console.log("END: ImportSpecializations") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment