bundle_build.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. # Copyright 2021-2024 Avaiga Private Limited
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  4. # the License. You may obtain a copy of the License at
  5. #
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. #
  8. # Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  9. # an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
  10. # specific language governing permissions and limitations under the License.
  11. # --------------------------------------------------------------------------------------------------
  12. # Builds the Taipy GUI or Taipy frontend bundle.
  13. #
  14. # Invoked from the workflows:
  15. # actions\install\action.yml
  16. # workflows\build-and-release.yml
  17. # workflows\build-and-release-single-package.yml
  18. # workflows\packaging.yml
  19. # workflows\partial-tests.yml
  20. # workflows\prebuild.yml
  21. # --------------------------------------------------------------------------------------------------
  22. import platform
  23. import subprocess
  24. import sys
  25. from pathlib import Path
  26. with_shell = platform.system() == "Windows"
  27. def usage() -> None:
  28. print(f"Usage: {sys.argv[0]} [<bundle>]") # noqa: T201
  29. print(" Builds the Taipy frontend bundles.") # noqa: T201
  30. print(" If <bundle> is 'gui', only the Taipy GUI bundle is built.") # noqa: T201
  31. print(" If <bundle> is 'taipy', only the Taipy bundle is built (expecting Taipy GUI's to exist).") # noqa: T201
  32. print(" In all other cases, both bundles are built.") # noqa: T201
  33. def build_gui(root_path: Path):
  34. print(f"Building taipy-gui frontend bundle in {root_path}.") # noqa: T201
  35. already_exists = (root_path / "taipy" / "gui" / "webapp" / "index.html").exists()
  36. if already_exists:
  37. print(f'Found taipy-gui frontend bundle in {root_path / "taipy" / "gui" / "webapp"}.') # noqa: T201
  38. else:
  39. subprocess.run(["npm", "ci"], cwd=root_path / "frontend" / "taipy-gui" / "dom", check=True, shell=with_shell)
  40. subprocess.run(["npm", "ci"], cwd=root_path / "frontend" / "taipy-gui", check=True, shell=with_shell)
  41. subprocess.run(["npm", "run", "build"], cwd=root_path / "frontend" / "taipy-gui", check=True, shell=with_shell)
  42. def build_taipy(root_path: Path):
  43. print(f"Building taipy frontend bundle in {root_path}.") # noqa: T201
  44. already_exists = (root_path / "taipy" / "gui_core" / "lib" / "taipy-gui-core.js").exists()
  45. if already_exists:
  46. print(f'Found taipy frontend bundle in {root_path / "taipy" / "gui_core" / "lib"}.') # noqa: T201
  47. else:
  48. # Specify the correct path to taipy-gui in gui/.env file
  49. env_file_path = root_path / "frontend" / "taipy" / ".env"
  50. if not env_file_path.exists():
  51. with open(env_file_path, "w") as env_file:
  52. env_file.write(f"TAIPY_DIR={root_path}\n")
  53. subprocess.run(["npm", "ci"], cwd=root_path / "frontend" / "taipy", check=True, shell=with_shell)
  54. subprocess.run(["npm", "run", "build"], cwd=root_path / "frontend" / "taipy", check=True, shell=with_shell)
  55. if __name__ == "__main__":
  56. root_path = Path(__file__).absolute().parent.parent.parent
  57. if len(sys.argv) > 1:
  58. if sys.argv[1] == "gui":
  59. build_gui(root_path)
  60. exit(0)
  61. elif sys.argv[1] == "taipy":
  62. build_taipy(root_path)
  63. exit(0)
  64. build_gui(root_path)
  65. build_taipy(root_path)