- Copy all used dart_framework files into lib/framework/ - Replace all package:dart_framework/ imports with package:oto_cli/framework/ - Add yaml, archive, ftpconnect as explicit direct dependencies - Remove dart_framework git dependency from pubspec.yaml Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
152 lines
No EOL
4.1 KiB
Dart
152 lines
No EOL
4.1 KiB
Dart
import 'dart:io';
|
|
|
|
import 'package:oto_cli/framework/platform/process.dart';
|
|
import 'package:oto_cli/framework/utils/system_util.dart';
|
|
|
|
class RegistPath {
|
|
static RegistPath? _instance;
|
|
// ignore: library_private_types_in_public_api, non_constant_identifier_names
|
|
static RegistPath? get OS {
|
|
if (_instance == null) {
|
|
if (Platform.isMacOS) {
|
|
_instance = _RegistPathOSX();
|
|
} else if (Platform.isLinux) {
|
|
_instance = _RegistPathLinux();
|
|
} else {
|
|
_instance = _RegistPathWindows();
|
|
}
|
|
}
|
|
return _instance;
|
|
}
|
|
|
|
Future add(String path) async {
|
|
return await _instance?.add(path);
|
|
}
|
|
|
|
Future remove(String path) async {
|
|
return await _instance?.remove(path);
|
|
}
|
|
}
|
|
|
|
/// ************* 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 {
|
|
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 {
|
|
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 {
|
|
return null;
|
|
}
|
|
|
|
@override
|
|
Future remove(String path) async {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/// ************* WINDOWS ***************
|
|
class _RegistPathWindows extends RegistPath {
|
|
var setUserPath = '''
|
|
@echo off
|
|
set "PathUser="
|
|
for /F "skip=2 tokens=1,2*" %%G in ('%SystemRoot%\\System32\\reg.exe query "HKCU\\Environment" /v "Path" 2^>nul') do if /I "%%G" == "Path" (
|
|
if /I "%%H" == "REG_EXPAND_SZ" (set "PathExpand=1" & set "PathUser=%%I") else if /I "%%H" == "REG_SZ" set "PathUser=%%I"
|
|
)
|
|
''';
|
|
|
|
@override
|
|
Future add(String path) async {
|
|
var shell = StringBuffer(setUserPath);
|
|
shell.writeln('setx path "%PathUser%;$path');
|
|
await ProcessExecutor.run(shell);
|
|
return simpleFuture;
|
|
}
|
|
|
|
@override
|
|
Future remove(String path) async {
|
|
var userPath = Platform.environment['UserProfile'];
|
|
var pathTxt = File('$userPath\\path.txt');
|
|
var shell = StringBuffer(setUserPath);
|
|
shell.writeln('echo "%PathUser%" > ${pathTxt.path}');
|
|
await ProcessExecutor.run(StringBuffer(shell));
|
|
|
|
await Future.delayed(Duration(seconds: 1));
|
|
|
|
if (pathTxt.existsSync()) {
|
|
var contents = pathTxt.readAsStringSync();
|
|
contents = contents.replaceAll(';$path', '');
|
|
contents = contents.trimLeft();
|
|
contents = contents.trimRight();
|
|
print('New User Path =====> $contents');
|
|
await ProcessExecutor.run(StringBuffer('setx path "$contents"'));
|
|
pathTxt.deleteSync();
|
|
}
|
|
return simpleFuture;
|
|
}
|
|
} |