| 27 | coloredlogs.install(level=logging.DEBUG, fmt='%(asctime)-15s %(name)s %(levelname)s: %(message)s') |
| 28 | |
| 29 | class PatchAndroidBuild(object): |
| 30 | def __init__(self) -> None: |
| 31 | self._line_separator = '\n' |
| 32 | self._patch_dict = { |
| 33 | '.getParameterCount()': '.getParameterTypes().length', |
| 34 | } |
| 35 | self._comment_list = [ |
| 36 | 'import javax.management.ListenerNotFoundException;', |
| 37 | 'import javax.management.Notification;', |
| 38 | 'import javax.management.NotificationEmitter;', |
| 39 | 'import javax.management.NotificationListener;', |
| 40 | 'import java.lang.management.ManagementFactory;', |
| 41 | 'import java.lang.management.MemoryNotificationInfo;', |
| 42 | 'import java.lang.management.MemoryPoolMXBean;', |
| 43 | 'import java.lang.management.MemoryType;', |
| 44 | ] |
| 45 | self._if_defined_android = '/* if defined ANDROID' |
| 46 | self._if_not_defined_android = '/* if not defined ANDROID */' |
| 47 | root_dir_path = pathlib.Path(__file__).parent.joinpath('../../').resolve().absolute() |
| 48 | self._source_dir_path = root_dir_path.joinpath('src/main/java/com/caoccao/javet').resolve().absolute() |
| 49 | self._target_dir_path = root_dir_path.joinpath('android/javet-android/src/main/java/com/caoccao/javet').resolve().absolute() |
| 50 | |
| 51 | def patch(self): |
| 52 | logging.info('Patch Android Build') |
| 53 | if not self._source_dir_path.exists(): |
| 54 | logging.error('%s is not found.', self._source_dir_path) |
| 55 | return 1 |
| 56 | if not self._target_dir_path.exists(): |
| 57 | self._target_dir_path.mkdir(parents=True) |
| 58 | logging.info('%s is created.', self._target_dir_path) |
| 59 | logging.info('From %s', self._source_dir_path) |
| 60 | logging.info(' To %s', self._target_dir_path) |
| 61 | |
| 62 | # Source to Target |
| 63 | for root, dir_names, file_names in os.walk(str(self._source_dir_path)): |
| 64 | root_path = pathlib.Path(root) |
| 65 | for file_name in file_names: |
| 66 | source_file_path = root_path.joinpath(file_name).resolve().absolute() |
| 67 | target_file_path = self._target_dir_path.joinpath(source_file_path.relative_to(self._source_dir_path)).resolve().absolute() |
| 68 | if not target_file_path.parent.exists(): |
| 69 | target_file_path.parent.mkdir(parents=True) |
| 70 | logging.info('%s is created.', target_file_path.parent) |
| 71 | lines = [] |
| 72 | for line in source_file_path.read_bytes().decode('utf-8').split(self._line_separator): |
| 73 | for (patch_key, patch_value) in self._patch_dict.items(): |
| 74 | if patch_key in line: |
| 75 | line = line.replace(patch_key, patch_value) |
| 76 | for comment_key in self._comment_list: |
| 77 | if comment_key in line: |
| 78 | line = f'// {line}' |
| 79 | if line.endswith(self._if_defined_android): |
| 80 | line += ' */' |
| 81 | if line.endswith(self._if_not_defined_android): |
| 82 | line = line[:-3] |
| 83 | lines.append(line) |
| 84 | buffer = self._line_separator.join(lines).encode('utf-8') |
| 85 | to_be_copied = True |
| 86 | if target_file_path.exists(): |
no outgoing calls
no test coverage detected
searching dependent graphs…