Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.itsaky.androidide.repositories
import android.util.Log
import com.itsaky.androidide.plugins.PluginInfo
import com.itsaky.androidide.plugins.PluginMetadata
import com.itsaky.androidide.plugins.manager.core.PluginBuildFiles
import com.itsaky.androidide.plugins.manager.core.PluginManager
import com.itsaky.androidide.plugins.manager.loaders.toPluginMetadata
import kotlinx.coroutines.Dispatchers
Expand Down Expand Up @@ -144,7 +145,9 @@ class PluginRepositoryImpl(

val fileExtension =
if (pluginFile.name.endsWith(".$PLUGIN_ARCHIVE_EXTENSION", ignoreCase = true)) ".$PLUGIN_ARCHIVE_EXTENSION" else ".apk"
val finalFileName = "${pluginId}$fileExtension"
// A name of its own per build: Android caches a plugin's resources, assets and native
// libraries by path, so the new build would otherwise run against the old one's.
val finalFileName = "${PluginBuildFiles.fileStem(pluginId, pluginFile)}$fileExtension"

if (!pluginsDir.exists()) {
pluginsDir.mkdirs()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.itsaky.androidide.repositories

import com.google.common.truth.Truth.assertThat
import com.itsaky.androidide.plugins.manager.core.PluginBuildFiles
import com.itsaky.androidide.plugins.manager.core.PluginManager
import com.itsaky.androidide.plugins.manager.core.PluginValidation
import com.itsaky.androidide.plugins.manager.loaders.PluginManifest
import io.mockk.every
import io.mockk.mockk
import kotlinx.coroutines.test.runTest
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TemporaryFolder
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner

/**
* Each plugin build is installed under a name of its own. Android caches a plugin's resources and
* assets by path, so a build installed at the previous build's path read the previous build's
* assets until the app restarted (ADFA-7160).
*/
@RunWith(RobolectricTestRunner::class)
class PluginRepositoryImplInstallTest {
@get:Rule
val temp = TemporaryFolder()

@Test
fun aBuildIsInstalledUnderItsContentHash() =
runTest {
val pluginsDir = temp.newFolder("plugins")
val build = temp.newFile("incoming.cgp").apply { writeText("build A") }
val expected = "${PluginBuildFiles.fileStem(ID, build)}.cgp"
val manager =
mockk<PluginManager>(relaxed = true) {
every { getPluginValidation(any()) } returns
Result.success(
PluginValidation(
manifest = PluginManifest(ID, "Example", "1.0", "", "", "Main", "1.0"),
isDebug = false,
iconDayEntryExists = false,
iconNightEntryExists = false,
),
)
every { getPlugin(ID) } returns mockk()
}

PluginRepositoryImpl({ manager }, pluginsDir).installPluginFromFile(build).getOrThrow()

assertThat(pluginsDir.list()!!.toList()).containsExactly(expected)
}

private companion object {
const val ID = "org.example.plugin"
}
}
2 changes: 1 addition & 1 deletion docs/PLUGIN_AUTHORING.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ these in `PluginLoader.getPluginMetadata()`

| Key | Type | Required | Notes |
|---------------------------|-------------------------|----------|-----------------------------------------------------------------------|
| `plugin.id` | string | yes\* | Unique plugin identifier. Falls back to the APK's package name. |
| `plugin.id` | string | yes\* | Unique plugin identifier. Falls back to the APK's package name. It names files, so it can't be empty, `.` or `..`, or contain `/`, `\` or NUL, and it can't be `oat` or end in `.cgp` (any case), which CoGo uses for its own files; CoGo refuses such a plugin. |
| `plugin.name` | string | yes\* | Display name. |
| `plugin.version` | string | yes\* | Defaults to `"1.0.0"`. Prefer `${pluginVersion}` from `pluginBuilder`. |
| `plugin.description` | string | no | Shown in the plugin list. |
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.itsaky.androidide.plugins.manager.core

import org.slf4j.LoggerFactory
import java.io.File
import java.io.IOException
import java.nio.file.FileVisitResult
import java.nio.file.Files
import java.nio.file.LinkOption
import java.nio.file.Path
import java.nio.file.SimpleFileVisitor
import java.nio.file.attribute.BasicFileAttributes

private val log = LoggerFactory.getLogger("LinkSafeDelete")

/**
* Deletes this file or folder tree without ever following a link: a link, the root included, is
* deleted as a link and its target is left alone. File.deleteRecursively() follows a link nested in
* the tree and empties its target, and plugin code can make a link anywhere in the app's storage.
*
* True when nothing is left, as with deleteRecursively(), so true for a path that doesn't exist. Keeps
* going past an entry it can't delete, as deleteRecursively() does.
*/
internal fun File.deleteWithoutFollowingLinks(): Boolean {
val root = toPath()
if (!Files.exists(root, LinkOption.NOFOLLOW_LINKS)) return true
// walkFileTree without FOLLOW_LINKS hands a link to visitFile, never into its target.
val visitor =
object : SimpleFileVisitor<Path>() {
override fun visitFile(
file: Path,
attrs: BasicFileAttributes,
) = tryDelete(file)

override fun visitFileFailed(
file: Path,
exc: IOException,
) = tryDelete(file)

override fun postVisitDirectory(
dir: Path,
exc: IOException?,
) = tryDelete(dir)

private fun tryDelete(path: Path): FileVisitResult {
try {
Files.deleteIfExists(path)
} catch (e: IOException) {
log.debug("Could not delete {}", path, e)
}
return FileVisitResult.CONTINUE
}
}
try {
Files.walkFileTree(root, visitor)
} catch (e: IOException) {
log.debug("Could not walk {}", root, e)
}
return !Files.exists(root, LinkOption.NOFOLLOW_LINKS)
}
Loading
Loading