Skip to content

Commit

Permalink
feat(ruby): add version resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
viceice committed Aug 6, 2024
1 parent bf8ea4a commit cc0809b
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 28 deletions.
40 changes: 17 additions & 23 deletions src/cli/install-tool/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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<void> {
// 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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -54,3 +54,8 @@ export class CocoapodsInstallService extends RubyBaseInstallService {
);
}
}

@injectable()
export class CocoapodsVersionResolver extends RubyGemVersionResolver {
override readonly tool: string = 'cocoapods';
}
5 changes: 5 additions & 0 deletions src/cli/tools/ruby/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { z } from 'zod';

export const RubyGemJson = z.object({
version: z.string(),
});
17 changes: 17 additions & 0 deletions src/cli/tools/ruby/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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/';

Expand Down Expand Up @@ -150,3 +152,18 @@ export abstract class RubyBaseInstallService extends BaseInstallService {
);
}
}

@injectable()
export abstract class RubyGemVersionResolver extends ToolVersionResolver {
async resolve(version: string | undefined): Promise<string | undefined> {
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;
}
}
9 changes: 5 additions & 4 deletions test/ruby/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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


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

0 comments on commit cc0809b

Please sign in to comment.