All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.toomuchcoding.uptodate.reporting.BuildBreaker.groovy Maven / Gradle / Ivy

package com.toomuchcoding.uptodate.reporting

import com.toomuchcoding.uptodate.UptodatePluginExtension
import com.toomuchcoding.uptodate.dependency.Dependency
import groovy.util.logging.Slf4j

@Slf4j
class BuildBreaker {

	private static final Set INCLUDE_ALL = ['.*'] as Set

	private final UptodatePluginExtension.BuildBreakerConfiguration buildBreakerConfiguration

	BuildBreaker(UptodatePluginExtension.BuildBreakerConfiguration buildBreakerConfiguration) {
		this.buildBreakerConfiguration = buildBreakerConfiguration
	}

	void breakTheBuildIfNecessary(List sortedUpdates) {
		if (!buildBreakerConfiguration.enabled) {
			log.debug('Build breaking is disabled')
			return
		}
		log.debug("Build breaking is enabled")
		List matchingDependencies = findAllDependenciesMatchingPatterns(sortedUpdates)
		log.debug("Found the following matching dependencies $matchingDependencies")
		breakTheBuildIfMatchingDepsWereFound(matchingDependencies)
	}

	private List findAllDependenciesMatchingPatterns(List sortedUpdates) {
		return sortedUpdates.findAll { Dependency dependency ->
			getPatternsForInclusionOrAllAsDefault().any { nameOrGroupMatchesRegex(dependency, it) } &&
					!buildBreakerConfiguration.filters.excluded.any { nameOrGroupMatchesRegex(dependency, it) }
		}
	}

	private Set getPatternsForInclusionOrAllAsDefault() {
		Set patternsForInclusion = buildBreakerConfiguration.filters.included
		return patternsForInclusion.empty ? INCLUDE_ALL : patternsForInclusion
	}

	private void breakTheBuildIfMatchingDepsWereFound(List matchingDependencies) {
		if (!matchingDependencies.empty) {
			throw new NewDependencyVersionsFoundException("For the following dependencies $matchingDependencies new versions have been found. Breaking the build")
		}
	}

	private static boolean nameOrGroupMatchesRegex(Dependency dependency, String regex) {
		return dependency.group.matches(regex) || dependency.name.matches(regex)
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy