-
-
Notifications
You must be signed in to change notification settings - Fork 55
ADFA-5307 | Return real build log from getBuildOutput #1763
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
jatezzz
wants to merge
2
commits into
stage
Choose a base branch
from
feature/ADFA-5307-agent-real-build-log
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
2 commits
Select commit
Hold shift + click to select a range
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
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
143 changes: 143 additions & 0 deletions
143
app/src/main/java/com/itsaky/androidide/app/PluginRunAppCoordinator.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,143 @@ | ||
| package com.itsaky.androidide.app | ||
|
|
||
| import android.app.Activity | ||
| import androidx.lifecycle.ViewModelProvider | ||
| import androidx.lifecycle.lifecycleScope | ||
| import com.itsaky.androidide.activities.editor.ProjectHandlerActivity | ||
| import com.itsaky.androidide.interfaces.IEditorHandler | ||
| import com.itsaky.androidide.plugins.services.BuildAndLaunchCallback | ||
| import com.itsaky.androidide.projects.IProjectManager | ||
| import com.itsaky.androidide.projects.isPluginProject | ||
| import com.itsaky.androidide.viewmodel.BuildState | ||
| import com.itsaky.androidide.viewmodel.BuildViewModel | ||
| import kotlinx.coroutines.CancellationException | ||
| import kotlinx.coroutines.Dispatchers | ||
| import kotlinx.coroutines.launch | ||
| import kotlinx.coroutines.withContext | ||
| import org.slf4j.LoggerFactory | ||
| import java.util.concurrent.atomic.AtomicBoolean | ||
|
|
||
| /** | ||
| * Runs the current project's app on behalf of a plugin. | ||
| * | ||
| * Delegates to [BuildViewModel.runQuickBuild], the same entry point as the editor's Run action, so | ||
| * a plugin gets APK resolution, the install and the launch prompt rather than a bare assemble. | ||
| * That path is activity-scoped, which is why this needs the foreground activity and the main | ||
| * thread; a plugin calls in from a background coroutine. | ||
| */ | ||
| internal object PluginRunAppCoordinator { | ||
| private val logger = LoggerFactory.getLogger(PluginRunAppCoordinator::class.java) | ||
|
|
||
| /** | ||
| * Builds, installs and launches the selected app module. | ||
| * | ||
| * @param foregroundActivity the activity currently on screen, or null when there is none. | ||
| * @param callback completed once, when the build resolves or the run is abandoned. Success | ||
| * means the installer has the APK, not that the app is on screen: the system install prompt | ||
| * and the launch prompt are the user's to answer. | ||
| */ | ||
| fun runApp( | ||
| foregroundActivity: Activity?, | ||
| callback: BuildAndLaunchCallback, | ||
| ) { | ||
| val activity = foregroundActivity as? ProjectHandlerActivity | ||
| if (activity == null) { | ||
| callback.onComplete(false, "No project is open in the editor. Open one before running the app.") | ||
| return | ||
| } | ||
|
|
||
| val reported = AtomicBoolean(false) | ||
|
|
||
| fun report( | ||
| success: Boolean, | ||
| message: String, | ||
| ) { | ||
| if (reported.compareAndSet(false, true)) { | ||
| callback.onComplete(success, message) | ||
| } | ||
| } | ||
|
|
||
| val job = | ||
| activity.lifecycleScope.launch { | ||
| try { | ||
| startBuild(activity, ::report) | ||
| } catch (e: CancellationException) { | ||
| throw e | ||
| } catch (e: Exception) { | ||
| logger.error("Failed to run the app for a plugin", e) | ||
| report(false, "Error: ${e.message}") | ||
| } | ||
| } | ||
|
|
||
| // An already-destroyed activity's scope drops the block without running it, so cancellation | ||
| // has to be reported from the job rather than from inside. Nothing else will report, and the | ||
| // caller would sit out its own timeout. A completed hand-off carries no cause, so the build | ||
| // still reports through runQuickBuild's callback. | ||
| job.invokeOnCompletion { cause -> | ||
| if (cause != null) { | ||
| report(false, "The editor was closed before the build started.") | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Resolves what to build and starts it. Returns as soon as the build is under way: the outcome | ||
| * arrives on [BuildViewModel.runQuickBuild]'s callback, which outlives this coroutine. | ||
| */ | ||
| private suspend fun startBuild( | ||
| activity: ProjectHandlerActivity, | ||
| report: (Boolean, String) -> Unit, | ||
| ) { | ||
| val projectManager = IProjectManager.getInstance() | ||
| val isPluginProject = withContext(Dispatchers.IO) { projectManager.isPluginProject() } | ||
| val module = | ||
| if (isPluginProject) { | ||
| projectManager.getAndroidModules().firstOrNull() | ||
| } else { | ||
| // The Run action asks the user which app module to build; a plugin has no one to | ||
| // ask, so it gets the first, as the previous provider did. | ||
| projectManager.getAndroidAppModules().firstOrNull() | ||
| } | ||
| val variant = module?.getSelectedVariant() | ||
| if (module == null || variant == null) { | ||
| report(false, "No app module or build variant is selected.") | ||
| return | ||
| } | ||
|
|
||
| val buildViewModel = ViewModelProvider(activity)[BuildViewModel::class.java] | ||
| (activity as? IEditorHandler)?.saveAllResult() | ||
| buildViewModel.runQuickBuild(module, variant, launchInDebugMode = false) { state -> | ||
| val (success, message) = state.toOutcome() | ||
| report(success, message) | ||
| } | ||
| } | ||
|
|
||
| /** What to tell the plugin about a run that ended on [this]. */ | ||
| private fun BuildState.toOutcome(): Pair<Boolean, String> = | ||
| when (this) { | ||
| is BuildState.AwaitingInstall -> { | ||
| true to "Build succeeded. Installing ${apkFile.name} - confirm the system prompt to launch the app." | ||
| } | ||
|
|
||
| is BuildState.AwaitingPluginInstall -> { | ||
| true to "Build succeeded. Installing plugin ${cgpFile.name} - confirm the prompt in the IDE." | ||
| } | ||
|
|
||
| is BuildState.Error -> { | ||
| false to reason | ||
| } | ||
|
|
||
| is BuildState.Success -> { | ||
| true to message | ||
| } | ||
|
|
||
| // runQuickBuild only ends on Idle when its scope was cancelled, and never on InProgress. | ||
| BuildState.Idle -> { | ||
| false to "The build was cancelled." | ||
| } | ||
|
|
||
| BuildState.InProgress -> { | ||
| false to "The build did not report a result." | ||
| } | ||
| } | ||
| } |
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
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.