Skip to content

Commit

Permalink
Merge pull request #169 from wimdeblauwe/feature/tailwind-handling-re…
Browse files Browse the repository at this point in the history
…factor

refactor: Make live reload selection simpler by moving the Tailwind C…
  • Loading branch information
wimdeblauwe authored Oct 14, 2024
2 parents cb9fab6 + 914182d commit 35d628b
Show file tree
Hide file tree
Showing 17 changed files with 139 additions and 48 deletions.
18 changes: 9 additions & 9 deletions src/main/java/io/github/wimdeblauwe/ttcli/Init.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package io.github.wimdeblauwe.ttcli;

import io.github.wimdeblauwe.ttcli.boot.*;
import io.github.wimdeblauwe.ttcli.tailwind.TailwindDependency;
import io.github.wimdeblauwe.ttcli.deps.TailwindCssWebDependency;
import io.github.wimdeblauwe.ttcli.deps.WebDependency;
import io.github.wimdeblauwe.ttcli.livereload.LiveReloadInitService;
import io.github.wimdeblauwe.ttcli.livereload.LiveReloadInitServiceFactory;
import io.github.wimdeblauwe.ttcli.livereload.LiveReloadInitServiceParameters;
import io.github.wimdeblauwe.ttcli.tailwind.TailwindDependency;
import io.github.wimdeblauwe.ttcli.util.InetUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.shell.component.context.ComponentContext;
Expand Down Expand Up @@ -58,11 +59,12 @@ public void init(@ShellOption(defaultValue = ".") String baseDir) throws IOExcep
String projectName = context.get("project-name");
String springBootVersion = context.get("spring-boot-version");

List<TailwindDependency> selectedTailwindDependencies = allowTailwindDependenciesSelection(context.get("live-reload"));

List<String> selectedWebDependencyOptions = context.get("web-dependencies");
List<WebDependency> selectedWebDependencies = webDependencies.stream().filter(webDependency -> selectedWebDependencyOptions.contains(webDependency.id())).toList();

boolean hasTailwindCssWebDependency = webDependencies.stream().anyMatch(webDependency -> webDependency instanceof TailwindCssWebDependency);
List<TailwindDependency> selectedTailwindDependencies = allowTailwindDependenciesSelection(hasTailwindCssWebDependency);

Path basePath = Path.of(baseDir).resolve(artifactId);

projectInitializationService.initialize(new ProjectInitializationParameters(basePath,
Expand All @@ -82,11 +84,8 @@ public void init(@ShellOption(defaultValue = ".") String baseDir) throws IOExcep
}
}

private List<TailwindDependency> allowTailwindDependenciesSelection(String liveReloadSelection) {
boolean tailwindAvailable = "npm-based-with-tailwind-css".equals(liveReloadSelection)
|| "dev-tools-based-with-tailwind-css".equals(liveReloadSelection)
|| "vite-with-tailwind-css".equals(liveReloadSelection);
if (tailwindAvailable) {
private List<TailwindDependency> allowTailwindDependenciesSelection(boolean hasTailwindCssWebDependency) {
if (hasTailwindCssWebDependency) {
ComponentFlow.Builder builder = flowBuilder.clone().reset();
addTailwindDependenciesInput(builder);
ComponentFlow build = builder.build();
Expand Down Expand Up @@ -148,7 +147,7 @@ private void addSpringBootVersionInput(ComponentFlow.Builder builder) {
private void addLiveReloadInput(ComponentFlow.Builder builder) {
Map<String, String> reloadOptions = new HashMap<>();
List<String> reloadOptionIdsInOrder = new ArrayList<>();
for (LiveReloadInitService initService : liveReloadInitServiceFactory.getInitServices()) {
for (LiveReloadInitService initService : liveReloadInitServiceFactory.getNormalServices()) {
reloadOptions.put(initService.getName(), initService.getId());
reloadOptionIdsInOrder.add(initService.getId());
}
Expand All @@ -170,6 +169,7 @@ private void addWebDependenciesInput(ComponentFlow.Builder builder) {
private List<SelectItem> buildWebDependencyOptions() {
return webDependencies.stream()
.map(webDependency -> new DefaultSelectItem(webDependency.displayName(), webDependency.id(), true, false))
.sorted(Comparator.comparing(DefaultSelectItem::name))
.collect(Collectors.toList());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package io.github.wimdeblauwe.ttcli;

import io.github.wimdeblauwe.ttcli.boot.SpringBootProjectCreationParameters;
import io.github.wimdeblauwe.ttcli.tailwind.TailwindDependency;
import io.github.wimdeblauwe.ttcli.deps.TailwindCssWebDependency;
import io.github.wimdeblauwe.ttcli.deps.WebDependency;
import io.github.wimdeblauwe.ttcli.livereload.LiveReloadInitServiceParameters;
import io.github.wimdeblauwe.ttcli.tailwind.TailwindDependency;

import java.nio.file.Path;
import java.util.List;
Expand All @@ -16,4 +17,8 @@ public record ProjectInitializationParameters(Path basePath,
public String projectName() {
return springBootProjectCreationParameters().projectName();
}

public boolean hasTailwindCssWebDependency() {
return webDependencies.stream().anyMatch(webDependency -> webDependency instanceof TailwindCssWebDependency);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import io.github.wimdeblauwe.ttcli.tailwind.TailwindDependencyInitService;
import io.github.wimdeblauwe.ttcli.thymeleaf.ThymeleafTemplatesInitService;
import io.github.wimdeblauwe.ttcli.util.FileUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.io.IOException;
Expand All @@ -17,6 +19,7 @@

@Component
public class ProjectInitializationService {
private static final Logger LOGGER = LoggerFactory.getLogger(ProjectInitializationService.class);
private final SpringBootInitializrService initializrService;
private final JavaCodeInitService javaCodeInitService;
private final LiveReloadInitServiceFactory liveReloadInitServiceFactory;
Expand Down Expand Up @@ -50,8 +53,9 @@ public void initialize(ProjectInitializationParameters parameters) throws IOExce
Files.createDirectories(basePath);
}

LiveReloadInitService liveReloadInitService = liveReloadInitServiceFactory.getInitService(parameters.liveReloadInitServiceParameters().initServiceId());

LiveReloadInitService liveReloadInitService = liveReloadInitServiceFactory.getInitService(parameters.liveReloadInitServiceParameters().initServiceId(),
parameters.hasTailwindCssWebDependency());
LOGGER.info("Using init service {}", liveReloadInitService);
initializrService.generate(basePath,
parameters.springBootProjectCreationParameters(),
liveReloadInitService.additionalSpringInitializrDependencies());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import java.util.List;

@Component
public class AlpineWebDependency implements WebDependency {
public class AlpineWebDependency implements WebjarsBasedWebDependency {
@Override
public String id() {
return "alpinejs";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import java.util.List;

@Component
public class BootstrapWebDependency implements WebDependency {
public class BootstrapWebDependency implements WebjarsBasedWebDependency {
@Override
public String id() {
return "bootstrap";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import java.util.List;

@Component
public class HtmxWebDependency implements WebDependency {
public class HtmxWebDependency implements WebjarsBasedWebDependency {

@Override
public String id() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* See <a href="https://shoelace.style/">https://shoelace.style/</a>
*/
@Component
public class ShoelaceWebDependency implements WebDependency {
public class ShoelaceWebDependency implements WebjarsBasedWebDependency {
@Override
public String id() {
return "shoelace";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.github.wimdeblauwe.ttcli.deps;

import org.springframework.stereotype.Component;

@Component
public class TailwindCssWebDependency implements WebDependency {
@Override
public String id() {
return "tailwind-css";
}

@Override
public String displayName() {
return "Tailwind CSS";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,4 @@ public interface WebDependency {
String id();

String displayName();

List<MavenDependency> getMavenDependencies(String springBootVersion);

String getCssLinksForLayoutTemplate();

String getJsLinksForLayoutTemplate();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.github.wimdeblauwe.ttcli.deps;

import io.github.wimdeblauwe.ttcli.maven.MavenDependency;

import java.util.List;

public interface WebjarsBasedWebDependency extends WebDependency {
List<MavenDependency> getMavenDependencies(String springBootVersion);

String getCssLinksForLayoutTemplate();

String getJsLinksForLayoutTemplate();

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,46 @@

import java.util.List;

import static java.util.function.Predicate.not;

@Component
public class LiveReloadInitServiceFactory {
private final List<LiveReloadInitService> initServices;
private final List<LiveReloadInitService> allServices;
private final List<LiveReloadInitService> normalServices;
private final List<TailwindCssSpecializedLiveReloadInitService> tailwindCssSpecializedServices;

public LiveReloadInitServiceFactory(List<LiveReloadInitService> allServices) {
this.allServices = allServices;
this.normalServices = allServices.stream().filter(not(TailwindCssSpecializedLiveReloadInitService.class::isInstance)).toList();
this.tailwindCssSpecializedServices = allServices.stream().filter(TailwindCssSpecializedLiveReloadInitService.class::isInstance)
.map(s -> (TailwindCssSpecializedLiveReloadInitService) s)
.toList();
}

public List<LiveReloadInitService> getAllServices() {
return allServices;
}

public List<LiveReloadInitService> getNormalServices() {
return normalServices;
}

public LiveReloadInitServiceFactory(List<LiveReloadInitService> initServices) {
this.initServices = initServices;
public List<TailwindCssSpecializedLiveReloadInitService> getTailwindCssSpecializedServices() {
return tailwindCssSpecializedServices;
}

public List<LiveReloadInitService> getInitServices() {
return initServices;
public LiveReloadInitService getInitService(String initServiceId,
boolean hasTailwindCssWebDependency) {
return allServices.stream().filter(service -> service.getId().equals(initServiceId))
.findAny()
.map(s -> hasTailwindCssWebDependency ? getCorrespondingTailwindCssSpecializedLiveReloadInitService(s) : s)
.orElseThrow(() -> new IllegalArgumentException("Not a known service: " + initServiceId));
}

public LiveReloadInitService getInitService(String initServiceId) {
return initServices.stream().filter(service -> service.getId().equals(initServiceId))
.findAny()
.orElseThrow(() -> new IllegalArgumentException("Not a known service: " + initServiceId));
private TailwindCssSpecializedLiveReloadInitService getCorrespondingTailwindCssSpecializedLiveReloadInitService(LiveReloadInitService initService) {
return getTailwindCssSpecializedServices().stream()
.filter(t -> t.isTailwindVersionOf(initService.getClass()))
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("Could not find the corresponding tailwind version of " + initService.getClass()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package io.github.wimdeblauwe.ttcli.livereload;

public interface TailwindCssSpecializedLiveReloadInitService extends LiveReloadInitService {
boolean isTailwindVersionOf(Class<? extends LiveReloadInitService> liveReloadInitServiceClass);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.github.wimdeblauwe.ttcli.ProjectInitializationParameters;
import io.github.wimdeblauwe.ttcli.livereload.LiveReloadInitService;
import io.github.wimdeblauwe.ttcli.livereload.LiveReloadInitServiceException;
import io.github.wimdeblauwe.ttcli.livereload.TailwindCssSpecializedLiveReloadInitService;
import io.github.wimdeblauwe.ttcli.livereload.helper.TailwindCssHelper;
import io.github.wimdeblauwe.ttcli.maven.MavenPomReaderWriter;
import io.github.wimdeblauwe.ttcli.npm.InstalledApplicationVersions;
Expand All @@ -21,7 +22,7 @@

@Component
@Order(4)
public class DevToolsBasedWithTailwindCssLiveReloadInitService implements LiveReloadInitService {
public class DevToolsBasedWithTailwindCssLiveReloadInitService implements LiveReloadInitService, TailwindCssSpecializedLiveReloadInitService {
private final NodeService nodeService;

public DevToolsBasedWithTailwindCssLiveReloadInitService(NodeService nodeService) {
Expand Down Expand Up @@ -191,4 +192,8 @@ private void updateGitIgnore(Path base) throws IOException {
}
}

@Override
public boolean isTailwindVersionOf(Class<? extends LiveReloadInitService> liveReloadInitServiceClass) {
return liveReloadInitServiceClass.isAssignableFrom(DevToolsBasedLiveReloadInitService.class);
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
package io.github.wimdeblauwe.ttcli.livereload.npm;

import io.github.wimdeblauwe.ttcli.ProjectInitializationParameters;
import io.github.wimdeblauwe.ttcli.livereload.LiveReloadInitService;
import io.github.wimdeblauwe.ttcli.livereload.LiveReloadInitServiceException;
import io.github.wimdeblauwe.ttcli.livereload.TailwindCssSpecializedLiveReloadInitService;
import io.github.wimdeblauwe.ttcli.livereload.helper.TailwindCssHelper;
import io.github.wimdeblauwe.ttcli.npm.NodeService;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.nio.file.Path;
import java.util.*;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

@Component
@Order(2)
public class NpmBasedWithTailwindCssLiveReloadInitService extends NpmBasedLiveReloadInitService {
public class NpmBasedWithTailwindCssLiveReloadInitService extends NpmBasedLiveReloadInitService implements TailwindCssSpecializedLiveReloadInitService {
public NpmBasedWithTailwindCssLiveReloadInitService(NodeService nodeService) {
super(nodeService);
}
Expand Down Expand Up @@ -81,4 +85,9 @@ protected LinkedHashMap<String, String> npmScripts() {
scripts.put("watch:serve", "browser-sync start --no-inject-changes --proxy localhost:8080 --files \"target/classes/templates\" \"target/classes/static\"");
return scripts;
}

@Override
public boolean isTailwindVersionOf(Class<? extends LiveReloadInitService> liveReloadInitServiceClass) {
return liveReloadInitServiceClass.isAssignableFrom(NpmBasedLiveReloadInitService.class);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package io.github.wimdeblauwe.ttcli.livereload.vite;

import io.github.wimdeblauwe.ttcli.ProjectInitializationParameters;
import io.github.wimdeblauwe.ttcli.livereload.LiveReloadInitService;
import io.github.wimdeblauwe.ttcli.livereload.LiveReloadInitServiceException;
import io.github.wimdeblauwe.ttcli.livereload.TailwindCssSpecializedLiveReloadInitService;
import io.github.wimdeblauwe.ttcli.livereload.helper.TailwindCssHelper;
import io.github.wimdeblauwe.ttcli.npm.NodeService;
import org.springframework.core.annotation.Order;
Expand All @@ -13,7 +15,7 @@

@Component
@Order(6)
public class ViteWithTailwindCssLiveReloadInitService extends ViteLiveReloadInitService {
public class ViteWithTailwindCssLiveReloadInitService extends ViteLiveReloadInitService implements TailwindCssSpecializedLiveReloadInitService {
public ViteWithTailwindCssLiveReloadInitService(NodeService nodeService) {
super(nodeService);
}
Expand Down Expand Up @@ -60,4 +62,8 @@ protected List<String> npmDevDependencies() {
"tailwindcss", "postcss", "autoprefixer");
}

@Override
public boolean isTailwindVersionOf(Class<? extends LiveReloadInitService> liveReloadInitServiceClass) {
return liveReloadInitServiceClass.isAssignableFrom(ViteLiveReloadInitService.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io.github.wimdeblauwe.ttcli.ProjectInitializationParameters;
import io.github.wimdeblauwe.ttcli.deps.WebDependency;
import io.github.wimdeblauwe.ttcli.deps.WebjarsBasedWebDependency;
import org.jsoup.nodes.Comment;
import org.springframework.stereotype.Component;

Expand Down Expand Up @@ -29,9 +30,11 @@ private void addMavenDependencies(MavenPomReaderWriter mavenPomReaderWriter,
});
mavenPomReaderWriter.addDependency("org.webjars", "webjars-locator", "0.46");
for (WebDependency webDependency : webDependencies) {
List<MavenDependency> mavenDependencies = webDependency.getMavenDependencies(springBootVersion);
for (MavenDependency mavenDependency : mavenDependencies) {
mavenPomReaderWriter.addDependency(mavenDependency);
if (webDependency instanceof WebjarsBasedWebDependency webjarsBasedWebDependency) {
List<MavenDependency> mavenDependencies = webjarsBasedWebDependency.getMavenDependencies(springBootVersion);
for (MavenDependency mavenDependency : mavenDependencies) {
mavenPomReaderWriter.addDependency(mavenDependency);
}
}
}

Expand Down
Loading

0 comments on commit 35d628b

Please sign in to comment.