Menu Builder Script
This script processes a solution.json file and combines the menus key from multiple module JSON files to build a complete menu structure. The resulting menu is sorted by position at both the menu and submenu levels. The script outputs a new file named generated-solution.json containing the combined and sorted menu structure.
How It Works
1. Input File:
The script reads
solution.jsonfrom the current working directory.The
solution.jsonfile should have amoduleskey containing a list of module bundle file names (e.g.,social-protection-bundle.json,user-management.json, etc.).
2. Module and Module Bundle Files:
Each module file listed under the
moduleskey should be present in the same directory assolution.json.Each module file should contain a
menuskey, which defines its menu structure.Some modules may also contain a
dependencykey, listing other module bundles that must be processed even if they are not explicitly included insolution.json.
3. Combining Menus:
The script reads all the module files, extracts their
menus, and combines them.If multiple entries share the same
id, theirsubmenusare merged instead of duplicating menu items.Both menus and their
submenusare sorted by thepositionfield.
4. Handling Dependencies:
If a module has a
dependencykey, the script ensures that the referenced module files are also processed, even if they are not explicitly listed insolution.json.Dependencies are resolved recursively to ensure all required modules are included.
5. Handling Bundles:
Bundles are collections of multiple related modules grouped together in a single file.
If a bundle file is listed in
solution.json, the script will process all its included modules.Bundles may also have dependencies on other bundles, which are resolved recursively.
6. Missing Files:
If any module, bundle, or dependency file is missing, the script will print a warning but will continue processing the available files.
Example
Example solution.json with Bundles
{
"modules": [
"social-protection-bundle.json",
"opensearch-report-bundle.json",
"core-bundle.json"
],
"roles": [
"role-eo.json"
]
}Example social-protection-bundle.json (Bundle with Dependencies)
{
"modules": [
"individual.json",
"api-import.json",
"social-protection.json",
"payroll.json",
"payment-cycle.json",
"deduplication.json"
],
"dependency": [
"grievance-bundle.json"
]
}Example opensearch-report-bundle.json (Bundle Without Dependencies)
{
"modules": [
"opensearch-report.json"
]
}Example generated-solution.json (Final Merged Output)
{
"menus": [
{
"position": 1,
"id": "SocialRegistryMainMenu",
"name": "Social Registry",
"icon": "task-icon",
"description": "Social Registry",
"submenus": [
{
"position": 1,
"id": "individual.api_imports"
},
{
"position": 3,
"id": "individual.individuals"
},
{
"position": 4,
"id": "individual.groups"
},
{
"position": 2,
"id": "socialProtection.benefitPlans"
}
]
}
]
}How to Use
Prerequisites
Python 3.x installed on your system.
Steps
Prepare
solution.json:Create a file named
solution.jsonin the directory where you will run the script.Add a
moduleskey listing all the module bundles you want to include. Example:{ "modules": [ "social-protection-bundle.json", "opensearch-report-bundle.json", "core-bundle.json", "xxxx-bundle.json", "formal-sector-bundle.json" ] }
Prepare <module>-bundle.json:
Create a file named
<module>-bundle.jsonin the directory where you will run the script.Add a
moduleskey listing all the modules you want to include in bundle. Example:{ "modules": [ "individual.json", "api-import.json", "social-protection.json", "payroll.json", "payment-cycle.json", "deduplication.json" ], "dependency": [ "grievance-bundle.json", "calculation-social-protection-bundle.json" ] }
dependencykey: it means that this bundle is related to other bundle and the additional package must be installed and considered in the final output. For example ifgrievance-bundle.jsonis not added insolution.json- this bundle package will be added in the final output even though is not presented insolution.jsonfile.Prepare Module Files:
Place all the module files listed in
<module>-bundle.jsonfiles in the same directory. Each file should have a structure similar to the example below:{ "menus": [ { "position": 1, "id": "MainMenu", "name": "Main", "icon": "main-icon", "description": "Main menu", "submenus": [ { "position": 1, "id": "submenu1" }, { "position": 2, "id": "submenu2" } ] } ] }
Run the Script:
Save the script as
build_solution.pyin the same directory assolution.jsonand the module files.Open a terminal or command prompt and navigate to the directory.
Run the script using the command:
python3 build_solution.py
View the Output:
After the script completes, a new files named
generated-menu.json,generated-roles.json,fe-openimis.jsonandbe-openimis.jsonwill be created in the same directory.This file contains the combined and sorted menu structure.
Example Directory Structure
/solution
|-- solution.json
|-- social-protection.json
|-- payroll.json
|-- payment-cycle.json
|-- individual.json
|-- grievance.json
|-- grievance-bundle.json
|-- social-protection-bundle.json
|-- core.json
|-- build_solution.py
Notes
Ensure all files are properly formatted JSON.
Missing or malformed files will result in warnings but will not stop the script.
Customize the
solution.jsonfile to include or exclude specific modules based on your requirements.Repository:
GitHub - openimis/solution-builder: solution builder
Did you encounter a problem or do you have a suggestion?
Please contact our Service Desk
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. https://creativecommons.org/licenses/by-sa/4.0/