Example #1
0
        public async Task CreateShortcutsForExecutableAsync(SnapOsShortcutDescription shortcutDescription, ILog logger = null, CancellationToken cancellationToken = default)
        {
            if (shortcutDescription == null)
            {
                throw new ArgumentNullException(nameof(shortcutDescription));
            }
            var exeName = Filesystem.PathGetFileName(shortcutDescription.ExeAbsolutePath);

            if (Username == null)
            {
                _logger?.Error($"Unable to create shortcut because username is null. Executable: {exeName}");
                return;
            }

            logger?.Info($"Creating shortcuts for executable: {shortcutDescription.ExeAbsolutePath}");

            var autoStartEnabled = shortcutDescription.ShortcutLocations.HasFlag(SnapShortcutLocation.Startup);
            var desktopEnabled   = shortcutDescription.ShortcutLocations.HasFlag(SnapShortcutLocation.Desktop);
            var startMenuEnabled = shortcutDescription.ShortcutLocations.HasFlag(SnapShortcutLocation.StartMenu);

            var desktopShortcutUtf8Content = BuildDesktopShortcut(shortcutDescription, shortcutDescription.NuspecReader.GetDescription());

            if (desktopShortcutUtf8Content == null)
            {
                _logger?.Warn(
                    $"Unknown error while building desktop shortcut for exe: {exeName}. Distro: {DistroType}. Maybe unsupported distro?");
                return;
            }

            var applicationsDirectoryAbsolutePath = Filesystem.PathCombine($"/home/{Username}", ".local/share/applications");
            var autoStartDirectoryAbsolutePath    = Filesystem.PathCombine($"/home/{Username}", ".config/autostart");

            var autoStartShortcutAbsolutePath = Filesystem.PathCombine(autoStartDirectoryAbsolutePath, $"{exeName}.desktop");
            var desktopShortcutAbsolutePath   = Filesystem.PathCombine(applicationsDirectoryAbsolutePath, $"{exeName}.desktop");

            if (startMenuEnabled)
            {
                _logger?.Warn("Creating start menu shortcuts is not supported on this OS.");
            }

            if (autoStartEnabled)
            {
                if (Filesystem.DirectoryCreateIfNotExists(autoStartDirectoryAbsolutePath))
                {
                    _logger?.Info($"Created autostart directory: {autoStartDirectoryAbsolutePath}");
                }

                if (Filesystem.FileDeleteIfExists(autoStartShortcutAbsolutePath))
                {
                    _logger?.Info($"Deleted existing auto start shortcut: {autoStartShortcutAbsolutePath}");
                }

                _logger?.Info($"Creating autostart shortcut: {autoStartShortcutAbsolutePath}. " +
                              $"Absolute path: {shortcutDescription.ExeAbsolutePath}.");

                await Filesystem.FileWriteUtf8StringAsync(desktopShortcutUtf8Content,
                                                          autoStartShortcutAbsolutePath, cancellationToken);

                _logger?.Info($"Attempting to mark shortcut as trusted: {autoStartShortcutAbsolutePath}.");
                var trustedSuccess = await OsProcessManager.ChmodExecuteAsync(autoStartShortcutAbsolutePath, cancellationToken);

                _logger?.Info($"Shortcut marked as trusted: {(trustedSuccess ? "yes" : "no")}");
            }

            if (desktopEnabled)
            {
                if (!Filesystem.DirectoryExists(applicationsDirectoryAbsolutePath))
                {
                    _logger?.Error($"Applications directory does not exist. Desktop shortcut will not be created. Path: {applicationsDirectoryAbsolutePath}");
                    goto next;
                }

                if (Filesystem.FileDeleteIfExists(desktopShortcutAbsolutePath))
                {
                    _logger?.Info($"Deleted existing shortcut: {desktopShortcutAbsolutePath}");
                }

                _logger?.Info($"Creating desktop shortcut: {desktopShortcutAbsolutePath}. " +
                              $"Auto startup: {autoStartEnabled}. " +
                              $"Absolute path: {shortcutDescription.ExeAbsolutePath}.");

                await Filesystem.FileWriteUtf8StringAsync(desktopShortcutUtf8Content,
                                                          desktopShortcutAbsolutePath, cancellationToken);

                _logger?.Info($"Attempting to mark shortcut as trusted: {desktopShortcutAbsolutePath}.");
                var trustedSuccess = await OsProcessManager.ChmodExecuteAsync(desktopShortcutAbsolutePath, cancellationToken);

                _logger?.Info($"Shortcut marked as trusted: {(trustedSuccess ? "yes" : "no")}");
            }

            next :;
        }