package cn.reghao.devops.web.mgr.builds.model.po;

import cn.reghao.devops.web.mgr.build.model.CompilerDto;
import cn.reghao.devops.web.mgr.build.model.constant.CompileType;
import cn.reghao.devops.web.util.provider.CompilerConfigGroupSequenceProvider;
import cn.reghao.devops.web.admin.util.validator.ValidEnum;
import cn.reghao.devops.web.admin.util.BaseEntity;
import lombok.*;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.group.GroupSequenceProvider;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Pattern;

/**
 * 编译方式配置
 *
 * @author reghao
 * @date 2021-02-05 17:37:28
 */
@GroupSequenceProvider(CompilerConfigGroupSequenceProvider.class)
@NoArgsConstructor
@Getter
@Setter
@Entity
@Table(name = "devops_compiler_config")
public class CompilerConfig extends BaseEntity {
    @ValidEnum(value = CompileType.class, message = "请选择正确的编译类型")
    private String type;
    @Pattern(regexp = "^\\S*$", message = "编译名字不能包含空白符")
    @Length(max = 255, message = "编译名字的最大长度不能超过 255 个字符")
    @Column(nullable = false, unique = true)
    private String name;
    @NotBlank(groups = { ShellCompiler.class, MavenCompiler.class }, message = "编译器主目录不能为空")
    @Length(max = 255, message = "编译器主目录的最大长度不能超过 255 个字符")
    private String homePath;
    @NotBlank(groups = { ShellCompiler.class}, message = "编译脚本不能为空")
    @Length(max = 10_000, message = "编译脚本的最大长度不能超过 10_000 个字符")
    @Column(columnDefinition="text")
    private String compileScript;

    public interface NoneCompiler {
    }

    public interface ShellCompiler {
    }

    public interface MavenCompiler {
    }

    public CompilerDto getCompilerDto() {
        return new CompilerDto(this.type, this.name, this.homePath, this.compileScript);
    }
}
