dcli
  • Introduction
  • What does DCli do?
  • Install DCli
    • Installing on Windows
  • Writing your first CLI app
  • Add DCli to your project
  • pub.dev
  • github
  • Dart basics
    • Dart lambda functions
    • Function Arguments
    • Futures
    • stdin/stdout/stderr a primer
  • Tour
    • Overview
    • Using DCli functions
    • User input
      • Ask Validators
    • Displaying information
    • Managing Files And Directories
    • Environment variables
    • Calling apps
    • Redirecting output
    • Command Line Arguments
    • Paths
    • Glob Expansion
    • Piping
    • Locking
    • Fetch
    • The evils of CD
    • Assets/Resources
    • Cross Platform
      • Posix
      • Windows
      • Docker
        • Detecting Docker
        • Add DCli to a Docker Container
        • Example DCli app in Docker
  • Elevated Privileges
    • Sudo
  • Performance
  • Dependency Management
    • Dependency Management
    • Pubspec Managment
  • DCli Tools
    • DCli tools
    • Use a shebang #!
    • DCli Compile
    • DCli Clean
    • DCli Create
    • DCli Doctor
    • DCli Install
    • DCli Run
    • DCli Warmup
    • DCli Pack
    • Upgrade DCli
  • Internal Workings
    • Internal Workings
    • waitForEx
  • Contributing
    • Creating a release
    • Running Unit tests
    • Implemention support for a shell
    • Templates
  • References
  • Examples
    • Projects
    • Code
      • hello world.
      • dcompress
      • dpath
      • dmysql
      • dshell
      • dwhich
      • dipaddr
      • gnome launcher
  • Articles
    • build CLI apps in dart - part 1
    • build CLI apps in dart - part 2
    • Dealing with permissions
    • 3rd Party console packages
  • Dart on Linux - the perfect CLI tooling
  • Improving your build environment
    • Existing tooling
    • Building with Dart
    • A home for your build tools
  • Olivier Revial - CLI apps made easy
  • Video: package of the week
Powered by GitBook
On this page

Was this helpful?

  1. Examples
  2. Code

gnome launcher

gnome launcher

The following dcli script creates a gnome launcher. You can use this to launch any dcli script (or any app in general) from the gnome menu.

#! /usr/bin/env dcli

import 'dart:io';
import 'package:dcli/dcli.dart';

String name;
List<String> categories;
bool terminal;
String comment;
String iconPath;
String exePath;

var parser = ArgParser();

///
/// Creates a gnome launcher.
///
void main(List<String> args) {
  Settings().setVerbose(enabled: false);

  parser.addOption('name',
      help: 'Name of the application to be displayed in the gnome menu');

  parser.addMultiOption('categories',
      defaultsTo: ['Development'],
      help:
          'Gnome list of categories. Controls where in the gnome menu the entry appears. e.g. \'Development\'');

  parser.addOption('terminal',
      defaultsTo: 'true',
      help:
          'If true (the default) then the app is launched in a terminal window.');

  parser.addOption('comment', help: 'Adds a comment to the Gnome menu item');

  parser.addOption('iconPath', help: 'Path to an icon for the Gnome menu');
  parser.addOption('exePath', help: 'Path to an executable to be run');

  var parsed = parser.parse(args);

  name = getRequiredString(parsed, 'name');
  comment = parsed['comment'] as String;
  terminal = getBool(parsed, 'terminal');
  categories = getList(parsed, 'categories');
  iconPath = getPath(parsed, 'iconPath');
  exePath = getRequiredPath(parsed, 'exePath');

  writeDesktopEntry();
}

String getRequiredPath(ArgResults parsed, String option) {
  var path = getRequiredString(parsed, option);

  if (!exists(path)) {
    print(red('The provided path for $option does not exists.'));
    showUsage();
  }

  return truepath(path);
}

String getPath(ArgResults parsed, String option) {
  if (!parsed.wasParsed(option)) {
    return null;
  }

  var path = parsed[option] as String;

  if (!exists(path)) {
    print(red('The provided path for $option does not exists.'));
    showUsage();
  }

  return truepath(path);
}

String getRequiredString(ArgResults parsed, String option) {
  if (!parsed.wasParsed(option)) {
    print(red('You must provide the --$option argument'));
    showUsage();
  }
  return parsed[option] as String;
}

bool getRequiredBool(ArgResults parsed, String option) {
  if (!parsed.wasParsed(option)) {
    print(red('You must provide the --$option argument'));
    showUsage();
  }
  return parsed[option] == 'true';
}

bool getBool(ArgResults parsed, String option) {
  return parsed[option] == 'true';
}

List<String> getRequiredList(ArgResults parsed, String option) {
  if (!parsed.wasParsed(option)) {
    print(red('You must provide the --$option argument'));
    showUsage();
  }

  return parsed[option] as List<String>;
}

List<String> getList(ArgResults parsed, String option) {
  return parsed[option] as List<String>;
}

void showUsage() {
  print('');
  print(green('Usage:'));
  print('./add_gnome_laucher.dart [options]');
  print(parser.usage);
  exit(-1);
}

void writeDesktopEntry() {
  var path = join(HOME, '.local', 'share', 'applications',
      '${name.replaceAll(RegExp('[^a-zA-Z0-9_]'), '_')}.desktop');
  // create desktop.ini
  if (exists(path)) {
    delete(path);
  }

  var content = StringBuffer();

  content.write('''
[Desktop Entry]
Version=1.0
Type=Application
Name=$name
''');

  if (comment != null) content.write('Comment=$comment\n');

  content.write('Categories=${categories.join(';')}\n');

  content.write('Terminal=$terminal\n');

  if (iconPath != null) content.write('Icon=$iconPath\n');

  if (terminal) {
    content.write('''gnome-terminal -e 'bash -c "$exePath;bash"' ''');
  } else {
    content.write('Exec=$exePath\n');
  }

  path.write(content.toString());

  print('Created: (in ${truepath(path)})\n');

  cat(path);
}
PreviousdipaddrNextbuild CLI apps in dart - part 1

Last updated 2 years ago

Was this helpful?