Skip to content

Commit

Permalink
feat(cli/java): convert gradle installer
Browse files Browse the repository at this point in the history
  • Loading branch information
viceice committed Jun 4, 2024
1 parent 977323f commit 9fabcd0
Show file tree
Hide file tree
Showing 8 changed files with 178 additions and 53 deletions.
8 changes: 7 additions & 1 deletion src/cli/install-tool/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ import {
InstallJavaJreService,
InstallJavaService,
} from '../tools/java';
import {
GradleVersionResolver,
InstallGradleService,
} from '../tools/java/gradle';
import { InstallMavenService } from '../tools/java/maven';
import {
JavaJdkVersionResolver,
Expand Down Expand Up @@ -60,6 +64,7 @@ function prepareInstallContainer(): Container {
container.bind(INSTALL_TOOL_TOKEN).to(InstallDotnetService);
container.bind(INSTALL_TOOL_TOKEN).to(InstallFlutterService);
container.bind(INSTALL_TOOL_TOKEN).to(InstallFluxService);
container.bind(INSTALL_TOOL_TOKEN).to(InstallGradleService);
container.bind(INSTALL_TOOL_TOKEN).to(InstallJavaService);
container.bind(INSTALL_TOOL_TOKEN).to(InstallJavaJreService);
container.bind(INSTALL_TOOL_TOKEN).to(InstallJavaJdkService);
Expand All @@ -82,10 +87,11 @@ function prepareResolveContainer(): Container {
container.bind(ToolVersionResolverService).toSelf();

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

logger.trace('preparing container done');
Expand Down
1 change: 1 addition & 0 deletions src/cli/tools/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export const NoPrepareTools = [
'corepack',
'flux',
'gleam',
'gradle',
'lerna',
'maven',
'node',
Expand Down
89 changes: 89 additions & 0 deletions src/cli/tools/java/gradle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import fs from 'node:fs/promises';
import { join } from 'node:path';
import is from '@sindresorhus/is';
import { execa } from 'execa';
import { inject, injectable } from 'inversify';
import semver from 'semver';
import { InstallToolBaseService } from '../../install-tool/install-tool-base.service';
import { ToolVersionResolver } from '../../install-tool/tool-version-resolver';
import {
CompressionService,
EnvService,
HttpService,
PathService,
} from '../../services';
import { GradleVersionData } from './schema';

@injectable()
export class InstallGradleService extends InstallToolBaseService {
readonly name = 'gradle';

constructor(
@inject(EnvService) envSvc: EnvService,
@inject(PathService) pathSvc: PathService,
@inject(HttpService) private http: HttpService,
@inject(CompressionService) private compress: CompressionService,
) {
super(pathSvc, envSvc);
}

override async install(version: string): Promise<void> {
const name = this.name;
const filename = `${name}-${version}-bin.zip`;
const url = `https://services.gradle.org/distributions/${filename}`;
const checksumFileUrl = `${url}.sha256`;

const expectedChecksum = await this.readChecksum(checksumFileUrl);
const file = await this.http.download({
url,
checksumType: 'sha256',
expectedChecksum,
});

let path = await this.pathSvc.findToolPath(this.name);
if (!path) {
path = await this.pathSvc.createToolPath(this.name);
}

path = await this.pathSvc.createVersionedToolPath(this.name, version);

await this.compress.extract({ file, cwd: path, strip: 1 });
}

override async link(version: string): Promise<void> {
const src = join(this.pathSvc.versionedToolPath(this.name, version), 'bin');
await this.shellwrapper({
srcDir: src,
exports: 'GRADLE_USER_HOME=$HOME/.gradle',
});
}

override async test(_version: string): Promise<void> {
await execa('gradle', ['--version'], {
stdio: ['inherit', 'inherit', 1],
});
}

override validate(version: string): Promise<boolean> {
return Promise.resolve(semver.coerce(version) !== null);
}

private async readChecksum(url: string): Promise<string | undefined> {
const checksumFile = await this.http.download({ url });
return (await fs.readFile(checksumFile, 'utf-8')).split(' ')[0]?.trim();
}
}

@injectable()
export class GradleVersionResolver extends ToolVersionResolver {
readonly tool = 'gradle';

async resolve(version: string | undefined): Promise<string | undefined> {
if (!is.nonEmptyStringAndNotWhitespace(version) || version === 'latest') {
return GradleVersionData.parse(
await this.http.getJson('https://services.gradle.org/versions/current'),
)?.version;
}
return version;
}
}
10 changes: 9 additions & 1 deletion src/cli/tools/java/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ import {
PathService,
} from '../../services';
import { logger } from '../../utils';
import { resolveJavaDownloadUrl, resolveLatestJavaLtsVersion } from './utils';
import {
createGradleSettings,
createMavenSettings,
resolveJavaDownloadUrl,
resolveLatestJavaLtsVersion,
} from './utils';

@injectable()
export class PrepareJavaService extends PrepareToolBaseService {
Expand All @@ -36,6 +41,9 @@ export class PrepareJavaService extends PrepareToolBaseService {
return;
}

await createMavenSettings(this.envSvc.userHome, this.envSvc.userId);
await createGradleSettings(this.envSvc.userHome, this.envSvc.userId);

const version = await resolveLatestJavaLtsVersion(
this.httpSvc,
'jre',
Expand Down
4 changes: 4 additions & 0 deletions src/cli/tools/java/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,7 @@ const AdoptiumRelease = z.object({
});

export const AdoptiumReleases = z.array(AdoptiumRelease);

export const GradleVersionData = z.object({
version: z.string(),
});
66 changes: 65 additions & 1 deletion src/cli/tools/java/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import fs from 'node:fs/promises';
import path from 'node:path';
import { codeBlock } from 'common-tags';
import { execa } from 'execa';
import type { HttpService } from '../../services';
import type { Arch } from '../../utils';
import { type Arch, fileExists, logger } from '../../utils';
import {
type AdoptiumPackage,
AdoptiumReleaseVersions,
Expand Down Expand Up @@ -42,3 +46,63 @@ export async function resolveJavaDownloadUrl(

return res?.[0]?.binaries?.[0]?.package;
}

export async function createMavenSettings(
home: string,
userId: number,
): Promise<void> {
const dir = path.join(home, '.m2');
const file = path.join(dir, 'settings.xml');
if (await fileExists(file)) {
logger.debug('Maven settings already found');
return;
}
logger.debug('Creating Maven settings');

await fs.mkdir(dir);

await fs.writeFile(
file,
codeBlock`
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
http://maven.apache.org/xsd/settings-1.0.0.xsd">
</settings>
`,
);

// fs isn't recursive, so we use system binaries
await execa('chown', ['-R', `${userId}`, dir]);
await execa('chmod', ['-R', 'g+w', dir]);
}

export async function createGradleSettings(
home: string,
userId: number,
): Promise<void> {
const dir = path.join(home, '.gradle');
const file = path.join(dir, 'gradle.properties');
if (await fileExists(file)) {
logger.debug('Gradle settings already found');
return;
}
logger.debug('Creating Gradle settings');

await fs.mkdir(dir);

await fs.writeFile(
file,
codeBlock`
org.gradle.parallel=true
org.gradle.configureondemand=true
org.gradle.daemon=false
org.gradle.caching=false
`,
);

// fs isn't recursive, so we use system binaries
await execa('chown', ['-R', `${userId}`, dir]);
await execa('chmod', ['-R', 'g+w', dir]);
}
50 changes: 0 additions & 50 deletions src/usr/local/containerbase/tools/v2/gradle.sh

This file was deleted.

3 changes: 3 additions & 0 deletions test/java/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ RUN install-tool scala v2.13.14
# renovate: datasource=github-releases packageName=sbt/sbt
RUN install-tool sbt v1.10.0

RUN install-tool gradle 8.8-rc-2

# doesn't work for arbitrary users
USER 1000

Expand All @@ -202,6 +204,7 @@ RUN set -ex; \
FROM base as test-latest-version

RUN install-tool java-jre
RUN install-tool gradle


#--------------------------------------
Expand Down

0 comments on commit 9fabcd0

Please sign in to comment.