json writer 추가
This commit is contained in:
parent
a4e98802f5
commit
3584245979
6 changed files with 186 additions and 42 deletions
|
|
@ -13,5 +13,4 @@ echo -e '
|
|||
"buildDisplayName":"'${BUILD_DISPLAY_NAME}'",
|
||||
"gitBranch":"'${GIT_BRANCH}'",
|
||||
"buildTag":"'${BUILD_TAG}'"
|
||||
}'
|
||||
echo -e ${BuildData} > "${WORKSPACE}/build_data.conf"
|
||||
}'
|
||||
|
|
@ -92,6 +92,7 @@ enum CommandType {
|
|||
Protobuf,
|
||||
JsonReader,
|
||||
JsonReaderFile,
|
||||
JsonWriterFile,
|
||||
AwsCli,
|
||||
Docker,
|
||||
Gradle,
|
||||
|
|
@ -146,7 +147,7 @@ abstract class Command {
|
|||
CommandType.GitCount: () => GitCount(),
|
||||
CommandType.GitReset: () => GitReset(),
|
||||
CommandType.GitStashPush: () => GitStashPush(),
|
||||
CommandType.GitStashApply: () => GitStashApply(),
|
||||
CommandType.GitStashApply: () => GitStashApply(),
|
||||
CommandType.GitHub: () => GitHub(),
|
||||
CommandType.GitHubPullRequestCreate: () => GitHubPullRequestCreate(),
|
||||
CommandType.GitHubPullRequestList: () => GitHubPullRequestList(),
|
||||
|
|
@ -154,6 +155,7 @@ abstract class Command {
|
|||
CommandType.Protobuf: () => Protobuf(),
|
||||
CommandType.JsonReader: () => JsonReader(),
|
||||
CommandType.JsonReaderFile: () => JsonReaderFile(),
|
||||
CommandType.JsonWriterFile: () => JsonWriterFile(),
|
||||
CommandType.AwsCli: () => AwsCli(),
|
||||
CommandType.Docker: () => Docker(),
|
||||
CommandType.Gradle: () => Gradle(),
|
||||
|
|
@ -228,28 +230,29 @@ abstract class Command {
|
|||
return newList;
|
||||
}
|
||||
|
||||
static Future setPropertyValue(String tag, dynamic value, {bool? showPrint}) async {
|
||||
static Future setPropertyValue(String tag, dynamic value,
|
||||
{bool? showPrint}) async {
|
||||
var match = regSetEx.firstMatch(tag);
|
||||
tag = match!.group(2)!;
|
||||
if (tag.startsWith('property')) {
|
||||
var key = tag.replaceFirst('property.', '');
|
||||
Application.instance.property[key] = value;
|
||||
// await CLI.println('[Return] $key : $value', color: Color.yellowStrong);
|
||||
if(showPrint ?? true) {
|
||||
if(value is List) {
|
||||
Application.log('[Return] $key :\n');
|
||||
var index = 0;
|
||||
var length = value.length.toString().length;
|
||||
for(var item in value) {
|
||||
var indexStr = index.toString().padLeft(length);
|
||||
Application.log('$indexStr - $item');
|
||||
index++;
|
||||
}
|
||||
} else {
|
||||
Application.log('[Return] $key : $value');
|
||||
if (showPrint ?? true) {
|
||||
if (value is List) {
|
||||
Application.log('[Return] $key :\n');
|
||||
var index = 0;
|
||||
var length = value.length.toString().length;
|
||||
for (var item in value) {
|
||||
var indexStr = index.toString().padLeft(length);
|
||||
Application.log('$indexStr - $item');
|
||||
index++;
|
||||
}
|
||||
} else {
|
||||
Application.log('[Return] $key : $value');
|
||||
}
|
||||
}
|
||||
}
|
||||
return simpleFuture;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:dart_framework/utils/string_util.dart';
|
||||
import 'package:oto_cli/cli/cli.dart';
|
||||
import 'package:oto_cli/oto/commands/command.dart';
|
||||
import 'package:oto_cli/oto/data/command_data.dart';
|
||||
|
|
@ -10,7 +11,9 @@ class JsonReader extends Command {
|
|||
Future execute(DataCommand command) async {
|
||||
var data = DataJsonReader.fromJson(getParam(command));
|
||||
try {
|
||||
Map<String, dynamic> temp = data.json is String ? jsonDecode(data.json) : data.json as Map<String, dynamic>;
|
||||
Map<String, dynamic> temp = data.json is String
|
||||
? jsonDecode(data.json)
|
||||
: data.json as Map<String, dynamic>;
|
||||
for (var item in data.pair) {
|
||||
var keys = item.from.split('.');
|
||||
for (var i = 0; i < keys.length; ++i) {
|
||||
|
|
@ -43,8 +46,8 @@ class JsonReaderFile extends Command {
|
|||
var content = await file.readAsString();
|
||||
try {
|
||||
Map<String, dynamic> json = jsonDecode(content);
|
||||
Map<String, dynamic> temp = json;
|
||||
for (var item in data.pair) {
|
||||
var temp = json;
|
||||
var keys = item.from.split('.');
|
||||
for (var i = 0; i < keys.length; ++i) {
|
||||
var key = keys[i];
|
||||
|
|
@ -69,3 +72,42 @@ class JsonReaderFile extends Command {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
class JsonWriterFile extends Command {
|
||||
@override
|
||||
Future execute(DataCommand command) async {
|
||||
var data = DataJsonWriterFile.fromJson(getParam(command));
|
||||
var path = Command.getAbs(data.path);
|
||||
var file = File(path);
|
||||
if (file.existsSync()) {
|
||||
var content = await file.readAsString();
|
||||
try {
|
||||
Map<String, dynamic> json = jsonDecode(content);
|
||||
for (var item in data.pair) {
|
||||
var temp = json;
|
||||
var keys = item.from.split('.');
|
||||
for (var i = 0; i < keys.length; ++i) {
|
||||
var key = keys[i];
|
||||
if (temp.containsKey(key)) {
|
||||
if (i == keys.length - 1) {
|
||||
temp[keys[i]] = item.value;
|
||||
break;
|
||||
} else {
|
||||
temp = temp[keys[i]];
|
||||
}
|
||||
} else {
|
||||
throw Exception('Not exist key: "$key" in $item');
|
||||
}
|
||||
}
|
||||
}
|
||||
content = jsonPerttyEncode(json);
|
||||
file.writeAsStringSync(content, flush: true);
|
||||
} on Exception catch (e) {
|
||||
throw Exception('Json parsing error: $e');
|
||||
}
|
||||
} else {
|
||||
await CLI.println('Json "$path" file not exit. Return value to be null.',
|
||||
color: Color.red);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,14 +8,13 @@ class StringSub extends Command {
|
|||
var text = data.text;
|
||||
var exitCode = 0;
|
||||
|
||||
try
|
||||
{
|
||||
try {
|
||||
int startIndex = data.startIndex == null ? 0 : data.startIndex!;
|
||||
int lastIndex = data.lastIndex == null ? text.length - startIndex : data.lastIndex!;
|
||||
int lastIndex =
|
||||
data.lastIndex == null ? text.length - startIndex : data.lastIndex!;
|
||||
text = text.substring(startIndex, lastIndex);
|
||||
await setProperty(data.setValue, text, showPrint: data.showPrint);
|
||||
}
|
||||
on Exception {
|
||||
} on Exception {
|
||||
rethrow;
|
||||
}
|
||||
|
||||
|
|
@ -30,20 +29,21 @@ class StringReplace extends Command {
|
|||
var text = data.text;
|
||||
var exitCode = 0;
|
||||
|
||||
try
|
||||
{
|
||||
if(data.fromRegExp != null) {
|
||||
text = text.replaceAll(RegExp(data.fromRegExp!), data.to);
|
||||
var list = data.pair;
|
||||
for (var pair in list) {
|
||||
try {
|
||||
if (pair.fromRegExp != null) {
|
||||
text = text.replaceAll(RegExp(pair.fromRegExp!), pair.to);
|
||||
}
|
||||
if (pair.from != null) {
|
||||
text = text.replaceAll(pair.from!, pair.to);
|
||||
}
|
||||
} on Exception {
|
||||
rethrow;
|
||||
}
|
||||
if(data.from != null) {
|
||||
text = text.replaceAll(data.from!, data.to);
|
||||
}
|
||||
await setProperty(data.setValue, text, showPrint: data.showPrint);
|
||||
}
|
||||
on Exception {
|
||||
rethrow;
|
||||
}
|
||||
await setProperty(data.setValue, text, showPrint: data.showPrint);
|
||||
|
||||
return await complete(exitCode, command);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -572,7 +572,6 @@ class DataShell extends DataParam {
|
|||
Map<String, dynamic> toJson() => _$DataShellToJson(this);
|
||||
}
|
||||
|
||||
|
||||
@JsonSerializable()
|
||||
class DataShellFile extends DataParam {
|
||||
DataShellFile();
|
||||
|
|
@ -813,6 +812,20 @@ class DataJsonReaderFile extends DataParam {
|
|||
Map<String, dynamic> toJson() => _$DataJsonReaderFileToJson(this);
|
||||
}
|
||||
|
||||
//JsonReaderFile
|
||||
@JsonSerializable()
|
||||
class DataJsonWriterFile extends DataParam {
|
||||
DataJsonWriterFile();
|
||||
|
||||
late String path;
|
||||
late List<DataJsonPair> pair;
|
||||
|
||||
factory DataJsonWriterFile.fromJson(Map<String, dynamic> json) =>
|
||||
_$DataJsonWriterFileFromJson(json);
|
||||
@override
|
||||
Map<String, dynamic> toJson() => _$DataJsonWriterFileToJson(this);
|
||||
}
|
||||
|
||||
@JsonSerializable()
|
||||
class DataExecuteApp {
|
||||
late String executable;
|
||||
|
|
@ -837,6 +850,18 @@ class DataPair {
|
|||
Map<String, dynamic> toJson() => _$DataPairToJson(this);
|
||||
}
|
||||
|
||||
@JsonSerializable()
|
||||
class DataJsonPair {
|
||||
late String from;
|
||||
late String value;
|
||||
|
||||
DataJsonPair();
|
||||
|
||||
factory DataJsonPair.fromJson(Map<String, dynamic> json) =>
|
||||
_$DataJsonPairFromJson(json);
|
||||
Map<String, dynamic> toJson() => _$DataJsonPairToJson(this);
|
||||
}
|
||||
|
||||
//AwsCli
|
||||
@JsonSerializable()
|
||||
class DataAwsCli extends DataParam {
|
||||
|
|
@ -902,9 +927,8 @@ class DataStringReplace extends DataParam {
|
|||
|
||||
late String text;
|
||||
late String setValue;
|
||||
late String? from;
|
||||
late String? fromRegExp;
|
||||
late String to;
|
||||
|
||||
late List<DataStringReplacePair> pair;
|
||||
|
||||
factory DataStringReplace.fromJson(Map<String, dynamic> json) =>
|
||||
_$DataStringReplaceFromJson(json);
|
||||
|
|
@ -912,6 +936,21 @@ class DataStringReplace extends DataParam {
|
|||
Map<String, dynamic> toJson() => _$DataStringReplaceToJson(this);
|
||||
}
|
||||
|
||||
//Web String Replace Item
|
||||
@JsonSerializable()
|
||||
class DataStringReplacePair extends DataParam {
|
||||
DataStringReplacePair();
|
||||
|
||||
late String? from;
|
||||
late String? fromRegExp;
|
||||
late String to;
|
||||
|
||||
factory DataStringReplacePair.fromJson(Map<String, dynamic> json) =>
|
||||
_$DataStringReplacePairFromJson(json);
|
||||
@override
|
||||
Map<String, dynamic> toJson() => _$DataStringReplacePairToJson(this);
|
||||
}
|
||||
|
||||
//Web Base
|
||||
@JsonSerializable()
|
||||
class DataWebBase extends DataParam {
|
||||
|
|
|
|||
|
|
@ -103,6 +103,7 @@ const _$CommandTypeEnumMap = {
|
|||
CommandType.Protobuf: 'Protobuf',
|
||||
CommandType.JsonReader: 'JsonReader',
|
||||
CommandType.JsonReaderFile: 'JsonReaderFile',
|
||||
CommandType.JsonWriterFile: 'JsonWriterFile',
|
||||
CommandType.AwsCli: 'AwsCli',
|
||||
CommandType.Docker: 'Docker',
|
||||
CommandType.Gradle: 'Gradle',
|
||||
|
|
@ -1392,6 +1393,31 @@ Map<String, dynamic> _$DataJsonReaderFileToJson(DataJsonReaderFile instance) =>
|
|||
'pair': instance.pair,
|
||||
};
|
||||
|
||||
DataJsonWriterFile _$DataJsonWriterFileFromJson(Map<String, dynamic> json) =>
|
||||
DataJsonWriterFile()
|
||||
..workspace = json['workspace'] as String?
|
||||
..passExitCodes = (json['passExitCodes'] as List<dynamic>?)
|
||||
?.map((e) => (e as num).toInt())
|
||||
.toList()
|
||||
..setResult = json['setResult'] as String?
|
||||
..setExitCode = json['setExitCode'] as String?
|
||||
..showPrint = json['showPrint'] as bool?
|
||||
..path = json['path'] as String
|
||||
..pair = (json['pair'] as List<dynamic>)
|
||||
.map((e) => DataJsonPair.fromJson(e as Map<String, dynamic>))
|
||||
.toList();
|
||||
|
||||
Map<String, dynamic> _$DataJsonWriterFileToJson(DataJsonWriterFile instance) =>
|
||||
<String, dynamic>{
|
||||
'workspace': instance.workspace,
|
||||
'passExitCodes': instance.passExitCodes,
|
||||
'setResult': instance.setResult,
|
||||
'setExitCode': instance.setExitCode,
|
||||
'showPrint': instance.showPrint,
|
||||
'path': instance.path,
|
||||
'pair': instance.pair,
|
||||
};
|
||||
|
||||
DataExecuteApp _$DataExecuteAppFromJson(Map<String, dynamic> json) =>
|
||||
DataExecuteApp()
|
||||
..executable = json['executable'] as String
|
||||
|
|
@ -1412,6 +1438,16 @@ Map<String, dynamic> _$DataPairToJson(DataPair instance) => <String, dynamic>{
|
|||
'setValue': instance.setValue,
|
||||
};
|
||||
|
||||
DataJsonPair _$DataJsonPairFromJson(Map<String, dynamic> json) => DataJsonPair()
|
||||
..from = json['from'] as String
|
||||
..value = json['value'] as String;
|
||||
|
||||
Map<String, dynamic> _$DataJsonPairToJson(DataJsonPair instance) =>
|
||||
<String, dynamic>{
|
||||
'from': instance.from,
|
||||
'value': instance.value,
|
||||
};
|
||||
|
||||
DataAwsCli _$DataAwsCliFromJson(Map<String, dynamic> json) => DataAwsCli()
|
||||
..workspace = json['workspace'] as String?
|
||||
..passExitCodes = (json['passExitCodes'] as List<dynamic>?)
|
||||
|
|
@ -1514,9 +1550,9 @@ DataStringReplace _$DataStringReplaceFromJson(Map<String, dynamic> json) =>
|
|||
..showPrint = json['showPrint'] as bool?
|
||||
..text = json['text'] as String
|
||||
..setValue = json['setValue'] as String
|
||||
..from = json['from'] as String?
|
||||
..fromRegExp = json['fromRegExp'] as String?
|
||||
..to = json['to'] as String;
|
||||
..pair = (json['pair'] as List<dynamic>)
|
||||
.map((e) => DataStringReplacePair.fromJson(e as Map<String, dynamic>))
|
||||
.toList();
|
||||
|
||||
Map<String, dynamic> _$DataStringReplaceToJson(DataStringReplace instance) =>
|
||||
<String, dynamic>{
|
||||
|
|
@ -1527,6 +1563,31 @@ Map<String, dynamic> _$DataStringReplaceToJson(DataStringReplace instance) =>
|
|||
'showPrint': instance.showPrint,
|
||||
'text': instance.text,
|
||||
'setValue': instance.setValue,
|
||||
'pair': instance.pair,
|
||||
};
|
||||
|
||||
DataStringReplacePair _$DataStringReplacePairFromJson(
|
||||
Map<String, dynamic> json) =>
|
||||
DataStringReplacePair()
|
||||
..workspace = json['workspace'] as String?
|
||||
..passExitCodes = (json['passExitCodes'] as List<dynamic>?)
|
||||
?.map((e) => (e as num).toInt())
|
||||
.toList()
|
||||
..setResult = json['setResult'] as String?
|
||||
..setExitCode = json['setExitCode'] as String?
|
||||
..showPrint = json['showPrint'] as bool?
|
||||
..from = json['from'] as String?
|
||||
..fromRegExp = json['fromRegExp'] as String?
|
||||
..to = json['to'] as String;
|
||||
|
||||
Map<String, dynamic> _$DataStringReplacePairToJson(
|
||||
DataStringReplacePair instance) =>
|
||||
<String, dynamic>{
|
||||
'workspace': instance.workspace,
|
||||
'passExitCodes': instance.passExitCodes,
|
||||
'setResult': instance.setResult,
|
||||
'setExitCode': instance.setExitCode,
|
||||
'showPrint': instance.showPrint,
|
||||
'from': instance.from,
|
||||
'fromRegExp': instance.fromRegExp,
|
||||
'to': instance.to,
|
||||
|
|
|
|||
Loading…
Reference in a new issue