Skip to content

Commit

Permalink
nop-cli gen增加input参数
Browse files Browse the repository at this point in the history
  • Loading branch information
entropy-cloud committed Jan 21, 2025
1 parent c6040d3 commit e5e9c26
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
10 changes: 10 additions & 0 deletions nop-cli-core/src/main/java/io/nop/cli/commands/CliGenCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import io.nop.commons.util.FileHelper;
import io.nop.commons.util.StringHelper;
import io.nop.core.lang.eval.IEvalScope;
import io.nop.core.lang.json.JsonTool;
import io.nop.core.resource.IResource;
import io.nop.core.resource.ResourceHelper;
import io.nop.core.resource.VirtualFileSystem;
Expand All @@ -25,6 +26,7 @@
import picocli.CommandLine;

import java.io.File;
import java.util.Map;
import java.util.concurrent.Callable;

@CommandLine.Command(
Expand All @@ -40,6 +42,9 @@ public class CliGenCommand implements Callable<Integer> {
description = "模板文件路径,至少需要指定一个模板。")
String[] templates;

@CommandLine.Option(names = {"-i", "--input"}, description = "输入参数")
String input;

@CommandLine.Option(names = {"-o", "--output"}, description = "输出目录,缺省为当前目录")
File outputDir;

Expand All @@ -54,6 +59,11 @@ public Integer call() {
try {
IResource resource = ResourceHelper.resolveRelativePathResource(file);
IEvalScope scope = XLang.newEvalScope();
if (input != null) {
Map<String, Object> map = (Map<String, Object>) JsonTool.parseNonStrict(null, input);
scope.setLocalValues(map);
}

scope.setLocalValue(null, CodeGenConstants.VAR_CODE_GEN_MODEL_PATH, resource.getPath());

ComponentModelConfig config = ResourceComponentManager.instance().getModelConfigByModelPath(resource.getPath());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,14 @@ public Integer call() {
}
}

IEvalScope scope = XLang.newEvalScope();
scope.setLocalValues(json);

if (template.endsWith(".xdef")) {
DslModelHelper.saveDslModel(template, json, new FileResource(outputFile));
} else if (template.endsWith(".imp.xml")) {
ExcelWorkbook xptModel = ExcelReportHelper.buildXptModelFromImpModel(template);

IEvalScope scope = XLang.newEvalScope();
scope.setLocalValue(null, XptConstants.VAR_ENTITY, json);

String renderType = StringHelper.fileExt(outputFile.getName());
Expand All @@ -89,7 +91,6 @@ public Integer call() {
// 报表模板
IResource tplResource = ResourceHelper.resolveRelativePathResource(template);
ExcelWorkbook xptModel = new XptModelLoader().loadModelFromResource(tplResource);
IEvalScope scope = XLang.newEvalScope();
scope.setLocalValue(null, XptConstants.VAR_ENTITY, json);

String renderType = StringHelper.fileExt(outputFile.getName());
Expand All @@ -98,7 +99,6 @@ public Integer call() {
// Word模板
IResource tplResource = ResourceHelper.resolveRelativePathResource(template);
WordTemplate tpl = new WordTemplateParser().parseFromResource(tplResource);
IEvalScope scope = XLang.newEvalScope();
scope.setLocalValue(null, XptConstants.VAR_ENTITY, json);
tpl.generateToFile(outputFile, scope);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,19 @@
import io.nop.api.core.exceptions.NopException;
import io.nop.commons.util.FileHelper;
import io.nop.core.lang.eval.IEvalScope;
import io.nop.core.lang.json.JsonTool;
import io.nop.core.resource.IResource;
import io.nop.core.resource.ResourceHelper;
import io.nop.core.resource.watch.FileChangeEvent;
import io.nop.core.resource.watch.FileWatcher;
import io.nop.xlang.api.XLang;
import io.nop.xlang.api.XplModel;
import io.nop.xlang.ast.XLangOutputMode;
import jakarta.inject.Inject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import picocli.CommandLine;

import jakarta.inject.Inject;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
Expand Down Expand Up @@ -55,6 +56,9 @@ public class CliWatchCommand implements Callable<Integer> {
@CommandLine.Option(names = {"-w", "--wait"}, description = "延迟处理等待间隔,缺省为100毫秒")
int debounceWait = 100;

@CommandLine.Option(names = {"-i", "--input"}, description = "输入参数")
String input;

@Inject
FileWatcherFactory fileWatcherFactory;

Expand Down Expand Up @@ -91,6 +95,12 @@ public Integer call() {

private void processEvents(XplModel xpl, Collection<FileChangeEvent> events, Map<String, Object> state) {
IEvalScope scope = XLang.newEvalScope();

if (input != null) {
Map<String, Object> map = (Map<String, Object>) JsonTool.parseNonStrict(null, input);
scope.setLocalValues(map);
}

scope.setLocalValue("changeEvents", events);
scope.setLocalValue("globalState", state);
scope.setLocalValue("watchDirs", watchDirs);
Expand Down
2 changes: 2 additions & 0 deletions nop-dyn/nop-dyn-service/src/test/resources/application.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
nop:
debug: true

0 comments on commit e5e9c26

Please sign in to comment.