Skip to content
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

Support custom UI. #310

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 29 additions & 8 deletions cli/src/main/java/gyro/cli/Gyro.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import java.io.File;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
Expand All @@ -41,6 +43,7 @@
import gyro.core.command.VersionCommand;
import gyro.core.scope.Defer;
import gyro.core.scope.RootScope;
import gyro.core.ui.GyroUINotAvailableException;
import gyro.core.validation.ValidationErrorException;
import gyro.lang.Locatable;
import gyro.lang.SyntaxError;
Expand Down Expand Up @@ -71,23 +74,38 @@ public class Gyro {
public static void main(String[] arguments) {
((Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME)).setLevel(Level.OFF);

Gyro gyro = new Gyro();
GyroCore.pushUi(new CliGyroUI());

int exitStatus = 0;
boolean uiOverridden = false;

try {
Optional.ofNullable(GyroCore.getRootDirectory())
.map(d -> new RootScope(GyroCore.INIT_FILE, new LocalFileBackend(d), null, null))
.ifPresent(r -> {
r.load();
GyroCore.putStateBackends(r.getSettings(StateBackendSettings.class).getStateBackends());
GyroCore.pushLockBackend(r.getSettings(LockBackendSettings.class).getLockBackend());
});
Path rootDirectory = GyroCore.getRootDirectory();

if (rootDirectory != null) {
if (Files.exists(rootDirectory.resolve(GyroCore.UI_FILE).normalize())) {
new RootScope(GyroCore.UI_FILE, new LocalFileBackend(rootDirectory), null, null).load();
uiOverridden = true;
}

Optional.of(rootDirectory)
.map(d -> new RootScope(GyroCore.INIT_FILE, new LocalFileBackend(d), null, null))
.ifPresent(r -> {
r.load();
GyroCore.putStateBackends(r.getSettings(StateBackendSettings.class).getStateBackends());
GyroCore.pushLockBackend(r.getSettings(LockBackendSettings.class).getLockBackend());
});
}

Gyro gyro = new Gyro();
gyro.init(Arrays.asList(arguments));
gyro.run();

} catch (GyroUINotAvailableException e) {
exitStatus = 4;
System.err.print("GyroCloudUI is not available.: ");
System.err.println(e.getMessage());

} catch (Abort error) {
exitStatus = 3;
GyroCore.ui().write("\n@|red Aborted!|@\n\n");
Expand All @@ -99,6 +117,9 @@ public static void main(String[] arguments) {
GyroCore.ui().write("\n");

} finally {
if (uiOverridden) {
GyroCore.popUi();
}
GyroCore.popUi();
GyroCore.popLockBackend();
System.exit(exitStatus);
Expand Down
2 changes: 2 additions & 0 deletions core/src/main/java/gyro/core/GyroCore.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@

public class GyroCore {

public static final String UI_FILE = ".gyro/ui.gyro";

public static final String INIT_FILE = ".gyro/init.gyro";

private static final ThreadLocalStack<GyroUI> UI = new ThreadLocalStack<>();
Expand Down
6 changes: 5 additions & 1 deletion core/src/main/java/gyro/core/scope/RootScope.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@
import gyro.core.scope.converter.IdObjectToResource;
import gyro.core.scope.converter.IterableToOne;
import gyro.core.scope.converter.ResourceToIdObject;
import gyro.core.ui.UIDirectiveProcessor;
import gyro.core.ui.UIPlugin;
import gyro.core.validation.ValidationError;
import gyro.core.validation.ValidationErrorException;
import gyro.core.virtual.VirtualDirectiveProcessor;
Expand Down Expand Up @@ -171,7 +173,8 @@ public RootScope(
new ModificationPlugin(),
new ReferencePlugin(),
new ResourcePlugin(),
new RootPlugin())
new RootPlugin(),
new UIPlugin())
.forEach(p -> getSettings(PluginSettings.class).getPlugins().add(p));

Stream.of(
Expand Down Expand Up @@ -200,6 +203,7 @@ public RootScope(
RepositoryDirectiveProcessor.class,
StateBackendDirectiveProcessor.class,
TypeDescriptionDirectiveProcessor.class,
UIDirectiveProcessor.class,
UpdateDirectiveProcessor.class,
UsesCredentialsDirectiveProcessor.class,
VirtualDirectiveProcessor.class,
Expand Down
32 changes: 32 additions & 0 deletions core/src/main/java/gyro/core/ui/GyroUINotAvailableException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright 2020, Perfect Sense, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package gyro.core.ui;

public class GyroUINotAvailableException extends RuntimeException {

public GyroUINotAvailableException(String message) {
super(message);
}

public GyroUINotAvailableException(String message, Throwable cause) {
super(message, cause);
}

public GyroUINotAvailableException(Throwable cause) {
super(cause);
}
}
67 changes: 67 additions & 0 deletions core/src/main/java/gyro/core/ui/UIDirectiveProcessor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright 2020, Perfect Sense, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package gyro.core.ui;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;

import com.google.common.base.CaseFormat;
import gyro.core.GyroCore;
import gyro.core.GyroException;
import gyro.core.GyroUI;
import gyro.core.Reflections;
import gyro.core.Type;
import gyro.core.directive.DirectiveProcessor;
import gyro.core.scope.RootScope;
import gyro.core.scope.Scope;
import gyro.lang.ast.block.DirectiveNode;

@Type("ui")
public class UIDirectiveProcessor extends DirectiveProcessor<RootScope> {

@Override
public void process(RootScope scope, DirectiveNode node) throws Exception {
validateArguments(node, 1, 1);

String type = getArgument(scope, node, String.class, 0);

UISettings settings = scope.getSettings(UISettings.class);
Class<? extends GyroUI> uiClass = settings.getUiClasses().get(type);

if (uiClass == null) {
throw new GyroException(
"Make sure @|magenta '@plugin'|@ directive for a UI provider is placed before @|magenta '@ui'|@ directive in @|magenta '.gyro/ui.gyro'|@ file.");
}

GyroUI ui = Reflections.newInstance(uiClass);

Scope bodyScope = evaluateBody(scope, node);

for (PropertyDescriptor property : Reflections.getBeanInfo(uiClass).getPropertyDescriptors()) {
Method setter = property.getWriteMethod();
Object value = bodyScope.get(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_HYPHEN, property.getName()));

if (setter != null && value != null) {
Reflections.invoke(setter, ui, scope.convertValue(setter.getGenericParameterTypes()[0], value));
}
}

if (!uiClass.isInstance(GyroCore.ui())) {
GyroCore.pushUi(ui);
}
}
}
37 changes: 37 additions & 0 deletions core/src/main/java/gyro/core/ui/UIPlugin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2020, Perfect Sense, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package gyro.core.ui;

import gyro.core.GyroUI;
import gyro.core.Reflections;
import gyro.core.plugin.Plugin;
import gyro.core.scope.RootScope;

public class UIPlugin extends Plugin {

@Override
public void onEachClass(RootScope root, Class<?> aClass) throws Exception {
if (GyroUI.class.isAssignableFrom(aClass)) {
@SuppressWarnings("unchecked")
Class<? extends GyroUI> uiClass = (Class<? extends GyroUI>) aClass;
String namespace = Reflections.getNamespace(uiClass);
String type = Reflections.getTypeOptional(uiClass).orElse("ui");

root.getSettings(UISettings.class).getUiClasses().put(namespace + "::" + type, uiClass);
}
}
}
39 changes: 39 additions & 0 deletions core/src/main/java/gyro/core/ui/UISettings.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright 2020, Perfect Sense, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package gyro.core.ui;

import java.util.HashMap;
import java.util.Map;

import gyro.core.GyroUI;
import gyro.core.scope.Settings;

public class UISettings extends Settings {

private Map<String, Class<? extends GyroUI>> uiClasses;

public Map<String, Class<? extends GyroUI>> getUiClasses() {
if (uiClasses == null) {
uiClasses = new HashMap<>();
}
return uiClasses;
}

public void setUiClasses(Map<String, Class<? extends GyroUI>> uiClasses) {
this.uiClasses = uiClasses;
}
}