• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
63             FileInputStream fis = null;
64             try {
65                 fis = new FileInputStream(sourceProp);
66                 p.load(fis);
67             } finally {
68                 if (fis != null) {
69                     try {
70                         fis.close();
71                     } catch (IOException ignore) {
72                     }
73                 }
74             }
75 
76             String value = p.getProperty("Pkg.Revision"); //$NON-NLS-1$
77             if (value != null) {
78                 return Integer.parseInt(value);
79             }
80         } catch (FileNotFoundException e) {
81             // couldn't find the file? return -1 below.
82         } catch (IOException e) {
83             // couldn't find the file? return -1 below.
84         }
85 
86         return -1;
87     }
88 
checkSinglePath(String attribute, Path path)89     static String checkSinglePath(String attribute, Path path) {
90         String[] paths = path.list();
91         if (paths.length != 1) {
92             throw new BuildException(String.format(
93                     "Value for '%1$s' is not valid. It must resolve to a single path", attribute));
94         }
95 
96         return paths[0];
97     }
98 }
99