import { GalaxyCollectionDatasource } from '../../datasource/galaxy-collection' ; import { GitTagsDatasource } from '../../datasource/git-tags' ; import * as datasourceGithubTags from '../../datasource/github-tags' ; import { SkipReason } from '../../types' ; import { regEx } from '../../util/regex' ; import type { PackageDependency } from '../types' ; import { blockLineRegEx , galaxyDepRegex , nameMatchRegex , newBlockRegEx , } from './util' ; function interpretLine ( lineMatch , lineNumber , dependency ) { const localDependency = dependency ; const key = lineMatch [ 2 ] ; const value = lineMatch [ 3 ] . replace ( regEx ( /["']/g ) , '' ) ; switch ( key ) { case 'name' : { localDependency . managerData . name = value ; break ; } case 'version' : { localDependency . managerData . version = value ; localDependency . currentValue = value ; break ; } case 'source' : { localDependency . managerData . source = value ; localDependency . registryUrls = value ? [ value ] : [ ] ; break ; } case 'type' : { localDependency . managerData . type = value ; break ; } default : { localDependency . skipReason = SkipReason . Unsupported ; } } } function handleGitDep ( dep , nameMatch ) { dep . datasource = GitTagsDatasource . id ; if ( nameMatch ) { if ( nameMatch . groups . hostname === 'github.com' ) { dep . datasource = datasourceGithubTags . id ; } else { dep . datasource = GitTagsDatasource . id ; } const source = nameMatch . groups . source ; const massagedDepName = nameMatch . groups . depName . replace ( regEx ( /.git$/ ) , '' ) ; dep . depName = `${ nameMatch . groups . hostname }/${ massagedDepName }` ; dep . lookupName = source . replace ( regEx ( /git\+/ ) , '' ) ; if ( nameMatch . groups . version ) { dep . currentValue = nameMatch . groups . version ; } else { dep . currentValue = dep . managerData . version ; } } } function handleGalaxyDep ( dep ) { dep . datasource = GalaxyCollectionDatasource . id ; dep . depName = dep . managerData . name ; dep . registryUrls = dep . managerData . source ? [ dep . managerData . source ] : [ ] ; dep . currentValue = dep . managerData . version ; } function finalize ( dependency ) { const dep = dependency ; dep . depName = dep . managerData . name ; const name = dep . managerData . name ; const nameMatch = nameMatchRegex . exec ( name ) ; switch ( dependency . managerData . type ) { case 'galaxy' : handleGalaxyDep ( dep ) ; break ; case 'git' : handleGitDep ( dep , nameMatch ) ; break ; case 'file' : dep . skipReason = SkipReason . LocalDependency ; break ; case null : if ( nameMatch ) { handleGitDep ( dep , nameMatch ) ; break ; } if ( galaxyDepRegex . exec ( dep . managerData . name ) ) { dep . datasource = GalaxyCollectionDatasource . id ; dep . depName = dep . managerData . name ; break ; } dep . skipReason = SkipReason . NoSourceMatch ; break ; default : dep . skipReason = SkipReason . Unsupported ; return true ; } if ( ! dependency . currentValue && ! dep . skipReason ) { dep . skipReason = SkipReason . NoVersion ; } return true ; } export function extractCollections ( lines ) { const deps = [ ] ; for ( let lineNumber = 0 ; lineNumber < lines . length ; lineNumber += 1 ) { let lineMatch = newBlockRegEx . exec ( lines [ lineNumber ] ) ; if ( lineMatch ) { const dep = { depType : 'galaxy-collection' , managerData : { name : null , version : null , type : null , source : null , } , } ; do { interpretLine ( lineMatch , lineNumber , dep ) ; const line = lines [ lineNumber + 1 ] ; if ( ! line ) { break ; } lineMatch = blockLineRegEx . exec ( line ) ; if ( lineMatch ) { lineNumber += 1 ; } } while ( lineMatch ) ; if ( finalize ( dep ) ) { delete dep . managerData ; deps . push ( dep ) ; } } } return deps ; }