Skip to content

Commit

Permalink
打包配置优化
Browse files Browse the repository at this point in the history
  • Loading branch information
aiselp committed Sep 15, 2023
1 parent 7f86d2b commit dfcf191
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public Single<? extends ExplorerPage> getExplorerPage(ExplorerPage page) {
return listFiles(new PFile(path))
.collectInto(createExplorerPage(path, parent), (p, file) -> {
if (file.isDirectory()) {
ProjectConfig projectConfig = ProjectConfig.Companion.fromProjectDir(file.getPath());
ProjectConfig projectConfig = ProjectConfig.Companion.fromProject(file);
if (projectConfig != null) {
p.addChild(new ExplorerProjectPage(file, parent, projectConfig));
return;
Expand Down
47 changes: 25 additions & 22 deletions app/src/main/java/org/autojs/autojs/ui/build/BuildViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.autojs.autojs.Pref
import org.autojs.autoxjs.R
import org.autojs.autojs.build.ApkBuilder
import org.autojs.autojs.build.ApkBuilderPluginHelper
import org.autojs.autojs.build.ApkKeyStore
Expand All @@ -35,6 +34,7 @@ import org.autojs.autojs.model.explorer.ExplorerFileItem
import org.autojs.autojs.model.explorer.Explorers
import org.autojs.autojs.model.script.ScriptFile
import org.autojs.autojs.tool.*
import org.autojs.autoxjs.R
import java.io.File
import java.net.URLDecoder

Expand All @@ -57,6 +57,7 @@ class BuildViewModelFactory(
*/
class BuildViewModel(private val app: Application, private var source: String) :
AndroidViewModel(app) {
private val mainScope = CoroutineScope(Dispatchers.Main)

companion object {
const val TAG = "BuildViewModel"
Expand Down Expand Up @@ -198,20 +199,21 @@ class BuildViewModel(private val app: Application, private var source: String) :
}
) {
syncToProjectConfig()
CoroutineScope(Dispatchers.Main).launch {
mainScope.launch {
writeProjectConfigAndRefreshView()
onCompletion()
}
}

suspend fun writeProjectConfigAndRefreshView() {
private suspend fun writeProjectConfigAndRefreshView() {
withContext(Dispatchers.IO) {
saveLogo()
saveSplashIcon()
PFiles.write(
ProjectConfig.configFileOfDir(directory!!, configName),
projectConfig.toJson()
)
println(projectConfig.toJson())
}
withContext(Dispatchers.Main) {
oldProjectConfig = projectConfig.copy()
Expand All @@ -220,13 +222,13 @@ class BuildViewModel(private val app: Application, private var source: String) :
}
}

val configName: String
private val configName: String
get() = if (isSingleFile) {
//test.js对应test_config.json
PFiles.getNameWithoutExtension(source) + "_config.json"
} else ProjectConfig.CONFIG_FILE_NAME

fun getConfigName1(isSingleFile: Boolean = this.isSingleFile): String {
private fun getConfigName1(isSingleFile: Boolean = this.isSingleFile): String {
return if (isSingleFile) {
//test.js对应test_config.json
PFiles.getNameWithoutExtension(source) + "_config.json"
Expand All @@ -236,7 +238,7 @@ class BuildViewModel(private val app: Application, private var source: String) :
/**
* 从viewModel保存配置
*/
fun syncToProjectConfig() {
private fun syncToProjectConfig() {
if (
projectConfig.mainScript.isNullOrEmpty()
&& source.isNotEmpty()
Expand Down Expand Up @@ -279,7 +281,7 @@ class BuildViewModel(private val app: Application, private var source: String) :
}
}

fun syncViewModelByConfig(projectConfig: ProjectConfig) {
private fun syncViewModelByConfig(projectConfig: ProjectConfig) {

projectConfig.sourcePath?.takeIf { it.isNotBlank() }?.let { sourcePath = it }
projectConfig.outputPath?.takeIf { it.isNotBlank() }?.let { outputPath = it }
Expand Down Expand Up @@ -409,7 +411,9 @@ class BuildViewModel(private val app: Application, private var source: String) :
var isRequiredMlKitOCRModels = false
projectConfig.libs.let {
when {
it.containsAll(Constant.Libraries.GOOGLE_ML_KIT_OCR) -> isRequiredMlKitOCRLibs = true
it.containsAll(Constant.Libraries.GOOGLE_ML_KIT_OCR) -> isRequiredMlKitOCRLibs =
true

it.containsAll(Constant.Libraries.PADDLE_OCR) -> isRequiredPaddleOCR = true
it.containsAll(Constant.Libraries.TESSERACT_OCR) -> isRequiredTesseractOCR = true
it.containsAll(Constant.Libraries.P7ZIP) -> isRequired7Zip = true
Expand All @@ -433,6 +437,7 @@ class BuildViewModel(private val app: Application, private var source: String) :
when (it) {
Constant.Permissions.ACCESSIBILITY_SERVICES -> isRequiredAccessibilityServices =
true

Constant.Permissions.BACKGROUND_START -> isRequiredBackgroundStart = true
Constant.Permissions.DRAW_OVERLAY -> isRequiredDrawOverlay = true
}
Expand Down Expand Up @@ -493,19 +498,15 @@ class BuildViewModel(private val app: Application, private var source: String) :
}
}

private suspend fun setSource(file: File) {
if (file.isFile) {
//如果是文件
private fun setSource(file: File) {
if (file.isFile) { //如果是文件
directory = file.parent
sourcePath = file.path
//尝试获取配置文件
oldProjectConfig =
ProjectConfig.fromProjectDirAsync(directory!!, configName)
} else {
//如果是目录
} else { //如果是目录
directory = source
oldProjectConfig = ProjectConfig.fromProjectDirAsync(file.path)
}
oldProjectConfig = ProjectConfig.fromProject(file)

oldProjectConfig?.let {
isOldProjectConfigExist = true
projectConfig = it.copy()
Expand Down Expand Up @@ -564,11 +565,9 @@ class BuildViewModel(private val app: Application, private var source: String) :
}


fun buildApk() {
CoroutineScope(Dispatchers.Main).launch {
syncToProjectConfig()
doBuildingApk()
}
fun buildApk() = mainScope.launch {
syncToProjectConfig()
doBuildingApk()
}

fun checkInputs(viewModel: BuildViewModel = this): Boolean {
Expand Down Expand Up @@ -628,15 +627,19 @@ class BuildViewModel(private val app: Application, private var source: String) :
ApkBuilder.BuildState.PREPARE -> {
buildDialogText = app.getString(R.string.apk_builder_prepare)
}

ApkBuilder.BuildState.BUILD -> {
buildDialogText = app.getString(R.string.apk_builder_build)
}

ApkBuilder.BuildState.SIGN -> {
buildDialogText = app.getString(R.string.apk_builder_sign)
}

ApkBuilder.BuildState.CLEAN -> {
buildDialogText = app.getString(R.string.apk_builder_clean)
}

ApkBuilder.BuildState.FINISH -> {
isShowBuildDialog = false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import com.bumptech.glide.request.RequestOptions
import com.google.android.material.textfield.TextInputLayout
import com.stardust.autojs.project.ProjectConfig
import com.stardust.autojs.project.ProjectConfig.Companion.configFileOfDir
import com.stardust.autojs.project.ProjectConfig.Companion.fromProjectDirAsync
import com.stardust.autojs.project.ProjectConfig.Companion.fromProject
import com.stardust.pio.PFiles.ensureDir
import com.stardust.pio.PFiles.write
import io.reactivex.Observable
Expand Down Expand Up @@ -98,7 +98,7 @@ open class ProjectConfigActivity : BaseActivity() {
}
mDirectory = File(dir)
lifecycleScope.launch {
mProjectConfig = fromProjectDirAsync(dir)
mProjectConfig = fromProject(mDirectory!!)
if (mProjectConfig == null) {
ThemeColorMaterialDialogBuilder(this@ProjectConfigActivity)
.title(R.string.text_invalid_project)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ import butterknife.BindView
import butterknife.ButterKnife
import butterknife.OnClick
import com.stardust.autojs.project.ProjectConfig
import com.stardust.autojs.project.ProjectConfig.Companion.fromProjectDirAsync
import com.stardust.autojs.project.ProjectConfig.Companion.fromProject
import com.stardust.autojs.project.ProjectLauncher
import com.stardust.pio.PFile
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.autojs.autojs.autojs.AutoJs
import org.autojs.autojs.model.explorer.ExplorerChangeEvent
import org.autojs.autojs.model.explorer.Explorers
Expand All @@ -24,6 +25,7 @@ import org.autojs.autojs.ui.build.ProjectConfigActivity
import org.autojs.autojs.ui.build.ProjectConfigActivity_
import org.autojs.autoxjs.R
import org.greenrobot.eventbus.Subscribe
import java.io.File

class ExplorerProjectToolbar : CardView {
private var mProjectConfig: ProjectConfig? = null
Expand Down Expand Up @@ -57,7 +59,10 @@ class ExplorerProjectToolbar : CardView {

fun setProject(dir: PFile) {
CoroutineScope(Dispatchers.Main).launch {
mProjectConfig = fromProjectDirAsync(dir.path)
withContext(Dispatchers.IO){
mProjectConfig = fromProject(File(dir.path))
}

if (mProjectConfig == null) {
visibility = GONE
return@launch
Expand Down
77 changes: 13 additions & 64 deletions autojs/src/main/java/com/stardust/autojs/project/ProjectConfig.kt
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package com.stardust.autojs.project

import android.content.Context
import android.graphics.Bitmap
import androidx.annotation.Keep
import com.google.gson.GsonBuilder
import com.google.gson.annotations.SerializedName
import com.stardust.app.GlobalAppContext
import com.stardust.autojs.R
import com.stardust.pio.PFiles
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import java.io.File
import java.util.zip.CRC32

Expand Down Expand Up @@ -54,6 +51,10 @@ data class ProjectConfig(
else File(this.projectDirectory, name).absolutePath
}

fun toJson(): String {
return GSON.toJson(this)
}

companion object {

const val CONFIG_FILE_NAME = "project.json"
Expand All @@ -64,7 +65,7 @@ data class ProjectConfig(
return if (!isValid(config)) null else config
}

fun isValid(config: ProjectConfig): Boolean {
private fun isValid(config: ProjectConfig): Boolean {
return with(config) {
!name.isNullOrBlank()
&& !packageName.isNullOrEmpty()
Expand All @@ -82,77 +83,25 @@ data class ProjectConfig(
}
}

fun fromFile(path: String): ProjectConfig? {
return try {
fromJson(File(path).readText())
} catch (e: Exception) {
fun fromProject(path: File): ProjectConfig? {
val file = with(path) {
if (isFile) return@with this
if (isDirectory) return@with File(this, CONFIG_FILE_NAME)
null
}
}

fun fromProjectDir(path: String): ProjectConfig? {
return fromFile(configFileOfDir(path))
}

fun fromProjectDir(path: String, configName: String): ProjectConfig? {
return fromFile(configFileOfDir(path, configName))
}

suspend fun fromAssetsAsync(context: Context, path: String): ProjectConfig? =
withContext(Dispatchers.IO) {
return@withContext try {
fromJson(context.assets.open(path).reader().use { it.readText() })
} catch (e: Exception) {
null
}
}

suspend fun fromFileAsync(path: String): ProjectConfig? = withContext(Dispatchers.IO) {
return@withContext try {
fromJson(File(path).readText())
} catch (e: Exception) {
return try {
file?.let { fromJson(it.readText()) }
} catch (_: Exception) {
null
}
}

suspend fun fromProjectDirAsync(path: String): ProjectConfig? = withContext(Dispatchers.IO) {
return@withContext fromFile(configFileOfDir(path))
}

suspend fun fromProjectDirAsync(path: String, configName: String): ProjectConfig? =
withContext(Dispatchers.IO) {
return@withContext fromFile(configFileOfDir(path, configName))
}

fun configFileOfDir(projectDir: String): String {
return PFiles.join(projectDir, CONFIG_FILE_NAME)
}

fun configFileOfDir(projectDir: String, configName: String): String {
fun configFileOfDir(projectDir: String, configName: String = CONFIG_FILE_NAME): String {
return PFiles.join(projectDir, configName)
}

}

fun toJson(): String {
return GSON.toJson(this)
}

fun getScriptConfig(path: String): ScriptConfig {
val scriptConfig = scripts[path] ?: ScriptConfig()
if (features.isEmpty()) {
return scriptConfig
}
//
val features = ArrayList(scriptConfig.features)
for (feature in features) {
if (!this.features.contains(feature)) {
this.features.add(feature)
}
}
scriptConfig.features = features
return scriptConfig
}
}

@Keep
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import java.io.File
* Modified by wilinz on 2022/5/23
*/
class ProjectLauncher(private val mProjectDir: String) {
private val mProjectConfig: ProjectConfig = ProjectConfig.fromProjectDir(mProjectDir)!!
private val mProjectConfig: ProjectConfig = ProjectConfig.fromProject(File(mProjectDir))!!
private val mMainScriptFile: File = File(mProjectDir, mProjectConfig.mainScript)
fun launch(service: ScriptEngineService) {
val config = ExecutionConfig()
Expand Down

0 comments on commit dfcf191

Please sign in to comment.