View Javadoc

1   /*
2   	Copyright (c) 2009 Olivier Chafik, All Rights Reserved
3   	
4   	This file is part of JNAerator (http://jnaerator.googlecode.com/).
5   	
6   	JNAerator is free software: you can redistribute it and/or modify
7   	it under the terms of the GNU General Public License as published by
8   	the Free Software Foundation, either version 3 of the License, or
9   	(at your option) any later version.
10  	
11  	JNAerator is distributed in the hope that it will be useful,
12  	but WITHOUT ANY WARRANTY; without even the implied warranty of
13  	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  	GNU General Public License for more details.
15  	
16  	You should have received a copy of the GNU General Public License
17  	along with JNAerator.  If not, see <http://www.gnu.org/licenses/>.
18  */
19  package com.ochafik.admin.visualstudio;
20  
21  import java.io.File;
22  import java.util.Arrays;
23  import java.util.List;
24  import java.util.logging.Level;
25  import java.util.logging.Logger;
26  import java.util.regex.Pattern;
27  
28  import com.ochafik.util.listenable.Adapter;
29  import com.ochafik.util.string.RegexUtils;
30  
31  public class VisualStudioUtils {
32  	private static Logger logger = Logger.getLogger(VisualStudioUtils.class.getName());
33  
34  	public static List<String> getMicrosoftIncludes() {
35  		return Arrays.asList(getProp("VISUAL_STUDIO_INCLUDES", 
36  			getMicrosoftVisualStudioHome() + "\\VC\\include" + ";" + getMicrosoftWindowsSDKHome() + "\\Include",
37  			true
38  		).split(";"));
39  	}
40  	public static String getMicrosoftWindowsSDKHome() {
41  		return getProp("WINDOWS_SDK_HOME", "C:\\Program Files\\Microsoft SDKs\\Windows\\v6.0A", true);
42  	}
43  	private static String getMicrosoftVisualStudioHome() {
44  		return getProp("VISUAL_STUDIO_HOME", "C:\\Program Files\\Microsoft Visual Studio 9.0", true);
45  	}
46  	static String getProp(String name, String defVal) {
47  		return getProp(name, defVal, false);
48  	}
49  	
50  	static String getProp(String name, String defVal, boolean remember) {
51  		String v = System.getenv(name);
52  		v = v == null ? System.getProperty(name, defVal) : v;
53  		logger.log(Level.INFO, "[environment] " + name + "=" + v);
54  		return v;
55  	}
56  
57  	static final Pattern variablePattern = Pattern.compile("$\\(([^)]+)\\)");
58  	enum VarType {
59  		SolutionName, 
60  		SolutionDir,
61  		ProjectName,
62  		ConfigurationName,
63  		TargetDir,
64  		TargetPath
65  	}
66  	public static String resolveVariables(String string, final Solution solution, final Project project, final Configuration configuration) {
67  		String res = RegexUtils.regexReplace(variablePattern, string, new Adapter<String[], String>() {
68  			public String adapt(String[] value) {
69  				String match = value[0];
70  				String variableName = value[1];
71  				VarType type = VarType.valueOf(variableName);
72  				String name;
73  				if (type != null) {
74  					switch (type) {
75  					case ProjectName:
76  						return project.name == null ? match : project.name;
77  					case TargetPath:
78  						return configuration.outputFile == null ? match : configuration.outputFile;
79  					case SolutionName:
80  						name = solution.getName();
81  						return name == null ? match : name;
82  					case SolutionDir:
83  						return solution.solutionFile == null ? match : solution.solutionFile.getParent();
84  					case ConfigurationName:
85  						return configuration.name;
86  					case TargetDir:
87  						if (configuration.outputFile == null)
88  							return match;
89  						
90  						return new File(configuration.outputFile).getParent();
91  					default:
92  						return match;
93  					}
94  				}
95  				String v = System.getenv().get(variableName);
96  				return v == null ? match : v;
97  			}
98  		});
99  		//System.err.println("resolved \"" + string + "\" as :\n\t\"" + res + "\"");
100 		return res;
101 	}
102 }