1 /* 2 * Copyright (c) 2022 Shenzhen Kaihong Digital Industry Development Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 package com.sk.utils; 16 17 import com.fasterxml.jackson.core.JsonProcessingException; 18 import com.fasterxml.jackson.databind.ObjectMapper; 19 import com.intellij.notification.NotificationType; 20 import com.intellij.openapi.diagnostic.Logger; 21 import com.intellij.openapi.project.Project; 22 import net.minidev.json.JSONObject; 23 import net.minidev.json.parser.JSONParser; 24 import net.minidev.json.parser.ParseException; 25 import org.apache.http.util.TextUtils; 26 27 import java.io.BufferedReader; 28 import java.io.File; 29 import java.io.FileInputStream; 30 import java.io.FileNotFoundException; 31 import java.io.FileWriter; 32 import java.io.IOException; 33 import java.io.InputStreamReader; 34 import java.nio.charset.StandardCharsets; 35 import java.util.Properties; 36 import java.util.regex.Pattern; 37 38 /** 39 * 文本文件工具 40 * 41 * @author: xudong 42 * @see: file utils 43 * @version: v1.0.0 44 * @since 2022-02-21 45 */ 46 public class FileUtil { 47 private static final Logger LOG = Logger.getInstance(FileUtil.class); 48 49 private static final int COMPILE_SDK_VERSION = 5; 50 51 private static final String LF = getNewline(); // 换行符 52 53 private static final String BUILD_OPTION = "{" + LF 54 + " \"externalNativeOptions\": {" + LF 55 + " \"path\": \"\"," + LF 56 + " \"arguments\": \"-v\"," + LF 57 + " \"abiFilters\": [" + LF 58 + " \"armeabi-v7a\"," + LF 59 + " \"arm64-v8a\"" + LF 60 + " ]," + LF 61 + " \"cppFlags\": \"\"," + LF 62 + " }" + LF 63 + " }"; 64 65 /** 66 * 改写build-profile.json5文件 67 * 68 * @param buildJsonFilePath build-profile.json5 文件路径 69 * @param cmakeFilePath CMakeList.txt 文件路径 70 */ writeBuildJsonFile(String buildJsonFilePath, String cmakeFilePath)71 public void writeBuildJsonFile(String buildJsonFilePath, String cmakeFilePath) { 72 try { 73 String buildStr = readWholeFile(buildJsonFilePath); 74 JSONParser jsParser = new JSONParser(JSONParser.DEFAULT_PERMISSIVE_MODE); 75 JSONObject buildObj = (JSONObject) jsParser.parse(buildStr); 76 JSONObject buildOptionObj = (JSONObject) jsParser.parse(BUILD_OPTION); 77 ((JSONObject) buildOptionObj.get("externalNativeOptions")).put("path", cmakeFilePath); 78 buildObj.put("buildOption", buildOptionObj); 79 ObjectMapper mapper = new ObjectMapper(); 80 buildStr = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(buildObj); 81 82 writeContentToFile(buildJsonFilePath, buildStr); 83 } catch (ParseException parseException) { 84 LOG.error("Failed to parse file [" + buildJsonFilePath + "], error: " + parseException); 85 } catch (JsonProcessingException jsonProcessingEx) { 86 LOG.error("Failed to write file [" + buildJsonFilePath + "], error: " + jsonProcessingEx); 87 } 88 } 89 90 /** 91 * 将数据写入到指定文件中 92 * 93 * @param path 文件路径 94 * @param content 数据内容 95 */ writeContentToFile(String path, String content)96 public void writeContentToFile(String path, String content) { 97 File file = new File(path); 98 try (FileWriter fw = new FileWriter(file, false)) { 99 fw.write(content + FileUtil.getNewline()); 100 } catch (IOException ioException) { 101 LOG.error("Failed to write file [" + path + "], error: " + ioException); 102 } 103 } 104 105 /** 106 * 创建文件 107 * 108 * @param path 文件路径 109 * @return 文件路径 110 */ makeFile(String path)111 public String makeFile(String path) { 112 File file = new File(path); 113 if (!file.exists()) { 114 try { 115 boolean isCreateFile = file.createNewFile(); 116 if (isCreateFile) { 117 LOG.info(String.format("makeFile %s success", path)); 118 } 119 } catch (IOException ioException) { 120 LOG.error(String.format("makeFile %s error:%s", path, ioException)); 121 return ""; 122 } 123 } 124 return file.getPath(); 125 } 126 127 /** 128 * 将错误信息输入到txt中 129 * 130 * @param path 路径 131 * @param content 内容 132 */ writeErrorToTxt(String path, String content)133 public void writeErrorToTxt(String path, String content) { 134 File file = new File(path); 135 try (FileWriter fw = new FileWriter(file, true)) { 136 fw.write(content + FileUtil.getNewline()); 137 } catch (IOException ioException) { 138 LOG.error("writeErrorToTxt io error" + ioException); 139 } 140 } 141 142 /** 143 * 判断文件是否包含指定字符串 144 * 145 * @param path 文件路径 146 * @param content 指定内容 147 * @return 是否包含指定字符串 148 * @throws IOException 异常信息 149 */ findStringInFile(String path, String content)150 public boolean findStringInFile(String path, String content) throws IOException { 151 File file = new File(path); 152 String[] command = content.split(FileUtil.getNewline()); 153 154 try (InputStreamReader read = new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8); 155 BufferedReader bufferedReader = new BufferedReader(read)) { 156 return isContainString(bufferedReader, command[1]); 157 } catch (FileNotFoundException foundException) { 158 LOG.error("file not found" + foundException); 159 return false; 160 } 161 } 162 isContainString(BufferedReader bufferedReader, String command)163 private boolean isContainString(BufferedReader bufferedReader, String command) { 164 String line = null; 165 while (true) { 166 try { 167 if ((line = bufferedReader.readLine()) == null) { 168 return false; 169 } 170 } catch (IOException ioException) { 171 LOG.error("findStringInFile IOException" + ioException); 172 } 173 174 if (line.contains(command)) { 175 return true; 176 } 177 } 178 } 179 180 /** 181 * 获取换行符 182 * 183 * @return 换行符 184 */ getNewline()185 public static String getNewline() { 186 return System.getProperty("line.separator"); 187 } 188 189 /** 190 * 正则匹配所选文件名是否符合规范 191 * 192 * @param fileName 文件名 193 * @return boolean 是否匹配 194 */ patternFileName(String fileName)195 public static boolean patternFileName(String fileName) { 196 String pattern = "(@ohos.([.a-z_A-Z0-9]+).d.ts|([.a-z_A-Z0-9]+).h)"; 197 return Pattern.matches(pattern, fileName); 198 } 199 200 /** 201 * 正则匹配所选文件名是否符合规范 202 * 203 * @param fileName 文件名 204 * @return boolean 是否匹配 205 */ patternFileNameH(String fileName)206 public static boolean patternFileNameH(String fileName) { 207 String pattern = "([.a-z_A-Z0-9]+).h"; 208 return Pattern.matches(pattern, fileName); 209 } 210 211 /** 212 * check project SDK 213 * 214 * @param project projectid 215 * @param baseFile project root file 216 * @return boolean 217 */ checkProjectSDK(Project project, String baseFile)218 public static boolean checkProjectSDK(Project project, String baseFile) { 219 220 String gradlePath = ""; 221 File baseDir = new File(baseFile); 222 if (baseDir.isDirectory()) { 223 File[] childFile = baseDir.listFiles(); 224 assert childFile != null; 225 for (File file : childFile) { 226 if (file.getName().equals("build.gradle") || file.getName().equals("build-profile.json5")) { 227 gradlePath = file.getPath(); 228 } 229 } 230 } 231 232 Properties properties = new Properties(); 233 if (TextUtils.isBlank(gradlePath)) { 234 GenNotification.notifyMessage(project, "项目结构中没有grandle配置文件。", 235 "当前项目结构不支持", 236 NotificationType.WARNING); 237 return true; 238 } 239 try { 240 properties.load(new FileInputStream(gradlePath)); 241 } catch (IOException e) { 242 GenNotification.notifyMessage(project, e.getMessage(), "提示", NotificationType.ERROR); 243 LOG.error(String.format("Can not load file :%s . %s", gradlePath, e)); 244 return true; 245 } 246 String ohosSDK = properties.getProperty("compileSdkVersion"); 247 248 if (ohosSDK != null && Integer.parseInt(ohosSDK) < COMPILE_SDK_VERSION) { 249 GenNotification.notifyMessage(project, "SKD版本过低,NAPI仅支持5.0及以上版本", 250 "提示", 251 NotificationType.WARNING); 252 return true; 253 } 254 return false; 255 } 256 readWholeFile(String fileName)257 private String readWholeFile(String fileName) { 258 File file = new File(fileName); 259 byte[] rdBuf = new byte[(int) file.length()]; 260 try(FileInputStream in = new FileInputStream(file)) { 261 in.read(rdBuf); 262 return new String(rdBuf, "UTF-8"); 263 } catch (FileNotFoundException foundException) { 264 LOG.error(String.format("File %s does not exist.", fileName)); 265 } catch (IOException ioException) { 266 LOG.error(String.format("Failed to read file %s. Error: %s", fileName, ioException)); 267 } 268 return ""; 269 } 270 } 271