index.html 13.8 KB
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>happy~happy~happy~</title>
	<script src="./static/js/jquery-3.6.1.min.js"></script>
	<script src="./static/js/vue-2.6.10.min.js"></script>
	<script src="./static/js/bootstrap.min.js"></script>
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<link rel="stylesheet" href="./static/css/bootstrap.min.css">
	<link rel="stylesheet" href="./static/css/bootstrap-theme.min.css">
</head>
<style>

</style>
<body>
<h1 style="text-align: center"></h1>
<div id="app" class="main container">
	<!-- 导航栏 -->
	<nav class="navbar navbar-default">
		<div class="container-fluid">
			<!-- Brand and toggle get grouped for better mobile display -->
			<div class="navbar-header">
				<button type="button" class="btn btn-danger navbar-btn" @click="cleanInput">清除输入</button>
				<a class="navbar-brand" href="">happy~happy~happy~</a>
				<div class="btn-group navbar-btn" role="group" aria-label="...">
					<button type="button" class="btn btn-default" @click="selectAll(1)">全选</button>
					<button type="button" class="btn btn-default" @click="selectAll(2)">全不选</button>
				</div>
				<button type="button" class="btn btn-default navbar-btn" @click="copyNew">复制出来</button>
			</div>
		</div>
	</nav>

	<!-- 文本输入框 -->
	<div>
		<div class="input-group">
			<span class="input-group-addon" id="basic-addon1">把配置贴到这</span>
			<textarea v-model="source" class="form-control" placeholder="请手动输入..." rows="2"></textarea>
		</div>
	</div>
	<!--搜索-->
	<div class="row navbar-btn" v-if="allServiceList.length">
		<div class="col-lg-6">
			<div class="input-group">
				<input type="text" class="form-control" v-model="searchText" placeholder="Search for...">
				<span class="input-group-btn">
        <button class="btn btn-danger" type="button" @click="cleanSearchText">clean</button>
      </span>
			</div>
		</div>
	</div>
	<!--表格-->
	<table class="table table-striped" v-if="allServiceList.length">
		<thead class="thead-dark">
		<tr>
			<th>No.</th>
			<th>服务名</th>
			<th>是否需要部署</th>
		</tr>
		</thead>
		<tbody>
		<tr v-for="(item,i) in serviceList">
			<td>{{i+1}}</td>
			<td @click="selectService(i)">
				<h3 v-if="!item.isWebService">{{item.serviceName}}</h3>
				<h4 v-else>{{item.serviceName}}</h4>
			</td>
			<td><input class="form-check-input" type="checkbox" v-model="item.isOpen"/></td>
		</tr>
		</tbody>
	</table>
</div>
</body>

<script type="text/javascript">
    new Vue({
        el: '#app',
        //监听source变动
        watch: {
            source: function (val) {
                this.source = val;
                this.analyseSource();
            },
            searchText: function (val) {
                //在allServiceList中找到包含val的
                let serviceList = [];
                this.allServiceList.forEach((item) => {
                    console.log(item)
                    if (item.serviceName.indexOf(val) != -1) {
                        serviceList.push(item);
                    }
                });
                this.serviceList = serviceList;
            }
        },
        data: {
            source: '',         //复制过来的
            sourceArr: [],      //按行分割的
            serviceList: [], 	//服务列表
            allServiceList: [], 	//服务列表
            sleepList: [],		//sleep列表
            searchText: "",		//搜索框的值
        },
        created: function () {

        },
        mounted() {
            //等一会儿再执行
            setTimeout(() => {
                let sysPayload = window.sysPayload;
                if (sysPayload) {
                    let source = ""
                    if (sysPayload.indexOf("node") !== -1) {
                        source = sysPayload;
                    }
                    this.source = source;
                    this.analyseSource();
                }
            }, 500);
        },
        methods: {
            //清除搜索框
            cleanSearchText: function () {
				this.searchText = "";
			},
            //清除输入
            cleanInput: function () {
                this.source = "";
            },
            //选择服务
            selectService: function (index) {
                this.serviceList[index].isOpen = !this.serviceList[index].isOpen;
            },
            //全选
            selectAll: function (type) {
                let serviceList = this.serviceList;
                let isOpen = true;
                if (type == 2) {
                    isOpen = false;
                }
                serviceList.forEach((item) => {
                    item.isOpen = isOpen;
                });
                this.serviceList = serviceList;
            },
            //分析source
            analyseSource: function () {
                let serviceMap = {};
                let serviceList = [];
                let sleepList = [];
                let source = this.source;
                //将source按行分割
                let sourceArr = source.split("\n");

                //upload的开始行
                let uploadStartRow = 0;
                //upload的结束行
                let uploadEndRow = 0;
                //imageBuild的开始行
                let imageBuildStart = 0;
                //imageBuild的结束行
                let imageBuildEnd = 0;
                //找出关键行的行号
                for (let i = 0; i < sourceArr.length; i++) {
                    let row = sourceArr[i].trim();
                    let nextRow = "";
                    if (i != sourceArr.length - 1) {
                        nextRow = sourceArr[i + 1].trim();
                    }
                    //upload的开始行
                    if (row == "stage('upload') {") {
                        uploadStartRow = i + 1;
                    }
                    //upload的结束行和imageBuild的开始行
                    if (row == "}" && nextRow == "stage('image build') {") {
                        uploadEndRow = i;
                        imageBuildStart = i + 2;
                    }
                    //imageBuild的结束行
                    if (row == "}" && uploadEndRow != 0 && imageBuildEnd == 0 && i != uploadEndRow) {
                        imageBuildEnd = i;
                    }
                    if (row.indexOf("sleep") != -1) {
                        let sleepRow = "sleep";
                        let strings = row.split("sleep");
                        strings.forEach((str,index)=>{
                            if (index>0){
                                sleepRow+=str;
							}
						});
                        row = sleepRow;
                        row = this.dealStr(row);
                        let rowObj = {
                            rowNo: i,
                            row: row,
                        }
                        sleepList.push(rowObj);
                    }
                }

                let uploadRow = 0;
                sourceArr.slice(uploadStartRow, uploadEndRow).forEach((item, index) => {
                    uploadRow = uploadStartRow + index;
                    let row = item.trim();
                    if (!row) {
                        return;
                    }
                    //过滤可能是注释的行
                    if (escape(row).indexOf("%u") !== -1 || item.indexOf("sh") === -1) {
                        return;
                    }
                    let isOpen = !!(row && !row.startsWith("//"));
                    if (!isOpen) {
                        row = this.dealStr(row);
                    }
                    let strings = item.split("*.jar")[0].split("/");
                    let serviceName = strings[strings.length - 1].substring(0, strings[strings.length - 1].length - 1);
                    let service = {
                        isOpen: isOpen,
                        uploadRowNo: uploadRow,
                        uploadRow: row,
                        serviceName: serviceName,
                        isWebService: serviceName.indexOf("web") > -1,
                    }
                    serviceMap[service.serviceName] = service;
                })

                let buildRowNo = 0;
                sourceArr.slice(imageBuildStart, imageBuildEnd).forEach((item, index) => {
                    buildRowNo = imageBuildStart + index;
                    let trimItem = item.trim();
                    if (item.indexOf("sleep") === -1 && trimItem) {
                        //过滤可能是注释的行
						if (escape(trimItem).indexOf("%u") !== -1 || item.indexOf(".sh") === -1) {
							return;
						}
                        let serviceName = item.split(".sh ")[1].split(" ")[0];
                        let service = serviceMap[serviceName];
                        if (!service) {
                            return;
                        }
                        service.buildRowNo = buildRowNo;
                        service.buildRow = trimItem;
                        if (service.isOpen) {
                            if (trimItem.startsWith("//")) {
                                service.buildRow = trimItem.substring(2);
                            }
                        } else {
                            if (!trimItem.startsWith("//")) {
                                service.buildRow = "//" + trimItem;
                            }
                        }
                        if (!service.isOpen) {
                            service.buildRow = this.dealStr(service.buildRow);
                        }
                    }
                });

                //serviceMap转为数组
                for (let key in serviceMap) {
                    serviceList.push(serviceMap[key]);
                }
                this.sourceArr = sourceArr;
                this.allServiceList = serviceList;
                this.serviceList = serviceList;
                this.sleepList = sleepList;
            },

            //复制出来
            copyNew: function () {
                let {serviceList, allServiceList, sourceArr, sleepList} = this;
                if (!allServiceList.length) {
                    alert("你输入的内容不对");
                } else {
                    allServiceList.forEach((item) => {
                        let uploadRow = item.uploadRow;
                        let buildRow = item.buildRow;
                        sourceArr[item.uploadRowNo] = uploadRow;
                        sourceArr[item.buildRowNo] = buildRow;
                        if (item.isOpen) {
                            if (uploadRow.startsWith("//")) {
                                uploadRow = uploadRow.split("//")[1];
                            }
                            if (buildRow.startsWith("//")) {
                                buildRow = buildRow.split("//")[1];
                            }
                            let sleepRow = item.buildRowNo + 1;
                            sleepList.forEach(item => {
                                if (item.rowNo == sleepRow) {
                                    if (item.row.startsWith("//")) {
                                        item.row = item.row.split("//")[1];
                                    }
                                }
                            });
                        } else {
                            //如果是关闭的 还没有注释的话,就注释掉
                            if (!uploadRow.startsWith("//")) {
                                uploadRow = "//" + uploadRow;
                            }
                            if (!buildRow.startsWith("//")) {
                                buildRow = "//" + buildRow;
                            }
                            let sleepRow = item.buildRowNo + 1;
                            sleepList.forEach(item => {
                                if (item.rowNo == sleepRow) {
                                    if (!item.row.startsWith("//")) {
                                        item.row = "//" + item.row;
                                    }
                                }
                            });
                        }
                        //格式
                        sourceArr[item.uploadRowNo] = "\t\t" + uploadRow;
                        sourceArr[item.buildRowNo] = "\t\t" + buildRow;
                    });
                    sleepList.forEach(item => {
                        item.row = "\t\t" + item.row;
                        sourceArr[item.rowNo] = item.row;
                    })
                    //去掉sourceArr的空行
                    sourceArr = sourceArr.filter(item => {
                        return item.trim();
                    })
                    //组合成原来的字符串
                    let newSource = "";
                    sourceArr.forEach(item => {
                        newSource += item + "\n";
                    })
                    //复制到剪切板
                    let txa = document.createElement('textarea')
                    txa.value = newSource
                    document.body.appendChild(txa)
                    txa.select()
                    document.execCommand('copy')
                    document.body.removeChild(txa)
                    alert("复制成功");
                }
            },

            //dealStr
            dealStr: function (str) {
                //如果是注释的
                if (str.startsWith("//")) {
                    if (str[2] === " ") {
                        //去除第三位的空格
                        str = str.substring(0, 2) + str.substring(3).trim();
                        return this.dealStr(str);
                    } else {
                        return str;
                    }
                } else {
                    return str;
                }
            }
        }
    })
</script>
</html>