View Javadoc

1   package org.apache.maven.continuum.utils;
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 org.apache.maven.continuum.configuration.ConfigurationService;
23  import org.apache.maven.continuum.model.project.Project;
24  import org.codehaus.plexus.util.StringUtils;
25  import org.springframework.stereotype.Service;
26  
27  import java.io.File;
28  import java.util.List;
29  import javax.annotation.Resource;
30  
31  /**
32   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
33   * @version $Id: DefaultWorkingDirectoryService.java 1372260 2012-08-13 04:29:09Z brett $
34   */
35  @Service( "workingDirectoryService" )
36  public class DefaultWorkingDirectoryService
37      implements WorkingDirectoryService
38  {
39      @Resource
40      private ConfigurationService configurationService;
41  
42      public void setConfigurationService( ConfigurationService configurationService )
43      {
44          this.configurationService = configurationService;
45      }
46  
47      public ConfigurationService getConfigurationService()
48      {
49          return configurationService;
50      }
51  
52      // ----------------------------------------------------------------------
53      // WorkingDirectoryService Implementation
54      // ----------------------------------------------------------------------
55  
56      public File getWorkingDirectory( Project project )
57      {
58          return getWorkingDirectory( project, null, null );
59      }
60  
61      public File getWorkingDirectory( Project project, boolean shouldSet )
62      {
63          return getWorkingDirectory( project, null, null, shouldSet );
64      }
65  
66      /**
67       * @param project
68       * @param projectScmRoot
69       * @param projects       projects under the same projectScmRoot
70       * @return
71       */
72      public File getWorkingDirectory( Project project, String projectScmRoot, List<Project> projects )
73      {
74          return getWorkingDirectory( project, projectScmRoot, projects, true );
75      }
76  
77      /**
78       * @param project
79       * @param projectScmRoot
80       * @param projects       projects under the same projectScmRoot
81       * @param shouldSet
82       * @return
83       */
84      public File getWorkingDirectory( Project project, String projectScmRoot, List<Project> projects, boolean shouldSet )
85      {
86  //        TODO: Enable, this is what we really want
87  //        ContinuumProjectGroup projectGroup = project.getProjectGroup();
88  //
89  //        return new File( projectGroup.getWorkingDirectory(),
90  //                         project.getPath() );
91  
92          String workingDirectory = project.getWorkingDirectory();
93  
94          if ( project.getWorkingDirectory() == null || "".equals( project.getWorkingDirectory() ) )
95          {
96              if ( project.isCheckedOutInSingleDirectory() && projectScmRoot != null && !"".equals( projectScmRoot ) )
97              {
98                  Project rootProject = project;
99                  if ( projects != null )
100                 {
101                     // the root project should have the lowest id since it's always added first                    
102                     for ( Project projectUnderScmRoot : projects )
103                     {
104                         if ( projectUnderScmRoot.getId() < rootProject.getId() )
105                         {
106                             rootProject = projectUnderScmRoot;
107                         }
108                     }
109                 }
110 
111                 // determine the path
112                 String projectScmUrl = project.getScmUrl();
113                 int indexDiff = StringUtils.differenceAt( projectScmUrl, projectScmRoot );
114 
115                 String pathToProject = "";
116                 if ( indexDiff != -1 )
117                 {
118                     pathToProject = projectScmUrl.substring( indexDiff );
119                 }
120 
121                 if ( pathToProject.startsWith( "\\" ) || pathToProject.startsWith( "/" ) )
122                 {
123                     workingDirectory = Integer.toString( rootProject.getId() ) + pathToProject;
124                 }
125                 else
126                 {
127                     workingDirectory = Integer.toString( rootProject.getId() ) + File.separatorChar + pathToProject;
128                 }
129             }
130             else
131             {
132                 workingDirectory = Integer.toString( project.getId() );
133             }
134         }
135 
136         if ( shouldSet )
137         {
138             project.setWorkingDirectory( workingDirectory );
139         }
140 
141         File workDir;
142         File projectWorkingDirectory = new File( workingDirectory );
143 
144         if ( projectWorkingDirectory.isAbsolute() )
145         {
146             // clean the project working directory path if it's a subdirectory of the global working directory
147             if ( projectWorkingDirectory.getAbsolutePath().startsWith(
148                 getConfigurationService().getWorkingDirectory().getAbsolutePath() ) )
149             {
150                 String pwd = projectWorkingDirectory.getAbsolutePath().substring(
151                     getConfigurationService().getWorkingDirectory().getAbsolutePath().length() );
152                 if ( pwd.startsWith( "/" ) || pwd.startsWith( "\\" ) )
153                 {
154                     pwd = pwd.substring( 1 );
155                 }
156 
157                 if ( shouldSet )
158                 {
159                     project.setWorkingDirectory( pwd );
160                 }
161             }
162 
163             workDir = projectWorkingDirectory;
164         }
165         else
166         {
167             File baseWorkingDir = getConfigurationService().getWorkingDirectory();
168 
169             workDir = new File( baseWorkingDir, workingDirectory );
170         }
171 
172         return workDir;
173     }
174 }