Skip to content
This repository was archived by the owner on Aug 4, 2025. It is now read-only.

Commit 6594cac

Browse files
committed
feat: load hidden resources configuration from external file
1 parent 844a07a commit 6594cac

File tree

6 files changed

+192
-59
lines changed

6 files changed

+192
-59
lines changed

src/main/java/tech/jhipster/lite/cli/JHLiteCliApp.java

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package tech.jhipster.lite.cli;
22

3+
import java.nio.file.Files;
4+
import java.nio.file.Path;
35
import org.springframework.boot.Banner;
46
import org.springframework.boot.SpringApplication;
57
import org.springframework.boot.WebApplicationType;
@@ -13,12 +15,34 @@
1315
@ExcludeFromGeneratedCodeCoverage(reason = "Not testing logs")
1416
public class JHLiteCliApp {
1517

18+
private static final String CONFIG_FILE_NAME = "/.config/jhlite-cli.yml";
19+
private static final String SPRING_CONFIG_LOCATION_PROPERTY = "spring.config.location";
20+
private static final String SPRING_CONFIG_LOCATION_VALUE = "classpath:/config/,file:";
21+
1622
public static void main(String[] args) {
17-
ConfigurableApplicationContext context = new SpringApplicationBuilder(JHLiteCliApp.class)
23+
ConfigurableApplicationContext context = loadExternalConfigFile(createApplicationBuilder()).run(args);
24+
25+
System.exit(SpringApplication.exit(context));
26+
}
27+
28+
private static SpringApplicationBuilder createApplicationBuilder() {
29+
return new SpringApplicationBuilder(JHLiteCliApp.class)
1830
.bannerMode(Banner.Mode.OFF)
1931
.web(WebApplicationType.NONE)
20-
.lazyInitialization(true)
21-
.run(args);
22-
System.exit(SpringApplication.exit(context));
32+
.lazyInitialization(true);
33+
}
34+
35+
private static SpringApplicationBuilder loadExternalConfigFile(SpringApplicationBuilder builder) {
36+
String configPath = getConfigPath();
37+
38+
if (Files.exists(Path.of(configPath))) {
39+
builder.properties(SPRING_CONFIG_LOCATION_PROPERTY + "=" + SPRING_CONFIG_LOCATION_VALUE + configPath);
40+
}
41+
42+
return builder;
43+
}
44+
45+
private static String getConfigPath() {
46+
return System.getProperty("user.home") + CONFIG_FILE_NAME;
2347
}
2448
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package tech.jhipster.lite.cli.command.infrastructure.primary;
2+
3+
import static tech.jhipster.lite.TestProjects.newTestFolder;
4+
5+
import java.io.IOException;
6+
import java.nio.file.Files;
7+
import java.nio.file.Path;
8+
import java.util.List;
9+
import picocli.CommandLine;
10+
import tech.jhipster.lite.module.application.JHipsterModulesApplicationService;
11+
import tech.jhipster.lite.module.infrastructure.secondary.git.GitTestUtil;
12+
import tech.jhipster.lite.project.application.ProjectsApplicationService;
13+
14+
class CliFixture {
15+
16+
static Path setupProjectTestFolder() throws IOException {
17+
String projectFolder = newTestFolder();
18+
Path projectPath = Path.of(projectFolder);
19+
Files.createDirectories(projectPath);
20+
loadGitConfig(projectPath);
21+
22+
return projectPath;
23+
}
24+
25+
static void loadGitConfig(Path project) {
26+
GitTestUtil.execute(project, "init");
27+
GitTestUtil.execute(project, "config", "init.defaultBranch", "main");
28+
GitTestUtil.execute(project, "config", "user.email", "\"[email protected]\"");
29+
GitTestUtil.execute(project, "config", "user.name", "\"Test\"");
30+
}
31+
32+
static CommandLine commandLine(JHipsterModulesApplicationService modules, ProjectsApplicationService projects) {
33+
ListModulesCommand listModulesCommand = new ListModulesCommand(modules);
34+
ApplyModuleSubCommandsFactory subCommandsFactory = new ApplyModuleSubCommandsFactory(modules, projects);
35+
ApplyModuleCommand applyModuleCommand = new ApplyModuleCommand(modules, subCommandsFactory);
36+
37+
JHLiteCommandsFactory jhliteCommandsFactory = new JHLiteCommandsFactory(List.of(listModulesCommand, applyModuleCommand), "1", "2");
38+
39+
return new CommandLine(jhliteCommandsFactory.buildCommandSpec());
40+
}
41+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package tech.jhipster.lite.cli.command.infrastructure.primary;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
import static tech.jhipster.lite.cli.command.infrastructure.primary.CliFixture.commandLine;
5+
import static tech.jhipster.lite.cli.command.infrastructure.primary.CliFixture.setupProjectTestFolder;
6+
7+
import java.io.IOException;
8+
import java.nio.file.Path;
9+
import org.junit.jupiter.api.Test;
10+
import org.springframework.beans.factory.annotation.Autowired;
11+
import org.springframework.core.env.Environment;
12+
import org.springframework.test.context.TestPropertySource;
13+
import tech.jhipster.lite.cli.IntegrationTest;
14+
import tech.jhipster.lite.module.application.JHipsterModulesApplicationService;
15+
import tech.jhipster.lite.project.application.ProjectsApplicationService;
16+
17+
@IntegrationTest
18+
@TestPropertySource(properties = { "spring.config.location=classpath:/cli/config/jhlite-cli-hidden-resources.yml" })
19+
class ExternalConfigTest {
20+
21+
@Autowired
22+
private ProjectsApplicationService projects;
23+
24+
@Autowired
25+
private JHipsterModulesApplicationService modules;
26+
27+
@Autowired
28+
private Environment environment;
29+
30+
@Test
31+
void shouldLoadExternalConfiguration() {
32+
assertThat(environment.getProperty("jhlite.hidden-resources.slugs[0]")).isEqualTo("gradle-java");
33+
}
34+
35+
@Test
36+
void shouldHideModuleWhenLoadExternalConfigurationFile() {
37+
String[] args = { "list" };
38+
39+
try (SystemOutputCaptor outputCaptor = new SystemOutputCaptor()) {
40+
int exitCode = commandLine(modules, projects).execute(args);
41+
42+
assertThat(exitCode).isZero();
43+
assertThat(outputCaptor.getOutput()).doesNotContain("gradle-java");
44+
}
45+
}
46+
47+
@Test
48+
void shouldHideApplyCommandWhenLoadExternalConfigurationFile() throws IOException {
49+
Path projectPath = setupProjectTestFolder();
50+
String[] args = { "apply", "gitpod", "--project-path", projectPath.toString() };
51+
52+
try (SystemOutputCaptor outputCaptor = new SystemOutputCaptor()) {
53+
int exitCode = commandLine(modules, projects).execute(args);
54+
55+
assertThat(exitCode).isEqualTo(2);
56+
assertThat(outputCaptor.getOutput()).contains("Unmatched arguments from index");
57+
}
58+
}
59+
}

src/test/java/tech/jhipster/lite/cli/command/infrastructure/primary/JHLiteCommandsFactoryTest.java

Lines changed: 27 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
package tech.jhipster.lite.cli.command.infrastructure.primary;
22

33
import static org.assertj.core.api.Assertions.assertThat;
4-
import static tech.jhipster.lite.TestProjects.newTestFolder;
4+
import static tech.jhipster.lite.cli.command.infrastructure.primary.CliFixture.commandLine;
5+
import static tech.jhipster.lite.cli.command.infrastructure.primary.CliFixture.setupProjectTestFolder;
56

67
import java.io.IOException;
7-
import java.nio.file.Files;
88
import java.nio.file.Path;
9-
import java.util.List;
109
import org.junit.jupiter.api.DisplayName;
1110
import org.junit.jupiter.api.Nested;
1211
import org.junit.jupiter.api.Test;
1312
import org.junit.jupiter.api.extension.ExtendWith;
1413
import org.springframework.beans.factory.annotation.Autowired;
1514
import org.springframework.boot.test.system.CapturedOutput;
1615
import org.springframework.boot.test.system.OutputCaptureExtension;
17-
import picocli.CommandLine;
1816
import tech.jhipster.lite.cli.IntegrationTest;
1917
import tech.jhipster.lite.module.application.JHipsterModulesApplicationService;
2018
import tech.jhipster.lite.module.infrastructure.secondary.git.GitTestUtil;
@@ -42,18 +40,18 @@ class JHLiteCommandsFactoryTest {
4240
void shouldShowHelpMessageWhenNoCommand(CapturedOutput output) {
4341
String[] args = {};
4442

45-
int exitCode = commandLine().execute(args);
43+
int exitCode = commandLine(modules, projects).execute(args);
4644

4745
assertThat(exitCode).isEqualTo(2);
4846
assertThat(output).contains(
49-
"""
50-
JHipster Lite CLI
51-
-h, --help Show this help message and exit.
52-
-V, --version Print version information and exit.
53-
54-
Commands:
55-
"""
56-
);
47+
"""
48+
JHipster Lite CLI
49+
-h, --help Show this help message and exit.
50+
-V, --version Print version information and exit.
51+
52+
Commands:
53+
"""
54+
);
5755
}
5856

5957
@Nested
@@ -64,7 +62,7 @@ class ListModules {
6462
void shouldListModules(CapturedOutput output) {
6563
String[] args = { "list" };
6664

67-
int exitCode = commandLine().execute(args);
65+
int exitCode = commandLine(modules, projects).execute(args);
6866

6967
assertThat(exitCode).isZero();
7068
assertThat(output)
@@ -84,7 +82,7 @@ class ApplyModule {
8482
void shouldNotApplyWithoutModuleSlugSubcommand(CapturedOutput output) {
8583
String[] args = { "apply" };
8684

87-
int exitCode = commandLine().execute(args);
85+
int exitCode = commandLine(modules, projects).execute(args);
8886

8987
assertThat(exitCode).isEqualTo(2);
9088
assertThat(output).contains("Missing required subcommand").contains("init").contains("prettier");
@@ -94,7 +92,7 @@ void shouldNotApplyWithoutModuleSlugSubcommand(CapturedOutput output) {
9492
void shouldEscapeCommandDescriptionInHelpCommand(CapturedOutput output) {
9593
String[] args = { "apply", "--help" };
9694

97-
int exitCode = commandLine().execute(args);
95+
int exitCode = commandLine(modules, projects).execute(args);
9896

9997
assertThat(exitCode).isZero();
10098
assertThat(output).doesNotContain(
@@ -106,7 +104,7 @@ void shouldEscapeCommandDescriptionInHelpCommand(CapturedOutput output) {
106104
void shouldDisplayModuleSlugsInHelpCommand(CapturedOutput output) {
107105
String[] args = { "apply", "--help" };
108106

109-
int exitCode = commandLine().execute(args);
107+
int exitCode = commandLine(modules, projects).execute(args);
110108

111109
assertThat(exitCode).isZero();
112110
assertThat(output)
@@ -128,7 +126,7 @@ void shouldDisplayModuleSlugsInHelpCommand(CapturedOutput output) {
128126
void shouldDisplayModuleSlugsInAlphabeticalOrderInApplyHelpCommand(CapturedOutput output) {
129127
String[] args = { "apply", "--help" };
130128

131-
int exitCode = commandLine().execute(args);
129+
int exitCode = commandLine(modules, projects).execute(args);
132130

133131
assertThat(exitCode).isZero();
134132
assertThat(output.toString().indexOf("angular-core"))
@@ -152,7 +150,7 @@ void shouldApplyInitModuleWithRequiredOptions() throws IOException {
152150
"npm",
153151
};
154152

155-
int exitCode = commandLine().execute(args);
153+
int exitCode = commandLine(modules, projects).execute(args);
156154

157155
assertThat(exitCode).isZero();
158156
assertThat(GitTestUtil.getCommits(projectPath)).contains("Apply module: init");
@@ -165,7 +163,7 @@ void shouldNotApplyInitModuleMissingRequiredOptions(CapturedOutput output) throw
165163
Path projectPath = setupProjectTestFolder();
166164
String[] args = { "apply", "init", "--project-path", projectPath.toString() };
167165

168-
int exitCode = commandLine().execute(args);
166+
int exitCode = commandLine(modules, projects).execute(args);
169167

170168
assertThat(exitCode).isEqualTo(2);
171169
assertThat(GitTestUtil.getCommits(projectPath)).isEmpty();
@@ -193,7 +191,7 @@ void shouldApplyInitModuleWithCommitDefaultValue() throws IOException {
193191
"npm",
194192
};
195193

196-
int exitCode = commandLine().execute(args);
194+
int exitCode = commandLine(modules, projects).execute(args);
197195

198196
assertThat(exitCode).isZero();
199197
assertThat(GitTestUtil.getCommits(projectPath)).contains("Apply module: init");
@@ -221,7 +219,7 @@ void shouldApplyInitModuleWithCommit() throws IOException {
221219
"--commit",
222220
};
223221

224-
int exitCode = commandLine().execute(args);
222+
int exitCode = commandLine(modules, projects).execute(args);
225223

226224
assertThat(exitCode).isZero();
227225
assertThat(GitTestUtil.getCommits(projectPath)).contains("Apply module: init");
@@ -244,7 +242,7 @@ void shouldApplyInitModuleWithoutCommit() throws IOException {
244242
"--no-commit",
245243
};
246244

247-
int exitCode = commandLine().execute(args);
245+
int exitCode = commandLine(modules, projects).execute(args);
248246

249247
assertThat(exitCode).isZero();
250248
assertThat(GitTestUtil.getCommits(projectPath)).isEmpty();
@@ -266,7 +264,7 @@ void shouldNotApplyModuleWithInvalidBaseName() throws IOException {
266264
"npm",
267265
};
268266

269-
int exitCode = commandLine().execute(args);
267+
int exitCode = commandLine(modules, projects).execute(args);
270268

271269
assertThat(exitCode).isEqualTo(1);
272270
}
@@ -289,7 +287,7 @@ void shouldApplyInitModuleWithIndentation() throws IOException {
289287
"4",
290288
};
291289

292-
int exitCode = commandLine().execute(args);
290+
int exitCode = commandLine(modules, projects).execute(args);
293291

294292
assertThat(exitCode).isZero();
295293
assertThat(projectPropertyValue(projectPath, INDENT_SIZE)).isEqualTo(4);
@@ -313,7 +311,7 @@ void shouldApplyInitModuleWithEndOfLine() throws IOException {
313311
"lf",
314312
};
315313

316-
int exitCode = commandLine().execute(args);
314+
int exitCode = commandLine(modules, projects).execute(args);
317315

318316
assertThat(exitCode).isZero();
319317
assertThat(projectPropertyValue(projectPath, END_OF_LINE)).isEqualTo("lf");
@@ -334,7 +332,7 @@ void shouldReuseParametersFromPreviousModuleApplications() throws IOException {
334332
"--node-package-manager",
335333
"npm",
336334
};
337-
int initModuleExitCode = commandLine().execute(initModuleArgs);
335+
int initModuleExitCode = commandLine(modules, projects).execute(initModuleArgs);
338336
assertThat(initModuleExitCode).isZero();
339337
String[] mavenJavaModuleArgs = {
340338
"apply",
@@ -345,7 +343,7 @@ void shouldReuseParametersFromPreviousModuleApplications() throws IOException {
345343
"com.my.company",
346344
};
347345

348-
int mavenJavaModuleExitCode = commandLine().execute(mavenJavaModuleArgs);
346+
int mavenJavaModuleExitCode = commandLine(modules, projects).execute(mavenJavaModuleArgs);
349347

350348
assertThat(mavenJavaModuleExitCode).isZero();
351349
assertThat(projectPropertyValue(projectPath, PROJECT_NAME)).isEqualTo("JHipster Sample Application");
@@ -357,36 +355,10 @@ void shouldReuseParametersFromPreviousModuleApplications() throws IOException {
357355
void shouldShowVersion(CapturedOutput output) {
358356
String[] args = { "--version" };
359357

360-
int exitCode = commandLine().execute(args);
358+
int exitCode = commandLine(modules, projects).execute(args);
361359

362360
assertThat(exitCode).isZero();
363361
assertThat(output).contains("JHipster Lite CLI v1").contains("JHipster Lite version: 2");
364362
}
365-
366-
private static Path setupProjectTestFolder() throws IOException {
367-
String projectFolder = newTestFolder();
368-
Path projectPath = Path.of(projectFolder);
369-
Files.createDirectories(projectPath);
370-
loadGitConfig(projectPath);
371-
372-
return projectPath;
373-
}
374-
375-
private static void loadGitConfig(Path project) {
376-
GitTestUtil.execute(project, "init");
377-
GitTestUtil.execute(project, "config", "init.defaultBranch", "main");
378-
GitTestUtil.execute(project, "config", "user.email", "\"[email protected]\"");
379-
GitTestUtil.execute(project, "config", "user.name", "\"Test\"");
380-
}
381-
}
382-
383-
private CommandLine commandLine() {
384-
ListModulesCommand listModulesCommand = new ListModulesCommand(modules);
385-
ApplyModuleSubCommandsFactory subCommandsFactory = new ApplyModuleSubCommandsFactory(modules, projects);
386-
ApplyModuleCommand applyModuleCommand = new ApplyModuleCommand(modules, subCommandsFactory);
387-
388-
JHLiteCommandsFactory jhliteCommandsFactory = new JHLiteCommandsFactory(List.of(listModulesCommand, applyModuleCommand), "1", "2");
389-
390-
return new CommandLine(jhliteCommandsFactory.buildCommandSpec());
391363
}
392364
}

0 commit comments

Comments
 (0)