/
Menu Builder Script

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.json from the current working directory.

  • The solution.json file should have a modules key 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 modules key should be present in the same directory as solution.json.

  • Each module file should contain a menus key, which defines its menu structure.

  • Some modules may also contain a dependency key, listing other module bundles that must be processed even if they are not explicitly included in solution.json.

3. Combining Menus:

  • The script reads all the module files, extracts their menus, and combines them.

  • If multiple entries share the same id, their submenus are merged instead of duplicating menu items.

  • Both menus and their submenus are sorted by the position field.

4. Handling Dependencies:

  • If a module has a dependency key, the script ensures that the referenced module files are also processed, even if they are not explicitly listed in solution.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.json in the directory where you will run the script.

    • Add a modules key 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.json in the directory where you will run the script.

  • Add a modules key 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" ] }
  • dependency key: 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 if grievance-bundle.json is not added in solution.json - this bundle package will be added in the final output even though is not presented in solution.json file.

  • Prepare Module Files:

    • Place all the module files listed in <module>-bundle.json files 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.py in the same directory as solution.json and the module files.

  • Open a terminal or command prompt and navigate to the directory.

  • Run the script using the command:

    python3 build_solution.py
  1. View the Output:

    • After the script completes, a new files named generated-menu.json, generated-roles.json, fe-openimis.json and be-openimis.json will 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.json file 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/