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 getSdkLocation(Project antProject)34 static File getSdkLocation(Project antProject) { 35 // get the SDK location 36 String sdkOsPath = antProject.getProperty(ProjectProperties.PROPERTY_SDK); 37 38 // check if it's valid and exists 39 if (sdkOsPath == null || sdkOsPath.length() == 0) { 40 throw new BuildException("SDK Location is not set."); 41 } 42 43 File sdk = new File(sdkOsPath); 44 if (sdk.isDirectory() == false) { 45 throw new BuildException(String.format("SDK Location '%s' is not valid.", sdkOsPath)); 46 } 47 48 return sdk; 49 } 50 51 /** 52 * Returns the revision of the tools for a given SDK. 53 * @param sdkFile the {@link File} for the root folder of the SDK 54 * @return the tools revision or -1 if not found. 55 */ getToolsRevision(File sdkFile)56 static int getToolsRevision(File sdkFile) { 57 Properties p = new Properties(); 58 try{ 59 // tools folder must exist, or this custom task wouldn't run! 60 File toolsFolder= new File(sdkFile, SdkConstants.FD_TOOLS); 61 File sourceProp = new File(toolsFolder, SdkConstants.FN_SOURCE_PROP); 62 p.load(new FileInputStream(sourceProp)); 63 String value = p.getProperty("Pkg.Revision"); //$NON-NLS-1$ 64 if (value != null) { 65 return Integer.parseInt(value); 66 } 67 } catch (FileNotFoundException e) { 68 // couldn't find the file? return -1 below. 69 } catch (IOException e) { 70 // couldn't find the file? return -1 below. 71 } 72 73 return -1; 74 } 75 checkSinglePath(String attribute, Path path)76 static String checkSinglePath(String attribute, Path path) { 77 String[] paths = path.list(); 78 if (paths.length != 1) { 79 throw new BuildException(String.format( 80 "Value for '%1$s' is not valid. It must resolve to a single path", attribute)); 81 } 82 83 return paths[0]; 84 } 85 } 86