Skip to content

Commit

Permalink
get rid of versionManager, move logic into dotnet-tool
Browse files Browse the repository at this point in the history
  • Loading branch information
arturcic committed Mar 13, 2024
1 parent 9732b7f commit b18924c
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 67 deletions.
37 changes: 24 additions & 13 deletions src/core/dotnet-tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import * as http from 'typed-rest-client/HttpClient'

import { inject, injectable } from 'inversify'
import { IBuildAgent, IExecResult, TYPES } from './models'
import { IVersionManager } from './versionManager'
import { ISetupSettings } from '../tools/common/models'
import * as semver from 'semver'

export interface IDotnetTool {
disableTelemetry(): void
Expand All @@ -17,14 +17,12 @@ export interface IDotnetTool {
@injectable()
export class DotnetTool implements IDotnetTool {
protected buildAgent: IBuildAgent
protected versionManager: IVersionManager
private httpClient: http.HttpClient

private static readonly nugetRoot: string = 'https://azuresearch-usnc.nuget.org/query'

constructor(@inject(TYPES.IBuildAgent) buildAgent: IBuildAgent, @inject(TYPES.IVersionManager) versionManager: IVersionManager) {
constructor(@inject(TYPES.IBuildAgent) buildAgent: IBuildAgent) {
this.buildAgent = buildAgent
this.versionManager = versionManager
this.httpClient = new http.HttpClient('dotnet', undefined, this.buildAgent.proxyConfiguration(DotnetTool.nugetRoot))
}

Expand All @@ -39,20 +37,20 @@ export class DotnetTool implements IDotnetTool {
}

public async toolInstall(toolName: string, versionRange: string, setupSettings: ISetupSettings): Promise<string> {
let version: string | null = this.versionManager.cleanVersion(setupSettings.versionSpec) || setupSettings.versionSpec
let version: string | null = semver.clean(setupSettings.versionSpec) || setupSettings.versionSpec
console.log('')
console.log('--------------------------')
console.log(`Acquiring ${toolName} version spec: ${version}`)
console.log('--------------------------')

if (!this.versionManager.isExplicitVersion(version)) {
if (!this.isExplicitVersion(version)) {
version = await this.queryLatestMatch(toolName, version, setupSettings.includePrerelease)
if (!version) {
throw new Error(`Unable to find ${toolName} version '${version}'.`)
}
}

if (!this.versionManager.satisfies(version, versionRange, { includePrerelease: setupSettings.includePrerelease })) {
if (!semver.satisfies(version, versionRange, { includePrerelease: setupSettings.includePrerelease })) {
throw new Error(
`Version spec '${setupSettings.versionSpec}' resolved as '${version}' does not satisfy the range '${versionRange}'.` +
'See https://github.com/GitTools/actions/blob/main/docs/versions.md for more information.'
Expand All @@ -70,7 +68,7 @@ export class DotnetTool implements IDotnetTool {
}

if (!toolPath) {
toolPath = await this.acquireTool(toolName, version, setupSettings.ignoreFailedSources)
toolPath = await this.installTool(toolName, version, setupSettings.ignoreFailedSources)
console.log('--------------------------')
console.log(`${toolName} version: ${version} installed.`)
console.log('--------------------------')
Expand Down Expand Up @@ -118,12 +116,17 @@ export class DotnetTool implements IDotnetTool {

this.buildAgent.debug(`got versions: ${versions.join(', ')}`)

return this.versionManager.evaluateVersions(versions, versionSpec, {
includePrerelease
})
const version = semver.maxSatisfying(versions, versionSpec, { includePrerelease })
if (version) {
this.buildAgent.info(`Found matching version: ${version}`)
} else {
this.buildAgent.info('match not found')
}

return version
}

private async acquireTool(toolName: string, version: string, ignoreFailedSources: boolean): Promise<string> {
private async installTool(toolName: string, version: string, ignoreFailedSources: boolean): Promise<string> {
const tempDirectory = await this.buildAgent.createTempDir()
let args = ['tool', 'install', toolName, '--tool-path', tempDirectory]

Expand All @@ -132,7 +135,7 @@ export class DotnetTool implements IDotnetTool {
}

if (version) {
version = this.versionManager.cleanVersion(version)
version = semver.clean(version)
args = args.concat(['--version', version])
}

Expand All @@ -148,4 +151,12 @@ export class DotnetTool implements IDotnetTool {

return await this.buildAgent.cacheDir(tempDirectory, toolName, version)
}

private isExplicitVersion(versionSpec: string): boolean {
const cleanedVersionSpec = semver.clean(versionSpec)
const valid = semver.valid(cleanedVersionSpec) != null
this.buildAgent.debug(`Is version explicit? ${valid}`)

return valid
}
}
2 changes: 0 additions & 2 deletions src/core/ioc.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { Container } from 'inversify'
import { IVersionManager, VersionManager } from './versionManager'
import { TYPES, IBuildAgent } from './models'
import { BuildAgent } from '../agents/local/build-agent'

const container = new Container()

container.bind<IVersionManager>(TYPES.IVersionManager).to(VersionManager)
container.bind<IBuildAgent>(TYPES.IBuildAgent).to(BuildAgent)

export default container
46 changes: 0 additions & 46 deletions src/core/versionManager.ts

This file was deleted.

5 changes: 2 additions & 3 deletions src/tools/gitreleasemanager/tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import * as path from 'path'
import { IBuildAgent, IExecResult, TYPES } from '../../core/models'
import { inject, injectable } from 'inversify'
import { DotnetTool, IDotnetTool } from '../../core/dotnet-tool'
import { IVersionManager } from '../../core/versionManager'

import {
GitReleaseManagerAddAssetSettings,
Expand All @@ -28,8 +27,8 @@ export interface IGitReleaseManagerTool extends IDotnetTool {

@injectable()
export class GitReleaseManagerTool extends DotnetTool implements IGitReleaseManagerTool {
constructor(@inject(TYPES.IBuildAgent) buildAgent: IBuildAgent, @inject(TYPES.IVersionManager) versionManager: IVersionManager) {
super(buildAgent, versionManager)
constructor(@inject(TYPES.IBuildAgent) buildAgent: IBuildAgent) {
super(buildAgent)
}

public async install(setupSettings: ISetupSettings): Promise<void> {
Expand Down
5 changes: 2 additions & 3 deletions src/tools/gitversion/tool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { inject, injectable } from 'inversify'
import { IBuildAgent, IExecResult, TYPES } from '../../core/models'
import { DotnetTool, IDotnetTool } from '../../core/dotnet-tool'
import { GitVersionOutput, GitVersionSettings } from './models'
import { IVersionManager } from '../../core/versionManager'
import { ISetupSettings } from '../common/models'

export interface IGitVersionTool extends IDotnetTool {
Expand All @@ -15,8 +14,8 @@ export interface IGitVersionTool extends IDotnetTool {

@injectable()
export class GitVersionTool extends DotnetTool implements IGitVersionTool {
constructor(@inject(TYPES.IBuildAgent) buildAgent: IBuildAgent, @inject(TYPES.IVersionManager) versionManager: IVersionManager) {
super(buildAgent, versionManager)
constructor(@inject(TYPES.IBuildAgent) buildAgent: IBuildAgent) {
super(buildAgent)
}

public async install(setupSettings: ISetupSettings): Promise<void> {
Expand Down

0 comments on commit b18924c

Please sign in to comment.