View Javadoc
1   /*
2    * Copyright 2014-2018 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.compiler;
21  
22  import nl.geodienstencentrum.maven.plugin.sass.AbstractSassMojo;
23  
24  import org.apache.maven.plugin.MojoExecutionException;
25  import org.apache.maven.plugin.MojoFailureException;
26  import org.apache.maven.plugins.annotations.Mojo;
27  
28  /**
29   * The Class WatchMojo runs the Sass compiler's {@code watch} process.
30   */
31  @Mojo(name = "watch", threadSafe = true)
32  public class WatchMojo extends AbstractSassMojo {
33  
34  	/** true when we are running on Windows. */
35  	private static final boolean IS_WINDOWS = 
36  	        System.getProperty("os.name").toLowerCase().contains("win");
37  
38  	/**
39  	 * Start the watch process.
40  	 * 
41  	 * @throws MojoExecutionException when the execution of the plugin
42  	 *         errored
43  	 * @throws MojoFailureException when the Sass compilation fails
44  	 * @see org.apache.maven.plugin.Mojo#execute()
45  	 */
46      @Override
47  	public void execute() throws MojoExecutionException, MojoFailureException {
48  		if (this.isSkip()) {
49  			return;
50  		}
51  		this.getLog().info("Watching Sass Templates");
52  
53  		// build sass script
54  		final StringBuilder sassBuilder = new StringBuilder();
55  		this.buildBasicSassScript(sassBuilder);
56  		if (IS_WINDOWS) {
57  			sassBuilder.append("require 'sass-listen'\nSass::Plugin.options.merge!(:poll => true)\n");
58  		}
59  		sassBuilder.append("Sass::Plugin.watch");
60  		final String sassScript = sassBuilder.toString();
61  
62  		// ...and execute
63  		this.executeSassScript(sassScript);
64  	}
65  }