package com.freeleaps.devops class DependenciesResolver { def steps def language def cachingEnabled def mgr DependenciesResolver(steps, language) { this.steps = steps this.language = language } enableCachingSupport() { this.cachingEnabled = true } disableCachingSupport() { this.cachingEnabled = false } useManager(DependenciesManager mgr) { if (mgr == DependenciesManager.UNKNOWN) { steps.error("Unknown dependencies manager") } this.mgr = mgr } def resolve(Map configurations) { if (mgr == null) { steps.error("Dependencies manager is not set") } echo "Ready to resolve dependencies for ${language}..." echo "Using ${mgr.manager} to resolve dependencies..." if (cachingEnabled) { echo "Dependencies caching is enabled" } switch (mgr) { case DependenciesManager.PIP: if (configurations.PIP_REQUIREMENTS_FILE == null || configurations.PIP_REQUIREMENTS_FILE.isEmpty()) { steps.error("PIP_REQUIREMENTS_FILE is required when using PIP as dependencies manager") } def requirementsFile = configurations.PIP_REQUIREMENTS_FILE if (cachingEnabled) { steps.cache(maxCacheSize: 512, caches: [[$class: 'ArbitraryFileCache', excludes: '', includes: '**/*', path: '.pip-cache']]) { steps.sh "pip install -r ${requirementsFile} --cache-dir .pip-cache" } } else { steps.sh "pip install -r ${requirementsFile}" } case DependenciesManager.NPM: if (configurations.NPM_PACKAGE_JSON_FILE == null || configurations.NPM_PACKAGE_JSON_FILE.isEmpty()) { steps.error("NPM_PACKAGE_JSON_FILE is required when using NPM as dependencies manager") } def packageJsonFile = configurations.NPM_PACKAGE_JSON_FILE if (cachingEnabled) { steps.cache(maxCacheSize: 512, caches: [[$class: 'ArbitraryFileCache', excludes: '', includes: '**/*', path: '.npm-cache']]) { steps.sh "npm install --cache .npm-cache" } } else { steps.sh "npm install" } case DependenciesManager.YARN: if (configurations.YARN_PACKAGE_JSON_FILE == null || configurations.YARN_PACKAGE_JSON_FILE.isEmpty()) { steps.error("YARN_PACKAGE_JSON_FILE is required when using YARN as dependencies manager") } def packageJsonFile = configurations.YARN_PACKAGE_JSON_FILE if (cachingEnabled) { steps.cache(maxCacheSize: 512, caches: [[$class: 'ArbitraryFileCache', excludes: '', includes: '**/*', path: '.yarn-cache']]) { steps.sh "yarn install --cache-folder .yarn-cache" } } else { steps.sh "yarn install" } default: steps.error("Unsupported dependencies manager") } }