diff --git a/.vscode/launch.json b/.vscode/launch.json index 3aeda50..1046394 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -16,7 +16,7 @@ "request": "launch", "type": "dart", "program": "./bin/oto_cli.dart", - "args": ["install"] + "args": ["uninstall"] }, { "name": "oto_cli_win", diff --git a/lib/cli/cli.dart b/lib/cli/cli.dart index 21b9001..5ca718f 100644 --- a/lib/cli/cli.dart +++ b/lib/cli/cli.dart @@ -175,7 +175,7 @@ abstract class Printer { var time = '${now.hour}${now.minute}${now.second}${now.millisecond}${now.microsecond}'; var tempFile = - File(path.join(Directory.current.path, 'temp_$time.$extension')); + File(path.join(Directory.systemTemp.path, 'temp_$time.$extension')); await tempFile.writeAsString(content); c.complete(tempFile); return c.future; diff --git a/lib/cli/commands/command_const.dart b/lib/cli/commands/command_const.dart index 281358e..107d8bb 100644 --- a/lib/cli/commands/command_const.dart +++ b/lib/cli/commands/command_const.dart @@ -21,6 +21,7 @@ class CommandConstant { class _CommandConstantBase { String get installPath { + var userPath = Platform.environment['HOME']; return ''; } @@ -32,12 +33,13 @@ class _CommandConstantBase { class _CommandConstantOSX extends _CommandConstantBase { @override String get installPath { - return ''; + var userPath = Platform.environment['HOME']; + return '$userPath/Applications/${CLI.serviceName}/'; } @override String get installFilePath { - return ''; + return '$installPath${CLI.serviceName}'; } } diff --git a/lib/cli/commands/command_install.dart b/lib/cli/commands/command_install.dart index 1510e47..6e0bbcd 100644 --- a/lib/cli/commands/command_install.dart +++ b/lib/cli/commands/command_install.dart @@ -2,6 +2,7 @@ import 'dart:async'; import 'dart:io'; import 'package:dart_framework/utils/os_startup.dart'; +import 'package:dart_framework/utils/system_util.dart'; import 'package:oto_cli/cli/cli.dart'; import 'package:oto_cli/cli/commands/command_base.dart'; import 'package:oto_cli/cli/commands/command_const.dart'; @@ -13,7 +14,6 @@ class CommandInstall extends CommandBase { @override Future execute(List parameters) async { - var c = Completer(); var label = CLI.serviceName; if (!await OSStartup.isRegistedStartup(label)) { var installPath = CommandConstant.OS!.installPath; @@ -39,7 +39,7 @@ class CommandInstall extends CommandBase { printOut('Installation failed because it is already installed.', PrintType.error); } - return c.future; + return simpleFuture; } @override diff --git a/lib/cli/commands/install/regist_path.dart b/lib/cli/commands/install/regist_path.dart index 8bb2205..19d03cd 100644 --- a/lib/cli/commands/install/regist_path.dart +++ b/lib/cli/commands/install/regist_path.dart @@ -28,18 +28,76 @@ class RegistPath { } } +/// ************* MAC *************** class _RegistPathOSX extends RegistPath { + String getTargetPath(String targetPath) { + return 'export PATH=\${PATH}:$targetPath'; + } + + String get zprofile { + var userPath = Platform.environment['HOME']; + return '$userPath/.zprofile'; + } + + String get bprofile { + var userPath = Platform.environment['HOME']; + return '$userPath/.bash_profile'; + } + @override Future add(String path) async { - return null; + await addProfile(zprofile, path); + await addProfile(bprofile, path); + await ProcessExecutor.run( + StringBuffer('source $zprofile && source $bprofile')); + return simpleFuture; + } + + Future addProfile(String profilePath, String targetPath) async { + var file = File(profilePath); + var contents = ''; + if (file.existsSync()) { + contents = await file.readAsString(); + } else { + file.createSync(); + } + if (contents.lastIndexOf('\n') != contents.length - 1) { + contents += '\n'; + } + var addItem = getTargetPath(targetPath); + if (!contents.contains(addItem)) { + contents += addItem; + } + + await file.writeAsString(contents, flush: true); + return simpleFuture; } @override Future remove(String path) async { - return null; + await removeProfile(zprofile, path); + await removeProfile(bprofile, path); + await ProcessExecutor.run( + StringBuffer('source $zprofile && source $bprofile')); + return simpleFuture; + } + + Future removeProfile(String profilePath, String targetPath) async { + var file = File(profilePath); + var contents = ''; + if (file.existsSync()) { + contents = await file.readAsString(); + var removeItem = getTargetPath(targetPath); + if (contents.contains(removeItem)) { + contents = contents.replaceAll(removeItem, ''); + await file.writeAsString(contents, flush: true); + } + } + return simpleFuture; } } +/// ************* LINUX *************** class _RegistPathLinux extends RegistPath { @override Future add(String path) async { @@ -52,6 +110,7 @@ class _RegistPathLinux extends RegistPath { } } +/// ************* WINDOWS *************** class _RegistPathWindows extends RegistPath { @override Future add(String path) async {