bundle_build.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # Copyright 2023 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. import os
  12. import platform
  13. import subprocess
  14. import sys
  15. from pathlib import Path
  16. def build_gui(root_path: Path):
  17. print(f"Building taipy-gui frontend bundle in {root_path}.")
  18. already_exists = (root_path / "taipy" / "gui" / "webapp" / "index.html").exists()
  19. if already_exists:
  20. print(f'Found taipy-gui frontend bundle in {root_path / "taipy" / "gui" / "webapp"}.')
  21. else:
  22. if platform.system() == "Windows":
  23. subprocess.run(["npm", "ci"], cwd=root_path / "frontend" / "taipy-gui" / "dom", check=True, shell=True)
  24. subprocess.run(
  25. ["npm", "ci", "--omit=optional"], cwd=root_path / "frontend" / "taipy-gui", check=True, shell=True
  26. )
  27. subprocess.run(["npm", "run", "build"], cwd=root_path / "frontend" / "taipy-gui", check=True, shell=True)
  28. else:
  29. # subprocess does not work in GH Action *nix :-(
  30. os.system(f'cd {root_path / "frontend" / "taipy-gui" / "dom"}; npm ci')
  31. os.system(f'cd {root_path / "frontend" / "taipy-gui"}; npm ci --omit=optional && npm run build')
  32. def build_taipy(root_path: Path):
  33. print(f"Building taipy frontend bundle in {root_path}.")
  34. already_exists = (root_path / "taipy" / "gui_core" / "lib" / "taipy-gui-core.js").exists()
  35. if already_exists:
  36. print(f'Found taipy frontend bundle in {root_path / "taipy" / "gui_core" / "lib"}.')
  37. else:
  38. # Specify the correct path to taipy-gui in gui/.env file
  39. env_file_path = root_path / "frontend" / "taipy" / ".env"
  40. if not env_file_path.exists():
  41. with open(env_file_path, "w") as env_file:
  42. env_file.write(f"TAIPY_GUI_DIR={root_path}\n")
  43. if platform.system() == "Windows":
  44. subprocess.run(["npm", "ci"], cwd=root_path / "frontend" / "taipy", check=True, shell=True)
  45. subprocess.run(["npm", "run", "build"], cwd=root_path / "frontend" / "taipy", check=True, shell=True)
  46. else:
  47. os.system(f'cd {root_path / "frontend" / "taipy"}; npm ci && npm run build')
  48. if __name__ == "__main__":
  49. root_path = Path(__file__).absolute().parent.parent.parent
  50. if len(sys.argv) > 1:
  51. if sys.argv[1] == "gui":
  52. build_gui(root_path)
  53. elif sys.argv[1] == "taipy":
  54. build_taipy(root_path)
  55. else:
  56. build_gui(root_path)
  57. build_taipy(root_path)