From 1e2f954724eabed161256f9fc80d6f43a63de769 Mon Sep 17 00:00:00 2001 From: melchior Date: Thu, 16 Oct 2014 00:20:36 -0400 Subject: [PATCH 1/4] Add a lua script to clean the aliases file Note that cmder doesn't ship with lua. Next step is converting this script to perl, which ships with msysgit. --- bin/alias.bat | 1 + scripts/clean_aliases.lua | 63 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 scripts/clean_aliases.lua diff --git a/bin/alias.bat b/bin/alias.bat index 4ec18829e..a323fec14 100644 --- a/bin/alias.bat +++ b/bin/alias.bat @@ -15,6 +15,7 @@ if not ["%_temp%"] == ["%_temp2%"] ( echo %* >> "%CMDER_ROOT%\config\aliases" doskey /macrofile="%CMDER_ROOT%\config\aliases" +lua "%CMDER_ROOT%\scripts\clean_aliases.lua" echo Alias created endlocal goto:eof diff --git a/scripts/clean_aliases.lua b/scripts/clean_aliases.lua new file mode 100644 index 000000000..d06831633 --- /dev/null +++ b/scripts/clean_aliases.lua @@ -0,0 +1,63 @@ +--[[ +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. +]] + +local aliases = {} +local alias_file = os.getenv('CMDER_ROOT') .. "/config/aliases" + +-- from http://www.lua.org/pil/19.3.html +-- function to create an iterator that returns the key-value pairs +-- sorted by keys +local function pairsByKeys (t, f) + + local a = {} + for n in pairs(t) do + table.insert(a, n) + end + table.sort(a, f) + + local i = 0 -- iterator variable + local iter = function () -- iterator function + i = i + 1 + if a[i] == nil then + return nil + else + return a[i], t[a[i]] + end + end + + return iter +end + +-- 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. +for line in io.lines(alias_file) do + + -- Doskey actually accepts a lot of weird characters in macros + -- definitions. + local key, value = line:match('([^=%s<>]+)=(.*)') + + if key then + aliases[key] = value + else + print('Invalid macro definition: '..line) + end + +end + +-- Second step +-- Write back the aliases. Sort them to make the file look nice. +local f = io.open(alias_file, 'w') + +for key, value in pairsByKeys(aliases) do + -- write the pair only if the value is not empty + if value then + f:write(key .. '=' .. value .. '\n') + end +end + +f:close() \ No newline at end of file From 5bd0c29c25b80ba84872db1c26fccfd02d75913e Mon Sep 17 00:00:00 2001 From: melchior Date: Thu, 16 Oct 2014 01:00:59 -0400 Subject: [PATCH 2/4] Convert the clean alias script to perl --- bin/alias.bat | 2 +- scripts/clean_aliases.pl | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 scripts/clean_aliases.pl diff --git a/bin/alias.bat b/bin/alias.bat index a323fec14..ae3966aa3 100644 --- a/bin/alias.bat +++ b/bin/alias.bat @@ -15,7 +15,7 @@ if not ["%_temp%"] == ["%_temp2%"] ( echo %* >> "%CMDER_ROOT%\config\aliases" doskey /macrofile="%CMDER_ROOT%\config\aliases" -lua "%CMDER_ROOT%\scripts\clean_aliases.lua" +perl "%CMDER_ROOT%\scripts\clean_aliases.pl" echo Alias created endlocal goto:eof diff --git a/scripts/clean_aliases.pl b/scripts/clean_aliases.pl new file mode 100644 index 000000000..77756f312 --- /dev/null +++ b/scripts/clean_aliases.pl @@ -0,0 +1,32 @@ +# 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; + } +} +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); + From 45c9742ea5d57cfc1cc6d153a238aecc0651afad Mon Sep 17 00:00:00 2001 From: melchior Date: Thu, 16 Oct 2014 01:03:19 -0400 Subject: [PATCH 3/4] remove lua version --- scripts/clean_aliases.lua | 63 --------------------------------------- 1 file changed, 63 deletions(-) delete mode 100644 scripts/clean_aliases.lua diff --git a/scripts/clean_aliases.lua b/scripts/clean_aliases.lua deleted file mode 100644 index d06831633..000000000 --- a/scripts/clean_aliases.lua +++ /dev/null @@ -1,63 +0,0 @@ ---[[ -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. -]] - -local aliases = {} -local alias_file = os.getenv('CMDER_ROOT') .. "/config/aliases" - --- from http://www.lua.org/pil/19.3.html --- function to create an iterator that returns the key-value pairs --- sorted by keys -local function pairsByKeys (t, f) - - local a = {} - for n in pairs(t) do - table.insert(a, n) - end - table.sort(a, f) - - local i = 0 -- iterator variable - local iter = function () -- iterator function - i = i + 1 - if a[i] == nil then - return nil - else - return a[i], t[a[i]] - end - end - - return iter -end - --- 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. -for line in io.lines(alias_file) do - - -- Doskey actually accepts a lot of weird characters in macros - -- definitions. - local key, value = line:match('([^=%s<>]+)=(.*)') - - if key then - aliases[key] = value - else - print('Invalid macro definition: '..line) - end - -end - --- Second step --- Write back the aliases. Sort them to make the file look nice. -local f = io.open(alias_file, 'w') - -for key, value in pairsByKeys(aliases) do - -- write the pair only if the value is not empty - if value then - f:write(key .. '=' .. value .. '\n') - end -end - -f:close() \ No newline at end of file From 247c65ef056706854e42166ecf7efdbacb18947d Mon Sep 17 00:00:00 2001 From: melchior Date: Thu, 16 Oct 2014 01:09:24 -0400 Subject: [PATCH 4/4] Add an error message if an alias is detected as invalid --- scripts/clean_aliases.pl | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/scripts/clean_aliases.pl b/scripts/clean_aliases.pl index 77756f312..c7cf699d8 100644 --- a/scripts/clean_aliases.pl +++ b/scripts/clean_aliases.pl @@ -17,6 +17,10 @@ { $aliases{ $1 } = $2; } + else + { + print "Invalid alias: $line" + } } close($file_handle);