1 package com.soebes.maven.extensions.incremental;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import java.io.File;
23 import java.nio.file.Path;
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.Objects;
27
28 import org.apache.maven.project.MavenProject;
29 import org.apache.maven.scm.ScmFile;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 /**
34 * @author Karl Heinz Marbaise <khmarbaise@apache.org>
35 */
36 public class ModuleCalculator
37 {
38 private final Logger logger = LoggerFactory.getLogger( getClass().getName() );
39
40 private List<MavenProject> projectList;
41
42 private List<ScmFile> changeList;
43
44 /**
45 * @param projectList The list of Maven Projects which are in the reactor.
46 * @param changeList The list of changes within this structure.
47 */
48 public ModuleCalculator( List<MavenProject> projectList, List<ScmFile> changeList )
49 {
50 this.projectList = Objects.requireNonNull( projectList, "projectList is not allowed to be null." );
51 this.changeList = Objects.requireNonNull( changeList, "changeList is not allowed to be null." );
52 }
53
54 /**
55 * Calculate the modules which needed to be rebuilt based on the list of changes from SCM.
56 *
57 * @param projectRootpath Root path of the project.
58 * @return The list of modules which needed to be rebuilt.
59 */
60 public List<MavenProject> calculateChangedModules( Path projectRootpath )
61 {
62 // TODO: Think about if we got only pom packaging modules? Do we
63 // need to do something special there?
64 List<MavenProject> result = new ArrayList<>();
65 for ( MavenProject project : projectList )
66 {
67 Path relativize = projectRootpath.relativize( project.getBasedir().toPath() );
68 for ( ScmFile fileItem : changeList )
69 {
70 boolean startsWith = new File( fileItem.getPath() ).toPath().startsWith( relativize );
71 logger.debug( "startswith: " + startsWith + " " + fileItem.getPath() + " " + relativize );
72 if ( startsWith )
73 {
74 if ( !result.contains( project ) )
75 {
76 result.add( project );
77 }
78 }
79 }
80 }
81 return result;
82 }
83
84 }