From 3584245979e374f9ed21d8919f0610c03f66e9b8 Mon Sep 17 00:00:00 2001 From: Toki Date: Tue, 31 Dec 2024 16:29:32 +0900 Subject: [PATCH] =?UTF-8?q?json=20writer=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- assets/script/shell/jenkins_env_params.sh | 3 +- lib/oto/commands/command.dart | 31 ++++++----- lib/oto/commands/util/json.dart | 46 +++++++++++++++- lib/oto/commands/util/string_util.dart | 34 ++++++------ lib/oto/data/command_data.dart | 47 ++++++++++++++-- lib/oto/data/command_data.g.dart | 67 ++++++++++++++++++++++- 6 files changed, 186 insertions(+), 42 deletions(-) diff --git a/assets/script/shell/jenkins_env_params.sh b/assets/script/shell/jenkins_env_params.sh index 8073f56..9675f46 100644 --- a/assets/script/shell/jenkins_env_params.sh +++ b/assets/script/shell/jenkins_env_params.sh @@ -13,5 +13,4 @@ echo -e ' "buildDisplayName":"'${BUILD_DISPLAY_NAME}'", "gitBranch":"'${GIT_BRANCH}'", "buildTag":"'${BUILD_TAG}'" -}' -echo -e ${BuildData} > "${WORKSPACE}/build_data.conf" \ No newline at end of file +}' \ No newline at end of file diff --git a/lib/oto/commands/command.dart b/lib/oto/commands/command.dart index c116233..64c4178 100644 --- a/lib/oto/commands/command.dart +++ b/lib/oto/commands/command.dart @@ -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; } diff --git a/lib/oto/commands/util/json.dart b/lib/oto/commands/util/json.dart index 38c0048..3bb9d13 100644 --- a/lib/oto/commands/util/json.dart +++ b/lib/oto/commands/util/json.dart @@ -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 temp = data.json is String ? jsonDecode(data.json) : data.json as Map; + Map temp = data.json is String + ? jsonDecode(data.json) + : data.json as Map; 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 json = jsonDecode(content); - Map 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 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); + } + } +} diff --git a/lib/oto/commands/util/string_util.dart b/lib/oto/commands/util/string_util.dart index 6c7337b..317c25a 100644 --- a/lib/oto/commands/util/string_util.dart +++ b/lib/oto/commands/util/string_util.dart @@ -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); } -} \ No newline at end of file +} diff --git a/lib/oto/data/command_data.dart b/lib/oto/data/command_data.dart index 428dcc5..7e61a3b 100644 --- a/lib/oto/data/command_data.dart +++ b/lib/oto/data/command_data.dart @@ -572,7 +572,6 @@ class DataShell extends DataParam { Map toJson() => _$DataShellToJson(this); } - @JsonSerializable() class DataShellFile extends DataParam { DataShellFile(); @@ -813,6 +812,20 @@ class DataJsonReaderFile extends DataParam { Map toJson() => _$DataJsonReaderFileToJson(this); } +//JsonReaderFile +@JsonSerializable() +class DataJsonWriterFile extends DataParam { + DataJsonWriterFile(); + + late String path; + late List pair; + + factory DataJsonWriterFile.fromJson(Map json) => + _$DataJsonWriterFileFromJson(json); + @override + Map toJson() => _$DataJsonWriterFileToJson(this); +} + @JsonSerializable() class DataExecuteApp { late String executable; @@ -837,6 +850,18 @@ class DataPair { Map toJson() => _$DataPairToJson(this); } +@JsonSerializable() +class DataJsonPair { + late String from; + late String value; + + DataJsonPair(); + + factory DataJsonPair.fromJson(Map json) => + _$DataJsonPairFromJson(json); + Map 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 pair; factory DataStringReplace.fromJson(Map json) => _$DataStringReplaceFromJson(json); @@ -912,6 +936,21 @@ class DataStringReplace extends DataParam { Map 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 json) => + _$DataStringReplacePairFromJson(json); + @override + Map toJson() => _$DataStringReplacePairToJson(this); +} + //Web Base @JsonSerializable() class DataWebBase extends DataParam { diff --git a/lib/oto/data/command_data.g.dart b/lib/oto/data/command_data.g.dart index 6b1e2a1..87011ad 100644 --- a/lib/oto/data/command_data.g.dart +++ b/lib/oto/data/command_data.g.dart @@ -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 _$DataJsonReaderFileToJson(DataJsonReaderFile instance) => 'pair': instance.pair, }; +DataJsonWriterFile _$DataJsonWriterFileFromJson(Map json) => + DataJsonWriterFile() + ..workspace = json['workspace'] as String? + ..passExitCodes = (json['passExitCodes'] as List?) + ?.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) + .map((e) => DataJsonPair.fromJson(e as Map)) + .toList(); + +Map _$DataJsonWriterFileToJson(DataJsonWriterFile instance) => + { + 'workspace': instance.workspace, + 'passExitCodes': instance.passExitCodes, + 'setResult': instance.setResult, + 'setExitCode': instance.setExitCode, + 'showPrint': instance.showPrint, + 'path': instance.path, + 'pair': instance.pair, + }; + DataExecuteApp _$DataExecuteAppFromJson(Map json) => DataExecuteApp() ..executable = json['executable'] as String @@ -1412,6 +1438,16 @@ Map _$DataPairToJson(DataPair instance) => { 'setValue': instance.setValue, }; +DataJsonPair _$DataJsonPairFromJson(Map json) => DataJsonPair() + ..from = json['from'] as String + ..value = json['value'] as String; + +Map _$DataJsonPairToJson(DataJsonPair instance) => + { + 'from': instance.from, + 'value': instance.value, + }; + DataAwsCli _$DataAwsCliFromJson(Map json) => DataAwsCli() ..workspace = json['workspace'] as String? ..passExitCodes = (json['passExitCodes'] as List?) @@ -1514,9 +1550,9 @@ DataStringReplace _$DataStringReplaceFromJson(Map 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) + .map((e) => DataStringReplacePair.fromJson(e as Map)) + .toList(); Map _$DataStringReplaceToJson(DataStringReplace instance) => { @@ -1527,6 +1563,31 @@ Map _$DataStringReplaceToJson(DataStringReplace instance) => 'showPrint': instance.showPrint, 'text': instance.text, 'setValue': instance.setValue, + 'pair': instance.pair, + }; + +DataStringReplacePair _$DataStringReplacePairFromJson( + Map json) => + DataStringReplacePair() + ..workspace = json['workspace'] as String? + ..passExitCodes = (json['passExitCodes'] as List?) + ?.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 _$DataStringReplacePairToJson( + DataStringReplacePair instance) => + { + 'workspace': instance.workspace, + 'passExitCodes': instance.passExitCodes, + 'setResult': instance.setResult, + 'setExitCode': instance.setExitCode, + 'showPrint': instance.showPrint, 'from': instance.from, 'fromRegExp': instance.fromRegExp, 'to': instance.to,