Browse Source

添加编译器版本详情 page

reghao 1 năm trước cách đây
mục cha
commit
18aa808a9d

+ 16 - 3
web/src/main/java/cn/reghao/devops/web/config/AppLifecycle.java

@@ -9,6 +9,8 @@ import cn.reghao.devops.web.ws.handler.LogHandler;
 import cn.reghao.devops.web.mgr.build.model.constant.CompileType;
 import cn.reghao.devops.web.mgr.log.Appenders;
 import cn.reghao.devops.web.mgr.log.LoggerConfig;
+import cn.reghao.jutil.jdk.shell.ShellExecutor;
+import cn.reghao.jutil.jdk.shell.ShellResult;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.DisposableBean;
 import org.springframework.boot.ApplicationArguments;
@@ -32,16 +34,18 @@ public class AppLifecycle implements ApplicationRunner, DisposableBean {
     private final MachineService machineService;
     private final BuildDirService buildDirService;
     private final AppProperties appProperties;
+    private final ShellExecutor shellExecutor;
 
     public AppLifecycle(LogHandler logHandler, CompilerConfigRepository compilerConfigRepository,
                         SysMessageService sysMessageService, MachineService machineService,
-                        BuildDirService buildDirService, AppProperties appProperties) {
+                        BuildDirService buildDirService, AppProperties appProperties, ShellExecutor shellExecutor) {
         this.logHandler = logHandler;
         this.compilerConfigRepository = compilerConfigRepository;
         this.sysMessageService = sysMessageService;
         this.machineService = machineService;
         this.buildDirService = buildDirService;
         this.appProperties = appProperties;
+        this.shellExecutor = shellExecutor;
     }
 
     @Override
@@ -106,14 +110,23 @@ public class AppLifecycle implements ApplicationRunner, DisposableBean {
                 continue;
             }
 
-            String name = compilerConfig.getName();
+            if (compilerConfig.getType().equals(CompileType.shell.getName())) {
+                String versionCmd = compilerConfig.getVersionCmd();
+                ShellResult shellResult = shellExecutor.exec(versionCmd.split("\\s+"));
+                if (shellResult.getExitCode() != 0) {
+                    String title = "编译配置异常";
+                    String errMsg = shellResult.getResult();
+                    sysMessageService.putSysMessage(title, errMsg);
+                }
+            }
+            /*String name = compilerConfig.getName();
             String homePath = compilerConfig.getHomePath();
             File file = new File(homePath);
             if (!file.exists()) {
                 String title = "系统异常";
                 String errMsg = String.format("编译配置 %s 的编译器 %s 不存在", name, homePath);
                 sysMessageService.putSysMessage(title, errMsg);
-            }
+            }*/
         }
     }
 }

+ 6 - 0
web/src/main/java/cn/reghao/devops/web/config/spring/BeansConfig.java

@@ -3,6 +3,7 @@ package cn.reghao.devops.web.config.spring;
 import cn.reghao.jutil.jdk.converter.ByteConverter;
 import cn.reghao.jutil.jdk.http.WebClient;
 import cn.reghao.jutil.jdk.http.WebRequest;
+import cn.reghao.jutil.jdk.shell.ShellExecutor;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
 import oshi.SystemInfo;
@@ -28,4 +29,9 @@ public class BeansConfig {
     public ByteConverter byteConverter() {
         return new ByteConverter();
     }
+
+    @Bean
+    public ShellExecutor shellExecutor() {
+        return new ShellExecutor();
+    }
 }

+ 13 - 1
web/src/main/java/cn/reghao/devops/web/mgr/builds/controller/page/CompilerPageController.java

@@ -4,6 +4,7 @@ import cn.reghao.devops.web.mgr.builds.db.repository.CompilerConfigRepository;
 import cn.reghao.devops.web.mgr.builds.model.po.CompilerConfig;
 import cn.reghao.devops.common.util.KeyValue;
 import cn.reghao.devops.web.mgr.build.model.constant.CompileType;
+import cn.reghao.devops.web.mgr.builds.service.CompilerConfigService;
 import cn.reghao.devops.web.util.PageSort;
 import cn.reghao.devops.common.machine.Machine;
 import io.swagger.annotations.Api;
@@ -31,9 +32,11 @@ import java.util.List;
 @RequestMapping("/build/compiler")
 public class CompilerPageController {
     private final CompilerConfigRepository compilerConfigRepository;
+    private final CompilerConfigService compilerConfigService;
 
-    public CompilerPageController(CompilerConfigRepository compilerConfigRepository) {
+    public CompilerPageController(CompilerConfigRepository compilerConfigRepository, CompilerConfigService compilerConfigService) {
         this.compilerConfigRepository = compilerConfigRepository;
+        this.compilerConfigService = compilerConfigService;
     }
 
     @ApiOperation(value = "编译配置页面")
@@ -81,4 +84,13 @@ public class CompilerPageController {
         model.addAttribute("compiler", compilerConfig);
         return "/devops/build/compiler/detail";
     }
+
+    @ApiOperation(value = "编译器版本页面")
+    @PreAuthorize("hasRole('ROLE_ADMIN')")
+    @GetMapping("/version/{id}")
+    public String compilerVersionPage(@PathVariable("id") int id, Model model) {
+        String result = compilerConfigService.getCompilerVersion(id);
+        model.addAttribute("content", result);
+        return "/devops/build/compiler/version";
+    }
 }

+ 1 - 0
web/src/main/java/cn/reghao/devops/web/mgr/builds/service/CompilerConfigService.java

@@ -10,4 +10,5 @@ import cn.reghao.jutil.jdk.result.Result;
 public interface CompilerConfigService {
     Result addOrUpdate(CompilerConfig compilerConfig);
     Result delete(int id);
+    String getCompilerVersion(int id);
 }

+ 73 - 1
web/src/main/java/cn/reghao/devops/web/mgr/builds/service/impl/CompilerConfigServiceImpl.java

@@ -1,6 +1,7 @@
 package cn.reghao.devops.web.mgr.builds.service.impl;
 
 import cn.reghao.devops.web.mgr.app.db.query.AppBuildQuery;
+import cn.reghao.devops.web.mgr.build.model.constant.CompileType;
 import cn.reghao.devops.web.mgr.builds.db.repository.CompilerConfigRepository;
 import cn.reghao.devops.web.mgr.app.model.po.config.AppConfig;
 import cn.reghao.devops.web.mgr.builds.model.po.CompilerConfig;
@@ -8,9 +9,13 @@ import cn.reghao.devops.web.mgr.build.chain.BuildTools;
 import cn.reghao.devops.web.mgr.builds.service.CompilerConfigService;
 import cn.reghao.jutil.jdk.result.Result;
 import cn.reghao.jutil.jdk.result.ResultStatus;
+import cn.reghao.jutil.jdk.shell.ShellExecutor;
+import cn.reghao.jutil.jdk.shell.ShellResult;
 import org.springframework.stereotype.Service;
 
 import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 import java.util.stream.Collectors;
 
 /**
@@ -21,10 +26,13 @@ import java.util.stream.Collectors;
 public class CompilerConfigServiceImpl implements CompilerConfigService {
     private final CompilerConfigRepository compilerConfigRepository;
     private final AppBuildQuery appBuildQuery;
+    private final ShellExecutor shellExecutor;
 
-    public CompilerConfigServiceImpl(CompilerConfigRepository compilerConfigRepository, AppBuildQuery appBuildQuery) {
+    public CompilerConfigServiceImpl(CompilerConfigRepository compilerConfigRepository, AppBuildQuery appBuildQuery,
+                                     ShellExecutor shellExecutor) {
         this.compilerConfigRepository = compilerConfigRepository;
         this.appBuildQuery = appBuildQuery;
+        this.shellExecutor = shellExecutor;
     }
 
     @Override
@@ -75,4 +83,68 @@ public class CompilerConfigServiceImpl implements CompilerConfigService {
         BuildTools.removeCodeCompiler(compilerConfig.getName());
         return Result.result(ResultStatus.SUCCESS);
     }
+
+    @Override
+    public String getCompilerVersion(int id) {
+        CompilerConfig compilerConfig = compilerConfigRepository.findById(id).orElse(null);
+        if (compilerConfig == null) {
+            return "not exist";
+        }
+
+        if (!compilerConfig.getType().equals(CompileType.shell.getName())) {
+            return "not shell type";
+        }
+
+        String versionCmd = compilerConfig.getVersionCmd();
+        ShellResult shellResult = shellExecutor.exec(versionCmd.split("\\s+"));
+        String html = txtToHtml(shellResult.getResult());
+        return html;
+    }
+
+    private String txtToHtml(String s) {
+        StringBuilder builder = new StringBuilder();
+        boolean previousWasASpace = false;
+        for (char c : s.toCharArray()) {
+            if (c == ' ') {
+                if (previousWasASpace) {
+                    builder.append(" ");
+                    previousWasASpace = false;
+                    continue;
+                }
+                previousWasASpace = true;
+            } else {
+                previousWasASpace = false;
+            }
+            switch (c) {
+                case '<':
+                    builder.append("&lt;");
+                    break;
+                case '>':
+                    builder.append("&gt;");
+                    break;
+                case '&':
+                    builder.append("&amp;");
+                    break;
+                case '"':
+                    builder.append("&quot;");
+                    break;
+                case '\n':
+                    builder.append("<br>");
+                    break;
+                // We need Tab support here, because we print StackTraces as HTML
+                case '\t':
+                    builder.append("&nbsp; &nbsp; &nbsp;");
+                    break;
+                default:
+                    builder.append(c);
+
+            }
+        }
+        String converted = builder.toString();
+        String str = "(?i)\\b((?:https?://|www\\d{0,3}[.]|[a-z0-9.\\-]+[.][a-z]{2,4}/)(?:[^\\s()<>]+|\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\))+(?:\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\)|[^\\s`!()\\[\\]{};:\'\".,<>?«»“”‘’]))";
+        Pattern patt = Pattern.compile(str);
+        Matcher matcher = patt.matcher(converted);
+        converted = matcher.replaceAll("<a href=\"$1\">$1</a>");
+        return converted;
+    }
 }

+ 6 - 1
web/src/main/resources/templates/devops/build/compiler/index.html

@@ -5,7 +5,7 @@
 <body class="timo-layout-page">
 <div class="layui-card">
     <div class="layui-card-header timo-card-header">
-        <span><i class="fa fa-bars"></i> 编译配置</span>
+        <span><i class="fa fa-bars"></i> 编译配置列表</span>
         <i class="layui-icon layui-icon-refresh refresh-btn"></i>
     </div>
     <div class="layui-card-body">
@@ -27,6 +27,7 @@
                     <th class="sortable" data-field="name">编译名字</th>
                     <th class="sortable" data-field="homePath">编译器主目录</th>
                     <th class="sortable" data-field="cmd">命令</th>
+                    <th class="sortable" data-field="version">版本</th>
                     <th>操作</th>
                 </tr>
                 </thead>
@@ -39,6 +40,10 @@
                         <a class="open-popup" data-title="命令" th:attr="data-url=@{'/build/compiler/detail/'+${item.id}}"
                            data-size="720,480" href="#">查看</a>
                     </td>
+                    <td>
+                        <a class="open-popup" th:attr="data-title='编译器 ' + ${item.name} + ' 版本', data-url=@{'/build/compiler/version/'+${item.id}}"
+                           data-size="720,480" href="#">查看</a>
+                    </td>
                     <td>
                         <a class="open-popup" data-title="修改命令" th:attr="data-url=@{'/build/compiler/edit/'+${item.id}}"
                            data-size="720,480" href="#">编辑</a>

+ 29 - 0
web/src/main/resources/templates/devops/build/compiler/version.html

@@ -0,0 +1,29 @@
+<!DOCTYPE html>
+<html xmlns:th="http://www.thymeleaf.org">
+<head th:replace="/common/template :: header(~{::title},~{::link},~{::style})">
+    <style>
+        .page-error {
+            display: -webkit-box;
+            display: -ms-flexbox;
+            display: flex;
+            -webkit-box-align: center;
+            -ms-flex-align: center;
+            align-items: center;
+            -webkit-box-pack: center;
+            -ms-flex-pack: center;
+            justify-content: center;
+            -webkit-box-orient: vertical;
+            -webkit-box-direction: normal;
+            -ms-flex-direction: column;
+            flex-direction: column;
+            min-height: calc(100vh - 110px);
+            margin-bottom: 0;
+        }
+    </style>
+</head>
+<body>
+<div class="page-error" style="color: #009688">
+    <div style="font-size: 18px" th:utext="${content}"></div>
+</div>
+</body>
+</html>