import { parseRange } from 'semver-utils' ; import { logger } from '../../logger' ; import { api as npm } from '../npm' ; import type { NewValueConfig , VersioningApi } from '../types' ; import { VERSION_PATTERN } from './patterns' ; import { npm2poetry , poetry2npm , poetry2semver , semver2poetry , } from './transform' ; export const id = 'poetry' ; export const displayName = 'Poetry' ; export const urls = [ 'https://python-poetry.org/docs/versions/' ] ; export const supportsRanges = true ; export const supportedRangeStrategies = [ 'bump' , 'extend' , 'pin' , 'replace' ] ; function equals ( a , b ) { return npm . equals ( poetry2semver ( a ) , poetry2semver ( b ) ) ; } function getMajor ( version ) { return npm . getMajor ( poetry2semver ( version ) ) ; } function getMinor ( version ) { return npm . getMinor ( poetry2semver ( version ) ) ; } function getPatch ( version ) { return npm . getPatch ( poetry2semver ( version ) ) ; } function isVersion ( input ) { return VERSION_PATTERN . test ( input ) ; } function isGreaterThan ( a , b ) { return npm . isGreaterThan ( poetry2semver ( a ) , poetry2semver ( b ) ) ; } function isLessThanRange ( version , range ) { return ( isVersion ( version ) && npm . isLessThanRange ( poetry2semver ( version ) , poetry2npm ( range ) ) ) ; } export function isValid ( input ) { return npm . isValid ( poetry2npm ( input ) ) ; } function isStable ( version ) { return npm . isStable ( poetry2semver ( version ) ) ; } function matches ( version , range ) { return ( isVersion ( version ) && npm . matches ( poetry2semver ( version ) , poetry2npm ( range ) ) ) ; } function getSatisfyingVersion ( versions , range ) { return semver2poetry ( npm . getSatisfyingVersion ( versions . map ( ( version ) => poetry2semver ( version ) ) , poetry2npm ( range ) ) ) ; } function minSatisfyingVersion ( versions , range ) { return semver2poetry ( npm . minSatisfyingVersion ( versions . map ( ( version ) => poetry2semver ( version ) ) , poetry2npm ( range ) ) ) ; } function isSingleVersion ( constraint ) { return ( ( constraint . trim ( ) . startsWith ( '=' ) && isVersion ( constraint . trim ( ) . substring ( 1 ) . trim ( ) ) ) || isVersion ( constraint . trim ( ) ) ) ; } function handleShort ( operator , currentValue , newVersion ) { const toVersionMajor = getMajor ( newVersion ) ; const toVersionMinor = getMinor ( newVersion ) ; const split = currentValue . split ( '.' ) ; if ( split . length === 1 ) { return `${ operator }${ toVersionMajor }` ; } if ( split . length === 2 ) { return `${ operator }${ toVersionMajor }.${ toVersionMinor }` ; } return null ; } function getNewValue ( { currentValue , rangeStrategy , currentVersion , newVersion , } ) { if ( rangeStrategy === 'replace' ) { const npmCurrentValue = poetry2npm ( currentValue ) ; try { const massagedNewVersion = poetry2semver ( newVersion ) ; if ( isVersion ( massagedNewVersion ) && npm . matches ( massagedNewVersion , npmCurrentValue ) ) { return currentValue ; } } catch ( err ) { logger . info ( { err } , 'Poetry versioning: Error caught checking if newVersion satisfies currentValue' ) ; } const parsedRange = parseRange ( npmCurrentValue ) ; const element = parsedRange [ parsedRange . length - 1 ] ; if ( parsedRange . length === 1 && element . operator ) { if ( element . operator === '^' ) { const version = handleShort ( '^' , npmCurrentValue , newVersion ) ; if ( version ) { return npm2poetry ( version ) ; } } if ( element . operator === '~' ) { const version = handleShort ( '~' , npmCurrentValue , newVersion ) ; if ( version ) { return npm2poetry ( version ) ; } } } } if ( ( VERSION_PATTERN . exec ( newVersion ) ?. groups ?. release || '' ) . split ( '.' ) . length !== 3 ) { logger . debug ( 'Cannot massage python version to npm - returning currentValue' ) ; return currentValue ; } try { const newSemver = npm . getNewValue ( { currentValue : poetry2npm ( currentValue ) , rangeStrategy , currentVersion : poetry2semver ( currentVersion ) , newVersion : poetry2semver ( newVersion ) , } ) ; const newPoetry = npm2poetry ( newSemver ) ; return newPoetry ; } catch ( err ) { logger . debug ( { currentValue , rangeStrategy , currentVersion , newVersion , err } , 'Could not generate new value using npm.getNewValue()' ) ; return currentValue ; } } function sortVersions ( a , b ) { return npm . sortVersions ( poetry2semver ( a ) , poetry2semver ( b ) ) ; } export const api = { equals , getMajor , getMinor , getPatch , getNewValue , getSatisfyingVersion , isCompatible : isVersion , isGreaterThan , isLessThanRange , isSingleVersion , isStable , isValid , isVersion , matches , minSatisfyingVersion , sortVersions , } ; export default api ;