Skip to content
This repository has been archived by the owner on Dec 5, 2019. It is now read-only.

Commit

Permalink
Add constants to packages and cleanup code
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitalii Chepeliuk committed Feb 15, 2018
1 parent 283df01 commit 8fd0a50
Show file tree
Hide file tree
Showing 32 changed files with 211 additions and 213 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@

public interface CLIRunner {
String executeSync(List<String> command) throws CLIException;

void executeAsync(List<String> command, Watcher w);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,22 @@
import java.util.concurrent.*;

public class CLIRunnerImpl implements CLIRunner {

MobileNotificationsService notificationsService;


private static final String OUTPUT_TYPE_ERROR = "error";
private static final String OUTPUT_TYPE_STD = "std";

private static final int CMD_TIMEOUT_SECONDS = 10;
private static final int EXECUTOR_NUM_THREADS = 3;

private final MobileNotificationsService notificationsService;
private final ExecutorService executorService;

private static final CLIRunner instance = CLIRunnerImpl.getInstance();

public CLIRunnerImpl() {
this.notificationsService = new MobileNotificationsService();
this.executorService = Executors.newFixedThreadPool(EXECUTOR_NUM_THREADS);
}

private final String OUTPUT_TYPE_ERROR = "error";
private final String OUTPUT_TYPE_STD = "std";

private static int CMD_TIMEOUT_SECONDS = 10;

@Override
public String executeSync(List<String> args) throws CLIException {
Expand Down Expand Up @@ -66,24 +71,15 @@ private List<String> prepareCmd(@NotNull List<String> args) {
}

public void executeAsync(List<String> args, Watcher w) {
final ExecutorService ex = Executors.newFixedThreadPool(3);
List<String> cmd = prepareCmd(args);
ex.execute(() -> {
executorService.execute(() -> {
List<String> cmd = prepareCmd(args);
ProcessBuilder pb = new ProcessBuilder(cmd);

try {
final Process p = pb.start();
Callable<String> inputRead = new Callable<String>() {
@Override public String call() throws Exception {
return readOutput(OUTPUT_TYPE_STD,p.getInputStream(), true);
}
};
Callable<String> errorRead = new Callable<String>() {
@Override public String call() throws Exception {
return readOutput(OUTPUT_TYPE_ERROR,p.getErrorStream(),true);
}
} ;
Future<String> cmdInput = ex.submit(inputRead);
Callable<String> inputRead = () -> readOutput(OUTPUT_TYPE_STD,p.getInputStream(), true);
Callable<String> errorRead = () -> readOutput(OUTPUT_TYPE_ERROR,p.getErrorStream(),true);
Future<String> cmdInput = executorService.submit(inputRead);
Future<String> cmdErr = Executors.newSingleThreadExecutor().submit(errorRead);
String input = cmdInput.get();
String error = cmdErr.get();
Expand All @@ -107,14 +103,20 @@ private String readOutput(String outputType,InputStream in, Boolean shouldNotify
try (BufferedReader bf = new BufferedReader(new InputStreamReader(in))) {
String line;
while ((line = bf.readLine()) != null) {
if (outputType.equals("error") && shouldNotify){
this.notificationsService.notifyError("cli error",line);
} else if (shouldNotify) {
this.notificationsService.notifyInformation("cli output", line);
if (shouldNotify) {
if (outputType.equals(OUTPUT_TYPE_ERROR)) {
this.notificationsService.notifyError("cli error", line);
} else {
this.notificationsService.notifyInformation("cli output", line);
}
}
sb.append(line + "\n");
sb.append(line).append(System.lineSeparator());
}
}
return sb.toString();
}

public static CLIRunner getInstance() {
return instance;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

public class MobileAPI {

private CLIRunner cliRunner;
private final CLIRunner cliRunner;

public MobileAPI(CLIRunner cliRunner) {
this.cliRunner = cliRunner;
Expand All @@ -23,8 +23,7 @@ public MobileServices getServices() throws CLIException {
String outPut = cliRunner.executeSync(Arrays.asList("get", "services", "--", "-o=json"));
Gson gson = new Gson();
try {
MobileServices services = gson.fromJson(outPut, MobileServices.class);
return services;
return gson.fromJson(outPut, MobileServices.class);
} catch (JsonSyntaxException e) {
throw new CLIException("unexpected response from CLI: " + outPut);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,13 @@ public void setAppIdentifier(String appIdentifier) {
return true;
if (o == null || getClass() != o.getClass())
return false;

MobileClientSpec that = (MobileClientSpec) o;

if (name != null ? !name.equals(that.name) : that.name != null)
return false;
if (apiKey != null ? !apiKey.equals(that.apiKey) : that.apiKey != null)
return false;
if (clientType != null ? !clientType.equals(that.clientType) : that.clientType != null)
return false;
return appIdentifier != null ? appIdentifier.equals(that.appIdentifier) : that.appIdentifier == null;

return (name != null ? name.equals(that.name) : that.name == null)
&& (apiKey != null ? apiKey.equals(that.apiKey) : that.apiKey == null)
&& (clientType != null ? clientType.equals(that.clientType) : that.clientType == null)
&& (appIdentifier != null ? appIdentifier.equals(that.appIdentifier) : that.appIdentifier == null);
}

public String toJsonPrettyPrint(){
Expand All @@ -74,11 +71,8 @@ public String toJsonPrettyPrint(){

@Override
public String toString() {
return "{\n" +
" name: " + name + ",\n" +
" apiKey: " + apiKey +",\n" +
" clientType: " + clientType +",\n" +
" appIdentifier: " + appIdentifier +"\n" +
'}';

Gson gson = new GsonBuilder().setPrettyPrinting().create();
return gson.toJson(this);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.aerogear.plugin.intellij.mobile.models;

import java.util.Arrays;

public class MobileServices
{
private ServiceClass[] items = new ServiceClass[0];
Expand All @@ -17,6 +19,6 @@ public void setItems (ServiceClass[] items)
@Override
public String toString()
{
return "items = " + items;
return "items = " + Arrays.toString(items);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
import com.intellij.openapi.vfs.VirtualFile;

import java.io.File;
import java.util.Objects;

public class OpenFileRunnable implements Runnable {
class OpenFileRunnable implements Runnable {

private Project project;
private String path;
private final Project project;
private final String path;


public OpenFileRunnable(Project project, String path) {
Expand All @@ -22,6 +23,6 @@ public OpenFileRunnable(Project project, String path) {
public void run() {
File file = new File(this.path);
VirtualFile vConfig = LocalFileSystem.getInstance().findFileByIoFile(file);
new OpenFileDescriptor(project, vConfig).navigate(true);
new OpenFileDescriptor(project, Objects.requireNonNull(vConfig)).navigate(true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,28 @@

import java.io.File;
import java.io.IOException;
import java.util.Objects;

public class SDKConfigManager {
public void createAndOpenFile(Project project, String path) throws CLIException, IOException {
File configFile = new File(path);
configFile.createNewFile();
boolean fileCreated = configFile.createNewFile();
if (fileCreated){
Runnable updateConfigRunner = new WriteToFileRunnable(project, path, getClientConfig(project));
Runnable openConfigRunner = new OpenFileRunnable(project, path);

Runnable updateConfigRunner = new WriteToFileRunnable(project, path, getClientConfig(project));
Runnable openConfigRunner = new OpenFileRunnable(project, path);

this.asyncRefreshFiles(() -> {
WriteCommandAction.runWriteCommandAction(project, updateConfigRunner);
ApplicationManager.getApplication().runReadAction(openConfigRunner);
AeroGearMobileConfiguration.getInstance(project).setConfigPath(path);
});
this.asyncRefreshFiles(() -> {
WriteCommandAction.runWriteCommandAction(project, updateConfigRunner);
ApplicationManager.getApplication().runReadAction(openConfigRunner);
Objects.requireNonNull(AeroGearMobileConfiguration.getInstance(project)).setConfigPath(path);
});
} else {
Objects.requireNonNull(AeroGearMobileConfiguration.getInstance(project)).setConfigPath(path);
}
}

public void updateSDKConfig(Project project) throws CLIException {
String sdkConfigPath = AeroGearMobileConfiguration.getInstance(project).getConfigPath();
String sdkConfigPath = Objects.requireNonNull(AeroGearMobileConfiguration.getInstance(project)).getConfigPath();
if (sdkConfigPath != null && !sdkConfigPath.isEmpty()) {
this.asyncRefreshFiles(() -> {
Runnable updateConfigRunner = new WriteToFileRunnable(project, sdkConfigPath, getClientConfig(project));
Expand All @@ -39,7 +43,7 @@ public void updateSDKConfig(Project project) throws CLIException {
}

private String getClientName(Project project) {
return AeroGearMobileConfiguration.getInstance(project).getClientName();
return Objects.requireNonNull(AeroGearMobileConfiguration.getInstance(project)).getClientName();
}

private void asyncRefreshFiles(Runnable r) {
Expand All @@ -48,7 +52,7 @@ private void asyncRefreshFiles(Runnable r) {

private CharSequence getClientConfig(Project project) throws CLIException {
String clientName = this.getClientName(project);
return new MobileAPI(new CLIRunnerImpl()).getClientConfig(clientName);
return new MobileAPI(CLIRunnerImpl.getInstance()).getClientConfig(clientName);
}

public static SDKConfigManager getInstance(Project project) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiManager;
import java.io.File;
import java.util.Objects;

public class WriteToFileRunnable implements Runnable {
class WriteToFileRunnable implements Runnable {

private Project project;
private String path;
private CharSequence cs;
private final Project project;
private final String path;
private final CharSequence cs;


public WriteToFileRunnable(Project project, String path, CharSequence cs) {
Expand All @@ -26,8 +27,8 @@ public WriteToFileRunnable(Project project, String path, CharSequence cs) {
public void run() {
File configFile = new File(this.path);
VirtualFile vConfig = LocalFileSystem.getInstance().findFileByIoFile(configFile);
PsiFile psiConfig = PsiManager.getInstance(this.project).findFile(vConfig);
Document d = PsiDocumentManager.getInstance(this.project).getDocument(psiConfig);
d.setText(this.cs);
PsiFile psiConfig = PsiManager.getInstance(this.project).findFile(Objects.requireNonNull(vConfig));
Document d = PsiDocumentManager.getInstance(this.project).getDocument(Objects.requireNonNull(psiConfig));
Objects.requireNonNull(d).setText(this.cs);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,22 @@
import org.aerogear.plugin.intellij.mobile.api.CLIRunner;
import org.aerogear.plugin.intellij.mobile.api.CLIRunnerImpl;
import org.aerogear.plugin.intellij.mobile.api.MobileAPI;
import org.jetbrains.annotations.NotNull;

import java.awt.*;

public class MobileToolWindowFactory implements ToolWindowFactory {
private JBPanel mobileToolWindowContent;
MobileNotificationsService notifier;
private CLIRunner cliRunner = new CLIRunnerImpl();
private final MobileNotificationsService notifier;
private final CLIRunner cliRunner = CLIRunnerImpl.getInstance();


public MobileToolWindowFactory() {
notifier = ServiceManager.getService(MobileNotificationsService.class);
}

@Override
public void createToolWindowContent(Project project, ToolWindow toolWindow){
mobileToolWindowContent = new JBPanel();
public void createToolWindowContent(@NotNull Project project, @NotNull ToolWindow toolWindow){
JBPanel mobileToolWindowContent = new JBPanel();
mobileToolWindowContent.setLayout(new GridLayout(0, 1));
ContentFactory contentFactory = ContentFactory.SERVICE.getInstance();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import org.aerogear.plugin.intellij.mobile.api.MobileAPI;
import org.aerogear.plugin.intellij.mobile.services.MobileNotificationsService;
import org.aerogear.plugin.intellij.mobile.ui.MobileIcons;
import org.aerogear.plugin.intellij.mobile.ui.createclientpopup.Constants;
import org.jetbrains.annotations.NotNull;

public class ClientCreatedCheck implements StartupActivity {
Expand All @@ -20,17 +19,13 @@ public void runActivity(@NotNull Project project) {
checkFile(projectPath + '/' + Constants.DOT_FILENAME, project);
}

public void checkFile(String filePath, @NotNull Project project) {
private void checkFile(String filePath, @NotNull Project project) {
VirtualFile file = LocalFileSystem.getInstance().refreshAndFindFileByPath(filePath);

if (file == null) {
MobileAPI mobileAPI = new MobileAPI(new CLIRunnerImpl());
MobileAPI mobileAPI = new MobileAPI(CLIRunnerImpl.getInstance());

NotificationListener notificationListener = (notification, event) -> {
{
new ClientCreatedCheckAction().showCreateClientForm(project, mobileAPI, filePath);
}
};
NotificationListener notificationListener = (notification, event) -> new ClientCreatedCheckAction().showCreateClientForm(project, mobileAPI, filePath);

new MobileNotificationsService().notifyWarning(
MobileIcons.AEROGEAR,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,22 @@
import org.aerogear.plugin.intellij.mobile.models.MobileClient;
import org.aerogear.plugin.intellij.mobile.services.AeroGearMobileConfiguration;
import org.aerogear.plugin.intellij.mobile.services.MobileNotificationsService;
import org.aerogear.plugin.intellij.mobile.ui.createclientpopup.Constants;
import org.aerogear.plugin.intellij.mobile.ui.createclientpopup.CreateClientForm;
import org.aerogear.plugin.intellij.mobile.utils.WriteToFileRunnable;

import java.util.Objects;

public class ClientCreatedCheckAction extends AnAction {
MobileNotificationsService notificationsService;
private final MobileNotificationsService notificationsService;

public ClientCreatedCheckAction() {
this.notificationsService = new MobileNotificationsService();
}

@Override
public void actionPerformed(AnActionEvent e) {
MobileAPI mobileAPI = new MobileAPI(new CLIRunnerImpl());
String filePath = e.getProject().getBasePath() + "/" + Constants.DOT_FILENAME;
MobileAPI mobileAPI = new MobileAPI(CLIRunnerImpl.getInstance());
String filePath = Objects.requireNonNull(e.getProject()).getBasePath() + "/" + Constants.DOT_FILENAME;
showCreateClientForm(e.getProject(), mobileAPI, filePath);
}

Expand All @@ -34,12 +35,12 @@ public void showCreateClientForm(Project project, MobileAPI mobileAPI, String fi

if (CreateClientForm.OK_EXIT_CODE == clientForm.getExitCode()) {
MobileClient mobileClient = mobileAPI.createClient(clientForm.getName(), clientForm.getClientType(), clientForm.getAppId());
AeroGearMobileConfiguration.getInstance(project).setClientName(mobileClient.getSpec().getName() + "-" + mobileClient.getSpec().getClientType());
Objects.requireNonNull(AeroGearMobileConfiguration.getInstance(project)).setClientName(mobileClient.getSpec().getName() + "-" + mobileClient.getSpec().getClientType());

CharSequence charSeq = mobileClient.getSpec().toJsonPrettyPrint();
WriteToFileRunnable writeToFile = new WriteToFileRunnable(project, filePath, charSeq, Constants.DOT_FILENAME, JsonFileType.INSTANCE);
WriteCommandAction.runWriteCommandAction(project, writeToFile);
this.notificationsService.notifyInformation("Mobile Client", "successfully created a mobile client ");
this.notificationsService.notifyInformation(Constants.MOBILE_CLIENT, "successfully created a mobile client ");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.aerogear.plugin.intellij.mobile.ui.actions;

class Constants {
public static final String AEROGEAR_MOBILE = "Aerogear Mobile";
public static final String MOBILE_CLIENT = "Mobile Client";
public static final String DOT_FILENAME = ".aerogear.json";
}
Loading

0 comments on commit 8fd0a50

Please sign in to comment.