public static string ToJSON(VSInstance inst)
        {
            List <string> outStrings = new List <string>();
            List <string> keys       = new List <string>(inst.Keys);

            keys.Sort();
            foreach (string key in keys)
            {
                string line = '"' + key + "\": ";
                switch (inst[key].GetType().Name)
                {
                case "String":
                    string str = ((String)inst[key]).Replace("\\", "\\\\");
                    if (str.Trim().IndexOf('{') == 0)
                    {
                        line += str;
                    }
                    else
                    {
                        line += '"' + str + '"';
                    }
                    break;

                case "String[]":
                    line += "[\n    " + String.Join(",\n    ", (string[])inst[key]) + "    \n    ]";
                    break;

                case "Boolean":
                    line += (Boolean)inst[key] ? "true" : "false";
                    break;

                default:
                    line = null;
                    break;
                }
                if (line != null)
                {
                    outStrings.Add(line);
                }
            }
            return("{\n        " + String.Join(",\n        ", outStrings.ToArray()) + "\n    }");
        }
Exemplo n.º 2
0
        private static VSInstance ParseInstance(ISetupInstance2 setupInstance2)
        {
            VSInstance inst = new VSInstance();

            string[] prodParts = setupInstance2.GetProduct().GetId().Split('.');
            Array.Reverse(prodParts);
            inst["Product"]              = prodParts[0];
            inst["ID"]                   = setupInstance2.GetInstanceId();
            inst["Name"]                 = setupInstance2.GetDisplayName(0x1000);
            inst["Description"]          = setupInstance2.GetDescription(0x1000);
            inst["InstallationName"]     = setupInstance2.GetInstallationName();
            inst["Version"]              = setupInstance2.GetInstallationVersion();
            inst["State"]                = Enum.GetName(typeof(InstanceState), setupInstance2.GetState());
            inst["InstallationPath"]     = setupInstance2.GetInstallationPath();
            inst["IsComplete"]           = setupInstance2.IsComplete();
            inst["IsLaunchable"]         = setupInstance2.IsLaunchable();
            inst["Common7ToolsPath"]     = inst["InstallationPath"] + @"\Common7\Tools\";
            inst["CmdPath"]              = inst["Common7ToolsPath"] + "VsDevCmd.bat";
            inst["VCAuxiliaryBuildPath"] = inst["InstallationPath"] + @"\VC\Auxiliary\Build\";
            inst["VCVarsAllPath"]        = inst["VCAuxiliaryBuildPath"] + "vcvarsall.bat";

            inst["Win8SDK"]        = "";
            inst["SDK10Full"]      = "";
            inst["VisualCppTools"] = "";

            Regex trimmer = new Regex(@"\.\d+$");

            List <string> packs = new List <String>();

            foreach (ISetupPackageReference package in setupInstance2.GetPackages())
            {
                string id = package.GetId();

                string ver    = package.GetVersion();
                string detail = "{\"id\": \"" + id + "\", \"version\":\"" + ver + "\"}";
                packs.Add("        " + detail);

                if (id.Contains("Component.MSBuild"))
                {
                    inst["MSBuild"]        = detail;
                    inst["MSBuildVerFull"] = ver;
                }
                else if (id.Contains("Microsoft.VisualCpp.Tools.Core"))
                {
                    inst["VCTools"] = detail;
                    inst["VisualCppToolsFullVersion"] = ver;
                    string majorMinor = trimmer.Replace(trimmer.Replace(ver, ""), "");
                    inst["VisualCppToolsVersionMinor"] = majorMinor;
                    inst["VCToolsVersionCode"]         = "vc" + majorMinor.Replace(".", "");
                }
                else if (id.Contains("Microsoft.Windows.81SDK"))
                {
                    if (inst["Win8SDK"].ToString().CompareTo(ver) > 0)
                    {
                        continue;
                    }
                    inst["Win8SDK"] = ver;
                }
                else if (id.Contains("Win10SDK_10"))
                {
                    if (inst["SDK10Full"].ToString().CompareTo(ver) > 0)
                    {
                        continue;
                    }
                    inst["SDK10Full"] = ver;
                }
            }
            packs.Sort();
            inst["Packages"] = packs.ToArray();

            string[] sdk10Parts = inst["SDK10Full"].ToString().Split('.');
            sdk10Parts[sdk10Parts.Length - 1] = "0";
            inst["SDK10"] = String.Join(".", sdk10Parts);
            inst["SDK"]   = inst["SDK10"].ToString() != "0" ? inst["SDK10"] : inst["Win8SDK"];
            if (inst.ContainsKey("MSBuildVerFull"))
            {
                string ver = trimmer.Replace(trimmer.Replace((string)inst["MSBuildVerFull"], ""), "");
                inst["MSBuildVer"]       = ver;
                inst["MSBuildToolsPath"] = inst["InstallationPath"] + @"\MSBuild\" + ver + @"\Bin\";
                inst["MSBuildPath"]      = inst["MSBuildToolsPath"] + "MSBuild.exe";
            }
            if (inst.ContainsKey("VCTools"))
            {
                string ver = trimmer.Replace((string)inst["VisualCppToolsFullVersion"], "");
                inst["VisualCppToolsX64"]   = inst["InstallationPath"] + @"\VC\Tools\MSVC\" + ver + @"\bin\HostX64\x64\";
                inst["VisualCppToolsX64CL"] = inst["VisualCppToolsX64"] + "cl.exe";
                inst["VisualCppToolsX86"]   = inst["InstallationPath"] + @"\VC\Tools\MSVC\" + ver + @"\bin\HostX86\x86\";
                inst["VisualCppToolsX86CL"] = inst["VisualCppToolsX86"] + "cl.exe";
                inst["VisualCppTools"]      = Is64() ? inst["VisualCppToolsX64"] : inst["VisualCppToolsX86"];
                inst["VisualCppToolsCL"]    = Is64() ? inst["VisualCppToolsX64CL"] : inst["VisualCppToolsX86CL"];
            }
            inst["IsVcCompatible"] = inst.JSONBool("SDK") && inst.JSONBool("MSBuild") && inst.JSONBool("VisualCppTools");
            return(inst);
        }