Skip to content

Commit

Permalink
Merge pull request #8 from penguineer/software-version
Browse files Browse the repository at this point in the history
Display the software version
  • Loading branch information
penguineer authored Jul 19, 2024
2 parents db83e3b + 178608c commit b0b03ab
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.penguineering.gartenplus;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Component;

import java.util.Optional;

@Component
public class ApplicationContextProvider implements ApplicationContextAware {
private static ApplicationContext applicationContext = null;

@Override
public void setApplicationContext(@NonNull ApplicationContext applicationContext) throws BeansException {
ApplicationContextProvider.applicationContext = applicationContext;
}

public static Optional<ApplicationContext> getApplicationContext() {
return Optional.ofNullable(applicationContext);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
package com.penguineering.gartenplus.ui.appframe;

import com.penguineering.gartenplus.ApplicationContextProvider;
import com.penguineering.gartenplus.auth.user.UserDTO;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.html.H1;
import com.vaadin.flow.component.html.Span;
import com.vaadin.flow.component.orderedlayout.FlexComponent;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.theme.lumo.LumoUtility;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.util.Map;
import java.util.Optional;
import java.util.function.Supplier;

public class GartenplusHeader extends Div {
Expand All @@ -18,14 +26,40 @@ public GartenplusHeader(Supplier<UserDTO> currentUser) {

headerLayout.add(new GartenplusLogo());

headerLayout.add(new H1("GartenPlus"));
HorizontalLayout headlineLayout = new HorizontalLayout();
headlineLayout.setPadding(false);
headlineLayout.setMargin(false);
headlineLayout.setAlignItems(FlexComponent.Alignment.BASELINE);
headerLayout.add(headlineLayout);

headlineLayout.add(new H1("GartenPlus"));

Optional.ofNullable(findVersion())
.or(() -> Optional.of("local"))
.map(Span::new)
.ifPresent(headlineLayout::add);


Div spacer = new Div();
spacer.setWidthFull();
headerLayout.add(spacer);
headerLayout.setFlexGrow(1, spacer);

headerLayout.add(new LoggedUserView(currentUser));

this.add(headerLayout);
}

private static String findVersion() {
// TODO better decoupling from the application context
return ApplicationContextProvider.getApplicationContext()
.map(c -> c.getBeansWithAnnotation(SpringBootApplication.class))
.map(Map::entrySet)
.flatMap(entries -> entries.stream().findFirst())
.map(Map.Entry::getValue)
.map(Object::getClass)
.map(Class::getPackage)
.map(Package::getImplementationVersion)
.orElse(null);
}
}

0 comments on commit b0b03ab

Please sign in to comment.