buildframework/helium/sf/java/environment/src/com/nokia/helium/environment/ant/taskdefs/EnvironmentTask.java
changeset 628 7c4a911dc066
child 645 b8d81fa19e7d
equal deleted inserted replaced
588:c7c26511138f 628:7c4a911dc066
       
     1 /*
       
     2  * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     3  * All rights reserved.
       
     4  * This component and the accompanying materials are made available
       
     5  * under the terms of the License "Eclipse Public License v1.0"
       
     6  * which accompanies this distribution, and is available
       
     7  * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8  *
       
     9  * Initial Contributors:
       
    10  * Nokia Corporation - initial contribution.
       
    11  *
       
    12  * Contributors:
       
    13  *
       
    14  * Description: 
       
    15  *
       
    16  */
       
    17 
       
    18 package com.nokia.helium.environment.ant.taskdefs;
       
    19 
       
    20 import java.io.File;
       
    21 import java.io.FileNotFoundException;
       
    22 import java.io.FileOutputStream;
       
    23 import java.io.IOException;
       
    24 import java.io.OutputStream;
       
    25 import java.util.ArrayList;
       
    26 import java.util.Iterator;
       
    27 import java.util.List;
       
    28 
       
    29 import org.apache.tools.ant.BuildException;
       
    30 import org.apache.tools.ant.Project;
       
    31 import org.apache.tools.ant.Task;
       
    32 
       
    33 import com.nokia.helium.environment.Environment;
       
    34 import com.nokia.helium.environment.EnvironmentXMLWriter;
       
    35 import com.nokia.helium.environment.ant.listener.ExecListener;
       
    36 import com.nokia.helium.environment.ant.types.EnvData;
       
    37 import com.nokia.helium.environment.ant.types.ExecutableInfo;
       
    38 
       
    39 /**
       
    40  * Checks the environment and logs executable information. Can validate tools versions if needed.
       
    41  * 
       
    42  * @ant.task name="environment" category="Environment"
       
    43  */
       
    44 public class EnvironmentTask extends Task {
       
    45     private File outputFile;
       
    46     private List<EnvData> envDataList = new ArrayList<EnvData>();
       
    47 
       
    48     public EnvironmentTask() {
       
    49         setTaskName("environment");
       
    50     }
       
    51 
       
    52     /**
       
    53      * Set the output file path.
       
    54      * 
       
    55      * @param outputFile
       
    56      */
       
    57     public void setOutput(File outputFile) {
       
    58         this.outputFile = outputFile;
       
    59     }
       
    60 
       
    61     /**
       
    62      * Add envdata types.
       
    63      * 
       
    64      * @ant.required
       
    65      */
       
    66     public void add(EnvData envData) {
       
    67         envDataList.add(envData);
       
    68     }
       
    69 
       
    70     @Override
       
    71     public void execute() {
       
    72         Project project = getProject();
       
    73         project.log("Scanning environment...", Project.MSG_DEBUG);
       
    74         OutputStream out = System.out;
       
    75         try {
       
    76             if (outputFile != null) {
       
    77                 out = new FileOutputStream(outputFile);
       
    78             }
       
    79             List<ExecutableInfo> executableDefs = new ArrayList<ExecutableInfo>();
       
    80             for (Iterator<EnvData> iterator = envDataList.iterator(); iterator.hasNext();) {
       
    81                 EnvData envData = (EnvData) iterator.next();
       
    82                 envData.getExecutableDefs(executableDefs);
       
    83             }
       
    84 
       
    85             Environment environment = new Environment(project);
       
    86             environment.setExecutableDefs(executableDefs);
       
    87             environment.scan(ExecListener.getExecCalls());
       
    88             EnvironmentXMLWriter writer = new EnvironmentXMLWriter(out);
       
    89             writer.write(environment);
       
    90         }
       
    91 
       
    92         catch (FileNotFoundException e) {
       
    93             e.printStackTrace();
       
    94             throw new BuildException("Could not find output file.");
       
    95         }
       
    96         catch (IOException e) {
       
    97             e.printStackTrace();
       
    98             throw new BuildException("Error reading version.");
       
    99         }
       
   100     }
       
   101 }