Commit c5f6e021 authored by Michael Pyne's avatar Michael Pyne

Merge branch 'kogiokka/kdesrc-build-first-run' into master

Closes !55, which has diverged from master since I've been able to
approve it.
parents f627779e f1bb8066
Pipeline #29239 passed with stage
in 2 minutes and 13 seconds
......@@ -29,27 +29,33 @@ environments as fielded in "minimal Docker container" forms of popular distros.
=cut
use constant {
SHELL_RC_SNIPPET => <<'RC'
# kdesrc-build ##################################################
## Add kdesrc-build to PATH
export PATH="$HOME/kde/src/kdesrc-build:$PATH"
## Run projects built with kdesrc-build
function kdesrc-run
{
source "$HOME/kde/build/$1/prefix.sh" && "$HOME/kde/usr/bin/$@"
}
#################################################################
RC
};
sub setupUserSystem
{
my $baseDir = shift;
my $os = ksb::OSSupport->new;
my %shellRcMap = (
'bash' => '.bashrc',
'zsh' => '.zshrc',
);
my $envShell = $ENV{'SHELL'} // '/bin/bash';
my $envShell = $ENV{'SHELL'} // 'undefined';
my $shellName = (split '/', $envShell)[-1];
my $rcName = $shellRcMap{$shellName};
if (!defined($rcName)) {
$rcName = '.bashrc';
say " - $envShell is not supported. Changes will go to ~/.bashrc.";
}
eval {
_installSystemPackages($os);
_setupBaseConfiguration($baseDir);
_setupShellRcFile($rcName);
_setupShellRcFile($shellName);
};
if (had_an_exception($@)) {
......@@ -155,14 +161,18 @@ sub _getNumCoresForLowMemory
sub _setupBaseConfiguration
{
my $baseDir = shift;
my @knownLocations = ("$ENV{PWD}/kdesrc-buildrc", "$ENV{HOME}/.kdesrc-buildrc");
my $locatedFile = first { -e $_ } @knownLocations;
if (-e "kdesrc-buildrc" || -e "$ENV{HOME}/.kdesrc-buildrc") {
if (defined $locatedFile) {
print colorize(<<DONE);
b[-] You b[y[already have a configuration file].
b[*] You already have a configuration file: b[y[$locatedFile].
DONE
} else {
return;
}
print colorize(<<DONE);
b[-] Installing b[sample configuration file]...
b[*] Creating b[sample configuration file]: b[y["$ENV{HOME}/.kdesrc-buildrc"]...
DONE
my $sampleRc = $packages{'sample-rc'} or
......@@ -183,47 +193,47 @@ DONE
close $sampleFh
or _throw("Error closing ~/.kdesrc-buildrc: $!");
}
}
sub _setupShellRcFile
{
my $rc = shift;
my $rcFilepath = "$ENV{HOME}/$rc";
my $rcModified = 0;
my $tag = '# auto-generated by kdesrc-build initial setup: do not remove!';
# Add kdesrc-build path to PATH if not already in there
# Create kdesrc-run alias for more convenient program execution
if (!ksb::Util::fileHasLine($rcFilepath, $tag)) {
say colorize(<<DONE);
b[-] Amending your ~/$rc
b[-] Add b[y[kdesrc-build] directory into PATH
b[-] Create b[y[kdesrc-run] alias
DONE
open(my $rcFh, '>>', "$rcFilepath") or _throw("Couldn't open ~/$rc: $!");
my $shellName = shift;
my $rcFilepath = undef;
my $isAuto = 1;
if ($shellName eq 'bash') {
$rcFilepath = "$ENV{'HOME'}/.bashrc";
} elsif ($shellName eq 'zsh') {
if (defined $ENV{'ZDOTDIR'}) {
$rcFilepath = "$ENV{'ZDOTDIR'}/.zshrc";
} else {
$rcFilepath = "$ENV{'HOME'}/.zshrc";
}
} else {
say colorize(" b[*] Updating rc-file for shell 'y[b[$shellName]' is not supported.");
$isAuto = 0;
}
say $rcFh '';
say $rcFh "$tag";
say $rcFh '# Add the kdesrc-build directory to the path';
say $rcFh 'export PATH="$HOME/kde/src/kdesrc-build:$PATH"';
say $rcFh '# Create alias for running software built with kdesrc-build';
say $rcFh 'kdesrc-run ()';
say $rcFh '{';
say $rcFh ' source "$HOME/kde/build/$1/prefix.sh" && "$HOME/kde/usr/bin/$@"';
say $rcFh '}';
$rcModified = 1;
if (defined $rcFilepath) {
$isAuto = ksb::Util::yesNoPrompt(colorize(" b[*] Update your b[y[$rcFilepath]?"));
}
if ($rcModified) {
if ($isAuto) {
open(my $rcFh, '>>', "$rcFilepath") or _throw("Couldn't open $rcFilepath: $!");
say $rcFh '';
say $rcFh SHELL_RC_SNIPPET;
close($rcFh);
say colorize(<<DONE);
b[-] Your b[y[~/$rc has been successfully setup].
- Added b[y[kdesrc-build] directory into PATH
- Added b[y[kdesrc-run] shell function
b[*] b[g[Shell rc-file is successfully setup].
DONE
} else {
say '';
say colorize(<<DONE);
b[-] Your b[y[~/$rc is already setup].
b[*] You can manually configure your shell rc-file with the snippet below:
DONE
say SHELL_RC_SNIPPET;
}
}
......
......@@ -701,4 +701,13 @@ sub fileHasLine
return 0;
}
sub yesNoPrompt {
my $msg = shift;
local $| = 1;
print "$msg (y/N) ";
chomp(my $answer = <STDIN>);
return lc($answer) eq 'y';
}
1;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment