-
Notifications
You must be signed in to change notification settings - Fork 18
ADFA-3580: (feat) Plugin Build Actions & Custom Scripts System #1150
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Daniel-ADFA
wants to merge
7
commits into
stage
Choose a base branch
from
ADFA-3580
base: stage
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
87ab909
feat/ADFA-3580 Plugin Build Actions & Custom Scripts System
Daniel-ADFA e64ffea
feat/ADFA-3580 Plugin Build Actions & Custom Scripts System
Daniel-ADFA d90259c
fix: address CodeRabbit review findings for plugin build actions
Daniel-ADFA a128f2a
fixes
Daniel-ADFA 307892c
fix: remove dead createPluginContext duplicate from PluginManager
Daniel-ADFA 4468c37
fixes
Daniel-ADFA 4952602
Merge branch 'stage' into ADFA-3580
Daniel-ADFA File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
153 changes: 153 additions & 0 deletions
153
app/src/main/java/com/itsaky/androidide/actions/build/PluginBuildActionItem.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,153 @@ | ||
| package com.itsaky.androidide.actions.build | ||
|
|
||
| import android.content.Context | ||
| import android.graphics.ColorFilter | ||
| import android.graphics.PorterDuff | ||
| import android.graphics.PorterDuffColorFilter | ||
| import android.graphics.drawable.Drawable | ||
| import androidx.core.content.ContextCompat | ||
| import com.itsaky.androidide.actions.ActionData | ||
| import com.itsaky.androidide.actions.ActionItem | ||
| import com.itsaky.androidide.actions.BaseBuildAction | ||
| import com.itsaky.androidide.actions.getContext | ||
| import com.itsaky.androidide.plugins.extensions.CommandOutput | ||
| import com.itsaky.androidide.plugins.manager.build.PluginBuildActionManager | ||
| import com.itsaky.androidide.plugins.manager.build.RegisteredBuildAction | ||
| import com.itsaky.androidide.plugins.manager.core.PluginManager | ||
| import com.itsaky.androidide.plugins.manager.ui.PluginDrawableResolver | ||
| import com.itsaky.androidide.plugins.services.IdeCommandService | ||
| import com.itsaky.androidide.resources.R | ||
| import com.itsaky.androidide.utils.resolveAttr | ||
| import com.itsaky.androidide.viewmodel.BottomSheetViewModel | ||
| import com.google.android.material.bottomsheet.BottomSheetBehavior | ||
| import androidx.lifecycle.lifecycleScope | ||
| import com.itsaky.androidide.activities.editor.EditorHandlerActivity | ||
| import kotlinx.coroutines.CancellationException | ||
| import kotlinx.coroutines.Dispatchers | ||
| import kotlinx.coroutines.launch | ||
| import kotlinx.coroutines.withContext | ||
|
|
||
| class PluginBuildActionItem( | ||
| context: Context, | ||
| private val registered: RegisteredBuildAction, | ||
| override val order: Int | ||
| ) : BaseBuildAction() { | ||
|
|
||
| override val id: String = "plugin.build.${registered.pluginId}.${registered.action.id}" | ||
|
|
||
| init { | ||
| label = registered.action.name | ||
| icon = resolvePluginIcon(context) | ||
| location = ActionItem.Location.EDITOR_TOOLBAR | ||
| requiresUIThread = true | ||
| } | ||
|
|
||
| override fun prepare(data: ActionData) { | ||
| val context = data.getActivity() | ||
| if (context == null) { | ||
| visible = false | ||
| return | ||
| } | ||
| visible = true | ||
|
|
||
| val manager = PluginBuildActionManager.getInstance() | ||
| val isRunning = manager.isActionRunning(registered.pluginId, registered.action.id) | ||
|
|
||
| if (isRunning) { | ||
| label = "Cancel ${registered.action.name}" | ||
| icon = ContextCompat.getDrawable(context, R.drawable.ic_stop) | ||
| enabled = true | ||
| } else { | ||
| label = registered.action.name | ||
| icon = resolvePluginIcon(context) | ||
| enabled = true | ||
| } | ||
| } | ||
|
|
||
| override fun createColorFilter(data: ActionData): ColorFilter? { | ||
| val context = data.getContext() ?: return null | ||
| val isRunning = PluginBuildActionManager.getInstance() | ||
| .isActionRunning(registered.pluginId, registered.action.id) | ||
| val attr = if (isRunning) R.attr.colorError else R.attr.colorOnSurface | ||
| return PorterDuffColorFilter( | ||
| context.resolveAttr(attr), | ||
| PorterDuff.Mode.SRC_ATOP | ||
| ) | ||
| } | ||
|
|
||
| private fun resolvePluginIcon(fallbackContext: Context): Drawable? { | ||
| val iconResId = registered.action.icon ?: return ContextCompat.getDrawable(fallbackContext, R.drawable.ic_run_outline) | ||
| return PluginDrawableResolver.resolve(iconResId, registered.pluginId, fallbackContext) | ||
| ?: ContextCompat.getDrawable(fallbackContext, R.drawable.ic_run_outline) | ||
| } | ||
|
|
||
| override suspend fun execAction(data: ActionData): Any { | ||
| val manager = PluginBuildActionManager.getInstance() | ||
| val pluginId = registered.pluginId | ||
| val actionId = registered.action.id | ||
|
|
||
| if (manager.isActionRunning(pluginId, actionId)) { | ||
| manager.cancelAction(pluginId, actionId) | ||
| data.getActivity()?.let { resetProgressIfIdle(it) } | ||
| return true | ||
| } | ||
|
|
||
| val activity = data.getActivity() ?: return false | ||
|
|
||
| val pluginManager = PluginManager.getInstance() ?: return false | ||
| val loadedPlugin = pluginManager.getLoadedPlugin(pluginId) ?: return false | ||
| val commandService = loadedPlugin.context.services.get(IdeCommandService::class.java) | ||
| ?: return false | ||
|
|
||
| val execution = manager.executeAction(pluginId, actionId, commandService) ?: return false | ||
|
|
||
| activity.editorViewModel.isBuildInProgress = true | ||
| val currentSheetState = activity.bottomSheetViewModel.sheetBehaviorState | ||
| val targetState = if (currentSheetState == BottomSheetBehavior.STATE_HIDDEN) | ||
| BottomSheetBehavior.STATE_COLLAPSED else currentSheetState | ||
| activity.bottomSheetViewModel.setSheetState( | ||
| sheetState = targetState, | ||
| currentTab = BottomSheetViewModel.TAB_BUILD_OUTPUT | ||
| ) | ||
| activity.appendBuildOutput("━━━ ${registered.action.name} ━━━") | ||
| activity.invalidateOptionsMenu() | ||
|
|
||
| activity.lifecycleScope.launch(Dispatchers.Default) { | ||
| runCatching { | ||
| execution.output.collect { output -> | ||
| val line = when (output) { | ||
| is CommandOutput.StdOut -> output.line | ||
| is CommandOutput.StdErr -> output.line | ||
| is CommandOutput.ExitCode -> | ||
| if (output.code != 0) "Process failed with code ${output.code}" else null | ||
| } | ||
| if (line != null) { | ||
| withContext(Dispatchers.Main) { | ||
| activity.appendBuildOutput(line) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| val result = execution.await() | ||
| manager.notifyActionCompleted(pluginId, actionId, result) | ||
| withContext(Dispatchers.Main) { resetProgressIfIdle(activity) } | ||
| }.onFailure { e -> | ||
| if (e is CancellationException) { | ||
| manager.cancelAction(pluginId, actionId) | ||
| throw e | ||
| } | ||
| withContext(Dispatchers.Main) { resetProgressIfIdle(activity) } | ||
| } | ||
Daniel-ADFA marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| return true | ||
| } | ||
|
|
||
| private fun resetProgressIfIdle(activity: EditorHandlerActivity) { | ||
| val manager = PluginBuildActionManager.getInstance() | ||
| if (buildService?.isBuildInProgress != true && !manager.hasActiveExecutions()) { | ||
| activity.editorViewModel.isBuildInProgress = false | ||
| } | ||
| activity.invalidateOptionsMenu() | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
plugin-api/src/main/kotlin/com/itsaky/androidide/plugins/extensions/BuildActionExtension.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| package com.itsaky.androidide.plugins.extensions | ||
|
|
||
| import com.itsaky.androidide.plugins.IPlugin | ||
|
|
||
| interface BuildActionExtension : IPlugin { | ||
| fun getBuildActions(): List<PluginBuildAction> | ||
| fun toolbarActionsToHide(): Set<String> = emptySet() | ||
| fun onActionStarted(actionId: String) {} | ||
| fun onActionCompleted(actionId: String, result: CommandResult) {} | ||
| } | ||
|
|
||
| object ToolbarActionIds { | ||
| const val QUICK_RUN = "ide.editor.build.quickRun" | ||
| const val PROJECT_SYNC = "ide.editor.syncProject" | ||
| const val DEBUG = "ide.editor.build.debug" | ||
| const val RUN_TASKS = "ide.editor.build.runTasks" | ||
| const val UNDO = "ide.editor.code.text.undo" | ||
| const val REDO = "ide.editor.code.text.redo" | ||
| const val SAVE = "ide.editor.files.saveAll" | ||
| const val PREVIEW_LAYOUT = "ide.editor.previewLayout" | ||
| const val FIND = "ide.editor.find" | ||
| const val FIND_IN_FILE = "ide.editor.find.inFile" | ||
| const val FIND_IN_PROJECT = "ide.editor.find.inProject" | ||
| const val LAUNCH_APP = "ide.editor.launchInstalledApp" | ||
| const val DISCONNECT_LOG_SENDERS = "ide.editor.service.logreceiver.disconnectSenders" | ||
| const val GENERATE_XML = "ide.editor.generatexml" | ||
|
|
||
| val ALL: Set<String> = setOf( | ||
| QUICK_RUN, PROJECT_SYNC, DEBUG, RUN_TASKS, | ||
| UNDO, REDO, SAVE, PREVIEW_LAYOUT, | ||
| FIND, FIND_IN_FILE, FIND_IN_PROJECT, | ||
| LAUNCH_APP, DISCONNECT_LOG_SENDERS, GENERATE_XML | ||
| ) | ||
| } | ||
|
|
||
| data class PluginBuildAction( | ||
| val id: String, | ||
| val name: String, | ||
| val description: String, | ||
| val icon: Int? = null, | ||
| val category: BuildActionCategory = BuildActionCategory.CUSTOM, | ||
| val command: CommandSpec, | ||
| val timeoutMs: Long = 600_000 | ||
| ) | ||
|
|
||
| sealed class CommandSpec { | ||
| data class ShellCommand( | ||
| val executable: String, | ||
| val arguments: List<String> = emptyList(), | ||
| val workingDirectory: String? = null, | ||
| val environment: Map<String, String> = emptyMap() | ||
| ) : CommandSpec() | ||
|
|
||
| data class GradleTask( | ||
| val taskPath: String, | ||
| val arguments: List<String> = emptyList() | ||
| ) : CommandSpec() | ||
| } | ||
|
|
||
| sealed class CommandOutput { | ||
| data class StdOut(val line: String) : CommandOutput() | ||
| data class StdErr(val line: String) : CommandOutput() | ||
| data class ExitCode(val code: Int) : CommandOutput() | ||
| } | ||
|
|
||
| sealed class CommandResult { | ||
| data class Success( | ||
| val exitCode: Int, | ||
| val stdout: String, | ||
| val stderr: String, | ||
| val durationMs: Long | ||
| ) : CommandResult() | ||
|
|
||
| data class Failure( | ||
| val exitCode: Int, | ||
| val stdout: String, | ||
| val stderr: String, | ||
| val error: String?, | ||
| val durationMs: Long | ||
| ) : CommandResult() | ||
|
|
||
| data class Cancelled( | ||
| val partialStdout: String, | ||
| val partialStderr: String | ||
| ) : CommandResult() | ||
| } | ||
|
|
||
| enum class BuildActionCategory { BUILD, TEST, DEPLOY, LINT, CUSTOM } |
20 changes: 20 additions & 0 deletions
20
plugin-api/src/main/kotlin/com/itsaky/androidide/plugins/services/IdeCommandService.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| package com.itsaky.androidide.plugins.services | ||
|
|
||
| import com.itsaky.androidide.plugins.extensions.CommandOutput | ||
| import com.itsaky.androidide.plugins.extensions.CommandResult | ||
| import com.itsaky.androidide.plugins.extensions.CommandSpec | ||
| import kotlinx.coroutines.flow.Flow | ||
|
|
||
| interface IdeCommandService { | ||
| fun executeCommand(spec: CommandSpec, timeoutMs: Long = 600_000): CommandExecution | ||
| fun isCommandRunning(executionId: String): Boolean | ||
| fun cancelCommand(executionId: String): Boolean | ||
| fun getRunningCommandCount(): Int | ||
| } | ||
|
|
||
| interface CommandExecution { | ||
| val executionId: String | ||
| val output: Flow<CommandOutput> | ||
| suspend fun await(): CommandResult | ||
| fun cancel() | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.