Skip to content

Commit

Permalink
Merge pull request #3476 from DamnClin/init-order
Browse files Browse the repository at this point in the history
Priorize init module
  • Loading branch information
DamnClin authored Sep 9, 2022
2 parents a426fd8 + 540db66 commit a5033c0
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
19 changes: 18 additions & 1 deletion src/main/java/tech/jhipster/lite/module/domain/JHipsterSlug.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import tech.jhipster.lite.common.domain.Generated;
import tech.jhipster.lite.error.domain.Assert;

public abstract sealed class JHipsterSlug permits JHipsterModuleSlug, JHipsterFeatureSlug {
public abstract sealed class JHipsterSlug implements Comparable<JHipsterSlug> permits JHipsterModuleSlug, JHipsterFeatureSlug {

private static final Pattern SLUG_FORMAT = Pattern.compile("^[a-z1-9-]+$");

Expand Down Expand Up @@ -34,6 +34,23 @@ public String get() {
return slug;
}

@Override
public int compareTo(JHipsterSlug other) {
if (isInit()) {
return -1;
}

if (other.isInit()) {
return 1;
}

return get().compareTo(other.get());
}

private boolean isInit() {
return get().equals("init");
}

@Override
@Generated
public int hashCode() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public Collection<JHipsterModuleSlug> sort(Collection<JHipsterModuleSlug> module
}

private Function<JHipsterLandscapeLevel, Stream<JHipsterModuleSlug>> toLevelModules(Collection<JHipsterModuleSlug> modules) {
return level -> modules.stream().filter(inLevel(level));
return level -> modules.stream().filter(inLevel(level)).sorted();
}

private Predicate<JHipsterModuleSlug> inLevel(JHipsterLandscapeLevel level) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import static org.assertj.core.api.Assertions.*;

import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import tech.jhipster.lite.UnitTest;
Expand All @@ -14,4 +16,19 @@ class JHipsterModuleSlugTest {
void shouldNotBuildInvalidSlug(String slug) {
assertThatThrownBy(() -> new JHipsterModuleSlug(slug)).isExactlyInstanceOf(InvalidJHipsterSlugException.class);
}

@Test
void shouldSortModules() {
Stream<JHipsterModuleSlug> modules = Stream
.of(new JHipsterModuleSlug("root"), new JHipsterModuleSlug("init"), new JHipsterModuleSlug("dummy"), new JHipsterModuleSlug("init"))
.sorted();

assertThat(modules)
.containsExactly(
new JHipsterModuleSlug("init"),
new JHipsterModuleSlug("init"),
new JHipsterModuleSlug("dummy"),
new JHipsterModuleSlug("root")
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -209,4 +209,14 @@ void shouldSortModuleInApplicableOrder() {
assertThat(landscape.sort(List.of(moduleSlug("second"), moduleSlug("first"))))
.containsExactly(moduleSlug("first"), moduleSlug("second"));
}

@Test
void shouldSortInitAsFirstModule() {
JHipsterModuleResource init = defaultModuleResourceBuilder().slug("init").build();
JHipsterModuleResource root = defaultModuleResourceBuilder().slug("root").build();

JHipsterLandscape landscape = JHipsterLandscape.from(moduleResources(root, init));

assertThat(landscape.sort(List.of(moduleSlug("root"), moduleSlug("init")))).containsExactly(moduleSlug("init"), moduleSlug("root"));
}
}

0 comments on commit a5033c0

Please sign in to comment.