Skip to content

Commit

Permalink
NCLSUP-620 Fix lockfile handling
Browse files Browse the repository at this point in the history
  • Loading branch information
rnc committed Mar 15, 2022
1 parent 5c508fc commit a684a33
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import static junit.framework.TestCase.assertTrue;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.tuple;
import static org.junit.Assert.assertFalse;
import static org.junit.Assume.assumeTrue;

public class DynamicWithLocksProjectFunctionalTest extends AbstractWiremockTest {
Expand Down Expand Up @@ -75,6 +76,9 @@ public void ensureAlignmentFileCreated() throws IOException, URISyntaxException,

final File gitDir = new File(projectRoot, "dotgit");
Files.move(gitDir.toPath(), projectRoot.toPath().resolve(".git"));

assertTrue(new File(projectRoot, "gradle/dependency-locks/compileClasspath.lockfile").exists());

final TestManipulationModel alignmentModel = TestUtils.align(projectRoot, projectRoot.getName(), false);

assertTrue(new File(projectRoot, AlignmentTask.GME).exists());
Expand Down Expand Up @@ -106,6 +110,8 @@ public void ensureAlignmentFileCreated() throws IOException, URISyntaxException,
});
});

assertFalse(new File(projectRoot, "gradle/dependency-locks/compileClasspath.lockfile").exists());

// make sure the project name was added
assertEquals("rootProject.name='undertow'",
FileUtils.getLastLine(new File(projectRoot, "settings.gradle")));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Dummy similar to what gradle writes when --write-locks is enabled
io.undertow:undertow-core:2.0.21.Final
org.apache.commons:commons-lang3:3.8
org.hdrhistogram:HdrHistogram:2.1.10
org.hdrhistogram:HdrHistogram:2.1.10=compileClasspath,classpath
# there are more transitives added by gradle, we only add one in order to not distract
com.google.guava:guava:25.1-android
org.jboss.resteasy:resteasy-jaxrs:3.6.3.SP1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ public void perform() {

// If processing the root project _and_ we have a Maven publication configured then verify artifactId / groupId.
Project rootProject = project.getRootProject();

if (project.equals(rootProject)) {
logger.debug("Processing root project in directory {}", root);
PublishingExtension extension = rootProject.getExtensions().findByType(PublishingExtension.class);
Expand Down Expand Up @@ -220,7 +221,8 @@ public void perform() {

try {
final Set<ProjectVersionRef> lockFileDeps = LockFileIO
.allProjectVersionRefsFromLockfiles(LockFileIO.getLocksRootPath(project));
.allProjectVersionRefsFromLockfiles(project.getRootDir().toPath());

final Map<RelaxedProjectVersionRef, ProjectVersionRef> dependencies = processAnyExistingManipulationFile(
project,
getDependencies(project, configuration, lockFileDeps));
Expand Down Expand Up @@ -251,10 +253,6 @@ public void perform() {
} else {
logger.debug("Still have {} projects to scan", cache.getProjectCounterRemaining());
}

// this needs to happen for each project, not just the last one
LockFileIO.renameAllLockFiles(LockFileIO.getLocksRootPath(project));

} catch (ManipulationException | IOException e) {
throw new ManipulationUncheckedException(e);
}
Expand Down Expand Up @@ -380,6 +378,10 @@ private void align(Configuration configuration, ManipulationCache cache, Manipul
writeGmeConfigMarkerFile(rootProject.getBuildFile());
writeGmeReposMarkerFile();
writeRepositorySettingsFile(cache.getRepositories());

// this needs to happen for each project, not just the last one
LockFileIO.renameAllLockFiles(rootProject.getRootDir().toPath());

final Set<ProjectVersionRef> nonAligned = new LinkedHashSet<>();
processAlignmentReport(rootProject, configuration, cache, nonAligned);
}
Expand Down Expand Up @@ -609,6 +611,7 @@ private Map<RelaxedProjectVersionRef, ProjectVersionRef> getDependencies(Project

String version = dep.getModuleVersion(); // this is the resolved version from gradle
// if the dependency is present in any of the lockfiles, then we use that version

for (ProjectVersionRef lockFileDep : lockFileDeps) {
if (lockFileDep.getGroupId().equals(dep.getModuleGroup())
&& lockFileDep.getArtifactId().equals(dep.getModuleName())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
Expand All @@ -21,7 +20,6 @@
import org.commonjava.maven.atlas.ident.ref.ProjectVersionRef;
import org.commonjava.maven.atlas.ident.ref.SimpleProjectVersionRef;
import org.commonjava.maven.ext.common.ManipulationUncheckedException;
import org.gradle.api.Project;

/**
* Utility class for lock file I/O.
Expand All @@ -35,11 +33,13 @@ public class LockFileIO {
* Returns a set containing all project version refs from lock files in the given path.
*
* @param locksRootPath the path to the root for the lock files
* @throws IOException if an error occurs
* @return the set of all project version refs
*/
public Set<ProjectVersionRef> allProjectVersionRefsFromLockfiles(Path locksRootPath) {
public Set<ProjectVersionRef> allProjectVersionRefsFromLockfiles(Path locksRootPath)
throws IOException {
final Set<ProjectVersionRef> result = new HashSet<>();
getAllLockfiles(locksRootPath).forEach(f -> result.addAll(readProjectVersionRefLocksOfFile(f)));
getAllLockFiles(locksRootPath).forEach(f -> result.addAll(readProjectVersionRefLocksOfFile(f)));
return result;
}

Expand All @@ -50,7 +50,7 @@ private Set<ProjectVersionRef> readProjectVersionRefLocksOfFile(File lockfile) {
.filter(l -> !l.startsWith("#"))
.map(l -> {
try {
return SimpleProjectVersionRef.parse(l);
return SimpleProjectVersionRef.parse(l.split("=")[0]);
} catch (InvalidRefException e) {
return null;
}
Expand All @@ -66,9 +66,11 @@ private Set<ProjectVersionRef> readProjectVersionRefLocksOfFile(File lockfile) {
* Renames all lock files under the given root.
*
* @param locksRootPath the root path for the lock files
* @throws IOException if an error occurs
*/
public void renameAllLockFiles(Path locksRootPath) {
getAllLockfiles(locksRootPath).forEach(f -> {
public void renameAllLockFiles(Path locksRootPath)
throws IOException {
getAllLockFiles(locksRootPath).forEach(f -> {
final Path path = f.toPath();
try {
Files.move(path, path.resolveSibling(path.getFileName() + ".unused"), StandardCopyOption.REPLACE_EXISTING);
Expand All @@ -78,27 +80,15 @@ public void renameAllLockFiles(Path locksRootPath) {
});
}

private List<File> getAllLockfiles(Path locksRootPath) {
private List<File> getAllLockFiles(Path locksRootPath)
throws IOException {
if (!locksRootPath.toFile().exists()) {
return Collections.emptyList();
}

File[] lockfiles = locksRootPath.toFile()
.listFiles((dir, filename) -> filename.endsWith(LOCKFILE_EXTENSION));
if (lockfiles == null) {
return Collections.emptyList();
}

return Arrays.asList(lockfiles);
}

/**
* Gets the lock file root path for the given project.
*
* @param project the project
* @return the lock file root path
*/
public Path getLocksRootPath(Project project) {
return project.getProjectDir().toPath().resolve("gradle/dependency-locks");
return Files.find(locksRootPath, Integer.MAX_VALUE,
(filePath, fileAttr) -> fileAttr.isRegularFile() && filePath.getFileName().toString().endsWith(
(LOCKFILE_EXTENSION)))
.map(Path::toFile).collect(
Collectors.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ public class LockFileIOTest {
public TemporaryFolder tempDir = new TemporaryFolder();

@Test
public void readNonExistingFileShouldReturnEmptySet() {
public void readNonExistingFileShouldReturnEmptySet()
throws IOException {
assertThat(LockFileIO.allProjectVersionRefsFromLockfiles(Paths.get("/lol"))).isEmpty();
}

Expand All @@ -42,7 +43,8 @@ public void readValidFileShouldReturnExpectedResults() throws URISyntaxException
}

@Test
public void renameNonExistingFileShouldNotResultInAnError() {
public void renameNonExistingFileShouldNotResultInAnError()
throws IOException {
LockFileIO.renameAllLockFiles(Paths.get("/lol"));
}

Expand Down
2 changes: 1 addition & 1 deletion analyzer/src/test/resources/compileClasspath.lockfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Dummy similar to what gradle writes when --write-locks is enabled
io.undertow:undertow-core:2.0.21.Final
org.apache.commons:commons-lang3:3.8
org.hdrhistogram:HdrHistogram:2.1.10
org.hdrhistogram:HdrHistogram:2.1.10=compileClasspath
# there are more transitives added by gradle, we only add one in order to not distract
com.google.guava:guava:25.1-android

0 comments on commit a684a33

Please sign in to comment.