View Javadoc

1   /*
2    * Copyright (C) 2013 Room Work eXperience
3    *
4    * This program is free software: you can redistribute it and/or modify
5    * it under the terms of the GNU General Public License as published by
6    * the Free Software Foundation, either version 3 of the License, or
7    * (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU General Public License
15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16   */
17  package net.rwx.maven.asciidoc;
18  
19  import net.rwx.maven.asciidoc.configuration.Document;
20  import com.google.inject.Guice;
21  import com.google.inject.Injector;
22  import java.io.File;
23  import java.util.List;
24  import net.rwx.maven.asciidoc.services.ServiceOrchestrator;
25  import net.rwx.maven.asciidoc.services.modules.AsciidocModule;
26  import org.apache.maven.plugin.AbstractMojo;
27  import org.apache.maven.plugin.MojoExecutionException;
28  
29  /**
30   * Goal to compile Asciidoc documents.
31   * 
32   * @author Arnaud Fonce <arnaud.fonce@r-w-x.net>
33   * 
34   * @goal asciidoc
35   */
36  public class AsciidocMojo extends AbstractMojo {
37  
38      /**
39       * Default document type which apply on each document.
40       * @parameter default-value="article"
41       * @since 0.1
42       */
43      private String defaultDocumentType;
44      /**
45       * Default target path for generated documents.
46       * @parameter default-value="${project.build.directory}/asciidoc"
47       * @since 0.1
48       */
49      private String defaultOutputPath;
50      /**
51       * Default backend which apply on each document.
52       * @parameter default-value="html5"
53       * @since 0.1
54       */
55      private String defaultBackend;
56      /**
57       * @parameter default-value="${project.file}"
58       * @readonly
59       */
60      private File projectFile;
61      
62      /**
63       * The documents list to generate.
64       * @parameter
65       * @since 0.1
66       */
67      private List<Document> documents;
68      
69      @Override
70      public void execute() throws MojoExecutionException {
71  
72          getLog().info( "Starting asciidoc compilation" );
73          getLog().info( "Default document type : " + defaultDocumentType );
74          getLog().info( "Default output path : " + defaultOutputPath );
75          getLog().info( "Default backend : " + defaultBackend );
76          
77          if( documents == null ) {
78              getLog().info( "Nothing to be done" );
79              return;
80          }
81  
82          ServiceOrchestrator orchestrator = getServiceOrchestrator();
83          orchestrator.setLogger( getLog() );
84          
85          for ( Document document : documents ) {
86              computeDocument( document );
87              orchestrator.execute( document );
88          }
89      }
90      
91      private String determineValue( String inputValue, String defaultValue ) {
92          return (inputValue == null)?defaultValue:inputValue;
93      }
94      
95      private void computeDocument( Document document ) {
96          document.setBackend( determineValue( document.getBackend(), defaultBackend) );
97          document.setDocumentType( determineValue( document.getDocumentType(), defaultDocumentType ) );
98          document.setOutputPath( determineValue( document.getOutputPath(), defaultOutputPath ) );
99          
100         String realPath = projectFile.getParent() + File.separator + document.getPath();
101         document.setPath( realPath );
102     }
103     
104     private ServiceOrchestrator getServiceOrchestrator() throws MojoExecutionException {
105         Injector injector = Guice.createInjector( new AsciidocModule() );
106         return injector.getInstance( ServiceOrchestrator.class );
107     }
108 
109     public String getDefaultDocumentType() {
110         return defaultDocumentType;
111     }
112 
113     public void setDefaultDocumentType(String defaultDocumentType) {
114         this.defaultDocumentType = defaultDocumentType;
115     }
116 
117     public String getDefaultOutputPath() {
118         return defaultOutputPath;
119     }
120 
121     public void setDefaultOutputPath(String defaultOutputPath) {
122         this.defaultOutputPath = defaultOutputPath;
123     }
124 
125     public String getDefaultBackend() {
126         return defaultBackend;
127     }
128 
129     public void setDefaultBackend(String defaultBackend) {
130         this.defaultBackend = defaultBackend;
131     }
132 
133     public File getProjectFile() {
134         return projectFile;
135     }
136 
137     public void setProjectFile(File projectFile) {
138         this.projectFile = projectFile;
139     }
140 
141     public List<Document> getDocuments() {
142         return documents;
143     }
144 
145     public void setDocuments(List<Document> documents) {
146         this.documents = documents;
147     }
148     
149     
150 }