Esempio n. 1
0
        private static void Main(string[] args)
        {
            try
            {
                var config = new VS2017Info.Vs2017SetupConfig();
                foreach (var instance in config.VSInstances)
                {
                    Console.WriteLine($"instance ID:{instance.Id}");
                    Console.WriteLine($"installed path:{instance.InstalledPath}");
                    var vsPath = VS2017Info.VS2017AppData.GetLocalAppDataPath(instance.Version, instance.Id);
                    Console.WriteLine($"local appdata path is {vsPath}");

                    var extensions = VS2017Info.VS2017AppData.GetInstalledExtensions(instance.Version, instance.Id);
                    foreach (var ext in extensions)
                    {
                        Console.WriteLine($"{ext.Key} {ext.Value}");
                    }


                    //var hivefile = VS2017Info.VS2017AppData.GetPrivateRegFilename(instance.Version, instance.Id);
                    //var hKey = RegistryNativeMethods.RegLoadAppKey(hivefile);

                    //var keyVersion = VS2017Info.VS2017AppData.GetVersionForRegistryKey(instance.Version, instance.Id);

                    //var keypath = $@"Software\Microsoft\VisualStudio\{keyVersion}\ExtensionManager\EnabledExtensions";
                    //using (var safeRegistryHandle = new SafeRegistryHandle(new IntPtr(hKey), true))
                    //using (var appKey = RegistryKey.FromHandle(safeRegistryHandle))
                    //using (var extensionsKey = appKey.OpenSubKey(keypath, true))
                    //{
                    //    if (extensionsKey == null)
                    //    {
                    //        throw new ApplicationException("Specified file is not a well-formed software registry hive: " + hivefile);
                    //    }

                    //    var allExtensions = extensionsKey.GetValueNames().Select(x=>x.Split(',').FirstOrDefault());
                    //    foreach (var extGuid in allExtensions)
                    //    {
                    //        var value = extensionsKey.GetValue(extGuid);
                    //        Console.WriteLine($"extension:{extGuid}: {value}");
                    //    }
                    //}
                }
            }
            catch (Exception ex)
            {
                var codeBase = System.Reflection.Assembly.GetExecutingAssembly().CodeBase;
                var progname = Path.GetFileNameWithoutExtension(codeBase);
                Console.Error.WriteLine(progname + ": Error: " + ex.ToString());
            }
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            Settings.Go();
            VSGlobals.ProjectGUID = Guid.NewGuid();
            var s = VSGlobals.ExpandMacros("$$(PROJECTGUID)");

            var codeBase = System.Reflection.Assembly.GetEntryAssembly().CodeBase;

            progname = Path.GetFileNameWithoutExtension(codeBase);
            try
            {
                var arglist = args.ToList();

                if (args.Count() == 0)
                {
                    Console.WriteLine($"{progname} - make various types of Visual Studio solution and start Visual Studio.\n");
                    Console.WriteLine("Usage:\n\n    proggen <SOLUTIONTYPE> <solution-name> <solution-name> ...\n");
                    Console.WriteLine("An instance of Visual Studio is started for each solution name specified.");
                    Console.WriteLine("Solutions types are as follows:\n");
                    foreach (var helpText in GeneratorManager.HelpTexts)
                    {
                        Console.WriteLine(helpText);
                    }
                    Console.WriteLine("\nAlternatively if this program has the name of one of the solution types\nabove, the solution type parameter is not needed (the solution type is determined from the program name).");
                    Console.WriteLine($"\nSpecifying {progname} -makeg with no other parameters causes proggen to\ncopy itself to each of the solution-types specified above (with\na .exe extension)");
                    Environment.Exit(0);
                }
                else if (args[0] == "-makeg" || args[0] == "--makeg")
                {
                    if (args.Count() > 1)
                    {
                        throw new Exception("-makeg option does not take parameters");
                    }
                    GeneratorManager.MakeAllGenerators();
                    Environment.Exit(0);
                }
                else if (args[0] == "-instances")
                {
                    if (args.Count() > 1)
                    {
                        throw new Exception("-instances does not take parameters.");
                    }
                    var vs2017Info = new VS2017Info.Vs2017SetupConfig();
                    var instances  = vs2017Info.VSInstances;
                    foreach (var instance in instances)
                    {
                        Console.WriteLine($"ID: {instance.Id} Path: {Path.Combine(instance.InstalledPath, instance.ProductPath)}");
                    }
                    Environment.Exit(0);
                }
                else if (args[0] == "-version" || args[0] == "--version")
                {
                    PrintProgramVersion();
                    Environment.Exit(0);
                }

                var generatorName = "";
                // is the program name a generator name?
                if (GeneratorManager.IsAGenarator(progname))
                {
                    generatorName = progname;
                    if (arglist[0] == "-g")
                    {
                        if (arglist.Count < 2)
                        {
                            throw new Exception("You must specify the name of a project to create.");
                        }
                        Utility.Shift(ref arglist);
                    }
                }
                else
                {
                    bool haveOption = arglist[0][0] == '-';
                    var  minargs    = haveOption ? 3 : 2;
                    if (arglist.Count < minargs)
                    {
                        throw new Exception("You must specify at least one project name.");
                    }

                    if (haveOption)
                    {
                        var option = arglist[0];
                        if (option != "-g")
                        {
                            throw new Exception($"'{option}' is an invalid option");
                        }
                        Utility.Shift(ref arglist);
                    }

                    generatorName = arglist[0];
                    if (!GeneratorManager.IsAGenarator(generatorName))
                    {
                        throw new Exception($"'{generatorName}' is not a valid project type.");
                    }
                    Utility.Shift(ref arglist);

                    // alternatively an option can go after the project type:
                    var optionAfterGenerator = arglist[0][0] == '-';
                    if (optionAfterGenerator)
                    {
                        var option = arglist[0];
                        if (option != "-g")
                        {
                            throw new Exception($"'{option}' is an invalid option");
                        }
                        Utility.Shift(ref arglist);
                    }
                }

                foreach (var project in arglist)
                {
                    if (project[0] == '-')
                    {
                        Console.Error.WriteLine($"{progname}: option {project} ignored.");
                    }
                    else
                    {
                        DoGenerate(project, generatorName);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine($"{progname}: error: {ex}");
            }
        }
Esempio n. 3
0
        public static void StartVisualStudio(string vsVersion, string command, string commandParam)
        {
            var vsExecutableName = "";
            var amsExtensions    = false;

            if (vsVersion == "2010")
            {
                var regkey = @"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\10.0";
                vsExecutableName  = (string)Registry.GetValue(regkey, "InstallDir", "");
                vsExecutableName += "devenv.exe";
            }
            else if (vsVersion == "2012")
            {
                var regkey = @"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\11.0";
                vsExecutableName  = (string)Registry.GetValue(regkey, "InstallDir", "");
                vsExecutableName += "devenv.exe";
            }
            else if (vsVersion == "2013")
            {
                var regkey = @"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\12.0";
                vsExecutableName  = (string)Registry.GetValue(regkey, "InstallDir", "");
                vsExecutableName += "devenv.exe";
            }
            else if (vsVersion == "2015")
            {
                var regkey = @"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\14.0";
                vsExecutableName  = (string)Registry.GetValue(regkey, "InstallDir", "");
                vsExecutableName += "devenv.exe";
            }
            else if (vsVersion == "2017" || vsVersion == "2019" || vsVersion == "2022")
            {
                // Both VS2017 and VS2019 have the same "private registry" mechanism for storing installed instances:
                var vsConfig  = new VS2017Info.Vs2017SetupConfig();
                var instances = vsConfig.VSInstances;

                // first get the numeric version number of the version of Visual Studio we wish to start. For VS2017, this is 15, for
                // VS2019 this is 16.
                int ver;
                switch (vsVersion)
                {
                case "2017":
                    ver = 15;
                    break;

                case "2019":
                    ver = 16;
                    break;

                case "2022":
                    ver = 17;
                    break;

                default:
                    throw new Exception($"Bad version ({vsVersion}) specified in generator");
                }

                //Console.WriteLine($"Looking for numeric version ID {ver}");

                // get instances of the specified version of Visual Studio:
                var instancesForVersion = instances.Where(x =>
                {
                    var dotPos         = x.Version.IndexOf(".");
                    var numericVersion = Convert.ToInt32(x.Version.Substring(0, dotPos));
                    return(numericVersion == ver);
                }).Where(y => y.ProductPath.EndsWith("devenv.exe"));

                var instanceToStart = instancesForVersion.First();

                instancesForVersion.ToList().ForEach(x => Console.WriteLine($"Found instance {x.Id} {x.InstalledPath}"));

                if (instancesForVersion.Count() > 1)
                {
                    var defaultInstanceId = ver == 15 ? Environment.GetEnvironmentVariable("VSDefaultInstanceID") : Environment.GetEnvironmentVariable("VSDefaultInstanceID19");

                    if (!string.IsNullOrWhiteSpace(defaultInstanceId))
                    {
                        foreach (var instance in instancesForVersion)
                        {
                            if (instance.Id == defaultInstanceId)
                            {
                                instanceToStart = instance;
                            }
                        }
                    }
                    else
                    {
                        var message = "Cannot start visual studio as I do not know which installed instance to use - it could be: \n";
                        foreach (var instance in instances)
                        {
                            message += Path.Combine(instance.InstalledPath, instance.ProductPath) + "\n";
                        }
                        message += $"Set the environment variable {InstanceEnvVar} to the instance ID you want to use to resolve this.";
                        throw new Exception(message);
                    }
                }
                vsExecutableName = Path.Combine(instanceToStart.InstalledPath, instanceToStart.ProductPath);

                // find out if AMSExtensions present:

                try
                {
                    var extensions = VS2017Info.VS2017AppData.GetInstalledExtensions(instanceToStart.Version, instanceToStart.Id);
                    if (extensions != null)
                    {
                        var amsExtPresent = extensions.Any(x => !string.IsNullOrWhiteSpace(x.Key) && x.Key.Split(',')[0] == "3c6063ca-5f33-4889-aaae-387e9d5a0368");
                        if (amsExtPresent)
                        {
                            Console.WriteLine("AMS extensions found");
                            amsExtensions = true;
                        }
                    }
                }
                catch (VS2017Info.VsInfoException ex)
                {
                    Console.WriteLine($"INFO: {ex.Message}");
                }
            }

            Process process = new Process();

            process.StartInfo.FileName = vsExecutableName;

            // if command has a parameter then the whole command-plus-parameter string needs to be in quotes:
            if (!string.IsNullOrWhiteSpace(command))
            {
                if (!string.IsNullOrWhiteSpace(commandParam))
                {
                    command = "\"" + command + " " + commandParam + "\"";
                }
                else
                {
                    command = VSGlobals.VSCommand;
                }
                command = "/command " + command;
            }

            if (!string.IsNullOrWhiteSpace(VSGlobals.VSCommandParam))
            {
                command = command + "";
                // command = "\"" + command + " " + VSGlobals.VSCommandParam + "\"";
            }
            process.StartInfo.Arguments   = Path.Combine(VSGlobals.ProjectName, VSGlobals.ProjectName + ".sln") + (amsExtensions ? (" " + command) : "");
            process.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
            process.Start();
            process.WaitForInputIdle();

            Console.WriteLine($"{process.Id} {vsExecutableName} {command}");
        }