Example #1
0
        private static void Main(string[] args)
        {
            string tmpl = null;

            if (args.Length > 0)
            {
                tmpl = args[0];
                if (!Path.HasExtension(tmpl))
                {
                    tmpl += ".xml";
                }
                // the template path argument is relative to the MGPG executable
                var exeDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                tmpl = Path.Combine(exeDir, tmpl);
            }

            if (args.Length == 0 || (args.Length == 1 && (args[0] == "--help" || args[0] == "-h")))
            {
                PrintHelp();
                Environment.Exit(1);
            }
            if (args.Length == 1)
            {
                var logger = new Logger {
                    LogLevel = LogLevel.Warning
                };
                var t = new Template(tmpl, logger);
                Console.WriteLine();
                WriteTemplateData(t);
                Environment.Exit(0);
            }

            var dst = args[1];
            var sln = args.Length >= 3 && args[2].EndsWith(".sln") ? args[2] : null;

            var varIndex = sln == null ? 2 : 3;
            var vars     = ParseVariables(args, varIndex);

            var g = new Generator();

            g.LoadTemplate(tmpl);

            var generatorArgs = new GeneratorArguments
            {
                DestinationFolder = dst,
                Solution          = sln,
                Variables         = vars
            };

            g.Generate(generatorArgs);
        }
Example #2
0
        /// <summary>
        /// Generate the template defined in the template file set in <see cref="Settings"/>.
        /// </summary>
        public void Generate(GeneratorArguments arguments)
        {
            _logger = new Logger
            {
                LogLevel      = Settings.LogLevel,
                LogWriter     = Settings.LogWriter ?? Console.Out,
                SupressErrors = Settings.SupressErrors
            };

            if (_template == null)
            {
                _logger.Log(LogLevel.Error, "A template needs to be loaded before calling generate.");
                return;
            }

            if (File.Exists(arguments.DestinationFolder) ||
                (Directory.Exists(arguments.DestinationFolder) && Directory.EnumerateFileSystemEntries(arguments.DestinationFolder).Any() && !Settings.Overwrite))
            {
                _logger.Log(LogLevel.Error, $"The destination directory '{arguments.DestinationFolder}' already exists and is not empty. Set the Overwrite flag to write anyway.");
                return;
            }

            var dst = Path.GetFullPath(arguments.DestinationFolder);

            if (_template.HasFatalError)
            {
                return;
            }

            var variables = _template.Variables.With(arguments.Variables);

            variables.Set(Template.SourceFileExtensionVariable, arguments.SourceLanguage.GetFileExtension());

            Directory.CreateDirectory(dst);
            var projectPaths = new List <string>();

            foreach (var pe in _template.ProjectEntries)
            {
                var adst = RenderFileEntry(pe, variables, dst);
                _logger.Log(LogLevel.Info, $"Rendered project '{pe.AbsoluteSrc}' to '{adst}'.");
                projectPaths.Add(adst);
                foreach (var fe in pe.FileEntries)
                {
                    adst = RenderFileEntry(fe, variables, dst);
                    _logger.Log(LogLevel.Info, $"Rendered file '{fe.AbsoluteSrc}' to '{adst}'.");
                }
            }

            if (arguments.Solution != null)
            {
                var     slnPath = Path.GetFullPath(arguments.Solution);
                SlnFile sln;
                if (File.Exists(arguments.Solution))
                {
                    sln = SlnFileFactory.CreateFromFileOrDirectory(arguments.Solution);
                }
                else
                {
                    sln = new SlnFile {
                        FullPath = slnPath
                    };
                    sln.FormatVersion = "12.00";
                    sln.MinimumVisualStudioVersion = "10.0.40219.1";
                    _logger.Log(LogLevel.Info, $"Created solution '{Path.GetFileName(slnPath)}'.");
                }
                foreach (var projectPath in projectPaths)
                {
                    sln.AddProject(projectPath);
                    _logger.Log(LogLevel.Info, $"Added project '{Path.GetFileName(projectPath)}' to solution '{Path.GetFileName(slnPath)}'.");
                }
                sln.Write();
            }
        }