Home | History | Annotate | Download | only in ant
      1 /*
      2  * Copyright (C) 2010 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.ant;
     18 
     19 import com.android.sdklib.SdkConstants;
     20 import com.android.sdklib.internal.project.ProjectProperties;
     21 
     22 import org.apache.tools.ant.BuildException;
     23 import org.apache.tools.ant.Project;
     24 import org.apache.tools.ant.types.Path;
     25 
     26 import java.io.File;
     27 import java.io.FileInputStream;
     28 import java.io.FileNotFoundException;
     29 import java.io.IOException;
     30 import java.util.Properties;
     31 
     32 final class TaskHelper {
     33 
     34     public final static String PROP_RULES_REV = "android.ant.rules.revision";
     35 
     36     static File getSdkLocation(Project antProject) {
     37         // get the SDK location
     38         String sdkLocation = antProject.getProperty(ProjectProperties.PROPERTY_SDK);
     39 
     40         // check if it's valid and exists
     41         if (sdkLocation == null || sdkLocation.length() == 0) {
     42             // LEGACY support: project created with 1.6 or before may be using a different
     43             // property to declare the location of the SDK. At this point, we cannot
     44             // yet check which target is running so we check both always.
     45             sdkLocation = antProject.getProperty(ProjectProperties.PROPERTY_SDK_LEGACY);
     46             if (sdkLocation == null || sdkLocation.length() == 0) {
     47                 throw new BuildException("SDK Location is not set.");
     48             }
     49         }
     50 
     51         File sdk = new File(sdkLocation);
     52         if (sdk.isDirectory() == false) {
     53             throw new BuildException(String.format("SDK Location '%s' is not valid.", sdkLocation));
     54         }
     55 
     56         return sdk;
     57     }
     58 
     59     /**
     60      * Returns the revision of the tools for a given SDK.
     61      * @param sdkFile the {@link File} for the root folder of the SDK
     62      * @return the tools revision or -1 if not found.
     63      */
     64     static int getToolsRevision(File sdkFile) {
     65         Properties p = new Properties();
     66         try{
     67             // tools folder must exist, or this custom task wouldn't run!
     68             File toolsFolder= new File(sdkFile, SdkConstants.FD_TOOLS);
     69             File sourceProp = new File(toolsFolder, SdkConstants.FN_SOURCE_PROP);
     70             p.load(new FileInputStream(sourceProp));
     71             String value = p.getProperty("Pkg.Revision"); //$NON-NLS-1$
     72             if (value != null) {
     73                 return Integer.parseInt(value);
     74             }
     75         } catch (FileNotFoundException e) {
     76             // couldn't find the file? return -1 below.
     77         } catch (IOException e) {
     78             // couldn't find the file? return -1 below.
     79         }
     80 
     81         return -1;
     82     }
     83 
     84     static String checkSinglePath(String attribute, Path path) {
     85         String[] paths = path.list();
     86         if (paths.length != 1) {
     87             throw new BuildException(String.format(
     88                     "Value for '%1$s' is not valid. It must resolve to a single path", attribute));
     89         }
     90 
     91         return paths[0];
     92     }
     93 }
     94