def __modify_other_modules_r(self, package_name, finder=None): if not finder: finder = self._finder r_path = android_tools.find_r_file(finder.get_dst_r_dir(), package_name=package_name) if r_path and os.path.exists(r_path): target_dir = os.path.join(self.__get_freeline_backup_r_dir(), package_name.replace('.', os.sep)) if not os.path.exists(target_dir): os.makedirs(target_dir) target_path = os.path.join(target_dir, 'R.java') if not os.path.exists(target_path): self.debug('copy {} to {}'.format(r_path, target_path)) shutil.copy(r_path, target_path) content = get_file_content(target_path) content = GradleIncBuildInvoker.remove_final_tag(content) content = GradleIncBuildInvoker.extend_main_r( content, self._config['package']) content = android_tools.fix_unicode_parse_error( content, target_path) write_file_content(target_path, content) return target_path
def check_other_modules_resources(self): if self._name == self._config[ 'main_project_name'] and self._all_module_info is not None: changed_modules = self._changed_modules if len(changed_modules) > 0: main_r_fpath = os.path.join( self._finder.get_backup_dir(), self._module_info['packagename'].replace('.', os.sep), 'R.java') self.debug('modify {}'.format(main_r_fpath)) write_file_content( main_r_fpath, GradleIncBuildInvoker.remove_final_tag( get_file_content(main_r_fpath))) target_main_r_dir = os.path.join( self.__get_freeline_backup_r_dir(), self._module_info['packagename'].replace('.', os.sep)) if not os.path.exists(target_main_r_dir): os.makedirs(target_main_r_dir) target_main_r_path = os.path.join(target_main_r_dir, 'R.java') self.debug('copy {} to {}'.format(main_r_fpath, target_main_r_path)) shutil.copy(main_r_fpath, target_main_r_path) for module in changed_modules: fpath = self.__modify_other_modules_r( self._all_module_info[module]['packagename']) self.debug('modify {}'.format(fpath))
def write_merge_result(path, content): if len(content) > 0: tmp_path = path + '.temp' write_file_content(tmp_path, content) result = merge_xml([path, tmp_path]) write_file_content(path, result) os.remove(tmp_path)
def append_r_file(self): if self._name != self._config['main_project_name']: backupdir = self.__get_freeline_backup_r_dir() main_r_path = os.path.join(backupdir, self._config['package'].replace('.', os.sep), 'R.java') # main_r_path existence means that resource modification exists, so that need to add R.java to classpath if os.path.exists(main_r_path): pns = [self._config['package'], self._module_info['packagename']] for m in self._module_info['local_module_dep']: pns.append(self._all_module_info[m]['packagename']) for pn in pns: rpath = os.path.join(backupdir, pn.replace('.', os.sep), 'R.java') if os.path.exists(rpath) and rpath not in self._changed_files['src']: self._changed_files['src'].append(rpath) self.debug('add R.java to changed list: ' + rpath) elif pn == self._module_info['packagename']: fpath = self.__modify_other_modules_r(pn) self.debug('modify {}'.format(fpath)) if os.path.exists(fpath): self._changed_files['src'].append(fpath) self.debug('add R.java to changed list: ' + fpath) else: if is_windows_system(): main_r_path = os.path.join(self._finder.get_backup_dir(), self._module_info['packagename'].replace('.', os.sep), 'R.java') content = self.__fix_unicode_parse_error(get_file_content(main_r_path), main_r_path) write_file_content(main_r_path, content)
def fix_package_name(config, manifest): if config and config['package'] != config['debug_package']: finder = GradleDirectoryFinder(config['main_project_name'], config['main_project_dir'], config['build_cache_dir'], config=config) target_manifest_path = os.path.join(finder.get_backup_dir(), 'AndroidManifest.xml') if os.path.exists(target_manifest_path): return target_manifest_path if manifest and os.path.isfile(manifest): Logger.debug( 'find app has debug package name, freeline will fix the package name in manifest' ) content = get_file_content(manifest) result = re.sub('package=\"(.*)\"', 'package=\"{}\"'.format(config['package']), content) Logger.debug('change package name from {} to {}'.format( config['debug_package'], config['package'])) from utils import write_file_content write_file_content(target_manifest_path, result) Logger.debug( 'save new manifest to {}'.format(target_manifest_path)) return target_manifest_path return manifest
def append_r_file(self): if self._name != self._config['main_project_name']: backupdir = self.__get_freeline_backup_r_dir() main_r_path = os.path.join(backupdir, self._config['package'].replace('.', os.sep), 'R.java') # main_r_path existence means that resource modification exists, so that need to add R.java to classpath if os.path.exists(main_r_path): pns = [self._config['package'], self._module_info['packagename']] for m in self._module_info['local_module_dep']: pns.append(self._all_module_info[m]['packagename']) for pn in pns: rpath = os.path.join(backupdir, pn.replace('.', os.sep), 'R.java') if os.path.exists(rpath) and rpath not in self._changed_files['src']: self._changed_files['src'].append(rpath) self.debug('add R.java to changed list: ' + rpath) elif pn == self._module_info['packagename']: fpath = self.__modify_other_modules_r(pn) self.debug('modify {}'.format(fpath)) if fpath and os.path.exists(fpath): self._changed_files['src'].append(fpath) self.debug('add R.java to changed list: ' + fpath) else: if is_windows_system(): main_r_path = os.path.join(self._finder.get_backup_dir(), self._module_info['packagename'].replace('.', os.sep), 'R.java') if os.path.exists(main_r_path): content = android_tools.fix_unicode_parse_error(get_file_content(main_r_path), main_r_path) write_file_content(main_r_path, content)
def _generate_java_compile_args(self, extra_javac_args_enabled=False): javacargs = [self._javac] arguments = ['-encoding', 'UTF-8', '-g'] if not self._is_retrolambda_enabled: arguments.extend(['-target', '1.7', '-source', '1.7']) arguments.append('-cp') arguments.append(os.pathsep.join(self._classpaths)) for fpath in self._changed_files['src']: arguments.append(fpath) if extra_javac_args_enabled: if 'apt' in self._changed_files: for fpath in self._changed_files['apt']: arguments.append(fpath) filter_tags = [] if self._is_databinding_enabled: filter_tags.extend( ['BindingAdapter', 'BindingConversion', 'Bindable']) if self._is_dagger_enabled: filter_tags.extend(['DaggerComponent', 'DaggerModule']) files = self._get_apt_related_files(filter_tags=filter_tags) for fpath in files: if fpath and os.path.exists( fpath) and fpath not in self._changed_files['src']: if 'apt' in self._changed_files and fpath in self._changed_files[ 'apt']: continue self.debug('add apt related file: {}'.format(fpath)) arguments.append(fpath) arguments.extend(self._extra_javac_args) arguments.append('-d') arguments.append(self._finder.get_patch_classes_cache_dir()) # ref: https://support.microsoft.com/en-us/kb/830473 if is_windows_system(): arguments_length = sum(map(len, arguments)) if arguments_length > 8000: argument_file_path = os.path.join( self._finder.get_module_cache_dir(), 'javac_args_file') self.debug( 'arguments length: {} > 8000, save args to {}'.format( arguments_length, argument_file_path)) if os.path.exists(argument_file_path): os.remove(argument_file_path) arguments_content = ' '.join(arguments) self.debug('javac arguments: ' + arguments_content) write_file_content(argument_file_path, arguments_content) arguments = ['@{}'.format(argument_file_path)] javacargs.extend(arguments) return javacargs
def rollback_last_sync_ticket(cache_dir): ticket = get_last_sync_ticket(cache_dir) if ticket > 0: ticket -= 1 else: ticket = 0 write_file_content(get_last_sync_ticket_path(cache_dir), ticket)
def get_apktime_path(config): adir = os.path.join(config['build_cache_dir'], 'freeline-assets') if not os.path.exists(adir): os.makedirs(adir) path = os.path.join(adir, 'apktime') if not os.path.exists(path): write_file_content(path, '') return path
def __save_parms_to_file(self, path, params): if os.path.exists(path): os.remove(path) content = '' for param in params: content += param + '\n' write_file_content(path, content) self.debug('save retrolambda params to {}'.format(path))
def _generate_java_compile_args(self, extra_javac_args_enabled=False): javacargs = [self._javac] arguments = ['-encoding', 'UTF-8', '-g'] if not self._is_retrolambda_enabled: arguments.extend(['-target', '1.7', '-source', '1.7']) arguments.append('-cp') arguments.append(os.pathsep.join(self._classpaths)) for fpath in self._changed_files['src']: arguments.append(fpath) if extra_javac_args_enabled: if 'apt' in self._changed_files: for fpath in self._changed_files['apt']: arguments.append(fpath) filter_tags = [] if self._is_databinding_enabled: filter_tags.extend(['BindingAdapter', 'BindingConversion', 'Bindable']) if self._is_dagger_enabled: filter_tags.extend(['DaggerComponent', 'DaggerModule']) files = self._get_apt_related_files(filter_tags=filter_tags) for fpath in files: if fpath and os.path.exists(fpath) and fpath not in self._changed_files['src']: if 'apt' in self._changed_files and fpath in self._changed_files['apt']: continue self.debug('add apt related file: {}'.format(fpath)) arguments.append(fpath) arguments.extend(self._extra_javac_args) arguments.append('-d') arguments.append(self._finder.get_patch_classes_cache_dir()) # ref: https://support.microsoft.com/en-us/kb/830473 if is_windows_system(): arguments_length = sum(map(len, arguments)) if arguments_length > 8000: argument_file_path = os.path.join(self._finder.get_module_cache_dir(), 'javac_args_file') self.debug('arguments length: {} > 8000, save args to {}'.format(arguments_length, argument_file_path)) if os.path.exists(argument_file_path): os.remove(argument_file_path) arguments_content = ' '.join(arguments) self.debug('javac arguments: ' + arguments_content) write_file_content(argument_file_path, arguments_content) arguments = ['@{}'.format(argument_file_path)] javacargs.extend(arguments) return javacargs
def generate_public_files_by_r(dst_r_path, public_path, ids_path): buf = get_file_content(dst_r_path) temp = re.findall('<tr><td><code>([^<]+)</code></td>', buf) diykv = [] for i in temp: if "{" not in i: diykv.append(i) dstbuf = '' idbuf = '<?xml version="1.0" encoding="utf-8"?>\n' idbuf += '<resources>\n' dstbuf += idbuf result = buf.split('\n') type_char = '' for r in result: if 'public static final class' in r: type_char = r.replace('public static final class ', '').replace( ' {', '').replace(' ', '').replace('\n', '').replace('\r', '') elif 'public static class' in r: type_char = r.replace('public static class ', '').replace( ' {', '').replace(' ', '').replace('\n', '').replace('\r', '') type_char = type_char.replace(' ', '').replace('\n', '').replace('\r', '') elif 'public static final int' in r and type_char != '' and '[]' not in r: kv = r.replace('public static final int ', '').replace(';', '').split('=') name = kv[0].replace(' ', '').replace('\n', '').replace('\r', '') id_char = kv[1].replace(' ', '').replace('\n', '').replace('\r', '') dstbuf += ' <public type="%s" name="%s" id="%s" />\n' % ( type_char, name, id_char) if type_char == 'id' and name not in diykv: idbuf += ' <item name="%s" type="id"/>\n' % name elif 'public static int' in r and type_char != '' and '[]' not in r: kv = r.replace('public static int ', '').replace(';', '').split('=') name = kv[0].replace(' ', '').replace('\n', '').replace('\r', '') id_char = kv[1].replace(' ', '').replace('\n', '').replace('\r', '') dstbuf += ' <public type="%s" name="%s" id="%s" />\n' % ( type_char, name, id_char) if type_char == 'id' and name not in diykv: idbuf += ' <item name="%s" type="id"/>\n' % name elif type_char != '' and '}' in r: type_char = '' dstbuf += '</resources>' idbuf += '</resources>' write_file_content(public_path, dstbuf) write_file_content(ids_path, idbuf)
def __modify_main_r(self): main_r_fpath = os.path.join(self._finder.get_backup_dir(), self._module_info['packagename'].replace('.', os.sep), 'R.java') self.debug('modify {}'.format(main_r_fpath)) write_file_content(main_r_fpath, GradleIncBuildInvoker.remove_final_tag(get_file_content(main_r_fpath))) target_main_r_dir = os.path.join(self.__get_freeline_backup_r_dir(), self._module_info['packagename'].replace('.', os.sep)) if not os.path.exists(target_main_r_dir): os.makedirs(target_main_r_dir) target_main_r_path = os.path.join(target_main_r_dir, 'R.java') self.debug('copy {} to {}'.format(main_r_fpath, target_main_r_path)) shutil.copy(main_r_fpath, target_main_r_path)
def backup_res_files(self): pending_remove = [] for fpath in self._changed_files['res']: # res/values/colors.xml -> build/target/generated-sources/res/values/colors.xml # res/values/colors.xml -> build/intermediates/res/merged/debug/values/colors.xml dst_path = self._get_res_incremental_dst_path(fpath) is_new_file = False if not os.path.exists(dst_path): is_new_file = True self._new_res_list.append(dst_path) if fpath in self._merged_xml_cache: backup_res_file(dst_path) # backup old file cache = self._merged_xml_cache[fpath] write_file_content(dst_path, cache) # write merged cache to dst path else: if is_new_file: shutil.copyfile( fpath, dst_path) # just copy to dst path, if this is new file self.debug('copy {} to {}'.format(fpath, dst_path)) continue old_file_md5 = get_md5(fpath) dst_file_md5 = get_md5(dst_path) if old_file_md5 != dst_file_md5: backup_res_file(dst_path) shutil.copyfile(fpath, dst_path) self.debug('copy {} to {}'.format(fpath, dst_path)) else: pending_remove.append( fpath ) # file is not changed, so remove from changed list os.utime(dst_path, None) for fpath in self._changed_files['assets']: dst_path = self._get_res_incremental_dst_path(fpath) if os.path.exists(dst_path): backup_res_file(dst_path) else: self._new_res_list.append(dst_path) shutil.copyfile(fpath, dst_path) for fpath in pending_remove: if fpath in self._changed_files['res']: self._changed_files['res'].remove(fpath)
def __modify_main_r(self): main_r_fpath = os.path.join(self._finder.get_backup_dir(), self._module_info['packagename'].replace('.', os.sep), 'R.java') self.debug('modify {}'.format(main_r_fpath)) buf = GradleIncBuildInvoker.remove_final_tag(get_file_content(main_r_fpath)) buf = self.__fix_unicode_parse_error(buf, main_r_fpath) write_file_content(main_r_fpath, buf) target_main_r_dir = os.path.join(self.__get_freeline_backup_r_dir(), self._module_info['packagename'].replace('.', os.sep)) if not os.path.exists(target_main_r_dir): os.makedirs(target_main_r_dir) target_main_r_path = os.path.join(target_main_r_dir, 'R.java') self.debug('copy {} to {}'.format(main_r_fpath, target_main_r_path)) shutil.copy(main_r_fpath, target_main_r_path)
def fix_package_name(config, manifest): if config and config['package'] != config['debug_package']: finder = GradleDirectoryFinder(config['main_project_name'], config['main_project_dir'], config['build_cache_dir'], config=config) target_manifest_path = os.path.join(finder.get_backup_dir(), 'AndroidManifest.xml') if os.path.exists(target_manifest_path): return target_manifest_path if manifest and os.path.isfile(manifest): Logger.debug('find app has debug package name, freeline will fix the package name in manifest') content = get_file_content(manifest) result = re.sub('package=\"(.*)\"', 'package=\"{}\"'.format(config['package']), content) Logger.debug('change package name from {} to {}'.format(config['debug_package'], config['package'])) from utils import write_file_content write_file_content(target_manifest_path, result) Logger.debug('save new manifest to {}'.format(target_manifest_path)) return target_manifest_path return manifest
def generate_public_files_by_r(dst_r_path, public_path, ids_path): buf = get_file_content(dst_r_path) temp = re.findall('<tr><td><code>([^<]+)</code></td>', buf) diykv = [] for i in temp: if "{" not in i: diykv.append(i) dstbuf = '' idbuf = '<?xml version="1.0" encoding="utf-8"?>\n' idbuf += '<resources>\n' dstbuf += idbuf result = buf.split('\n') type_char = '' for r in result: if 'public static final class' in r: type_char = r.replace('public static final class ', '').replace(' {', '').replace(' ', '').replace('\n', '').replace('\r', '') elif 'public static class' in r: type_char = r.replace('public static class ', '').replace(' {', '').replace(' ', '').replace('\n', '').replace('\r', '') type_char = type_char.replace(' ', '').replace('\n', '').replace('\r', '') elif 'public static final int' in r and type_char != '' and '[]' not in r: kv = r.replace('public static final int ', '').replace(';', '').split('=') name = kv[0].replace(' ', '').replace('\n', '').replace('\r', '') id_char = kv[1].replace(' ', '').replace('\n', '').replace('\r', '') dstbuf += ' <public type="%s" name="%s" id="%s" />\n' % (type_char, name, id_char) if type_char == 'id' and name not in diykv: idbuf += ' <item name="%s" type="id"/>\n' % name elif 'public static int' in r and type_char != '' and '[]' not in r: kv = r.replace('public static int ', '').replace(';', '').split('=') name = kv[0].replace(' ', '').replace('\n', '').replace('\r', '') id_char = kv[1].replace(' ', '').replace('\n', '').replace('\r', '') dstbuf += ' <public type="%s" name="%s" id="%s" />\n' % (type_char, name, id_char) if type_char == 'id' and name not in diykv: idbuf += ' <item name="%s" type="id"/>\n' % name elif type_char != '' and '}' in r: type_char = '' dstbuf += '</resources>' idbuf += '</resources>' write_file_content(public_path, dstbuf) write_file_content(ids_path, idbuf)
def backup_res_files(self): pending_remove = [] for fpath in self._changed_files['res']: # res/values/colors.xml -> build/target/generated-sources/res/values/colors.xml # res/values/colors.xml -> build/intermediates/res/merged/debug/values/colors.xml dst_path = self._get_res_incremental_dst_path(fpath) is_new_file = False if not os.path.exists(dst_path): is_new_file = True self._new_res_list.append(dst_path) if fpath in self._merged_xml_cache: backup_res_file(dst_path) # backup old file cache = self._merged_xml_cache[fpath] write_file_content(dst_path, cache) # write merged cache to dst path else: if is_new_file: shutil.copyfile(fpath, dst_path) # just copy to dst path, if this is new file self.debug('copy {} to {}'.format(fpath, dst_path)) continue old_file_md5 = get_md5(fpath) dst_file_md5 = get_md5(dst_path) if old_file_md5 != dst_file_md5: backup_res_file(dst_path) shutil.copyfile(fpath, dst_path) self.debug('copy {} to {}'.format(fpath, dst_path)) else: pending_remove.append(fpath) # file is not changed, so remove from changed list os.utime(dst_path, None) for fpath in self._changed_files['assets']: dst_path = self._get_res_incremental_dst_path(fpath) if os.path.exists(dst_path): backup_res_file(dst_path) else: self._new_res_list.append(dst_path) shutil.copyfile(fpath, dst_path) for fpath in pending_remove: if fpath in self._changed_files['res']: self._changed_files['res'].remove(fpath)
def __modify_other_modules_r(self, package_name, finder=None): if not finder: finder = self._finder r_path = android_tools.find_r_file(finder.get_dst_r_dir(), package_name=package_name) if os.path.exists(r_path): target_dir = os.path.join(self.__get_freeline_backup_r_dir(), package_name.replace('.', os.sep)) if not os.path.exists(target_dir): os.makedirs(target_dir) target_path = os.path.join(target_dir, 'R.java') if not os.path.exists(target_path): self.debug('copy {} to {}'.format(r_path, target_path)) shutil.copy(r_path, target_path) content = get_file_content(target_path) content = GradleIncBuildInvoker.remove_final_tag(content) content = GradleIncBuildInvoker.extend_main_r(content, self._config['package']) content = self.__fix_unicode_parse_error(content, target_path) write_file_content(target_path, content) return target_path
def update_clean_build_created_flag(apktime_path): flag = str(datetime.datetime.now().microsecond) Logger.debug("update apk time path: " + apktime_path) Logger.debug("new clean build flag value: " + flag) write_file_content(apktime_path, flag)
def fix_for_windows(path): if is_windows_system(): buf = fix_unicode_parse_error(get_file_content(path), path) write_file_content(path, buf)
def mark_res_build_job(job_path): if not os.path.exists(job_path): write_file_content(job_path, '')
def mark_res_changed(cache_dir): path = get_res_changed_flag_path(cache_dir) if not os.path.exists(path): write_file_content(path, '')
def mark_res_sync_status(sync_file_path): if not os.path.exists(sync_file_path): write_file_content(sync_file_path, '')
def mark_restart_flag(cache_dir): path = os.path.join(cache_dir, 'increment.restart') if not os.path.exists(path): write_file_content(path, '')
def update_last_sync_ticket(cache_dir): ticket = get_last_sync_ticket(cache_dir) + 1 ticket_path = get_last_sync_ticket_path(cache_dir) write_file_content(ticket_path, ticket)
def _generate_java_compile_args(self, extra_javac_args_enabled=False): javacargs = [self._javac] arguments = ['-encoding', 'UTF-8', '-g'] if not self._is_retrolambda_enabled: arguments.extend(['-target', '1.7', '-source', '1.7']) arguments.append('-cp') arguments.append(os.pathsep.join(self._classpaths)) patch_classes_cache_dir = self._finder.get_patch_classes_cache_dir() dst_classes_dir = self._finder.get_dst_classes_dir() for fpath in self._changed_files['src']: if self._is_dagger_enabled: # remove *_MembersInjector.class which generated by dagger2 # -5 means '.java' path_members_injector = fpath[:-5] + '_MembersInjector.class' for root, dirs, list in os.walk(patch_classes_cache_dir): for i in list: file = os.path.join(root, i) if file[len(patch_classes_cache_dir ):] in path_members_injector: os.remove(file) for root, dirs, list in os.walk(dst_classes_dir): for i in list: file = os.path.join(root, i) if file[len(dst_classes_dir ):] in path_members_injector: os.remove(file) arguments.append(fpath) for fpath in self._force_annotation_processor_files: arguments.append(fpath) if extra_javac_args_enabled: if 'apt' in self._changed_files: for fpath in self._changed_files['apt']: arguments.append(fpath) filter_tags = [] if self._is_databinding_enabled: filter_tags.extend( ['BindingAdapter', 'BindingConversion', 'Bindable']) if self._is_dagger_enabled: filter_tags.extend(['DaggerComponent', 'DaggerModule']) files = self._get_apt_related_files(filter_tags=filter_tags) for fpath in files: if fpath and os.path.exists( fpath) and fpath not in self._changed_files['src']: if 'apt' in self._changed_files and fpath in self._changed_files[ 'apt']: continue self.debug('add apt related file: {}'.format(fpath)) arguments.append(fpath) arguments.extend(self._extra_javac_args) arguments.append('-d') arguments.append(self._finder.get_patch_classes_cache_dir()) # ref: https://support.microsoft.com/en-us/kb/830473 if is_windows_system(): arguments_length = sum(map(len, arguments)) if arguments_length > 8000: argument_file_path = os.path.join( self._finder.get_module_cache_dir(), 'javac_args_file') self.debug( 'arguments length: {} > 8000, save args to {}'.format( arguments_length, argument_file_path)) if os.path.exists(argument_file_path): os.remove(argument_file_path) arguments_content = ' '.join(arguments) self.debug('javac arguments: ' + arguments_content) write_file_content(argument_file_path, arguments_content) arguments = ['@{}'.format(argument_file_path)] javacargs.extend(arguments) return javacargs