Warning, /sdk/kdesrc-build/kdesrc-run is written in an unsupported language. File is not indexed.

0001 #!/usr/bin/env perl
0002 
0003 =pod
0004 
0005 =encoding UTF-8
0006 
0007 =head1 NAME
0008 
0009 kdesrc-run - Run KDE applications built with kdesrc-build.
0010 
0011 =head1 SYNOPSIS
0012 
0013 kdesrc-run [options] <module-name> [arguments]
0014 
0015 =head1 OPTIONS
0016 
0017   -e, --exec <program>    Specify program of the module. Default to module name.
0018   -f, --fork              Launch the program in a new session.
0019   -q, --quiet             Do not print run information.
0020   -h, --help              Print usage information and exit.
0021   --list-installed        Print installed modules and exit.
0022 
0023 =head1 EXAMPLES
0024 
0025 B<kdesrc-run -f kate -l 5 file1.txt>
0026 
0027   Launch kate in a new session with '-l 5 file1.txt' arguments.
0028 
0029 B<kdesrc-run -e kate-syntax-highlighter kate --list-themes>
0030 
0031   Launch kate-syntax-highlighter of module kate with '--list-themes' argument.
0032 
0033 =cut
0034 
0035 use 5.014;
0036 use strict;
0037 use warnings;
0038 use autodie;
0039 
0040 use Pod::Usage;
0041 use Getopt::Long qw(:config pass_through require_order);
0042 use JSON::PP;
0043 use List::Util qw(first);
0044 
0045 binmode STDOUT, 'encoding(UTF-8)';
0046 binmode STDERR, 'encoding(UTF-8)';
0047 
0048 our $optExec;
0049 our $optFork          = 0;
0050 our $optHelp          = 0;
0051 our $optQuiet         = 0;
0052 our $optListInstalled = 0;
0053 
0054 GetOptions(
0055     'exec|e=s'       => \$optExec,
0056     'fork|f'         => \$optFork,
0057     'help|h'         => sub { pod2usage(0); },
0058     'quiet|q'        => \$optQuiet,
0059     'list-installed' => \$optListInstalled,
0060 );
0061 
0062 if ($#ARGV == -1 && not $optListInstalled) {
0063     pod2usage(0);
0064 }
0065 
0066 my $module = shift @ARGV;
0067 my $exec   = $optExec // $module;
0068 
0069 # According to XDG spec, if $XDG_STATE_HOME is not set, then we should default
0070 # to ~/.local/state
0071 my $xdgStateHome = $ENV{XDG_STATE_HOME} // "$ENV{HOME}/.local/state";
0072 my $dataFileName = "kdesrc-build-data";
0073 my @possibleDataPaths = ("./.$dataFileName", "$xdgStateHome/$dataFileName");
0074 my $buildDataFile = first { -e $_ } (@possibleDataPaths);
0075 
0076 if (not defined $buildDataFile) {
0077     say qq("$dataFileName" file is not available. Exit now.);
0078     exit 1;
0079 }
0080 
0081 my $buildData = do {
0082     open my $fh, '<', $buildDataFile;
0083     local $/ = undef;
0084     decode_json(<$fh>);
0085 };
0086 
0087 if ($optListInstalled) {
0088     say foreach sort { $a cmp $b }
0089         grep { defined $buildData->{$_}{'install-dir'} } keys %$buildData;
0090     exit;
0091 }
0092 
0093 if (not defined $buildData->{$module}) {
0094     say qq(Module "$module" has not been built yet.);
0095     exit 1;
0096 }
0097 
0098 my $buildDir   = $buildData->{$module}{'build-dir'};
0099 my $installDir = $buildData->{$module}{'install-dir'};
0100 my $revision   = $buildData->{$module}{'last-build-rev'};
0101 my $execPath   = "$installDir/bin/$exec";
0102 
0103 if (not -e $execPath) {
0104     say qq(Program "$exec" does not exist.);
0105     say qq(Try to set executable name with -e option.);
0106     exit 127;    # Command not found
0107 }
0108 
0109 # Most of the logic is done by Perl, so the shell script here should be POSIX
0110 # compliant. Consider using ShellCheck to make sure of that.
0111 my $script = qq{
0112     #!/bin/sh
0113 
0114     # Set up environment variables (dot command).
0115     . "$buildDir/prefix.sh";
0116 
0117     # Launch the program with optional arguments.
0118     if [ "$optFork" = 1 ]; then
0119         setsid -f "$execPath" "\$@";
0120     else
0121         "$execPath" "\$@";
0122     fi;
0123 };
0124 
0125 # Print run information
0126 if (not $optQuiet) {
0127     print '#' x 80, "\n";
0128     print ' ' x 35, 'kdesrc-run', "\n";
0129     say "Module:             $module";
0130     say "Program:            $exec";
0131     say "Revision:           $revision";
0132     say "Arguments:          @ARGV";
0133     print '#' x 80, "\n";
0134     print "\n";
0135 }
0136 
0137 # Instead of embedding @ARGV in shell script with string interpolation, pass
0138 # them as arguments of the script. Let the shell handle the list through "$@",
0139 # so it will do the quoting on each one of them.
0140 #
0141 # Run the script with sh options specification:
0142 #     sh -c command_string command_name $1 $2 $3...
0143 exec('/bin/sh', '-c', $script, $exec, @ARGV);