File Coverage

blib/lib/App/Rakubrew/Shell/Cmd.pm
Criterion Covered Total %
statement 29 51 56.8
branch 1 6 16.6
condition 0 6 0.0
subroutine 10 16 62.5
pod 0 7 0.0
total 40 86 46.5


line stmt bran cond sub pod time code
1             package App::Rakubrew::Shell::Cmd;
2 1     1   8 use App::Rakubrew::Shell;
  1         2  
  1         96  
3             our @ISA = "App::Rakubrew::Shell";
4 1     1   7 use strict;
  1         2  
  1         29  
5 1     1   5 use warnings;
  1         2  
  1         77  
6 1     1   22 use 5.010;
  1         4  
7              
8 1     1   6 use Encode::Locale qw(env);
  1         3  
  1         76  
9              
10 1     1   5 use App::Rakubrew::Variables;
  1         3  
  1         213  
11 1     1   7 use App::Rakubrew::Tools;
  1         2  
  1         107  
12 1     1   7 use App::Rakubrew::VersionHandling;
  1         3  
  1         196  
13 1     1   10 use App::Rakubrew::Build;
  1         3  
  1         2046  
14              
15             # https://superuser.com/a/302553
16              
17             sub supports_hooking {
18 0     0 0 0 my $self = shift;
19 0         0 1;
20             }
21              
22             sub install_note {
23             # The autorun guard to prevent endless loops is based on this StackOverflow
24             # answer: https://stackoverflow.com/a/57451662/1975049
25              
26 1     1 0 1054 my $text = <
27             To load $brew_name in CMD automatically you have to do two things:
28              
29             1. Check that you don't already have a CMD autorun script set.
30              
31             reg query "HKCU\\Software\\Microsoft\\Command Processor" /v AutoRun
32              
33             If you don't have an autorun script set (the above command returns an error) you can set one using:
34              
35             reg add "HKCU\\Software\\Microsoft\\Command Processor" /v AutoRun /t REG_EXPAND_SZ /d \\""\%"USERPROFILE"\%\\Documents\\CMD_profile.cmd"\\" /f
36              
37             2. Add the following code to the end of the autorun script you linked in step 1:
38              
39             \@echo off
40             setlocal EnableDelayedExpansion
41             set "cmd=!cmdcmdline!"
42             if "!cmd!" == "!cmd:/=!" (
43             endlocal
44             FOR /f "delims=" \%\%i in ('"$brew_exec" init Cmd') do \@\%\%i
45             )
46              
47             You can easily do that from a CMD prompt using the following command:
48              
49             (
50             echo \@echo off
51             echo setlocal EnableDelayedExpansion
52             echo set "cmd=!cmdcmdline!"
53             echo if "!cmd!" == "!cmd:/=!" ^(
54             echo endlocal
55             echo FOR /f "delims=" \%\%i in ^('"$brew_exec" init Cmd'^) do \@\%\%i
56             echo ^)
57             ) >> "\%USERPROFILE\%\\Documents\\CMD_profile.cmd"
58              
59             If you use a different autorun script location, replace the path in the command above.
60              
61             (Note that the above does *not* enable auto-loading in PowerShell, that needs a
62             separate installation procedure. Call `$brew_exec init` in a PowerShell window
63             for respective installation instructions.)
64             EOT
65              
66 1 50       8 if ($prefix =~ / /) {
67 0         0 $text .= <
68              
69             =================================== WARNING ==================================
70              
71             rakubrews home directory is currently
72              
73             $prefix
74              
75             That folder contains spaces. This will break building rakudos as the build
76             system currently doesn't work in such a path. You can work around this problem
77             by changing that folder to a directory without spaces. Do so by putting
78              
79             set RAKUBREW_HOME=/some/folder/without/space/rakubrew
80              
81             in your profile file *before* the other code.
82             EOW
83             }
84 1         25 return $text;
85             }
86              
87             sub get_init_code {
88 0     0 0   my $self = shift;
89 0           my $path = env('PATH');
90 0           $path = $self->clean_path($path);
91 0 0         if (get_brew_mode() eq 'env') {
92 0           my $version = get_global_version();
93 0 0 0       if ($version && $version ne 'system' && !is_version_broken($version)) {
      0        
94 0           $path = join(';', get_bin_paths($version), $path);
95             }
96             }
97             else { # get_brew_mode() eq 'shim'
98 0           $path = join(';', $shim_dir, $path);
99             }
100              
101             # https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/doskey
102             # https://devblogs.microsoft.com/oldnewthing/20120731-00/?p=7003
103             # The command that post_call_eval() returns is always a single line, so we can get away with having an empty delimiter.
104             # The second for is there to not error on empty lines: https://stackoverflow.com/a/31316333
105 0           return <
106             SET PATH=$path
107             doskey rakubrew="$brew_exec" internal_hooked Cmd \$* && FOR /f "delims=" \%i in ('"$brew_exec" internal_shell_hook Cmd post_call_eval \$*') do \@\%i
108             EOT
109             }
110              
111             sub post_call_eval {
112 0     0 0   my $self = shift;
113 0           $self->print_shellmod_code(@_);
114             }
115              
116             sub get_path_setter_code {
117 0     0 0   my $self = shift;
118 0           my $path = shift;
119 0           return "SET PATH=$path";
120             }
121              
122             sub get_shell_setter_code {
123 0     0 0   my $self = shift;
124 0           my $version = shift;
125 0           return "SET $env_var=$version"
126             }
127              
128             sub get_shell_unsetter_code {
129 0     0 0   my $self = shift;
130 0           return "UNSET $env_var";
131             }
132              
133             1;