View Javadoc
1   /*
2    * Copyright 2014-2015 Mark Prins, GeoDienstenCentrum.
3    * Copyright 2010-2014 Jasig.
4    *
5    * See the NOTICE file distributed with this work for additional information
6    * regarding copyright ownership.
7    *
8    * Licensed under the Apache License, Version 2.0 (the "License");
9    * you may not use this file except in compliance with the License.
10   * 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, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  package nl.geodienstencentrum.maven.plugin.sass;
21  
22  import java.io.File;
23  import java.util.LinkedHashMap;
24  import java.util.Map;
25  
26  import org.apache.commons.io.FilenameUtils;
27  import org.apache.maven.model.FileSet;
28  import org.apache.maven.plugin.logging.Log;
29  import org.codehaus.plexus.util.DirectoryScanner;
30  import org.codehaus.plexus.util.StringUtils;
31  
32  /**
33   * A resource describes a set of sass files to compile and a target.
34   */
35  public class Resource {
36  
37  	/** Directories containing Sass files. */
38  	protected FileSet source;
39  
40  	/**
41  	 * Defines an additional path section when calculating the destination for
42  	 * the SCSS file. Allows, for example
43  	 * "/media/skins/universality/coal/scss/portal.scss" to end up at
44  	 * "/media/skins/universality/coal/portal.css" by specifying ".."
45  	 * <strong>NB</strong>This location is relative to the source {@link #source}
46  	 */
47  	protected String relativeOutputDirectory;
48  
49  	/** Where to put the compiled CSS files. */
50  	protected File destination;
51  
52  	/**
53  	 * Gets the source directories and target destinations.
54  	 *
55  	 * @param log
56  	 *            the maven logging instance to use for messages
57  	 * @return the directories and destinations, may be an empty map if the
58  	 *            source does not exist.
59  	 */
60  	public Map<String, String> getDirectoriesAndDestinations(final Log log) {
61  		final File sourceDirectory = new File(this.source.getDirectory());
62  		final Map<String, String> result = new LinkedHashMap<>();
63  
64  		if (!sourceDirectory.exists()) {
65  			log.error("Specified sourcedirectory (" + sourceDirectory
66  			           + ") does not exist.");
67  			return result;
68  		}
69  
70  		// Scan for directories
71  		final DirectoryScanner scanner = new DirectoryScanner();
72  		scanner.setBasedir(sourceDirectory);
73  		scanner.setIncludes(this.source.getIncludes().toArray(
74  		        new String[this.source.getIncludes().size()]));
75  		scanner.setExcludes(this.source.getExcludes().toArray(
76  		        new String[this.source.getExcludes().size()]));
77  
78  		scanner.addDefaultExcludes();
79  		scanner.scan();
80  
81  		result.put(FilenameUtils.separatorsToUnix(sourceDirectory.toString()),
82  		        FilenameUtils.separatorsToUnix(this.destination.toString()));
83  
84  		for (final String included : scanner.getIncludedDirectories()) {
85  			if (!included.isEmpty()) {
86  				final String subdir = StringUtils.difference(
87  				        sourceDirectory.toString(), included);
88  
89  				final File sourceDir = new File(sourceDirectory, included);
90  
91  				File destDir = new File(this.destination, subdir);
92  				if (this.relativeOutputDirectory != null
93  				        && !this.relativeOutputDirectory.isEmpty()) {
94  					destDir = new File(destDir, this.relativeOutputDirectory);
95  				}
96  
97  				result.put(
98  				        FilenameUtils.separatorsToUnix(sourceDir.toString()),
99  				        FilenameUtils.separatorsToUnix(destDir.toString()));
100 			}
101 		}
102 
103 		return result;
104 	}
105 }