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
Fix icons when sources are processed
  • Loading branch information
Vitalii Chepeliuk committed Feb 16, 2018
1 parent 890a77c commit a92dd42
Show file tree
Hide file tree
Showing 31 changed files with 228 additions and 216 deletions.
6 changes: 5 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -166,5 +166,9 @@ task wrapper(type: Wrapper) {

processResources {
inputs.property "version", project.version
filter(org.apache.tools.ant.filters.ReplaceTokens, tokens: [version: project.version])
eachFile { file ->
if (!file.name.endsWith('.png')) {
filter(org.apache.tools.ant.filters.ReplaceTokens, tokens: [version: project.version])
}
}
}
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;

public CLIRunnerImpl() {

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 = new CLIRunnerImpl();

private 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,32 +71,23 @@ 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);
Future<String> cmdErr = Executors.newSingleThreadExecutor().submit(errorRead);
String input = cmdInput.get();
Callable<String> inputRead = () -> readOutput(OUTPUT_TYPE_STD,p.getInputStream(), true);
Callable<String> errorRead = () -> readOutput(OUTPUT_TYPE_ERROR,p.getErrorStream(),true);
Future<String> cmdOut = executorService.submit(inputRead);
Future<String> cmdErr = executorService.submit(errorRead);
String out = cmdOut.get();
String error = cmdErr.get();
if (!error.isEmpty()) {
w.onError(new CLIException(error));
}
else if (!input.isEmpty()) {
w.onSuccess(input);
else if (!out.isEmpty()) {
w.onSuccess(out);
} else
p.destroyForcibly();
} catch (Exception e) {
Expand All @@ -107,14 +103,22 @@ 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);
}
sb.append(line + "\n");
sb.append(line).append(System.lineSeparator());
}
}

String out = sb.toString();
if (shouldNotify) {
if (outputType.equals(OUTPUT_TYPE_ERROR)) {
this.notificationsService.notifyError("cli error", out);
} else {
this.notificationsService.notifyInformation("cli output", out);
}
}
return sb.toString();
return out;
}

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
Loading

0 comments on commit a92dd42

Please sign in to comment.