From 7f3f27959fa55077bfa36dad7d5b5696b870d003 Mon Sep 17 00:00:00 2001 From: "leedongmyung[desktop]" Date: Sat, 18 Nov 2023 17:04:09 +0900 Subject: [PATCH] code refactoring --- lib/oto/application.dart | 183 +++++++++++++++----------- lib/oto/defined_data.dart | 270 ++------------------------------------ 2 files changed, 115 insertions(+), 338 deletions(-) diff --git a/lib/oto/application.dart b/lib/oto/application.dart index 9406a06..aad672f 100644 --- a/lib/oto/application.dart +++ b/lib/oto/application.dart @@ -16,6 +16,12 @@ class Application { static final Application _instance = Application._privateConstructor(); static Application get instance => _instance; + final Map _mapComposer = { + BuildType.test: DataComposerTest(), + BuildType.jenkins: DataComposerJenkins(), + BuildType.file: DataComposerFile() + }; + Map? _property; Map? get property { return _property; @@ -30,71 +36,42 @@ class Application { setUTF8(); Map? buildMap; - Map? jenkinsMap; - String buildContent = ''; + // var envArgs = envArguments(arguments); // bool isTest = envArgs.containsKey('isTest'); //for Unit Test try { - switch (buildType) { - case BuildType.test: - //Test map - _jenkinsData = DataJenkins.fromJson(TestData.jenkinsMap!); - buildMap = TestData.buildMap; - print('Start test build...'); - break; - - case BuildType.jenkins: - String os = Platform.operatingSystem; - var enable = true; - print('Current OS: $os'); - print('Start build...'); - String jsonStr = ''; - if (Platform.isMacOS || Platform.isLinux) { - await Process.run('zsh', [ - '${Directory.current.path}/assets/script/shell/jenkins_env_params.sh' - ]).then((ProcessResult result) => jsonStr = result.stdout); - jenkinsMap = getMapFromJson(jsonStr)!; - } else if (Platform.isWindows) { - await Process.run('cmd', [ - '/C', - '${Directory.current.path}/assets/script/batch/jenkins_env_params.bat' - ]).then((ProcessResult result) => jsonStr = result.stdout); - jsonStr = jsonStr.substring(jsonStr.indexOf('{')); - jsonStr = jsonStr.replaceAll('\\', '/'); - jenkinsMap = getMapFromJson(jsonStr); - } else { - enable = false; - print( - 'is not Mac/Windows/Linux. build must be execute in Mac/Windows os system'); - } - if (enable) { - await printBuildStep('Jenkins Variables'); - print(jsonStr); - } - _jenkinsData = DataJenkins.fromJson(jenkinsMap!); - buildContent = await getBuildData(); - buildMap = getMapFromYaml(buildContent); - break; - - case BuildType.file: - _jenkinsData = DataJenkins.fromJson(FileData.jenkinsMap!); - buildContent = yamlContent!; - buildMap = getMapFromYaml(buildContent); - break; - } + var composer = _mapComposer[buildType]!; + composer.compose(yamlContent, printBuildStep); + _jenkinsData = composer.jenkinsData; + buildMap = getMapFromYaml(composer.buildYaml); + await CLI.println( + '********************************* Build Data *************************************', + color: Color.magenta); + print(composer.buildYaml); } on Exception catch (e, stacktace) { await CLI.printString(e.toString(), color: Color.redStrong); await CLI.printString(stacktace.toString(), color: Color.yellowStrong); - await printBuildStep('Build Failed', color: Color.redStrong); + await printBuildStep('Build Failed', Color.redStrong); return; } - await CLI.println( - '********************************* Build Data *************************************', - color: Color.magenta); - print(buildContent); + var c = Completer(); + DataBuild? build; + build = DataBuild.fromJson(buildMap!); - await execute(buildMap!); + _property = build.common; + for (var command in build.commands) { + String commandStr = + command.command.toString().replaceAll('CommandType.', ''); + await printBuildStep('Phase Start: $commandStr', Color.green); + if (!await Command(command.command).execute(command)) { + exit(10); + } + } + + c.complete(); + await printBuildStep('Build Successfully Complete', Color.cyan); + exit(0); } Future setUTF8() async { @@ -106,27 +83,7 @@ class Application { return simpleFuture; } - Future execute(Map buildMap) async { - var c = Completer(); - DataBuild? build; - build = DataBuild.fromJson(buildMap); - - _property = build.common; - for (var command in build.commands) { - String commandStr = - command.command.toString().replaceAll('CommandType.', ''); - await printBuildStep('Phase Start: $commandStr'); - if (!await Command(command.command).execute(command)) { - exit(10); - } - } - - c.complete(); - await printBuildStep('Build Successfully Complete', color: Color.cyan); - exit(0); - } - - Future printBuildStep(String name, {Color? color}) async { + Future printBuildStep(String name, Color? color) async { var message = '''************************************************************************************ * $name @@ -134,10 +91,80 @@ class Application { await CLI.printString(message, color: color ?? Color.green); return simpleFuture; } +} + +//############################ DataComposer ##########################// +abstract class DataComposer { + DataJenkins? jenkinsData; + String buildYaml = ''; + + Future compose(String? yaml, Future Function(String, Color?) printer) async { + return simpleFuture; + } +} + +class DataComposerTest extends DataComposer { + @override + Future compose(String? yaml, Future Function(String, Color?) printer) async { + jenkinsData = DataJenkins.fromJson(TestData.jenkinsMap!); + buildYaml = TestData.buildYaml; + return simpleFuture; + } +} + +class DataComposerFile extends DataComposer { + DataJenkins? jenkinsData; + String buildYaml = ''; + + @override + Future compose(String? yaml, Future Function(String, Color?) printer) async { + jenkinsData = DataJenkins.fromJson(FileData.jenkinsMap!); + buildYaml = yaml ?? ''; + return simpleFuture; + } +} + +class DataComposerJenkins extends DataComposer { + @override + Future compose(String? yaml, Future Function(String, Color?) printer) async { + Map? jenkinsMap; + String os = Platform.operatingSystem; + var enable = true; + print('Current OS: $os'); + print('Start build...'); + String jsonStr = ''; + if (Platform.isMacOS || Platform.isLinux) { + await Process.run('zsh', [ + '${Directory.current.path}/assets/script/shell/jenkins_env_params.sh' + ]).then((ProcessResult result) => jsonStr = result.stdout); + jenkinsMap = getMapFromJson(jsonStr)!; + } else if (Platform.isWindows) { + await Process.run('cmd', [ + '/C', + '${Directory.current.path}/assets/script/batch/jenkins_env_params.bat' + ]).then((ProcessResult result) => jsonStr = result.stdout); + jsonStr = jsonStr.substring(jsonStr.indexOf('{')); + jsonStr = jsonStr.replaceAll('\\', '/'); + jenkinsMap = getMapFromJson(jsonStr); + } else { + enable = false; + printer( + 'is not Mac/Windows/Linux. build must be execute in Mac/Windows os system', + Color.redStrong); + } + if (enable) { + await printer('Jenkins Variables', Color.green); + print(jsonStr); + } + jenkinsData = DataJenkins.fromJson(jenkinsMap!); + buildYaml = await getBuildData(); + + return simpleFuture; + } Future getBuildData() async { var c = Completer(); - var file = File('${_jenkinsData!.workspace}/build_data.conf'); + var file = File('${jenkinsData!.workspace}/build_data.conf'); var content = ''; if (Platform.isWindows) { content = file @@ -154,7 +181,7 @@ class Application { } } - var temp = File('${_jenkinsData!.workspace}/param_temp.bat'); + var temp = File('${jenkinsData!.workspace}/param_temp.bat'); var bat = ''; for (var param in params) { bat += '@echo %$param%\n'; diff --git a/lib/oto/defined_data.dart b/lib/oto/defined_data.dart index 94d387e..e549550 100644 --- a/lib/oto/defined_data.dart +++ b/lib/oto/defined_data.dart @@ -25,99 +25,10 @@ class FileData { } class TestData { - static Map get buildMap { - Map map = { - "common": {"workspace": ""}, - "commands": [] - }; + static String get buildYaml { + String yaml = ''; if (Platform.isMacOS) { - map = { - "common": { - "workspace": Directory.current.path.replaceAll('\\', '/'), - }, - "commands": [ - // { - // "command": "BuildDart", - // "param": {"targetPath": "./"} - // }, - // { - // "command": "BuildFlutter", - // "param": { - // "targetPath": "../desktop_service_frontend", - // "platform": BuildPlatform.Macos.toString() - // } - // }, - // { - // "command": "BuildFlutter", - // "param": { - // "targetPath": "../desktop_service_frontend", - // "platform": BuildPlatform.Web.toString() - // } - // }, - { - "command": "BuildDartCompile", - "param": { - "scm": "Git", - "name": "OTO", - "targetPath": "/Users/toki/works/dart/desktop_service_cli", - "targetDartFile": "./bin/desktop_service_cli.dart", - "buildFileName": "desktop_service_cli" - } - }, - // { - // "command": "Copy", - // "param": { - // "ignorePattern": [ - // ".git", - // ".dart_tool", - // ".gitignore", - // ".idea", - // "example" - // ], - // "isMove": false, - // "copyMap": { - // "/*": "/Users/toki/server/temp", - // // "../toki_flutter_framework/*": - // // "/Users/toki/server/toki_flutter_framework" - // } - // } - // }, - // { - // "command": "Zip", - // "param": { - // "zipFile": - // "/test_v_.zip", - // "zipList": [ - // "/bin/*", - // "/README.md" - // ] - // } - // }, - // { - // "command": "Upload", - // "param": { - // "sftp": false, - // "host": "toki-labs.com", - // "port": 215, - // "user": "spritex", - // "password": "Na2674126", - // "uploadMap": { - // "README.md": "/HDD1/WEB/_tmp", - // "bin/*": "/HDD1/WEB/_tmp/bin" - // } - // } - // }, - // { - // "command": "Shell", - // "param": { - // "workspace": "", - // "path": "/lib/shell/jenkins_env_params.sh" - // } - // } - ] - }; - - var json = jsonEncode(yaml.loadYaml(''' + yaml = ''' --- common: workspace: ${Directory.current.path} @@ -234,10 +145,9 @@ commands: # downloadURL: http://toki-labs.com/build/tplayer/android/tplayer_arm64_.apk # channel: release -''')); - map = jsonDecode(json); +'''; } else if (Platform.isWindows) { - var json = jsonEncode(yaml.loadYaml(''' + yaml = ''' --- common: workspace: ${Directory.current.path} @@ -354,170 +264,10 @@ commands: # downloadURL: http://toki-labs.com/build/tplayer/android/tplayer_arm64_.apk # channel: release -''')); - map = jsonDecode(json); +'''; + } else if (Platform.isLinux) {} - // map = { - // "common": {"workspace": Directory.current.path}, - // "commands": [ - // { - // "command": "CreateAppData", - // "param": { - // "scm": "Git", - // "relativeAssetPath": "assets/data", - // "returnVersion": "", - // "returnHash": "" - // } - // }, - // { - // "command": "BuildDart", - // "param": {"targetPath": "./"} - // }, - // { - // "command": "BuildFlutter", - // "param": { - // "targetPath": "../desktop_service_frontend", - // "platform": BuildPlatform.Windows.toString() - // } - // }, - // { - // "command": "BuildDartCompile", - // "param": { - // "targetPath": "d:\\work\\dart\\desktop_service_cli", - // "buildFileName": "desktop_service_cli" - // } - // }, - // { - // "command": "Copy", - // "param": { - // "ignorePattern": [], - // "isMove": false, - // "copyMap": { - // "/README.md": "/../temp", - // "/bin/*": "/../temp/bin" - // } - // } - // }, - // { - // "command": "Zip", - // "param": { - // "zipFile": - // "/test_v_.zip", - // "zipList": [ - // "/bin/*", - // "/README.md" - // ] - // } - // }, - // { - // "command": "Upload", - // "param": { - // "sftp": false, - // "host": "toki-labs.com", - // "port": 215, - // "user": "spritex", - // "password": "Na2674126", - // "uploadMap": { - // "README.md": "/HDD1/WEB/_tmp", - // "bin/*": "/HDD1/WEB/_tmp/bin" - // } - // } - // }, - // { - // "command": "Shell", - // "param": { - // "workspace": "", - // "path": "/lib/shell/jenkins_env_params.bat" - // } - // }, - // { - // "command": "ExecuteApp", - // "param": { - // "executable": - // "D:/Program Files/TokiSystemTrading/SystemTradingAPIApp.exe", - // "desktopServicePath": "D:/work/dart/toki_desktop_service" - // } - // } - // ] - // }; - } else if (Platform.isLinux) { - map = { - "common": {"workspace": Directory.current.path}, - "commands": [ - // { - // "command": "BuildDart", - // "param": {"targetPath": ""} - // }, - { - "command": "BuildDartCompile", - "param": { - "targetPath": "/home/deck/work/desktop_service_cli", - "targetDartFile": "./bin/desktop_service_cli.dart", - "buildFileName": "desktop_service_cli" - } - }, - // { - // "command": "BuildFlutter", - // "param": { - // "targetPath": "../desktop_service_frontend", - // "platform": BuildPlatform.Linux.toString() - // } - // }, - // { - // "command": "Copy", - // "param": { - // "ignorePattern": [ - // ".git", - // ".dart_tool", - // ".gitignore", - // ".idea", - // "example" - // ], - // "isMove": false, - // "copyMap": { - // "/*": "/home/deck/temp", - // // "../toki_flutter_framework/*": - // // "/Users/toki/server/toki_flutter_framework" - // } - // } - // }, - // { - // "command": "Zip", - // "param": { - // "zipFile": - // "/test_v_.zip", - // "zipList": [ - // "/bin/*", - // "/README.md" - // ] - // } - // }, - // { - // "command": "Upload", - // "param": { - // "sftp": false, - // "host": "toki-labs.com", - // "port": 215, - // "user": "spritex", - // "password": "Na2674126", - // "uploadMap": { - // "README.md": "/HDD1/WEB/_tmp", - // "bin/*": "/HDD1/WEB/_tmp/bin" - // } - // } - // }, - // { - // "command": "Shell", - // "param": { - // "workspace": "", - // "path": "/lib/shell/jenkins_env_params.sh" - // } - // } - ] - }; - } - - return map; + return yaml; } static Map? get jenkinsMap { @@ -536,7 +286,7 @@ commands: "buildDisplayName": "#54", "gitBranch": "origin/master", "buildTag": "jenkins-toki_os-54", - "buildData": buildMap + "buildData": buildYaml }; } else if (Platform.isWindows) { map = { @@ -552,7 +302,7 @@ commands: "buildDisplayName": "#18", "gitBranch": "origin/master", "buildTag": "jenkins-toki_system_trading-18", - "buildData": buildMap + "buildData": buildYaml }; } return map;