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

Priorize init module #3476

Merged
merged 1 commit into from
Sep 9, 2022
Merged
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
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"));
}
}