|
| 1 | +package org.zstack.devtool.generator; |
| 2 | + |
| 3 | +import org.zstack.devtool.model.ApiMessageInfo; |
| 4 | + |
| 5 | +import java.io.IOException; |
| 6 | +import java.nio.charset.StandardCharsets; |
| 7 | +import java.nio.file.Files; |
| 8 | +import java.nio.file.Path; |
| 9 | +import java.nio.file.StandardOpenOption; |
| 10 | +import java.util.ArrayList; |
| 11 | +import java.util.HashSet; |
| 12 | +import java.util.List; |
| 13 | +import java.util.Set; |
| 14 | +import java.util.regex.Matcher; |
| 15 | +import java.util.regex.Pattern; |
| 16 | + |
| 17 | +public class ApiHelperGenerator { |
| 18 | + |
| 19 | + public int generate(List<ApiMessageInfo> messages, Path apiHelperFile) { |
| 20 | + if (!Files.exists(apiHelperFile)) { |
| 21 | + System.out.println("[ApiHelper] WARN - ApiHelper.groovy not found at " + apiHelperFile); |
| 22 | + return 0; |
| 23 | + } |
| 24 | + |
| 25 | + String content; |
| 26 | + try { |
| 27 | + content = new String(Files.readAllBytes(apiHelperFile), StandardCharsets.UTF_8); |
| 28 | + } catch (IOException e) { |
| 29 | + System.err.println("[ApiHelper] ERROR - Cannot read " + apiHelperFile + ": " + e.getMessage()); |
| 30 | + return 0; |
| 31 | + } |
| 32 | + |
| 33 | + // Extract existing method names |
| 34 | + Set<String> existingMethods = new HashSet<>(); |
| 35 | + Pattern pattern = Pattern.compile("def\\s+(\\w+)\\s*\\("); |
| 36 | + Matcher matcher = pattern.matcher(content); |
| 37 | + while (matcher.find()) { |
| 38 | + existingMethods.add(matcher.group(1)); |
| 39 | + } |
| 40 | + |
| 41 | + // Collect missing methods |
| 42 | + List<ApiMessageInfo> missing = new ArrayList<>(); |
| 43 | + for (ApiMessageInfo msg : messages) { |
| 44 | + if (!existingMethods.contains(msg.getHelperMethodName())) { |
| 45 | + missing.add(msg); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + if (missing.isEmpty()) { |
| 50 | + return 0; |
| 51 | + } |
| 52 | + |
| 53 | + // Find insertion point: just before the closing brace of the class |
| 54 | + // ApiHelper.groovy ends with a closing "}" on its own line |
| 55 | + int insertionPoint = findInsertionPoint(content); |
| 56 | + if (insertionPoint < 0) { |
| 57 | + System.err.println("[ApiHelper] ERROR - Cannot find insertion point in " + apiHelperFile); |
| 58 | + return 0; |
| 59 | + } |
| 60 | + |
| 61 | + StringBuilder newMethods = new StringBuilder(); |
| 62 | + for (ApiMessageInfo msg : missing) { |
| 63 | + newMethods.append(generateMethod(msg)); |
| 64 | + } |
| 65 | + |
| 66 | + String newContent = content.substring(0, insertionPoint) |
| 67 | + + newMethods |
| 68 | + + content.substring(insertionPoint); |
| 69 | + |
| 70 | + try { |
| 71 | + Files.write(apiHelperFile, newContent.getBytes(StandardCharsets.UTF_8), |
| 72 | + StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING); |
| 73 | + System.out.println("[ApiHelper] Added " + missing.size() + " method(s)"); |
| 74 | + for (ApiMessageInfo msg : missing) { |
| 75 | + System.out.println(" Added: " + msg.getHelperMethodName()); |
| 76 | + } |
| 77 | + } catch (IOException e) { |
| 78 | + System.err.println("[ApiHelper] ERROR - Cannot write " + apiHelperFile + ": " + e.getMessage()); |
| 79 | + return 0; |
| 80 | + } |
| 81 | + |
| 82 | + return missing.size(); |
| 83 | + } |
| 84 | + |
| 85 | + private int findInsertionPoint(String content) { |
| 86 | + // Find the last closing brace which ends the class body |
| 87 | + int lastBrace = content.lastIndexOf('}'); |
| 88 | + if (lastBrace < 0) return -1; |
| 89 | + // Walk back to find a newline before it |
| 90 | + int lineStart = content.lastIndexOf('\n', lastBrace); |
| 91 | + if (lineStart < 0) lineStart = 0; |
| 92 | + // Return the position of that newline (insert before last closing brace line) |
| 93 | + return lineStart + 1; |
| 94 | + } |
| 95 | + |
| 96 | + private String generateMethod(ApiMessageInfo msg) { |
| 97 | + String methodName = msg.getHelperMethodName(); |
| 98 | + String actionFqn = "org.zstack.sdk." + msg.getActionName(); |
| 99 | + |
| 100 | + StringBuilder sb = new StringBuilder(); |
| 101 | + sb.append("\n\n"); |
| 102 | + sb.append(" def ").append(methodName) |
| 103 | + .append("(@DelegatesTo(strategy = Closure.OWNER_FIRST, value = ") |
| 104 | + .append(actionFqn).append(".class) Closure c) {\n"); |
| 105 | + sb.append(" def a = new ").append(actionFqn).append("()\n"); |
| 106 | + sb.append(" a.sessionId = Test.currentEnvSpec?.session?.uuid\n"); |
| 107 | + sb.append(" c.resolveStrategy = Closure.OWNER_FIRST\n"); |
| 108 | + sb.append(" c.delegate = a\n"); |
| 109 | + sb.append(" c()\n"); |
| 110 | + sb.append(" \n"); |
| 111 | + |
| 112 | + // Query actions need conditions coercion |
| 113 | + if (msg.isQuery()) { |
| 114 | + sb.append(" a.conditions = a.conditions.collect { it.toString() }\n"); |
| 115 | + } |
| 116 | + |
| 117 | + sb.append("\n\n"); |
| 118 | + sb.append(" if (System.getProperty(\"apipath\") != null) {\n"); |
| 119 | + sb.append(" if (a.apiId == null) {\n"); |
| 120 | + sb.append(" a.apiId = Platform.uuid\n"); |
| 121 | + sb.append(" }\n"); |
| 122 | + sb.append(" \n"); |
| 123 | + sb.append(" def tracker = new ApiPathTracker(a.apiId)\n"); |
| 124 | + sb.append(" def out = errorOut(a.call())\n"); |
| 125 | + sb.append(" def path = tracker.getApiPath()\n"); |
| 126 | + sb.append(" if (!path.isEmpty()) {\n"); |
| 127 | + sb.append(" Test.apiPaths[a.class.name] = path.join(\" --->\\n\")\n"); |
| 128 | + sb.append(" }\n"); |
| 129 | + sb.append(" \n"); |
| 130 | + sb.append(" return out\n"); |
| 131 | + sb.append(" } else {\n"); |
| 132 | + sb.append(" return errorOut(a.call())\n"); |
| 133 | + sb.append(" }\n"); |
| 134 | + sb.append(" }\n"); |
| 135 | + |
| 136 | + return sb.toString(); |
| 137 | + } |
| 138 | +} |
0 commit comments