compiler.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import pathlib
  2. import subprocess
  3. class compiler:
  4. """
  5. This class is responsible for building and bundling source code into
  6. result file. This could return to command line after error, or raise
  7. exception to handle it in build script.
  8. """
  9. def __init__(self, source: pathlib.Path, throw: bool = False) -> None:
  10. """
  11. This function create new compiler instance.
  12. Parameters:
  13. source (pathlib.Path) - Source file to build
  14. throw (bool) - When True failed compilation raise Exception
  15. """
  16. self.__source = source
  17. self.__throw = throw
  18. @property
  19. def throw(self) -> bool:
  20. """ When throw is True, failed command would raise Exception. """
  21. return self.__throw
  22. @property
  23. def source(self) -> pathlib.Path:
  24. """ This return source file to build. """
  25. return self.__source
  26. def build(self, result: pathlib.Path) -> None:
  27. """
  28. This function is responsible for building process.
  29. Parameters:
  30. result (pathlib.Path): Result file to store compiled code
  31. """
  32. if result.is_dir():
  33. self._error("Build file \"" + str(result) + "\" is directory.")
  34. if result.exists():
  35. result.unlink()
  36. command = self._command(self.source, result)
  37. returned = subprocess.run(command, capture_output = True)
  38. if returned.returncode == 0:
  39. return
  40. failed = returned.stdout.decode("UTF-8")
  41. failed = failed + returned.stderr.decode("UTF-8")
  42. self._error(failed)
  43. def _error(self, content: str) -> None:
  44. """
  45. This function raise Exception if throw flag is set, or print
  46. error to command line, and return there.
  47. Parameters:
  48. content (str): Content of the error
  49. """
  50. if self.throw:
  51. raise Exception(content)
  52. print("Error while compilation: ")
  53. print("\"" + content + "\"")
  54. exit(-1)
  55. def _command(self, source: pathlib.Path, result: pathlib.Path) -> list:
  56. """
  57. This function is responsible for creating command for compilation
  58. process.
  59. Parameters:
  60. source (pathlib.Path) - Source file to compile
  61. result (pathlib.Path) - Result compiled file
  62. Returns:
  63. (list) - List of strings, for subprocess creation
  64. """
  65. raise TypeError("This is abstract class. Overwrite this function.")
  66. class sass_compiler(compiler):
  67. def _command(self, source: pathlib.Path, result: pathlib.Path) -> list:
  68. """
  69. This function is responsible for creating command for compilation
  70. process.
  71. Parameters:
  72. source (pathlib.Path) - Source file to compile
  73. result (pathlib.Path) - Result compiled file
  74. Returns:
  75. (list) - List of strings, for subprocess creation
  76. """
  77. return [
  78. "sass",
  79. "--style=compressed",
  80. str(source),
  81. str(result)
  82. ]
  83. class esbuild_compiler(compiler):
  84. def _command(self, source: pathlib.Path, result: pathlib.Path) -> list:
  85. """
  86. This function is responsible for creating command for compilation
  87. process.
  88. Parameters:
  89. source (pathlib.Path) - Source file to compile
  90. result (pathlib.Path) - Result compiled file
  91. Returns:
  92. (list) - List of strings, for subprocess creation
  93. """
  94. return [
  95. "esbuild",
  96. str(source),
  97. "--bundle",
  98. "--outfile=" + str(result)
  99. ]