Skip to content

Commit

Permalink
Added: Add support for %stdout_original_length and %stderr_original_l…
Browse files Browse the repository at this point in the history
…ength result variables as per termux/termux-app@f62febbf and termux/termux-app@a2209ddd
  • Loading branch information
agnostic-apollo committed Sep 9, 2021
1 parent d9a172d commit 1c1567f
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,9 @@ public void finish() {
if (TaskerPlugin.hostSupportsRelevantVariables(getIntent().getExtras())) {
TaskerPlugin.addRelevantVariableList(resultIntent, new String[]{
PluginUtils.PLUGIN_VARIABLE_STDOUT + "\nStandard Output\nThe <B>stdout</B> of the command.",
PluginUtils.PLUGIN_VARIABLE_STDOUT_ORIGINAL_LENGTH + "\nStandard Output Original Length\nThe original length of <B>stdout</B>.",
PluginUtils.PLUGIN_VARIABLE_STDERR + "\nStandard Error\nThe <B>stderr</B> of the command.",
PluginUtils.PLUGIN_VARIABLE_STDERR_ORIGINAL_LENGTH + "\nStandard Error Original Length\nThe original length of <B>stderr</B>.",
PluginUtils.PLUGIN_VARIABLE_EXIT_CODE + "\nExit Code\nThe <B>exit code</B> of the command. " +
"0 often means success and anything else is usually a failure of some sort."
});
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/java/com/termux/tasker/FireReceiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void onReceive(final Context context, final Intent intent) {
errmsg = PluginBundleManager.isBundleValid(context, bundle);
if (errmsg != null) {
Logger.logError(LOG_TAG, errmsg);
PluginUtils.sendImmediateResultToPluginHostApp(this, intent, null, null, null, TaskerPlugin.Setting.RESULT_CODE_FAILED, errmsg);
PluginUtils.sendImmediateResultToPluginHostApp(this, intent, TaskerPlugin.Setting.RESULT_CODE_FAILED, errmsg);
return;
}

Expand All @@ -79,7 +79,7 @@ public void onReceive(final Context context, final Intent intent) {
errmsg = TermuxUtils.isTermuxAppAccessible(context);
if (errmsg != null) {
Logger.logError(LOG_TAG, errmsg);
PluginUtils.sendImmediateResultToPluginHostApp(this, intent, null, null, null, TaskerPlugin.Setting.RESULT_CODE_FAILED, errmsg);
PluginUtils.sendImmediateResultToPluginHostApp(this, intent, TaskerPlugin.Setting.RESULT_CODE_FAILED, errmsg);
return;
}

Expand All @@ -88,7 +88,7 @@ public void onReceive(final Context context, final Intent intent) {
if (executionCommand.executable == null || executionCommand.executable.isEmpty()) {
errmsg = context.getString(R.string.error_null_or_empty_executable);
Logger.logError(LOG_TAG, errmsg);
PluginUtils.sendImmediateResultToPluginHostApp(this, intent, null, null, null, TaskerPlugin.Setting.RESULT_CODE_FAILED, errmsg);
PluginUtils.sendImmediateResultToPluginHostApp(this, intent, TaskerPlugin.Setting.RESULT_CODE_FAILED, errmsg);
return;
}

Expand Down
55 changes: 48 additions & 7 deletions app/src/main/java/com/termux/tasker/utils/PluginUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,12 @@ public class PluginUtils {

/** Plugin variable for stdout value of termux command */
public static final String PLUGIN_VARIABLE_STDOUT = "%stdout"; // Default: "%stdout"
/** Plugin variable for original length of stdout value of termux command */
public static final String PLUGIN_VARIABLE_STDOUT_ORIGINAL_LENGTH = "%stdout_original_length"; // Default: "%stdout_original_length"
/** Plugin variable for stderr value of termux command */
public static final String PLUGIN_VARIABLE_STDERR = "%stderr"; // Default: "%stderr"
/** Plugin variable for original length of stderr value of termux command */
public static final String PLUGIN_VARIABLE_STDERR_ORIGINAL_LENGTH = "%stderr_original_length"; // Default: "%stderr_original_length"
/** Plugin variable for exit code value of termux command */
public static final String PLUGIN_VARIABLE_EXIT_CODE = "%result"; // Default: "%result"
/** Plugin variable for err value of termux command */
Expand Down Expand Up @@ -162,7 +166,7 @@ public static void sendExecuteIntentToExecuteService(final Context context, fina
executionIntent.putExtra(TERMUX_SERVICE.EXTRA_PENDING_INTENT, pendingIntent);
} else {
// If execute in background is not enabled, do not expect results back from execution service and return result now so that plugin action does not timeout
sendImmediateResultToPluginHostApp(receiver, originalIntent, null, null, null, TaskerPlugin.Setting.RESULT_CODE_OK, null);
sendImmediateResultToPluginHostApp(receiver, originalIntent, TaskerPlugin.Setting.RESULT_CODE_OK, null);
}

try {
Expand All @@ -176,22 +180,43 @@ public static void sendExecuteIntentToExecuteService(final Context context, fina
} catch (Exception e) {
String errmsg = Logger.getMessageAndStackTraceString("Failed to send execution intent to " + executionIntent.getComponent().toString(), e);
Logger.logErrorAndShowToast(context, LOG_TAG, errmsg);
PluginUtils.sendImmediateResultToPluginHostApp(receiver, originalIntent, null, null, null, TaskerPlugin.Setting.RESULT_CODE_FAILED, errmsg);
PluginUtils.sendImmediateResultToPluginHostApp(receiver, originalIntent, TaskerPlugin.Setting.RESULT_CODE_FAILED, errmsg);
}
}

/**
* Send immediate result to plugin host app in a variables bundle.
*
* @param receiver The {@link BroadcastReceiver} of the originalIntent.
* @param originalIntent The original {@link Intent} received by {@link FireReceiver}.
* @param errCode The value for {@link #PLUGIN_VARIABLE_ERR} variable of plugin action.
* @param errmsg The value for {@link #PLUGIN_VARIABLE_ERRMSG} variable of plugin action.
*/
public static void sendImmediateResultToPluginHostApp(final BroadcastReceiver receiver, final Intent originalIntent,
final int errCode, final String errmsg) {
sendImmediateResultToPluginHostApp(receiver, originalIntent, null, null,
null, null, null, errCode, errmsg);
}

/**
* Send immediate result to plugin host app in a variables bundle.
*
* @param receiver The {@link BroadcastReceiver} of the originalIntent.
* @param originalIntent The original {@link Intent} received by {@link FireReceiver}.
* @param stdout The value for {@link #PLUGIN_VARIABLE_STDOUT} variable of plugin action.
* @param stdoutOriginalLength The value for {@link #PLUGIN_VARIABLE_STDOUT_ORIGINAL_LENGTH}
* variable of plugin action.
* @param stderr The value for {@link #PLUGIN_VARIABLE_STDERR} variable of plugin action.
* @param stderrOriginalLength The value for {@link #PLUGIN_VARIABLE_STDERR_ORIGINAL_LENGTH}
* variable of plugin action.
* @param exitCode The value for {@link #PLUGIN_VARIABLE_EXIT_CODE} variable of plugin action.
* @param errCode The value for {@link #PLUGIN_VARIABLE_ERR} variable of plugin action.
* @param errmsg The value for {@link #PLUGIN_VARIABLE_ERRMSG} variable of plugin action.
*/
public static void sendImmediateResultToPluginHostApp(final BroadcastReceiver receiver, final Intent originalIntent, final String stdout, final String stderr, final String exitCode, final int errCode, final String errmsg) {
public static void sendImmediateResultToPluginHostApp(final BroadcastReceiver receiver, final Intent originalIntent,
final String stdout, String stdoutOriginalLength,
final String stderr, String stderrOriginalLength,
final String exitCode, final int errCode, final String errmsg) {
if (receiver == null) return;

// If timeout for plugin action is 0, then don't send anything
Expand All @@ -202,7 +227,8 @@ public static void sendImmediateResultToPluginHostApp(final BroadcastReceiver re
Logger.logInfo(LOG_TAG, "Sending immediate result to plugin host app. " + PLUGIN_VARIABLE_ERR + ": " + ((err == TaskerPlugin.Setting.RESULT_CODE_OK) ? "success" : "failed") + " (" + err + ")");

if (TaskerPlugin.Setting.hostSupportsVariableReturn(originalIntent.getExtras())) {
final Bundle varsBundle = createVariablesBundle(stdout, stderr, exitCode, err, errmsg);
final Bundle varsBundle = createVariablesBundle(stdout, stdoutOriginalLength,
stderr, stderrOriginalLength, exitCode, err, errmsg);
TaskerPlugin.addVariableBundle(receiver.getResultExtras(true), varsBundle);
}

Expand Down Expand Up @@ -248,7 +274,9 @@ public static void sendPendingResultToPluginHostApp(final Context context, final

final Bundle varsBundle = createVariablesBundle(
resultBundle.getString(TERMUX_SERVICE.EXTRA_PLUGIN_RESULT_BUNDLE_STDOUT, ""),
resultBundle.getString(TERMUX_SERVICE.EXTRA_PLUGIN_RESULT_BUNDLE_STDOUT_ORIGINAL_LENGTH, ""),
resultBundle.getString(TERMUX_SERVICE.EXTRA_PLUGIN_RESULT_BUNDLE_STDERR, ""),
resultBundle.getString(TERMUX_SERVICE.EXTRA_PLUGIN_RESULT_BUNDLE_STDERR_ORIGINAL_LENGTH, ""),
exitCode, err, resultBundle.getString(TERMUX_SERVICE.EXTRA_PLUGIN_RESULT_BUNDLE_ERRMSG, ""));

if(context != null)
Expand Down Expand Up @@ -287,8 +315,7 @@ public static void processPluginExecutionCommandError(final Context context, fin
true, isExecutionCommandLoggingEnabled));

PluginUtils.sendImmediateResultToPluginHostApp(receiver, originalIntent,
null, null, null, errCode,
ResultData.getErrorsListMinimalString(executionCommand.resultData));
errCode, ResultData.getErrorsListMinimalString(executionCommand.resultData));
}


Expand All @@ -297,17 +324,25 @@ public static void processPluginExecutionCommandError(final Context context, fin
* Create variables bundle to send back to plugin host app.
*
* @param stdout The value for {@link #PLUGIN_VARIABLE_STDOUT} variable of plugin action.
* @param stdoutOriginalLength The value for {@link #PLUGIN_VARIABLE_STDOUT_ORIGINAL_LENGTH}
* variable of plugin action.
* @param stderr The value for {@link #PLUGIN_VARIABLE_STDERR} variable of plugin action.
* @param stderrOriginalLength The value for {@link #PLUGIN_VARIABLE_STDERR_ORIGINAL_LENGTH}
* variable of plugin action.
* @param exitCode The value for {@link #PLUGIN_VARIABLE_EXIT_CODE} variable of plugin action.
* @param errCode The value for {@link #PLUGIN_VARIABLE_ERR} variable of plugin action.
* @param errmsg The value for {@link #PLUGIN_VARIABLE_ERRMSG} variable of plugin action.
* @return Returns the variables {@code Bundle}.
*/
public static Bundle createVariablesBundle(String stdout, String stderr, String exitCode, int errCode, String errmsg) {
public static Bundle createVariablesBundle(String stdout, String stdoutOriginalLength,
String stderr, String stderrOriginalLength,
String exitCode, int errCode, String errmsg) {

Logger.logDebugExtended(LOG_TAG, "Variables bundle for plugin host app:\n" +
PLUGIN_VARIABLE_STDOUT + ": `" + stdout + "`\n" +
PLUGIN_VARIABLE_STDOUT_ORIGINAL_LENGTH + ": `" + stdoutOriginalLength + "`\n" +
PLUGIN_VARIABLE_STDERR + ": `" + stderr + "`\n" +
PLUGIN_VARIABLE_STDERR_ORIGINAL_LENGTH + ": `" + stderrOriginalLength + "`\n" +
PLUGIN_VARIABLE_EXIT_CODE + ": `" + exitCode + "`\n" +
PLUGIN_VARIABLE_ERR + ": `" + errCode + "`\n" +
PLUGIN_VARIABLE_ERRMSG + ": `" + errmsg + "`");
Expand All @@ -322,16 +357,22 @@ public static Bundle createVariablesBundle(String stdout, String stderr, String
// since if multiple actions are run in the same task, some variables from previous actions
// may still be set and get mixed in with current ones.
if (stdout == null) stdout = "";
if (stdoutOriginalLength == null) stdoutOriginalLength = "";
if (stderr == null) stderr = "";
if (stderrOriginalLength == null) stderrOriginalLength = "";
if (exitCode == null) exitCode = "";
if (errmsg == null) errmsg = "";

final Bundle variablesBundle = new Bundle();

if (isPluginHostAppVariableNameValid(PLUGIN_VARIABLE_STDOUT))
variablesBundle.putString(PLUGIN_VARIABLE_STDOUT, stdout);
if (isPluginHostAppVariableNameValid(PLUGIN_VARIABLE_STDOUT_ORIGINAL_LENGTH))
variablesBundle.putString(PLUGIN_VARIABLE_STDOUT_ORIGINAL_LENGTH, stdoutOriginalLength);
if (isPluginHostAppVariableNameValid(PLUGIN_VARIABLE_STDERR))
variablesBundle.putString(PLUGIN_VARIABLE_STDERR, stderr);
if (isPluginHostAppVariableNameValid(PLUGIN_VARIABLE_STDERR_ORIGINAL_LENGTH))
variablesBundle.putString(PLUGIN_VARIABLE_STDERR_ORIGINAL_LENGTH, stderrOriginalLength);
if (isPluginHostAppVariableNameValid(PLUGIN_VARIABLE_EXIT_CODE))
variablesBundle.putString(PLUGIN_VARIABLE_EXIT_CODE, exitCode);
if (isPluginHostAppVariableNameValid(PLUGIN_VARIABLE_ERRMSG))
Expand Down

0 comments on commit 1c1567f

Please sign in to comment.