Skip to content

Commit

Permalink
feat(shortcut): add support for removing shortcuts
Browse files Browse the repository at this point in the history
This commit adds a new flag to the `ShortcutCommand` class, `--remove` or `-r`, which allows the user to remove a shortcut. If this flag is present, the `Shortcut` class will call the `_delete` method instead of the `_create` method.

Additionally, the `Shortcut` class now has a `_delete` method that deletes the shortcut. This method is implemented for Linux-based systems and Windows, but not for macOS.

Finally, the `Shortcut` class now imports the `path` package, which was previously missing.
  • Loading branch information
insign committed May 15, 2023
1 parent 252011a commit 7949dca
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 6 deletions.
18 changes: 14 additions & 4 deletions lib/commands/devs/shortcut.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class ShortcutCommand extends Command {
ShortcutCommand() {
argParser.addOption("name", abbr: "n", help: "Name of the application", valueHelp: 'name', mandatory: true);

argParser.addOption("path", abbr: "p", help: "Path of the executable", valueHelp: 'path', mandatory: true);
argParser.addOption("path", abbr: "p", help: "Path of the executable", valueHelp: 'path');

argParser.addOption("icon", abbr: "i", help: "Name or path of the icon", valueHelp: 'name|path');

Expand All @@ -27,16 +27,20 @@ class ShortcutCommand extends Command {
abbr: "c", help: "Categories, multiple times or once using comma", valueHelp: 'category[,category2]');

argParser.addFlag('sudo', abbr: 's', help: 'Run as sudo', negatable: true, defaultsTo: true);

// Remove shortcut flag
argParser.addFlag('remove', abbr: 'r', help: 'Remove shortcut', negatable: false);
}

@override
void run() async {
final String name = argResults!['name'];
final String executablePath = argResults!['path'];
final String? executablePath = argResults!['path'];
final String? icon = argResults!['icon'];
final String? description = argResults!['description'];
final List<String> category = argResults!['category'];
final bool sudo = argResults!['sudo'];
final bool remove = argResults!['remove'];

var shortcut = Shortcut(
name: name,
Expand All @@ -46,9 +50,15 @@ class ShortcutCommand extends Command {
categories: category.join(';'),
sudo: sudo);

await shortcut.create();
if (remove) {
await shortcut.delete();

Logger.info("Shortcut removed.");
} else {
await shortcut.create();

Logger.info("Shortcut created successfully!");
Logger.info("Shortcut created.");
}
exit(success);
}
}
71 changes: 69 additions & 2 deletions lib/os/shortcut.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import 'dart:io';

import 'package:path/path.dart';
import 'package:process_run/shell.dart';
import 'package:xpm/os/run.dart';
import 'package:xpm/xpm.dart';

/// A class that represents a shortcut to an executable file.
/// This class is used to create shortcuts on the system.
/// Currently, only Linux, macOS and Windows are supported.
///
class Shortcut {
final String name;
final String executablePath;
final String? executablePath;
final String? icon;
final String? comment;
final bool? terminal;
Expand All @@ -18,7 +22,7 @@ class Shortcut {

Shortcut({
required this.name,
required this.executablePath,
this.executablePath,
this.icon,
this.comment,
this.terminal = false,
Expand All @@ -31,7 +35,13 @@ class Shortcut {
String home = XPM.userHome.path;

/// Creates the shortcut.
///
/// Returns the path of the shortcut.
Future<String> create() async {
if (executablePath == null) {
throw ArgumentError.notNull('executablePath');
}

if (Platform.isMacOS) {
return _createMacOSShortcut();
} else if (Platform.isWindows) {
Expand All @@ -41,7 +51,22 @@ class Shortcut {
return await _createLinuxShortcut();
}

/// Deletes the shortcut.
///
/// Returns true if the shortcut was deleted successfully, false otherwise.
Future<bool> delete() async {
if (Platform.isMacOS) {
return _deleteMacOSShortcut();
} else if (Platform.isWindows) {
return _deleteWindowsShortcut();
}

return await _deleteLinuxShortcut();
}

/// Creates a shortcut on a Linux-based system.
///
/// Returns the path of the shortcut.
Future<String> _createLinuxShortcut() async {
final runner = Run();
final dest = destination ?? '/usr/share/applications';
Expand Down Expand Up @@ -70,6 +95,7 @@ class Shortcut {
}

/// Creates a shortcut on a macOS system.
/// @FIXME This function was not tested.
Future<String> _createMacOSShortcut() async {
final linkPath = "$home/Desktop/$name.lnk";
final runner = Run();
Expand All @@ -80,6 +106,7 @@ class Shortcut {
}

/// Creates a shortcut on a Windows system.
/// @FIXME This function was not tested.
Future<String> _createWindowsShortcut() async {
final linkPath = "$home/Desktop/$name.lnk";
final command = "cmd /c "
Expand All @@ -97,4 +124,44 @@ class Shortcut {

return linkPath;
}

/// Deletes a shortcut on a Linux-based system.
///
/// Returns true if the shortcut was deleted successfully, false otherwise.
Future<bool> _deleteLinuxShortcut() async {
final runner = Run();
final dest = destination ?? '/usr/share/applications';
final filePath = join(dest, '$name.desktop');
final file = File(filePath);

if (await file.exists()) {
await runner.delete(filePath, sudo: sudo, force: true);
}

return true;
}

/// Deletes a shortcut on a macOS system.
/// @FIXME This function was not tested.
Future<bool> _deleteMacOSShortcut() async {
final linkPath = join(home, 'Desktop', '$name.lnk');
final runner = Run();
final command = "rm $linkPath";
await runner.simple('ln', command.split(' '));

return true;
}

/// Deletes a shortcut on a Windows system.
/// @FIXME This function was not tested.
Future<bool> _deleteWindowsShortcut() async {
final linkPath = join(home, 'Desktop', '$name.lnk');
final command = "cmd /c del $linkPath";

final Shell shell = Shell();

await shell.run(command);

return true;
}
}

0 comments on commit 7949dca

Please sign in to comment.