001    /*
002     * Copyright 2005 Stephen J. McConnell
003     *
004     * Licensed  under the  Apache License,  Version 2.0  (the "License");
005     * you may not use  this file  except in  compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *   http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed  under the  License is distributed on an "AS IS" BASIS,
012     * WITHOUT  WARRANTIES OR CONDITIONS  OF ANY KIND, either  express  or
013     * implied.
014     *
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    
019    package net.dpml.tools.tasks;
020    
021    import java.io.File;
022    
023    import net.dpml.library.Resource;
024    
025    import net.dpml.tools.Context;
026    
027    import org.apache.tools.ant.Project;
028    
029    
030    /**
031     * Prepare the target build directory based on content presented under the
032     * ${basedir}/src and ${basedir}/etc directories.
033     *
034     * @author <a href="http://www.dpml.net">Digital Product Meta Library</a>
035     * @version 1.1.3
036     */
037    public class PrepareTask extends GenericTask
038    {
039        private static final String SRC_FILTERED_INCLUDES_KEY =
040          "project.prepare.src.filtered.includes";
041        private static final String SRC_FILTERED_INCLUDES_VALUE =
042          "**/*.java,**/*.x*,**/*.properties,**/*.html";
043    
044        private static final String ETC_FILTERED_INCLUDES_KEY =
045          "project.prepare.etc.filtered.includes";
046        private static final String ETC_FILTERED_INCLUDES_VALUE =
047          "**/*";
048        
049        private static final String ETC_FILTERED_EXCLUDES_KEY =
050          "project.prepare.etc.filtered.excludes";
051        private static final String ETC_FILTERED_EXCLUDES_VALUE =
052          "**/*.exe,**/*.jar*,**/*.dll,**/*.gif,**/*.jpeg,**/*.jpg,**/*.ico,**/*.png,**/*.keystore";
053        
054        private boolean m_init = false;
055        
056       /**
057        * Task initiaization during which filter values are established.
058        */
059        public void init()
060        {
061            if( !m_init )
062            {
063                log( "Prepare initialization for: " 
064                  + getResource(), Project.MSG_VERBOSE );
065                final Project project = getProject();
066                project.setProperty(
067                  SRC_FILTERED_INCLUDES_KEY, SRC_FILTERED_INCLUDES_VALUE );
068                project.setProperty(
069                  ETC_FILTERED_INCLUDES_KEY, ETC_FILTERED_INCLUDES_VALUE );
070                project.setProperty(
071                  ETC_FILTERED_EXCLUDES_KEY, ETC_FILTERED_EXCLUDES_VALUE );
072                m_init = true;
073            }
074        }
075        
076       /**
077        * Replicates the content of the src and etc directory to the target directory applying
078        * a set of fixed rules - see prepare task documentation for details.
079        */
080        public void execute()
081        {
082            final Project project = getProject();
083            Resource resource = getResource();
084            Context context = getContext();
085            context.init();
086            
087            log( "basedir: " + getResource().getBaseDir(), Project.MSG_VERBOSE );
088            
089            //
090            // setup the file system
091            //
092            
093            String filters = context.getProperty( SRC_FILTERED_INCLUDES_KEY, SRC_FILTERED_INCLUDES_VALUE );
094            if( resource.getTypes().length > 0 )
095            {
096                mkDir( context.getTargetDirectory() );
097            }
098            if( context.getSrcMainDirectory().exists() )
099            {
100                log( "preparing 'main' src.", Project.MSG_VERBOSE );
101                File src = context.getSrcMainDirectory();
102                File dest = context.getTargetBuildMainDirectory();
103                mkDir( dest );
104                copy( src, dest, true, filters, "" );
105                copy( src, dest, false, "**/*.*", filters );
106            }
107            else
108            {
109                log( "project does not contain 'main' src.", Project.MSG_VERBOSE );
110            }
111            if( context.getSrcDocsDirectory().exists() )
112            {
113                log( "preparing 'docs' src.", Project.MSG_VERBOSE );
114                File src = context.getSrcDocsDirectory();
115                File dest = context.getTargetBuildDocsDirectory();
116                mkDir( dest );
117                copy( src, dest, true, filters, "" );
118                copy( src, dest, false, "**/*.*", filters );
119            }
120            else
121            {
122                log( "project does not contain 'doc' src.", Project.MSG_VERBOSE );
123            }
124            if( context.getSrcTestDirectory().exists() )
125            {
126                log( "preparing 'test' src.", Project.MSG_VERBOSE );
127                File src = context.getSrcTestDirectory();
128                File dest = context.getTargetDirectory( "build/test" );
129                mkDir( dest );
130                copy( src, dest, true, filters, "" );
131                copy( src, dest, false, "**/*.*", filters );
132                File test = context.getTargetDirectory( "test" );
133                mkDir( test );
134            }
135            else
136            {
137                log( "project does not contain 'test' src.", Project.MSG_VERBOSE );
138            }
139            
140            if( context.getEtcMainDirectory().exists() )
141            {
142                final String includes = 
143                  context.getProperty( ETC_FILTERED_INCLUDES_KEY, ETC_FILTERED_INCLUDES_VALUE );
144                final String excludes = 
145                  context.getProperty( ETC_FILTERED_EXCLUDES_KEY, ETC_FILTERED_EXCLUDES_VALUE );
146                
147                //
148                // copy ${etc}/main content to ${target}/build/main
149                //
150                
151                final File etcMain = context.getEtcMainDirectory();
152                final File buildMainDir = context.getTargetDirectory( "build/main" );
153                copy( etcMain, buildMainDir, true, includes, excludes );
154                copy( etcMain, buildMainDir, false, excludes, "" );
155            }
156            
157            if( context.getEtcTestDirectory().exists() )
158            {
159                final String includes = 
160                  context.getProperty( ETC_FILTERED_INCLUDES_KEY, ETC_FILTERED_INCLUDES_VALUE );
161                final String excludes = 
162                  context.getProperty( ETC_FILTERED_EXCLUDES_KEY, ETC_FILTERED_EXCLUDES_VALUE );
163                
164                //
165                // copy ${etc}/test content to ${target}/build/test
166                //
167                
168                final File etcTest = context.getEtcTestDirectory();
169                final File test = context.getTargetDirectory( "build/test" );
170                copy( etcTest, test, true, includes, excludes );
171                copy( etcTest, test, false, excludes, "" );
172            }
173            
174            if( context.getEtcDataDirectory().exists() )
175            {
176                final String includes = 
177                  context.getProperty( ETC_FILTERED_INCLUDES_KEY, ETC_FILTERED_INCLUDES_VALUE );
178                final String excludes = 
179                  context.getProperty( ETC_FILTERED_EXCLUDES_KEY, ETC_FILTERED_EXCLUDES_VALUE );
180                
181                //
182                // copy ${etc}/test content to ${target}/build/test
183                //
184                
185                final File data = context.getEtcDataDirectory();
186                final File test = context.getTargetDirectory( "test" );
187                copy( data, test, true, includes, excludes );
188                copy( data, test, false, excludes, "" );
189            }
190    
191            if( context.getEtcDirectory().exists() )
192            {
193                final String includes = 
194                  context.getProperty( ETC_FILTERED_INCLUDES_KEY, ETC_FILTERED_INCLUDES_VALUE );
195                final String excludes = 
196                  context.getProperty( ETC_FILTERED_EXCLUDES_KEY, ETC_FILTERED_EXCLUDES_VALUE );
197                final File etc = context.getEtcDirectory();
198                
199                //
200                //  ${etc}/* directories (excluding test, main and deliverables)
201                // directly to the target directory
202                //
203    
204                String main = context.getEtcMainDirectory().toString() + "/**";
205                String test = context.getEtcTestDirectory().toString() + "/**";
206                String data = context.getEtcDataDirectory().toString() + "/**";
207                File target = context.getTargetDirectory();
208                final String standard = main + "," + test + "," + data;
209                copy( etc, target, true, includes, standard + "," + excludes );
210                copy( etc, target, false, excludes, standard );
211            }
212        }
213    }