示例#1
0
    public void Compile([Option("config", new[] { 'c' })][FilePathExists] string configPath)
    {
        var configs = YamlHelper.ReadFile <AppConfig[]>(configPath);

        foreach (var config in configs)
        {
            var includeFiles = GlobFiles(config.Includes);

            foreach (var target in config.CompileTargets ?? throw new NullReferenceException(nameof(config.CompileTargets)))
            {
                try
                {
                    var input  = target.Input ?? throw new NullReferenceException(nameof(target.Input));
                    var output = target.Output ?? throw new NullReferenceException(nameof(target.Output));

                    // 読み込み
                    var(rootDefinition, includedDefinitions) = DefinitionLoader.Load(input, includeFiles);

                    // 出力先フォルダが存在しない場合は作成する
                    CreateParentDirectory(output);

                    // 書き込み
                    using var writer = new StreamWriter(output, false, new UTF8Encoding(false));
                    var codeGenerator = new CodeGenerator(rootDefinition, includedDefinitions);
                    writer.Write(codeGenerator.Generate());
                }
                catch (Exception e)
                {
                    _logger.Error(e);

                    var sb = new StringBuilder();
                    sb.AppendLine($"input: {target.Input}");
                    sb.AppendLine($"output: {target.Output}");

                    _logger.Error(sb.ToString());

                    throw;
                }
            }
        }
    }