refactor: replace not to !

This commit is contained in:
2023-11-04 02:06:20 +08:00
parent 2337d5710b
commit 33abb4bafa
10 changed files with 26 additions and 26 deletions

View File

@@ -48,7 +48,7 @@ internal class ProjectDescriptor private constructor() {
it.type = Type.SETTINGS it.type = Type.SETTINGS
it.name = name.noBlank() ?: settings.rootProject.name it.name = name.noBlank() ?: settings.rootProject.name
it.currentDir = (if (isRootProject) settings.rootProject else settings.findProject(name))?.projectDir it.currentDir = (if (isRootProject) settings.rootProject else settings.findProject(name))?.projectDir
?: SError.make("Project \"$name\" not found${if (name.startsWith(":").not()) ", $subProjectNotice" else ""}") ?: SError.make("Project \"$name\" not found${if (!name.startsWith(":")) ", $subProjectNotice" else ""}")
it.rootDir = settings.rootDir it.rootDir = settings.rootDir
it.homeDir = settings.gradle.gradleUserHomeDir it.homeDir = settings.gradle.gradleUserHomeDir
} }

View File

@@ -37,7 +37,7 @@ import org.gradle.api.plugins.ExtensionAware
*/ */
internal fun ExtensionAware.getOrCreate(name: String, clazz: Class<*>, vararg args: Any?) = name.toSafeExtName().let { sName -> internal fun ExtensionAware.getOrCreate(name: String, clazz: Class<*>, vararg args: Any?) = name.toSafeExtName().let { sName ->
runCatching { extensions.create(sName, clazz, *args).asExtension() }.getOrElse { runCatching { extensions.create(sName, clazz, *args).asExtension() }.getOrElse {
if ((it is IllegalArgumentException && it.message?.startsWith("Cannot add extension with name") == true).not()) throw it if (!(it is IllegalArgumentException && it.message?.startsWith("Cannot add extension with name") == true)) throw it
runCatching { extensions.getByName(sName).asExtension() }.getOrNull() ?: SError.make("Create or get extension failed with name \"$sName\"") runCatching { extensions.getByName(sName).asExtension() }.getOrNull() ?: SError.make("Create or get extension failed with name \"$sName\"")
} }
} }
@@ -50,7 +50,7 @@ internal fun ExtensionAware.getOrCreate(name: String, clazz: Class<*>, vararg ar
*/ */
internal inline fun <reified T> ExtensionAware.getOrCreate(name: String, vararg args: Any?) = name.toSafeExtName().let { sName -> internal inline fun <reified T> ExtensionAware.getOrCreate(name: String, vararg args: Any?) = name.toSafeExtName().let { sName ->
runCatching { extensions.create(sName, T::class.java, *args) as T }.getOrElse { runCatching { extensions.create(sName, T::class.java, *args) as T }.getOrElse {
if ((it is IllegalArgumentException && it.message?.startsWith("Cannot add extension with name") == true).not()) throw it if (!(it is IllegalArgumentException && it.message?.startsWith("Cannot add extension with name") == true)) throw it
runCatching { extensions.getByName(sName) as? T? }.getOrNull() ?: SError.make("Create or get extension failed with name \"$sName\"") runCatching { extensions.getByName(sName) as? T? }.getOrNull() ?: SError.make("Create or get extension failed with name \"$sName\"")
} }
} }

View File

@@ -44,7 +44,7 @@ internal fun Project.fullName(isUseColon: Boolean = true): String {
project.parent?.also { if (it != it.rootProject) fetchChild(it) } project.parent?.also { if (it != it.rootProject) fetchChild(it) }
baseNames.add(project.name) baseNames.add(project.name)
}; fetchChild(project = this) }; fetchChild(project = this)
return buildString { baseNames.onEach { append(":$it") }.clear() }.let { if (isUseColon && isRoot.not()) it else it.drop(1) } return buildString { baseNames.onEach { append(":$it") }.clear() }.let { if (isUseColon && !isRoot) it else it.drop(1) }
} }
/** /**

View File

@@ -474,13 +474,13 @@ open class SweetPropertyConfigureExtension internal constructor() {
/** 检查合法包名 */ /** 检查合法包名 */
fun String.checkingValidPackageName() { fun String.checkingValidPackageName() {
if (isNotBlank() && matches("^[a-zA-Z_][a-zA-Z0-9_]*(\\.[a-zA-Z_][a-zA-Z0-9_]*)*$".toRegex()).not()) if (isNotBlank() && !matches("^[a-zA-Z_][a-zA-Z0-9_]*(\\.[a-zA-Z_][a-zA-Z0-9_]*)*$".toRegex()))
SError.make("Invalid package name \"$this\"") SError.make("Invalid package name \"$this\"")
} }
/** 检查合法类名 */ /** 检查合法类名 */
fun String.checkingValidClassName() { fun String.checkingValidClassName() {
if (isNotBlank() && matches("^[a-zA-Z][a-zA-Z0-9_]*$".toRegex()).not()) if (isNotBlank() && !matches("^[a-zA-Z][a-zA-Z0-9_]*$".toRegex()))
SError.make("Invalid class name \"$this\"") SError.make("Invalid class name \"$this\"")
} }

View File

@@ -98,7 +98,7 @@ internal class PropertiesAccessorsGenerator {
*/ */
private inline fun noRepeated(vararg tags: String, block: () -> Unit) { private inline fun noRepeated(vararg tags: String, block: () -> Unit) {
val allTag = tags.joinToString("-") val allTag = tags.joinToString("-")
if (usedSuccessiveTags.contains(allTag).not()) block() if (!usedSuccessiveTags.contains(allTag)) block()
usedSuccessiveTags.add(allTag) usedSuccessiveTags.add(allTag)
} }
@@ -313,12 +313,12 @@ internal class PropertiesAccessorsGenerator {
} }
noRepeated(className, nextMethodName, nextClassName) { noRepeated(className, nextMethodName, nextClassName) {
getOrCreateClassSpec(className, accessorsName).apply { getOrCreateClassSpec(className, accessorsName).apply {
if (isPreLastIndex.not()) { if (!isPreLastIndex) {
addSuccessiveField(nextAccessorsName, nextClassName) addSuccessiveField(nextAccessorsName, nextClassName)
addSuccessiveMethod(nextAccessorsName, nextMethodName, nextClassName) addSuccessiveMethod(nextAccessorsName, nextMethodName, nextClassName)
} else addFinalValueMethod(successiveName, lastMethodName, lastClassName, value) } else addFinalValueMethod(successiveName, lastMethodName, lastClassName, value)
} }
if (isPreLastIndex.not()) preAddConstructorSpecNames.add(className to nextClassName) if (!isPreLastIndex) preAddConstructorSpecNames.add(className to nextClassName)
} }
} }
} }

View File

@@ -52,7 +52,7 @@ internal fun Any.parseTypedValue(isAutoConversion: Boolean): Pair<KClass<*>, Str
it.drop(1).dropLast(1) it.drop(1).dropLast(1)
} else it.replace("\"", "\\\"") } else it.replace("\"", "\\\"")
} }
if (isAutoConversion.not()) return Pair(String::class, "\"$valueString\"") if (!isAutoConversion) return Pair(String::class, "\"$valueString\"")
val typeSpec = when { val typeSpec = when {
isStringType -> String::class isStringType -> String::class
valueString.trim().toIntOrNull() != null -> Int::class valueString.trim().toIntOrNull() != null -> Int::class
@@ -62,6 +62,6 @@ internal fun Any.parseTypedValue(isAutoConversion: Boolean): Pair<KClass<*>, Str
valueString.trim() == "true" || valueString.trim() == "false" -> Boolean::class valueString.trim() == "true" || valueString.trim() == "false" -> Boolean::class
else -> String::class else -> String::class
}; return Pair(typeSpec, if (typeSpec == String::class) "\"$valueString\"" else valueString.let { }; return Pair(typeSpec, if (typeSpec == String::class) "\"$valueString\"" else valueString.let {
if (typeSpec == Long::class && it.endsWith("L").not()) "${it}L" else it if (typeSpec == Long::class && !it.endsWith("L")) "${it}L" else it
}) })
} }

View File

@@ -73,7 +73,7 @@ internal object PluginUpdateHelper {
* @return [String] * @return [String]
*/ */
private fun String.findLatest() = runCatching { private fun String.findLatest() = runCatching {
if ((contains("<metadata ") || contains("<metadata>")).not() || endsWith("</metadata>").not()) return@runCatching "" if (!(contains("<metadata ") || contains("<metadata>")) || !endsWith("</metadata>")) return@runCatching ""
DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(InputSource(StringReader(this))).let { document -> DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(InputSource(StringReader(this))).let { document ->
document.getElementsByTagName("latest")?.let { if (it.length > 0) it.item(0)?.textContent ?: "" else "" } document.getElementsByTagName("latest")?.let { if (it.length > 0) it.item(0)?.textContent ?: "" else "" }
} }

View File

@@ -103,7 +103,7 @@ internal object PropertiesDeployHelper {
internal fun initialize(settings: Settings, configs: ISweetPropertyConfigs) { internal fun initialize(settings: Settings, configs: ISweetPropertyConfigs) {
this.configs = configs this.configs = configs
checkingConfigsModified(settings) checkingConfigsModified(settings)
if (configs.isEnable.not()) return if (!configs.isEnable) return
generatedAccessors(settings) generatedAccessors(settings)
} }
@@ -112,7 +112,7 @@ internal object PropertiesDeployHelper {
* @param rootProject 当前根项目 * @param rootProject 当前根项目
*/ */
internal fun resolve(rootProject: Project) { internal fun resolve(rootProject: Project) {
if (configs.isEnable.not()) return if (!configs.isEnable) return
resolveAccessors(rootProject) resolveAccessors(rootProject)
} }
@@ -121,7 +121,7 @@ internal object PropertiesDeployHelper {
* @param rootProject 当前根项目 * @param rootProject 当前根项目
*/ */
internal fun deploy(rootProject: Project) { internal fun deploy(rootProject: Project) {
if (configs.isEnable.not()) return if (!configs.isEnable) return
deployAccessors(rootProject) deployAccessors(rootProject)
deploySourcesCode(rootProject) deploySourcesCode(rootProject)
} }
@@ -153,13 +153,13 @@ internal object PropertiesDeployHelper {
allConfigs.add(configs.global.buildScript) allConfigs.add(configs.global.buildScript)
} }
configs.projects.forEach { (name, subConfigs) -> configs.projects.forEach { (name, subConfigs) ->
if (subConfigs.buildScript.isEnable.not()) return@forEach if (!subConfigs.buildScript.isEnable) return@forEach
allProperties.add(generatedProperties(subConfigs.buildScript, ProjectDescriptor.create(settings, name))) allProperties.add(generatedProperties(subConfigs.buildScript, ProjectDescriptor.create(settings, name)))
allConfigs.add(subConfigs.buildScript) allConfigs.add(subConfigs.buildScript)
} }
if (isConfigsModified.not() && if (!isConfigsModified &&
allProperties == cachedSettingsProperties && allProperties == cachedSettingsProperties &&
accessorsDir.resolve(accessorsPomData.relativePomPath).isEmpty().not() !accessorsDir.resolve(accessorsPomData.relativePomPath).isEmpty()
) return ) return
cachedSettingsProperties = allProperties cachedSettingsProperties = allProperties
accessorsGenerator.build(allConfigs, allProperties).compile(accessorsPomData, accessorsDir.absolutePath, accessorsGenerator.compileStubFiles) accessorsGenerator.build(allConfigs, allProperties).compile(accessorsPomData, accessorsDir.absolutePath, accessorsGenerator.compileStubFiles)
@@ -170,7 +170,7 @@ internal object PropertiesDeployHelper {
* @param rootProject 当前根项目 * @param rootProject 当前根项目
*/ */
private fun resolveAccessors(rootProject: Project) { private fun resolveAccessors(rootProject: Project) {
if (accessorsDir.resolve(accessorsPomData.relativePomPath).isEmpty().not()) if (!accessorsDir.resolve(accessorsPomData.relativePomPath).isEmpty())
rootProject.addDependencyToBuildScript(accessorsDir.absolutePath, accessorsPomData) rootProject.addDependencyToBuildScript(accessorsDir.absolutePath, accessorsPomData)
} }
@@ -182,7 +182,7 @@ internal object PropertiesDeployHelper {
/** 部署扩展方法 */ /** 部署扩展方法 */
fun Project.deploy() { fun Project.deploy() {
val configs = configs.with(this).buildScript val configs = configs.with(this).buildScript
if (configs.isEnable.not()) return if (!configs.isEnable) return
val className = accessorsGenerator.propertiesClass(configs.name) val className = accessorsGenerator.propertiesClass(configs.name)
val accessorsClass = loadBuildScriptClass(className) ?: SError.make( val accessorsClass = loadBuildScriptClass(className) ?: SError.make(
""" """
@@ -206,9 +206,9 @@ internal object PropertiesDeployHelper {
fun Project.generate() { fun Project.generate() {
val configs = configs.with(this).sourcesCode val configs = configs.with(this).sourcesCode
val outputDir = file(configs.generateDirPath) val outputDir = file(configs.generateDirPath)
if (configs.isEnable.not()) return if (!configs.isEnable) return
val properties = generatedProperties(configs, ProjectDescriptor.create(project = this)) val properties = generatedProperties(configs, ProjectDescriptor.create(project = this))
if (isConfigsModified.not() && properties == cachedProjectProperties[fullName()] && outputDir.isEmpty().not()) { if (!isConfigsModified && properties == cachedProjectProperties[fullName()] && !outputDir.isEmpty()) {
if (configs.isEnable) configureSourceSets(project = this) if (configs.isEnable) configureSourceSets(project = this)
return return
}; outputDir.apply { if (exists()) deleteRecursively() } }; outputDir.apply { if (exists()) deleteRecursively() }

View File

@@ -52,7 +52,7 @@ internal fun String.parseUnixFileSeparator() = replace("\\", "/")
* - 如果文件不存在 - 返回 true * - 如果文件不存在 - 返回 true
* @return [Boolean] * @return [Boolean]
*/ */
internal fun File.isEmpty() = exists().not() || isDirectory.not() || listFiles().isNullOrEmpty() internal fun File.isEmpty() = !exists() || !isDirectory || listFiles().isNullOrEmpty()
/** 删除目录下的空子目录 */ /** 删除目录下的空子目录 */
internal fun File.deleteEmptyRecursively() { internal fun File.deleteEmptyRecursively() {

View File

@@ -59,7 +59,7 @@ internal object CodeCompiler {
if (files.isEmpty()) { if (files.isEmpty()) {
if (outputDir.exists()) outputDir.deleteRecursively() if (outputDir.exists()) outputDir.deleteRecursively()
return return
} else outputDir.also { if (it.exists().not()) it.mkdirs() } } else outputDir.also { if (!it.exists()) it.mkdirs() }
val outputBuildDir = "$outputDirPath/build".toFile().also { if (it.exists()) it.deleteRecursively(); it.mkdirs() } val outputBuildDir = "$outputDirPath/build".toFile().also { if (it.exists()) it.deleteRecursively(); it.mkdirs() }
val outputClassesDir = "${outputBuildDir.absolutePath}/classes".toFile().apply { mkdirs() } val outputClassesDir = "${outputBuildDir.absolutePath}/classes".toFile().apply { mkdirs() }
val outputSourcesDir = "${outputBuildDir.absolutePath}/sources".toFile().apply { mkdirs() } val outputSourcesDir = "${outputBuildDir.absolutePath}/sources".toFile().apply { mkdirs() }
@@ -99,7 +99,7 @@ internal object CodeCompiler {
* @param sourcesDir 编译源码目录 * @param sourcesDir 编译源码目录
*/ */
private fun createJarAndPom(pomData: MavenPomData, outputDir: File, buildDir: File, classesDir: File, sourcesDir: File) { private fun createJarAndPom(pomData: MavenPomData, outputDir: File, buildDir: File, classesDir: File, sourcesDir: File) {
val pomDir = outputDir.resolve(pomData.relativePomPath).also { if (it.exists().not()) it.mkdirs() } val pomDir = outputDir.resolve(pomData.relativePomPath).also { if (!it.exists()) it.mkdirs() }
packageToJar(classesDir, pomDir, pomData, isSourcesJar = false) packageToJar(classesDir, pomDir, pomData, isSourcesJar = false)
packageToJar(sourcesDir, pomDir, pomData, isSourcesJar = true) packageToJar(sourcesDir, pomDir, pomData, isSourcesJar = true)
writePom(pomDir, pomData) writePom(pomDir, pomData)
@@ -159,7 +159,7 @@ internal object CodeCompiler {
* @throws IllegalStateException 如果编译输出目录不存在 * @throws IllegalStateException 如果编译输出目录不存在
*/ */
private fun packageToJar(buildDir: File, outputDir: File, pomData: MavenPomData, isSourcesJar: Boolean) { private fun packageToJar(buildDir: File, outputDir: File, pomData: MavenPomData, isSourcesJar: Boolean) {
if (buildDir.exists().not()) SError.make("Jar file output path not found: ${buildDir.absolutePath}") if (!buildDir.exists()) SError.make("Jar file output path not found: ${buildDir.absolutePath}")
val jarFile = outputDir.resolve("${pomData.artifactId}-${pomData.version}${if (isSourcesJar) "-sources" else ""}.jar") val jarFile = outputDir.resolve("${pomData.artifactId}-${pomData.version}${if (isSourcesJar) "-sources" else ""}.jar")
if (jarFile.exists()) jarFile.delete() if (jarFile.exists()) jarFile.delete()
ZipFile(jarFile).addFolder(buildDir, ZipParameters().apply { isIncludeRootFolder = false }) ZipFile(jarFile).addFolder(buildDir, ZipParameters().apply { isIncludeRootFolder = false })