Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean aliases script #290

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions bin/alias.bat
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ if not ["%_temp%"] == ["%_temp2%"] (

echo %* >> "%CMDER_ROOT%\config\aliases"
doskey /macrofile="%CMDER_ROOT%\config\aliases"
perl "%CMDER_ROOT%\scripts\clean_aliases.pl"
echo Alias created
endlocal
goto:eof
Expand Down
36 changes: 36 additions & 0 deletions scripts/clean_aliases.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Cmder adds aliases to its aliases file without caring for duplicates.
# This can result in the aliases file becoming bloated. This script cleans
#the aliases file.
use Env;

my %aliases;
my $alias_file = $CMDER_ROOT . "/config/aliases";

# First step
# Read the aliases file line by line, and put every entry in
# a dictionary. The newer aliases being the last, the new will
# always be kept over the old.
open (my $file_handle, '<', $alias_file) or die "cannot open '$alias_file' $!";
while(my $line = <$file_handle>)
{
if ($line =~ /([^=\s<>]+)=(.*)/)
{
$aliases{ $1 } = $2;
}
else
{
print "Invalid alias: $line"
}
}
close($file_handle);


# Second step
# Write back the aliases. Sort them to make the file look nice.
open(my $file_handle, '>', $alias_file) or die "cannot open '$alias_file' $!";
foreach my $key (sort keys %aliases)
{
print $file_handle "$key=$aliases{ $key }\n";
}
close($file_handle);