Initial commit 🎉
fbshipit-source-id: b6fc29740c6875d2e78953b8a7123890a67930f2 Co-authored-by: Sebastian McKenzie <sebmck@fb.com> Co-authored-by: John Knox <jknox@fb.com> Co-authored-by: Emil Sjölander <emilsj@fb.com> Co-authored-by: Pritesh Nandgaonkar <prit91@fb.com>
This commit is contained in:
88
android/plugins/console/iface/ConsoleCommandReceiver.java
Normal file
88
android/plugins/console/iface/ConsoleCommandReceiver.java
Normal file
@@ -0,0 +1,88 @@
|
||||
/*
|
||||
* Copyright (c) 2018-present, Facebook, Inc.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the LICENSE
|
||||
* file in the root directory of this source tree.
|
||||
*
|
||||
*/
|
||||
package com.facebook.sonar.plugins.console.iface;
|
||||
|
||||
import android.support.annotation.Nullable;
|
||||
import com.facebook.sonar.core.SonarConnection;
|
||||
import com.facebook.sonar.core.SonarObject;
|
||||
import com.facebook.sonar.core.SonarReceiver;
|
||||
import com.facebook.sonar.core.SonarResponder;
|
||||
import com.facebook.sonar.plugins.common.MainThreadSonarReceiver;
|
||||
import org.json.JSONObject;
|
||||
|
||||
/**
|
||||
* Convenience class for adding console execution to a Sonar Plugin. Calling {@link
|
||||
* ConsoleCommandReceiver#listenForCommands(SonarConnection, ScriptingEnvironment, ContextProvider)}
|
||||
* will add the necessary listeners for responding to command execution calls.
|
||||
*/
|
||||
public class ConsoleCommandReceiver {
|
||||
|
||||
/**
|
||||
* Incoming command execution calls may reference a context ID that means something to your
|
||||
* plugin. Implement {@link ContextProvider} to provide a mapping from context ID to java object.
|
||||
* This will allow your sonar plugin to control the execution context of the command.
|
||||
*/
|
||||
public interface ContextProvider {
|
||||
@Nullable
|
||||
Object getObjectForId(String id);
|
||||
}
|
||||
|
||||
public static void listenForCommands(
|
||||
final SonarConnection connection,
|
||||
final ScriptingEnvironment scriptingEnvironment,
|
||||
final ContextProvider contextProvider) {
|
||||
|
||||
final ScriptingSession session = scriptingEnvironment.startSession();
|
||||
final SonarReceiver executeCommandReceiver =
|
||||
new MainThreadSonarReceiver(connection) {
|
||||
@Override
|
||||
public void onReceiveOnMainThread(SonarObject params, SonarResponder responder)
|
||||
throws Exception {
|
||||
final String command = params.getString("command");
|
||||
final String contextObjectId = params.getString("context");
|
||||
final Object contextObject = contextProvider.getObjectForId(contextObjectId);
|
||||
try {
|
||||
JSONObject o =
|
||||
contextObject == null
|
||||
? session.evaluateCommand(command)
|
||||
: session.evaluateCommand(command, contextObject);
|
||||
responder.success(new SonarObject(o));
|
||||
} catch (Exception e) {
|
||||
responder.error(new SonarObject.Builder().put("message", e.getMessage()).build());
|
||||
}
|
||||
}
|
||||
};
|
||||
final SonarReceiver isEnabledReceiver =
|
||||
new SonarReceiver() {
|
||||
@Override
|
||||
public void onReceive(SonarObject params, SonarResponder responder) throws Exception {
|
||||
responder.success(
|
||||
new SonarObject.Builder()
|
||||
.put("isEnabled", scriptingEnvironment.isEnabled())
|
||||
.build());
|
||||
}
|
||||
};
|
||||
|
||||
connection.receive("executeCommand", executeCommandReceiver);
|
||||
connection.receive("isConsoleEnabled", isEnabledReceiver);
|
||||
}
|
||||
|
||||
public static void listenForCommands(
|
||||
SonarConnection connection, ScriptingEnvironment scriptingEnvironment) {
|
||||
listenForCommands(connection, scriptingEnvironment, nullContextProvider);
|
||||
}
|
||||
|
||||
private static final ContextProvider nullContextProvider =
|
||||
new ContextProvider() {
|
||||
@Override
|
||||
@Nullable
|
||||
public Object getObjectForId(String id) {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
}
|
||||
43
android/plugins/console/iface/NullScriptingEnvironment.java
Normal file
43
android/plugins/console/iface/NullScriptingEnvironment.java
Normal file
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* Copyright (c) 2018-present, Facebook, Inc.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the LICENSE
|
||||
* file in the root directory of this source tree.
|
||||
*
|
||||
*/
|
||||
package com.facebook.sonar.plugins.console.iface;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
public class NullScriptingEnvironment implements ScriptingEnvironment {
|
||||
|
||||
@Override
|
||||
public ScriptingSession startSession() {
|
||||
return new NoOpScriptingSession();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerGlobalObject(String name, Object object) {}
|
||||
|
||||
static class NoOpScriptingSession implements ScriptingSession {
|
||||
|
||||
@Override
|
||||
public JSONObject evaluateCommand(String userScript) throws JSONException {
|
||||
throw new UnsupportedOperationException("Console plugin not enabled in this app");
|
||||
}
|
||||
|
||||
@Override
|
||||
public JSONObject evaluateCommand(String userScript, Object context) throws JSONException {
|
||||
return evaluateCommand(userScript);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEnabled() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
17
android/plugins/console/iface/ScriptingEnvironment.java
Normal file
17
android/plugins/console/iface/ScriptingEnvironment.java
Normal file
@@ -0,0 +1,17 @@
|
||||
/*
|
||||
* Copyright (c) 2018-present, Facebook, Inc.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the LICENSE
|
||||
* file in the root directory of this source tree.
|
||||
*
|
||||
*/
|
||||
package com.facebook.sonar.plugins.console.iface;
|
||||
|
||||
public interface ScriptingEnvironment {
|
||||
|
||||
ScriptingSession startSession();
|
||||
|
||||
void registerGlobalObject(String name, Object object);
|
||||
|
||||
boolean isEnabled();
|
||||
}
|
||||
20
android/plugins/console/iface/ScriptingSession.java
Normal file
20
android/plugins/console/iface/ScriptingSession.java
Normal file
@@ -0,0 +1,20 @@
|
||||
/*
|
||||
* Copyright (c) 2018-present, Facebook, Inc.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the LICENSE
|
||||
* file in the root directory of this source tree.
|
||||
*
|
||||
*/
|
||||
package com.facebook.sonar.plugins.console.iface;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
public interface ScriptingSession {
|
||||
|
||||
JSONObject evaluateCommand(String userScript) throws JSONException;
|
||||
|
||||
JSONObject evaluateCommand(String userScript, Object context) throws JSONException;
|
||||
|
||||
void close();
|
||||
}
|
||||
Reference in New Issue
Block a user