Skip to content

Commit

Permalink
feat(cli/java): resolve latest lts version
Browse files Browse the repository at this point in the history
  • Loading branch information
viceice committed Jun 4, 2024
1 parent b4c96f2 commit 9a44ab0
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/cli/install-tool/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ import {
InstallJavaService,
} from '../tools/java';
import { InstallMavenService } from '../tools/java/maven';
import {
JavaJdkVersionResolver,
JavaJreVersionResolver,
JavaVersionResolver,
} from '../tools/java/resolver';
import { InstallNodeService } from '../tools/node';
import {
InstallRenovateService,
Expand Down Expand Up @@ -77,6 +82,9 @@ function prepareResolveContainer(): Container {

// tool version resolver
container.bind(TOOL_VERSION_RESOLVER).to(NodeVersionResolver);
container.bind(TOOL_VERSION_RESOLVER).to(JavaVersionResolver);
container.bind(TOOL_VERSION_RESOLVER).to(JavaJreVersionResolver);
container.bind(TOOL_VERSION_RESOLVER).to(JavaJdkVersionResolver);

logger.trace('preparing container done');
return container;
Expand Down
7 changes: 5 additions & 2 deletions src/cli/install-tool/tool-version-resolver.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { inject, injectable } from 'inversify';
import { HttpService } from '../services';
import { EnvService, HttpService } from '../services';

export const TOOL_VERSION_RESOLVER = Symbol('TOOL_VERSION_RESOLVER');

@injectable()
export abstract class ToolVersionResolver {
abstract readonly tool: string;

constructor(@inject(HttpService) protected readonly http: HttpService) {}
constructor(
@inject(HttpService) protected readonly http: HttpService,
@inject(EnvService) protected readonly env: EnvService,
) {}

abstract resolve(version: string | undefined): Promise<string | undefined>;
}
31 changes: 31 additions & 0 deletions src/cli/tools/java/resolver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import is from '@sindresorhus/is';
import { injectable } from 'inversify';
import { ToolVersionResolver } from '../../install-tool/tool-version-resolver';
import { resolveLatestJavaLtsVersion } from './utils';

@injectable()
export class JavaVersionResolver extends ToolVersionResolver {
readonly tool: string = 'java';

async resolve(version: string | undefined): Promise<string | undefined> {
if (!is.nonEmptyStringAndNotWhitespace(version) || version === 'latest') {
// we know that the latest version is the first entry, so search for first lts
return await resolveLatestJavaLtsVersion(
this.http,
this.tool === 'java-jre' ? 'jre' : 'jdk',
this.env.arch,
);
}
return version;
}
}

@injectable()
export class JavaJreVersionResolver extends JavaVersionResolver {
override readonly tool = 'java-jre';
}

@injectable()
export class JavaJdkVersionResolver extends JavaVersionResolver {
override readonly tool = 'java-jdk';
}
10 changes: 10 additions & 0 deletions test/java/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,15 @@ RUN set -ex; \
ls -la; env; \
sbt compile


#--------------------------------------
# test: Resolve latest versions
#--------------------------------------
FROM base as test-latest-version

RUN install-tool java-jre


#--------------------------------------
# final
#--------------------------------------
Expand All @@ -206,3 +215,4 @@ COPY --from=test-gradle-c /.dummy /.dummy
COPY --from=test-gradle-d /.dummy /.dummy
COPY --from=test-maven-a /.dummy /.dummy
COPY --from=test-others /.dummy /.dummy
COPY --from=test-latest-version /.dummy /.dummy

0 comments on commit 9a44ab0

Please sign in to comment.