Esempio n. 1
0
 private string LoadTemplate(IChallengeTemplateOptions options)
 {
     try
     {
         return(File.ReadAllText(FindTemplateFilePath(options.TemplateName)));
     }
     catch (FileNotFoundException)
     {
         return(LoadEmbeddedTemplate(options));
     }
 }
Esempio n. 2
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());
        }
Esempio n. 3
0
        public void AddChallenges(IChallengeTemplateOptions options)
        {
            var root = _project.FindDirectory(options.ChallengeDir);

            if (options.ChallengeCount != null)
            {
                for (int i = options.CounterStart; i <= options.CounterStart + options.ChallengeCount; i++)
                {
                    AddChallenge(options, root, i);
                }
            }
            else
            {
                AddChallenge(options, root);
            }
        }
Esempio n. 4
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);
            }
        }