| 28 | } |
| 29 | |
| 30 | def analyze(self, params): |
| 31 | source_dir = params.source_dir |
| 32 | work_dir = os.getcwd() |
| 33 | rules = params["rules"] |
| 34 | |
| 35 | error_output = os.path.join(work_dir, "flow_result.json") |
| 36 | config_file = os.path.join(source_dir, ".flowconfig") |
| 37 | user_config_file = os.path.join(work_dir, "user_flowconfig") |
| 38 | default_rule = "type-error" |
| 39 | |
| 40 | # 分析 |
| 41 | # 如果项目中已有.flowconfig配置文件,需要保存起来,后面要恢复现场 |
| 42 | if os.path.exists(config_file): |
| 43 | copyfile(config_file, user_config_file) |
| 44 | os.remove(config_file) |
| 45 | SubProcController(["flow", "init", "--lints", "all=error"], cwd=source_dir).wait() |
| 46 | scan_cmd = [ |
| 47 | "flow", |
| 48 | "check", |
| 49 | "--all", |
| 50 | "--json", |
| 51 | "--show-all-errors", |
| 52 | "--include-warnings", |
| 53 | "--strip-root", |
| 54 | "--temp-dir", |
| 55 | work_dir, |
| 56 | ] |
| 57 | self.print_log("scan_cmd: %s" % " ".join(scan_cmd)) |
| 58 | SubProcController(scan_cmd, cwd=source_dir, stdout_filepath=error_output).wait() |
| 59 | SubProcController(["flow", "stop"], cwd=source_dir).wait() |
| 60 | |
| 61 | # 删除flow生成的配置文件,并将客户代码库自身的配置文件拷贝会代码库中,恢复现场 |
| 62 | if os.path.exists(config_file): |
| 63 | os.remove(config_file) |
| 64 | if os.path.exists(user_config_file): |
| 65 | copyfile(user_config_file, config_file) |
| 66 | |
| 67 | # 结果解析 |
| 68 | with open(error_output, "r") as f: |
| 69 | error_json = json.loads(f.read())["errors"] |
| 70 | issues = [] |
| 71 | for error in error_json: |
| 72 | msg = [] |
| 73 | refs = [] |
| 74 | kind = error["kind"] |
| 75 | for message in error["message"]: |
| 76 | if message["type"] != "Comment": |
| 77 | ref_line = message["line"] |
| 78 | ref_msg = message["descr"] |
| 79 | msg.append(ref_msg) |
| 80 | ref_tag = message["type"] |
| 81 | ref_path = message["path"] |
| 82 | refs.append({"line": ref_line, "msg": ref_msg, "tag": ref_tag, "path": ref_path}) |
| 83 | else: |
| 84 | msg.append(message["descr"]) |
| 85 | path = refs[0].get("path") |
| 86 | line = refs[0].get("line") |
| 87 | |