001    /*
002            Copyright (c) 2009 Olivier Chafik, All Rights Reserved
003            
004            This file is part of JNAerator (http://jnaerator.googlecode.com/).
005            
006            JNAerator is free software: you can redistribute it and/or modify
007            it under the terms of the GNU General Public License as published by
008            the Free Software Foundation, either version 3 of the License, or
009            (at your option) any later version.
010            
011            JNAerator is distributed in the hope that it will be useful,
012            but WITHOUT ANY WARRANTY; without even the implied warranty of
013            MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014            GNU General Public License for more details.
015            
016            You should have received a copy of the GNU General Public License
017            along with JNAerator.  If not, see <http://www.gnu.org/licenses/>.
018    */
019    package com.ochafik.admin.visualstudio;
020    
021    import java.io.File;
022    import java.util.Arrays;
023    import java.util.List;
024    import java.util.logging.Level;
025    import java.util.logging.Logger;
026    import java.util.regex.Pattern;
027    
028    import com.ochafik.util.listenable.Adapter;
029    import com.ochafik.util.string.RegexUtils;
030    
031    public class VisualStudioUtils {
032            private static Logger logger = Logger.getLogger(VisualStudioUtils.class.getName());
033    
034            public static List<String> getMicrosoftIncludes() {
035                    return Arrays.asList(getProp("VISUAL_STUDIO_INCLUDES", 
036                            getMicrosoftVisualStudioHome() + "\\VC\\include" + ";" + getMicrosoftWindowsSDKHome() + "\\Include",
037                            true
038                    ).split(";"));
039            }
040            public static String getMicrosoftWindowsSDKHome() {
041                    return getProp("WINDOWS_SDK_HOME", "C:\\Program Files\\Microsoft SDKs\\Windows\\v6.0A", true);
042            }
043            private static String getMicrosoftVisualStudioHome() {
044                    return getProp("VISUAL_STUDIO_HOME", "C:\\Program Files\\Microsoft Visual Studio 9.0", true);
045            }
046            static String getProp(String name, String defVal) {
047                    return getProp(name, defVal, false);
048            }
049            
050            static String getProp(String name, String defVal, boolean remember) {
051                    String v = System.getenv(name);
052                    v = v == null ? System.getProperty(name, defVal) : v;
053                    logger.log(Level.INFO, "[environment] " + name + "=" + v);
054                    return v;
055            }
056    
057            static final Pattern variablePattern = Pattern.compile("$\\(([^)]+)\\)");
058            enum VarType {
059                    SolutionName, 
060                    SolutionDir,
061                    ProjectName,
062                    ConfigurationName,
063                    TargetDir,
064                    TargetPath
065            }
066            public static String resolveVariables(String string, final Solution solution, final Project project, final Configuration configuration) {
067                    String res = RegexUtils.regexReplace(variablePattern, string, new Adapter<String[], String>() {
068                            public String adapt(String[] value) {
069                                    String match = value[0];
070                                    String variableName = value[1];
071                                    VarType type = VarType.valueOf(variableName);
072                                    String name;
073                                    if (type != null) {
074                                            switch (type) {
075                                            case ProjectName:
076                                                    return project.name == null ? match : project.name;
077                                            case TargetPath:
078                                                    return configuration.outputFile == null ? match : configuration.outputFile;
079                                            case SolutionName:
080                                                    name = solution.getName();
081                                                    return name == null ? match : name;
082                                            case SolutionDir:
083                                                    return solution.solutionFile == null ? match : solution.solutionFile.getParent();
084                                            case ConfigurationName:
085                                                    return configuration.name;
086                                            case TargetDir:
087                                                    if (configuration.outputFile == null)
088                                                            return match;
089                                                    
090                                                    return new File(configuration.outputFile).getParent();
091                                            default:
092                                                    return match;
093                                            }
094                                    }
095                                    String v = System.getenv().get(variableName);
096                                    return v == null ? match : v;
097                            }
098                    });
099                    //System.err.println("resolved \"" + string + "\" as :\n\t\"" + res + "\"");
100                    return res;
101            }
102    }