diff --git a/src/cli/install-tool/index.ts b/src/cli/install-tool/index.ts index b50763dfe..c2d7367b9 100644 --- a/src/cli/install-tool/index.ts +++ b/src/cli/install-tool/index.ts @@ -50,8 +50,14 @@ import { } from '../tools/python/conan'; import { PipVersionResolver } from '../tools/python/pip'; import { PipBaseInstallService } from '../tools/python/utils'; -import { CocoapodsInstallService } from '../tools/ruby/gem'; -import { RubyBaseInstallService } from '../tools/ruby/utils'; +import { + CocoapodsInstallService, + CocoapodsVersionResolver, +} from '../tools/ruby/cocoapods'; +import { + RubyBaseInstallService, + RubyGemVersionResolver, +} from '../tools/ruby/utils'; import { SkopeoInstallService } from '../tools/skopeo'; import { SopsInstallService } from '../tools/sops'; import { logger } from '../utils'; @@ -112,6 +118,7 @@ function prepareResolveContainer(): Container { container.bind(ToolVersionResolverService).toSelf(); // tool version resolver + container.bind(TOOL_VERSION_RESOLVER).to(CocoapodsVersionResolver); container.bind(TOOL_VERSION_RESOLVER).to(ConanVersionResolver); container.bind(TOOL_VERSION_RESOLVER).to(ComposerVersionResolver); container.bind(TOOL_VERSION_RESOLVER).to(GradleVersionResolver); @@ -216,27 +223,14 @@ export async function resolveVersion( if (type) { switch (type) { - // case 'gem': { - // @injectable() - // class InstallGenericGemService extends InstallRubyBaseService { - // override readonly name: string = tool; - - // override needsPrepare(): boolean { - // return false; - // } - - // override async test(version: string): Promise { - // try { - // // some npm packages may not have a `--version` flag - // await super.test(version); - // } catch (err) { - // logger.debug(err); - // } - // } - // } - // container.bind(INSTALL_TOOL_TOKEN).to(InstallGenericGemService); - // break; - // } + case 'gem': { + @injectable() + class GenericRubyGemVersionResolver extends RubyGemVersionResolver { + override readonly tool: string = tool; + } + container.bind(TOOL_VERSION_RESOLVER).to(GenericRubyGemVersionResolver); + break; + } case 'npm': { @injectable() class GenericNpmVersionResolver extends NpmVersionResolver { diff --git a/src/cli/tools/ruby/gem.ts b/src/cli/tools/ruby/cocoapods.ts similarity index 86% rename from src/cli/tools/ruby/gem.ts rename to src/cli/tools/ruby/cocoapods.ts index 385cc0491..998dc2fe9 100644 --- a/src/cli/tools/ruby/gem.ts +++ b/src/cli/tools/ruby/cocoapods.ts @@ -2,7 +2,7 @@ import { join } from 'node:path'; import { execa } from 'execa'; import { injectable } from 'inversify'; import { semverSatisfies } from '../../utils'; -import { RubyBaseInstallService } from './utils'; +import { RubyBaseInstallService, RubyGemVersionResolver } from './utils'; @injectable() export class CocoapodsInstallService extends RubyBaseInstallService { @@ -54,3 +54,8 @@ export class CocoapodsInstallService extends RubyBaseInstallService { ); } } + +@injectable() +export class CocoapodsVersionResolver extends RubyGemVersionResolver { + override readonly tool: string = 'cocoapods'; +} diff --git a/src/cli/tools/ruby/schema.ts b/src/cli/tools/ruby/schema.ts new file mode 100644 index 000000000..7ee4e7e52 --- /dev/null +++ b/src/cli/tools/ruby/schema.ts @@ -0,0 +1,5 @@ +import { z } from 'zod'; + +export const RubyGemJson = z.object({ + version: z.string(), +}); diff --git a/src/cli/tools/ruby/utils.ts b/src/cli/tools/ruby/utils.ts index 58d877fdf..e5a2dae4c 100644 --- a/src/cli/tools/ruby/utils.ts +++ b/src/cli/tools/ruby/utils.ts @@ -4,8 +4,10 @@ import { isNonEmptyStringAndNotWhitespace } from '@sindresorhus/is'; import { execa } from 'execa'; import { inject, injectable } from 'inversify'; import { BaseInstallService } from '../../install-tool/base-install.service'; +import { ToolVersionResolver } from '../../install-tool/tool-version-resolver'; import { EnvService, PathService, VersionService } from '../../services'; import { logger } from '../../utils'; +import { RubyGemJson } from './schema'; const defaultRegistry = 'https://rubygems.org/'; @@ -150,3 +152,18 @@ export abstract class RubyBaseInstallService extends BaseInstallService { ); } } + +@injectable() +export abstract class RubyGemVersionResolver extends ToolVersionResolver { + async resolve(version: string | undefined): Promise { + if (version === undefined || version === 'latest') { + const meta = RubyGemJson.parse( + await this.http.getJson( + `https://rubygems.org/api/v1/gems/${this.tool}.json`, + ), + ); + return meta.version; + } + return version; + } +} diff --git a/test/ruby/Dockerfile b/test/ruby/Dockerfile index 60b3e5a66..747f791fa 100644 --- a/test/ruby/Dockerfile +++ b/test/ruby/Dockerfile @@ -128,16 +128,17 @@ RUN set -ex; \ true #-------------------------------------- -# test: install-gem +# test: install-gem, no version #-------------------------------------- FROM build AS test-gem USER 1000 -# renovate: datasource=rubygems -RUN install-gem rake 13.2.1 +RUN install-tool cocoapods + +RUN install-gem rake -RUN rake --help +RUN rake --version #--------------------------------------