Browse Source

code improvements

milan jurkulák 2 years ago
parent
commit
7a3799901b

+ 4 - 0
app/build.gradle.kts

@@ -13,7 +13,11 @@ plugins {
 appConfig {
     namespace = "org.mjdev.tvapp"
     description = "Smart TV android app for any android device"
+
+    createDocumentation = true // ok
+    createWebApp = true // todo, false does not affect
     createWebSiteFromGit = false
+
     // todo
     default {
 //        buildConfigString(

+ 52 - 0
buildSrc/src/main/kotlin/org/mjdev/gradle/extensions/FileExt.kt

@@ -11,6 +11,8 @@
 package org.mjdev.gradle.extensions
 
 import org.gradle.internal.impldep.org.apache.commons.io.FileUtils
+import java.io.BufferedInputStream
+import java.io.BufferedOutputStream
 import java.io.File
 import java.nio.file.Files
 import java.nio.file.LinkOption
@@ -21,6 +23,56 @@ import java.nio.file.attribute.BasicFileAttributes
 import java.time.Instant
 import java.time.LocalDateTime
 import java.time.ZoneId
+import java.util.zip.ZipEntry
+import java.util.zip.ZipOutputStream
+
+private const val DEFAULT_BUFFER_SIZE: Int = 8 * 1024
+
+fun File.bufferedOutputStream(size: Int = kotlin.io.DEFAULT_BUFFER_SIZE) =
+    BufferedOutputStream(this.outputStream(), size)
+
+fun File.zipOutputStream(size: Int = kotlin.io.DEFAULT_BUFFER_SIZE) =
+    ZipOutputStream(this.bufferedOutputStream(size))
+
+fun File.bufferedInputStream(size: Int = kotlin.io.DEFAULT_BUFFER_SIZE) =
+    BufferedInputStream(this.inputStream(), size)
+
+fun zipArchive(
+    files: List<File>,
+    destination: File,
+    excludes: List<String> = emptyList() // todo
+) {
+    destination.zipOutputStream().use { zipStream ->
+        zipArchive(files, zipStream, "", excludes)
+    }
+}
+
+private fun zipArchive(
+    files: List<File>,
+    zipStream: ZipOutputStream,
+    dirName:String="",
+    excludes: List<String> = emptyList() // todo
+) {
+    files.forEach { file ->
+        if (file.exists()) {
+            val fileName = if (file.isDirectory) {
+                "${if (dirName.isNotEmpty()) dirName else ""}${file.name}/"
+            } else {
+                "${if (dirName.isNotEmpty()) dirName else ""}${file.name}"
+            }
+            zipStream.putNextEntry(ZipEntry(fileName))
+            if (file.isFile) {
+                file.bufferedInputStream().use { bis ->
+                    bis.copyTo(zipStream)
+                }
+            } else {
+                file.listFiles()?.let {
+                    zipArchive(it.toList(), zipStream, fileName, excludes)
+                }
+            }
+        }
+    }
+}
 
 fun File.file(
     name: String

+ 0 - 1
buildSrc/src/main/kotlin/org/mjdev/gradle/extensions/TaskExt.kt

@@ -262,7 +262,6 @@ inline fun <reified T : Task> Task.shouldRunAfter() = tasks.forEach { task ->
     }
 }
 
-
 fun Task.runAfterAssembleTask() {
     project.assembleTasks.forEach { t -> t.finalizedBy(this) }
 }

+ 56 - 54
buildSrc/src/main/kotlin/org/mjdev/gradle/plugin/AppPlugin.kt

@@ -9,6 +9,7 @@
 package org.mjdev.gradle.plugin
 
 import com.android.build.api.dsl.ApplicationExtension
+import com.android.build.gradle.internal.api.BaseVariantOutputImpl
 import io.gitlab.arturbosch.detekt.DetektPlugin
 import io.gitlab.arturbosch.detekt.extensions.DetektExtension
 import org.gradle.api.Project
@@ -55,6 +56,8 @@ import org.mjdev.gradle.tasks.WebServiceCreateTask
 import org.mjdev.gradle.tasks.CreatePropsTask
 import org.kordamp.gradle.plugin.markdown.MarkdownPlugin
 import org.mjdev.gradle.extensions.assembleTasks
+import org.mjdev.gradle.extensions.markDownToHtmlTask
+import org.mjdev.gradle.extensions.variants
 import org.mjdev.gradle.extensions.zipReleaseCreateTask
 import org.mjdev.gradle.tasks.ZipReleaseCreateTask
 
@@ -195,35 +198,35 @@ class AppPlugin : BasePlugin() {
             }
         }
         afterEvaluate {
-        detektTask {
-            if (appConfig.autoCorrectCode) {
-                mustRunAfter(assembleTasks)
-                runAfterAssembleTask()
-            }
-        }
-        dokkaTask {
-            outputDirectory.set(rootDir.resolve(appConfig.documentationDir))
-            moduleName.set(projectName)
-            suppressObviousFunctions.set(false)
-            dokkaSourceSets.configureEach {
-                offlineMode.set(false)
-                includeNonPublic.set(false)
-                skipDeprecated.set(false)
-                failOnWarning.set(appConfig.failOnDocumentationWarning)
-                reportUndocumented.set(appConfig.reportUndocumentedFiles)
-                skipEmptyPackages.set(false)
-                platform.set(Platform.jvm)
-                jdkVersion.set(AppConfig.javaVersion.asInt())
-                noStdlibLink.set(false)
-                noJdkLink.set(false)
-                noAndroidSdkLink.set(false)
+            detektTask {
+                if (appConfig.autoCorrectCode) {
+                    mustRunAfter(assembleTasks)
+                    runAfterAssembleTask()
+                }
             }
-            if (appConfig.createDocumentation) {
-                mustRunAfter(assembleTasks)
-                runAfterAssembleTask()
+            dokkaTask {
+                outputDirectory.set(rootDir.resolve(appConfig.documentationDir))
+                moduleName.set(projectName)
+                suppressObviousFunctions.set(false)
+                dokkaSourceSets.configureEach {
+                    offlineMode.set(false)
+                    includeNonPublic.set(false)
+                    skipDeprecated.set(false)
+                    failOnWarning.set(appConfig.failOnDocumentationWarning)
+                    reportUndocumented.set(appConfig.reportUndocumentedFiles)
+                    skipEmptyPackages.set(false)
+                    platform.set(Platform.jvm)
+                    jdkVersion.set(AppConfig.javaVersion.asInt())
+                    noStdlibLink.set(false)
+                    noJdkLink.set(false)
+                    noAndroidSdkLink.set(false)
+                }
+                if (appConfig.createDocumentation) {
+                    mustRunAfter(assembleTasks)
+                    runAfterAssembleTask()
+                }
             }
-        }
-//        markDownToHtmlTask {
+            markDownToHtmlTask {
 //            enabled = appConfig.createWebSiteFromGit
 //                sourceDir = rootDir
 //                outputDir = rootDir.resolve("web")
@@ -245,24 +248,25 @@ class AppPlugin : BasePlugin() {
 //                if(createWebSiteFromGit) {
 //                    runAfterAssembleTask()
 //                }
-//        }
-        releaseNotesCreateTask {
-            if (appConfig.createReleaseNotes) {
-                runAfterAssembleTask()
             }
-        }
-        webServiceCreateTask {
-            domain = "localhost"
-            servicePort = 2222
-            serviceName = project.name
-            serviceVersion = project.versionName
-            serviceCompany = "mjdev"
-            serviceLicense = ""
-            if (appConfig.createWebApp) {
-                runAfterAssembleTask()
+            releaseNotesCreateTask {
+                if (appConfig.createReleaseNotes) {
+                    runAfterAssembleTask()
+                }
+            }
+            webServiceCreateTask {
+                domain = "localhost"
+                servicePort = 2222
+                serviceName = project.name
+                serviceVersion = project.versionName
+                serviceCompany = "mjdev"
+                serviceLicense = ""
+                if (appConfig.createWebApp) {
+                    runAfterAssembleTask()
+                }
             }
-        }
             zipReleaseCreateTask {
+                // todo files here
                 if (appConfig.createZipRelease) {
                     runAfterAssembleTask()
                 }
@@ -286,6 +290,16 @@ class AppPlugin : BasePlugin() {
                 ignoreFailures = appConfig.ignoreCodeFailures
                 reporters = arrayOf("checkstyle", "plain")
             }
+            if (appConfig.renameApkOutputByAppID) {
+                this.variants.forEach { variant ->
+                    variant.outputs.map {
+                        it as BaseVariantOutputImpl
+                    }.forEach { output ->
+                        val outputFileName = "$applicationId-$versionName.apk"
+                        output.outputFileName = outputFileName
+                    }
+                }
+            }
         }
         dependencyLocking {
             lockMode.set(LockMode.STRICT)
@@ -404,15 +418,3 @@ class AppPlugin : BasePlugin() {
         }
     }
 }
-
-// todo task
-//if (appConfig.renameApkOutputByAppID) {
-//    applicationVariants.all {
-//        outputs.map {
-//            it as BaseVariantOutputImpl
-//        }.forEach { output ->
-//            val outputFileName = "$applicationId-$versionName.apk"
-//            output.outputFileName = outputFileName
-//        }
-//    }
-//}

+ 2 - 2
buildSrc/src/main/kotlin/org/mjdev/gradle/tasks/WebServiceCreateTask.kt

@@ -108,9 +108,9 @@ open class WebServiceCreateTask : BaseTask() {
                 "  \"scripts\": {\n" +
                 "    \"start\": \"export PORT=$servicePort && node $appName\",\n" +
                 "    \"dev\": \"nodemon\",\n" +
-                "    \"build\": \"npm install && npm run build\",\n" +
+                "    \"build\": \"npm install.sh && npm run build\",\n" +
                 "    \"serve\": \"node $appName\",\n" +
-                "    \"release\": \"npm install && npm run build && export PORT=$servicePort && node $appName\"\n" +
+                "    \"release\": \"npm install.sh && npm run build && export PORT=$servicePort && node $appName\"\n" +
                 "  },\n" +
                 "  \"author\": \"$serviceAuthor $serviceCompany (c) ${Date().year}\",\n" +
                 "  \"license\": \"$serviceLicense\",\n" +

+ 24 - 25
buildSrc/src/main/kotlin/org/mjdev/gradle/tasks/ZipReleaseCreateTask.kt

@@ -9,35 +9,39 @@
 package org.mjdev.gradle.tasks
 
 import org.mjdev.gradle.base.BaseTask
-import org.mjdev.gradle.extensions.file
 import org.mjdev.gradle.extensions.projectVersion
+import org.mjdev.gradle.extensions.zipArchive
 
 open class ZipReleaseCreateTask : BaseTask() {
 
-    private val releaseFile = project.rootProject
-        .file("documentation")
-        .file("release-notes-$projectVersion.md")
-
-    private val readmeFile = project.rootProject
-        .file("readme.md")
-
-    private val files = arrayListOf(
-        releaseFile,
-        readmeFile,
-        project.rootProject.file("dependencies.txt"),
-        project.rootProject.file("reports"),
-        project.rootProject.file("web"),
-        project.file("build").file("outputs").file("apk").file("debug"),
-        project.file("build").file("outputs").file("apk").file("release"),
+    private val zipFileName = "release.zip"
+
+    @Suppress("DEPRECATION")
+    private val files = mutableListOf(
+        project.rootDir
+            .resolve("documentation"),
+        project.rootDir
+            .resolve("readme.md"),
+        project.rootDir
+            .resolve("reports"),
+        project.rootDir
+            .resolve("web"),
+        project.buildDir
+            .resolve("outputs")
+            .resolve("apk")
+            .resolve("debug"),
+        project.buildDir
+            .resolve("outputs")
+            .resolve("apk")
+            .resolve("release"),
     )
 
-    private val zipFileName = "zipFileName"
-    private val zipFile = project.rootDir.resolve(zipFileName)
+    private val zipFile
+        get() = project.rootDir.resolve(zipFileName)
 
     init {
         group = "mjdev"
         description = "This task makes archive from build artifacts and wiki, and logs."
-
         outputs.upToDateWhen { false }
     }
 
@@ -48,12 +52,7 @@ open class ZipReleaseCreateTask : BaseTask() {
     }
 
     override fun onAssemble() {
-//        from(files)
-//        include("*", "*/*", "*/**")
-//        includeEmptyDirs = true
-//        destinationDirectory.set(project.rootProject.rootDir)
-//        archiveFileName.set("release.zip")
-        // todo
+        zipArchive(files, zipFile)
     }
 
 }

+ 5 - 1
documentation/release-notes-1.2.7.md

@@ -1,4 +1,8 @@
-** Release notes 1.0.0 - 1. 5. 2024, 21:24:41
+** Release notes 1.0.0 - 1. 5. 2024, 23:08:08
+
+* 01.05.2024
+
+* [milan jurkulák] : code improvements 
 
 * 30.04.2024
 

+ 0 - 0
web/install.kts


+ 2 - 2
web/install → web/install.sh

@@ -10,7 +10,7 @@ service_name="app"
 # store actual dir
 pwd=$(pwd)
 # sw needed
-sudo apt-get install certbot nginx-full nodejs npm ufw yarn
+sudo apt-get install.sh certbot nginx-full nodejs npm ufw yarn
 # stop service
 sudo systemctl stop "service-$service_name"
 sudo systemctl disable "service-$service_name"
@@ -31,7 +31,7 @@ sudo ln -s "/opt/services/$service_name/$service_name.nginx" "/etc/nginx/sites-e
 # go to directory
 cd "/opt/services/$service_name" || exit
 # build
-sudo npm install
+sudo npm install.sh
 sudo npm audit fix
 sudo npm upgrade
 #sudo npm run build

+ 0 - 0
web/uninstall.kts


+ 0 - 0
web/uninstall → web/uninstall.sh