示例#1
0
        private string LoadEmbeddedTemplate(IChallengeTemplateOptions options)
        {
            var assembly = options.GetType().Assembly;

            _logger.LogTrace("Loading embedded template {name} from {assembly}", options.TemplateName, assembly.GetName().Name);

            using var stream = new EmbeddedFileProvider(assembly, typeof(ChallengeTemplateService).Namespace)
                               .GetFileInfo(GetTemplateFileName(options.TemplateName)).CreateReadStream();

            using var reader = new StreamReader(stream);
            return(reader.ReadToEnd());
        }
示例#2
0
        public void AddChallenge(IChallengeTemplateOptions options, string root, int?number = null)
        {
            var className     = options.ChallengeClass.ToPascalCase() ?? options.ChallengeName.ToPascalCase();
            var challengeName = (string.IsNullOrEmpty(options.ChallengeName) ? className : options.ChallengeName).Replace("\"", "\\\"");

            if (string.IsNullOrEmpty(className))
            {
                throw new Exception("Challenge class is invalid or empty.");
            }

            if (number != null)
            {
                className += ((int)number).ToString("D" + options.CounterPadding);
            }

            var path = Path.Combine(root, Path.ChangeExtension(className, "cs"));

            if (!File.Exists(path))
            {
                var contents = LoadTemplate(options);

                ReplaceNamespace(ref contents);
                ReplacePlaceholder(ref contents, "class", className);
                ReplacePlaceholder(ref contents, "name", challengeName);

                foreach (var property in options.GetType().GetProperties().Where(prop => Attribute.IsDefined(prop, typeof(OptionAttribute))))
                {
                    ReplacePlaceholder(ref contents, property.GetCustomAttributes(typeof(OptionAttribute), false).Cast <OptionAttribute>().First().LongName, property.GetValue(options));
                }

                _launchSettings.AddChallenge(challengeName, className);

                if (options.HasResourceDir)
                {
                    _logger.LogTrace("Create resource directory for {name}", className);
                    Directory.CreateDirectory(Path.Combine(root, className));
                }

                File.WriteAllText(path, contents);

                _logger.LogTrace("Challenge {name} created in {path}", challengeName, path);
            }
            else
            {
                _logger.LogError("The challenge {name} file already exist in {path}", challengeName, path);
            }
        }