From 3f786baf6c227094f75c12d251c42436e5cdf1b7 Mon Sep 17 00:00:00 2001 From: Suomm <1474983351@qq.com> Date: Fri, 23 Jun 2023 15:59:14 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E6=8F=90=E5=8F=96=E5=B7=A5?= =?UTF-8?q?=E5=85=B7=E7=B1=BB=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mybatisflex/processor/util/FileUtil.java | 64 ++++++++++ .../mybatisflex/processor/util/StrUtil.java | 114 ++++++++++++++++++ 2 files changed, 178 insertions(+) create mode 100644 mybatis-flex-processor/src/main/java/com/mybatisflex/processor/util/FileUtil.java create mode 100644 mybatis-flex-processor/src/main/java/com/mybatisflex/processor/util/StrUtil.java diff --git a/mybatis-flex-processor/src/main/java/com/mybatisflex/processor/util/FileUtil.java b/mybatis-flex-processor/src/main/java/com/mybatisflex/processor/util/FileUtil.java new file mode 100644 index 00000000..60e6e8cf --- /dev/null +++ b/mybatis-flex-processor/src/main/java/com/mybatisflex/processor/util/FileUtil.java @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.mybatisflex.processor.util; + +import java.io.File; + +/** + * 文件工具类。 + * + * @author 王帅 + * @since 2023-06-22 + */ +public class FileUtil { + + private FileUtil() { + } + + public static boolean isFromTestSource(String path) { + return path.contains("test-sources") || path.contains("test-annotations"); + } + + public static boolean isAbsolutePath(String path) { + return path != null && (path.startsWith("/") || path.contains(":")); + } + + /** + * 获取项目的根目录,也就是根节点 pom.xml 所在的目录 + */ + public static String getProjectRootPath(String genFilePath) { + File file = new File(genFilePath); + int count = 20; + return getProjectRootPath(file, count); + } + + public static String getProjectRootPath(File file, int count) { + if (count <= 0) { + return null; + } + if (file.isFile()) { + return getProjectRootPath(file.getParentFile(), --count); + } else { + if (new File(file, "pom.xml").exists() && !new File(file.getParentFile(), "pom.xml").exists()) { + return file.getAbsolutePath(); + } else { + return getProjectRootPath(file.getParentFile(), --count); + } + } + } + +} \ No newline at end of file diff --git a/mybatis-flex-processor/src/main/java/com/mybatisflex/processor/util/StrUtil.java b/mybatis-flex-processor/src/main/java/com/mybatisflex/processor/util/StrUtil.java new file mode 100644 index 00000000..15e7c0fd --- /dev/null +++ b/mybatis-flex-processor/src/main/java/com/mybatisflex/processor/util/StrUtil.java @@ -0,0 +1,114 @@ +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.mybatisflex.processor.util; + +/** + * 字符串工具类。 + * + * @author 王帅 + * @since 2023-06-22 + */ +@SuppressWarnings("all") +public class StrUtil { + + private StrUtil() { + } + + /** + * com.mybatisflex.test.entity.Account -> Account + */ + public static String getClassName(String str) { + return str.substring(str.lastIndexOf(".") + 1); + } + + public static boolean isBlank(String str) { + return str == null || str.trim().length() == 0; + } + + public static String camelToUnderline(String str) { + if (isBlank(str)) { + return ""; + } + int len = str.length(); + StringBuilder sb = new StringBuilder(len); + for (int i = 0; i < len; i++) { + char c = str.charAt(i); + if (Character.isUpperCase(c) && i > 0) { + sb.append('_'); + } + sb.append(Character.toLowerCase(c)); + } + return sb.toString(); + } + + public static String firstCharToLowerCase(String str) { + char firstChar = str.charAt(0); + if (firstChar >= 'A' && firstChar <= 'Z') { + char[] arr = str.toCharArray(); + arr[0] += ('a' - 'A'); + return new String(arr); + } + return str; + } + + public static String firstCharToUpperCase(String str) { + char firstChar = str.charAt(0); + if (firstChar >= 'a' && firstChar <= 'z') { + char[] arr = str.toCharArray(); + arr[0] -= ('a' - 'A'); + return new String(arr); + } + return str; + } + + public static String buildFieldName(String name, String style) { + if ("upperCase".equalsIgnoreCase(style)) { + return camelToUnderline(name).toUpperCase(); + } else if ("lowerCase".equalsIgnoreCase(style)) { + return camelToUnderline(name).toLowerCase(); + } else if ("upperCamelCase".equalsIgnoreCase(style)) { + return firstCharToUpperCase(name); + } else { + //lowerCamelCase + return firstCharToLowerCase(name); + } + } + + public static String buildTableDefPackage(String entityClass) { + StringBuilder guessPackage = new StringBuilder(); + if (!entityClass.contains(".")) { + guessPackage.append("table"); + } else { + guessPackage.append(entityClass, 0, entityClass.lastIndexOf(".")).append(".table"); + } + return guessPackage.toString(); + } + + public static String buildMapperPackage(String entityClass) { + if (!entityClass.contains(".")) { + return "mapper"; + } else { + String entityPackage = entityClass.substring(0, entityClass.lastIndexOf(".")); + if (entityPackage.contains(".")) { + return entityPackage.substring(0, entityPackage.lastIndexOf(".")) + ".mapper"; + } else { + return "mapper"; + } + } + } + +} \ No newline at end of file