Skip to content

Commit 740954c

Browse files
authored
Merge pull request #23 from proDOOMman/develop
Develop
2 parents b97e5a5 + 7505b8b commit 740954c

File tree

10 files changed

+155
-43
lines changed

10 files changed

+155
-43
lines changed

build.gradle

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ plugins {
66
}
77

88
group 'com.clouds42'
9-
version '2.0-SNAPSHOT'
9+
version '2.1-SNAPSHOT'
1010

1111
sourceCompatibility = 11
1212

@@ -54,6 +54,8 @@ dependencies {
5454

5555
compile group: 'com.google.guava', name: 'guava', version: '28.2-jre'
5656

57+
compile group: 'de.vandermeer', name:'asciitable', version: '0.3.2'
58+
5759
implementation 'com.github.1c-syntax:mdclasses:42fcc53f34b08a7a748528c9dd53ae1a2e04bb40' // TODO: change to release
5860

5961
implementation('com.github.1c-syntax:bsl-parser:0.13.0') {

src/main/java/com/clouds42/CommandLineOptions/DebuggerOptions.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package com.clouds42.CommandLineOptions;
22

3+
import com._1c.g5.v8.dt.debug.model.base.data.DebugTargetType;
34
import org.slf4j.Logger;
45
import org.slf4j.LoggerFactory;
56
import picocli.CommandLine.Option;
67

78
import java.lang.invoke.MethodHandles;
89
import java.util.ArrayList;
10+
import java.util.LinkedList;
911
import java.util.List;
1012

1113
public class DebuggerOptions {
@@ -18,9 +20,12 @@ public class DebuggerOptions {
1820
@Option(names = {"-p:env", "--password:env"}, description = "Password environment variable name", defaultValue = "")
1921
private String passwordEnv;
2022

21-
@Option(names = {"-n", "--areanames"}, description = "Debug area names (not for general use!)")
23+
@Option(names = {"-n", "--areanames"}, description = "Debug area names (not for general use!)", arity = "0..*")
2224
private List<String> debugAreaNames;
2325

26+
@Option(names = {"-a", "--autoconnectTargets"}, description = "Autoconnect debug targets (not for general use!): ${COMPLETION-CANDIDATES}", arity = "0..*")
27+
private List<DebugTargetType> autoconnectTargets;
28+
2429
@Option(names = {"-t", "--timeout"}, description = "Ping timeout. Default - ${DEFAULT-VALUE}", defaultValue = "1000")
2530
private Integer pingTimeout;
2631

@@ -59,4 +64,16 @@ public Integer getPingTimeout() {
5964
public void setPingTimeout(Integer pingTimeout) {
6065
this.pingTimeout = pingTimeout;
6166
}
67+
68+
public void setAutoconnectTargets(List<DebugTargetType> autoconnectTargets) { this.autoconnectTargets = autoconnectTargets; }
69+
70+
public List<DebugTargetType> getAutoconnectTargets() {
71+
if (autoconnectTargets == null || autoconnectTargets.isEmpty()) {
72+
autoconnectTargets = new LinkedList<DebugTargetType>();
73+
autoconnectTargets.addAll(DebugTargetType.VALUES);
74+
autoconnectTargets.remove(DebugTargetType.UNKNOWN);
75+
}
76+
return autoconnectTargets;
77+
}
78+
6279
}

src/main/java/com/clouds42/Commands/ConvertCommand.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,6 @@ public class ConvertCommand implements Callable<Integer> {
4242
@Mixin
4343
private MetadataOptions metadataOptions;
4444

45-
@Mixin
46-
private FilterOptions filterOptions;
47-
4845
@Mixin
4946
private LoggingOptions loggingOptions;
5047

@@ -53,7 +50,7 @@ public Integer call() throws Exception {
5350

5451
Map<URI, Map<BigDecimal, Boolean>> coverageData = new HashMap<URI,Map<BigDecimal, Boolean>>();
5552

56-
Map<String, URI> uriListByKey = Utils.readMetadata(metadataOptions, filterOptions, coverageData);
53+
Map<String, URI> uriListByKey = Utils.readMetadata(metadataOptions, coverageData);
5754

5855
FileInputStream fileIS = new FileInputStream(convertOptions.getInputRawXmlFile());
5956
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();

src/main/java/com/clouds42/Commands/CoverageCommand.java

Lines changed: 45 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import com._1c.g5.v8.dt.debug.core.runtime.client.RuntimeDebugClientException;
44
import com._1c.g5.v8.dt.debug.model.base.data.BSLModuleIdInternal;
55
import com._1c.g5.v8.dt.debug.model.base.data.DebugTargetId;
6-
import com._1c.g5.v8.dt.debug.model.base.data.DebugTargetType;
76
import com._1c.g5.v8.dt.debug.model.dbgui.commands.DBGUIExtCmdInfoBase;
87
import com._1c.g5.v8.dt.debug.model.dbgui.commands.DBGUIExtCmds;
98
import com._1c.g5.v8.dt.debug.model.dbgui.commands.impl.DBGUIExtCmdInfoMeasureImpl;
@@ -24,6 +23,7 @@
2423
import picocli.CommandLine;
2524
import picocli.CommandLine.Command;
2625
import picocli.CommandLine.Mixin;
26+
import picocli.CommandLine.Option;
2727

2828
import java.io.BufferedReader;
2929
import java.io.IOException;
@@ -67,6 +67,9 @@ public class CoverageCommand implements Callable<Integer> {
6767
@Mixin
6868
private LoggingOptions loggingOptions;
6969

70+
@Option(names = {"--opid"}, description = "Owner process PID", defaultValue = "-1")
71+
Integer opid;
72+
7073
private RuntimeDebugHttpClient client;
7174

7275
private Map<URI, Map<BigDecimal, Boolean>> coverageData = new HashMap<URI,Map<BigDecimal, Boolean>> () {
@@ -104,6 +107,10 @@ private Boolean listenSocket(Socket clientSocket) {
104107
Utils.dumpCoverageFile(coverageData, metadataOptions, outputOptions);
105108
out.println(PipeMessages.OK_RESULT);
106109
return true;
110+
} else if (PipeMessages.STATS_COMMAND.equals(line)) {
111+
Utils.printCoverageStats(coverageData, metadataOptions);
112+
out.println(PipeMessages.OK_RESULT);
113+
return true;
107114
} else if (PipeMessages.CLEAN_COMMAND.equals(line)) {
108115
coverageData.forEach((uri, bigDecimalBooleanMap) -> {
109116
for (var key : bigDecimalBooleanMap.keySet()) {
@@ -186,7 +193,7 @@ public Integer call() throws Exception {
186193

187194
rawMode = metadataOptions.isRawMode();
188195

189-
Map<String, URI> uriListByKey = Utils.readMetadata(metadataOptions, filterOptions, coverageData);
196+
Map<String, URI> uriListByKey = Utils.readMetadata(metadataOptions, coverageData);
190197

191198
boolean firstRun = true;
192199

@@ -287,39 +294,47 @@ public void run()
287294
}
288295
} catch (RuntimeDebugClientException e) {
289296
logger.info(e.getLocalizedMessage());
290-
try {
291-
client.connect(debuggerOptions.getPassword());
292-
client.initSettings(false);
293-
List<DebugTargetType> debugTargetTypes = new LinkedList<DebugTargetType>();
294-
debugTargetTypes.addAll(DebugTargetType.VALUES);
295-
debugTargetTypes.remove(DebugTargetType.UNKNOWN);
296-
client.setAutoconnectDebugTargets(debuggerOptions.getDebugAreaNames(), debugTargetTypes);
297-
298-
List<DebugTargetId> debugTargets;
299-
if (debuggerOptions.getDebugAreaNames().isEmpty()) {
300-
debugTargets = client.getRuntimeDebugTargets(null);
301-
} else {
302-
debugTargets = new LinkedList<DebugTargetId>();
303-
debuggerOptions.getDebugAreaNames().forEach(areaName -> {
304-
try {
305-
debugTargets.addAll(client.getRuntimeDebugTargets(areaName));
306-
} catch (RuntimeDebugClientException ex) {
307-
logger.error(ex.getLocalizedMessage());
308-
}
309-
});
310-
}
311-
connectAllTargets(debugTargets);
297+
if (systemStarted) {
298+
logger.info("Can't send ping to dbgs. Coverage analyzing finished");
299+
gracefulShutdown(null);
300+
} else {
301+
try {
302+
client.connect(debuggerOptions.getPassword());
303+
client.initSettings(false);
304+
client.setAutoconnectDebugTargets(
305+
debuggerOptions.getDebugAreaNames(),
306+
debuggerOptions.getAutoconnectTargets());
307+
308+
List<DebugTargetId> debugTargets;
309+
if (debuggerOptions.getDebugAreaNames().isEmpty()) {
310+
debugTargets = client.getRuntimeDebugTargets(null);
311+
} else {
312+
debugTargets = new LinkedList<DebugTargetId>();
313+
debuggerOptions.getDebugAreaNames().forEach(areaName -> {
314+
try {
315+
debugTargets.addAll(client.getRuntimeDebugTargets(areaName));
316+
} catch (RuntimeDebugClientException ex) {
317+
logger.error(ex.getLocalizedMessage());
318+
}
319+
});
320+
}
321+
connectAllTargets(debugTargets);
312322

313-
client.toggleProfiling(null);
314-
client.toggleProfiling(measureUuid);
323+
client.toggleProfiling(null);
324+
client.toggleProfiling(measureUuid);
315325

316-
systemStarted = true;
317-
} catch (RuntimeDebugClientException e1) {
318-
logger.error(e1.getLocalizedMessage());
319-
return CommandLine.ExitCode.SOFTWARE;
326+
systemStarted = true;
327+
} catch (RuntimeDebugClientException e1) {
328+
logger.error(e1.getLocalizedMessage());
329+
return CommandLine.ExitCode.SOFTWARE;
330+
}
320331
}
321332
}
322333
Thread.sleep(debuggerOptions.getPingTimeout());
334+
if (opid > 0 && !Utils.isProcessStillAlive(opid)) {
335+
logger.info("Owner process stopped: " + opid);
336+
gracefulShutdown(null);
337+
}
323338
}
324339

325340
logger.info("Disconnecting from dbgs...");

src/main/java/com/clouds42/Commands/SendMessageCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public Integer call() throws Exception {
4949
pipeOut.println(commandText);
5050
logger.info("Command send finished: " + commandText);
5151
String result = "";
52-
for(int i = 0; i < 60; i++) {
52+
for(int i = 0; i < 10; i++) {
5353
try {
5454
result = pipeIn.readLine();
5555
break;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.clouds42.Commands;
2+
3+
import com.clouds42.PipeMessages;
4+
import picocli.CommandLine.Command;
5+
6+
@Command(name = PipeMessages.STATS_COMMAND,
7+
description = "Print coverage statistic report",
8+
sortOptions = false)
9+
public class SendStatsMessageCommand extends SendMessageCommand {
10+
}

src/main/java/com/clouds42/Coverage41C.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
SendCheckMessageCommand.class,
1818
SendCleanMessageCommand.class,
1919
SendDumpMessageCommand.class,
20+
SendStatsMessageCommand.class,
2021
ConvertCommand.class}
2122
)
2223
public class Coverage41C implements Callable<Integer> {

src/main/java/com/clouds42/PipeMessages.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ public class PipeMessages {
1010
public static final String CHECK_COMMAND = "check";
1111
public static final String CONVERT_COMMAND = "convert";
1212
public static final String START_COMMAND = "start";
13+
public static final String STATS_COMMAND = "stats";
1314
}

src/main/java/com/clouds42/Utils.java

Lines changed: 75 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.clouds42;
22

33
import com.clouds42.CommandLineOptions.ConnectionOptions;
4-
import com.clouds42.CommandLineOptions.FilterOptions;
54
import com.clouds42.CommandLineOptions.MetadataOptions;
65
import com.clouds42.CommandLineOptions.OutputOptions;
76
import com.github._1c_syntax.bsl.parser.BSLParser;
@@ -14,6 +13,8 @@
1413
import com.github._1c_syntax.mdclasses.metadata.Configuration;
1514
import com.github._1c_syntax.mdclasses.metadata.additional.ModuleType;
1615
import com.github._1c_syntax.mdclasses.metadata.additional.SupportVariant;
16+
import de.vandermeer.asciitable.AsciiTable;
17+
import de.vandermeer.asciitable.CWC_LongestLine;
1718
import org.antlr.v4.runtime.tree.TerminalNodeImpl;
1819
import org.antlr.v4.runtime.tree.Tree;
1920
import org.antlr.v4.runtime.tree.Trees;
@@ -34,10 +35,7 @@
3435
import javax.xml.xpath.XPath;
3536
import javax.xml.xpath.XPathConstants;
3637
import javax.xml.xpath.XPathFactory;
37-
import java.io.File;
38-
import java.io.FileInputStream;
39-
import java.io.FileOutputStream;
40-
import java.io.IOException;
38+
import java.io.*;
4139
import java.lang.invoke.MethodHandles;
4240
import java.math.BigDecimal;
4341
import java.net.URI;
@@ -150,7 +148,6 @@ private static boolean mustCovered(Tree node) {
150148
}
151149

152150
public static Map<String, URI> readMetadata(MetadataOptions metadataOptions,
153-
FilterOptions filterOptions,
154151
Map<URI, Map<BigDecimal, Boolean>> coverageData) throws Exception {
155152

156153
boolean rawMode = false;
@@ -348,6 +345,40 @@ public static void dumpCoverageFile(Map<URI, Map<BigDecimal, Boolean>> coverageD
348345
}
349346
}
350347

348+
public static void printCoverageStats(Map<URI, Map<BigDecimal, Boolean>> coverageData,
349+
MetadataOptions metadataOptions) {
350+
List<Object[]> dataList = new LinkedList<>();
351+
URI projectUri = Path.of(metadataOptions.getProjectDirName()).toUri();
352+
coverageData.forEach((uri, bigDecimalsMap) -> {
353+
if (bigDecimalsMap.isEmpty()) {
354+
return;
355+
}
356+
String path = projectUri.relativize(uri).getPath();
357+
long linesToCover = bigDecimalsMap.size();
358+
long coveredLinesCount = bigDecimalsMap.values().stream().filter(aBoolean -> aBoolean.booleanValue()).count();
359+
Double coverage = Math.floorDiv(coveredLinesCount * 10000, linesToCover) / 100.;
360+
Object[] dataRow = {
361+
path,
362+
linesToCover,
363+
coveredLinesCount,
364+
coverage};
365+
dataList.add(dataRow);
366+
});
367+
Collections.sort(dataList, Comparator.comparing(objects -> ((Double) objects[3])));
368+
AsciiTable at = new AsciiTable();
369+
at.addRule();
370+
at.addRow("Path", "Lines to cover", "Covered lines", "Coverage, %");
371+
at.addRule();
372+
dataList.forEach(objects -> {
373+
at.addRow(objects);
374+
at.addRule();
375+
});
376+
CWC_LongestLine cwc = new CWC_LongestLine();
377+
at.getRenderer().setCWC(cwc);
378+
String rend = at.render();
379+
System.out.println(rend);
380+
}
381+
351382
public static String getPipeName(ConnectionOptions connectionOptions) throws IOException {
352383
boolean isWindows = System.getProperty ("os.name").toLowerCase().contains("win");
353384

@@ -364,4 +395,42 @@ public static String getPipeName(ConnectionOptions connectionOptions) throws IOE
364395
}
365396
return pipeName;
366397
}
398+
399+
public static boolean isProcessStillAlive(Integer pid) {
400+
String OS = System.getProperty("os.name").toLowerCase();
401+
String command = null;
402+
if (OS.indexOf("win") >= 0) {
403+
logger.debug("Check alive Windows mode. Pid: [{}]", pid);
404+
command = "cmd /c tasklist /FI \"PID eq " + pid + "\"";
405+
} else if (OS.indexOf("nix") >= 0 || OS.indexOf("nux") >= 0) {
406+
logger.debug("Check alive Linux/Unix mode. Pid: [{}]", pid);
407+
command = "ps -p " + pid;
408+
} else {
409+
logger.warn("Unsuported OS: Check alive for Pid: [{}] return false", pid);
410+
return false;
411+
}
412+
return isProcessIdRunning(String.valueOf(pid), command);
413+
}
414+
415+
private static boolean isProcessIdRunning(String pid, String command) {
416+
logger.debug("Command [{}]",command );
417+
try {
418+
Runtime rt = Runtime.getRuntime();
419+
Process pr = rt.exec(command);
420+
421+
InputStreamReader isReader = new InputStreamReader(pr.getInputStream());
422+
BufferedReader bReader = new BufferedReader(isReader);
423+
String strLine = null;
424+
while ((strLine= bReader.readLine()) != null) {
425+
if (strLine.contains(" " + pid + " ")) {
426+
return true;
427+
}
428+
}
429+
430+
return false;
431+
} catch (Exception ex) {
432+
logger.warn("Got exception using system command [{}].", command, ex);
433+
return true;
434+
}
435+
}
367436
}
9.83 KB
Binary file not shown.

0 commit comments

Comments
 (0)