コード例 #1
0
ファイル: ProgramGenerator.cs プロジェクト: Ams627/proggen
        }                                                   // any empty folders required - can be null
        public virtual void Generate()
        {
            VSGlobals.GeneratorName       = Name;
            VSGlobals.GeneratedDate       = DateTime.Now;
            VSGlobals.ProjectGUID         = Guid.NewGuid();
            VSGlobals.ProjectTypeGuid     = ProjectTypeGUID;
            VSGlobals.VisualStudioVersion = VSVersion;
            VSGlobals.PlatformToolset     = PlatformToolset;
            VSGlobals.VSCommand           = Command;
            VSGlobals.VSCommandParam      = CommandParam;
            VSGlobals.ProjectSuffix       = ProjectSuffix;
            VSGlobals.SolutionConfig      = SolutionConfig;

            // Make empty folders if any are specified:
            if (Folders != null)
            {
                foreach (var folder in Folders)
                {
                    var pathname = Path.Combine(VSGlobals.ProjectName, VSGlobals.ExpandMacros(folder));
                    if (!Directory.Exists(pathname))
                    {
                        Directory.CreateDirectory(pathname);
                    }
                }
            }

            foreach (var filespec in FileSpecs)
            {
                var pathname = Path.Combine(VSGlobals.ProjectName, VSGlobals.ExpandMacros(filespec.Pathname));

                // get relative dir from filename (i.e. relative to current working directory):
                var fullRelativeDir = Path.GetDirectoryName(pathname);
                if (!string.IsNullOrWhiteSpace(fullRelativeDir))
                {
                    Directory.CreateDirectory(fullRelativeDir);
                }
            }

            foreach (var filespec in FileSpecs)
            {
                var relativePathname = VSGlobals.ExpandMacros(filespec.Pathname);
                var fullPathname     = Path.Combine(VSGlobals.ProjectName, relativePathname);
                var output           = filespec.Contents.Select(VSGlobals.ExpandMacros);
                File.WriteAllLines(fullPathname, output);
            }
        }
コード例 #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}");
            }
        }