resolvinations
[feisty_meow.git] / scripts / core / generate_aliases.pl
1 #!/usr/bin/perl
2
3 ##############
4 #
5 #  Name   : generate_aliases
6 #  Author : Chris Koeritz
7 #  Rights : Copyright (C) 1996-$now by Author
8 #
9 #  Purpose:
10 #
11 #    This script generates feisty meow script alias files.  Alias files
12 #  contain a list of definitions for command aliases that are written in the
13 #  specified shell dialect (such as bash or perl) and which are additionally
14 #  tailored for the operating system to be used.
15 #
16 ##############
17 #  This program is free software; you can redistribute it and/or modify it
18 #  under the terms of the GNU General Public License as published by the Free
19 #  Software Foundation; either version 2 of the License or (at your option)
20 #  any later version.  See: "http://www.gruntose.com/Info/GNU/GPL.html" for a
21 #  version of the License.  Please send any updates to "fred@gruntose.com".
22 ##############
23
24 require "filename_helper.pl";
25
26 use Env qw(FEISTY_MEOW_BINARIES BUILD_TOP FEISTY_MEOW_APEX FEISTY_MEOW_LOADING_DOCK FEISTY_MEOW_SCRIPTS SHELL_DEBUG );
27
28 # given a possible aliasable filename, this will decide whether to create a perl
29 # or bash alias for it.  it needs the filename of the possible alias and the
30 # directory where that file resides.
31 sub make_alias {
32   local($file, $dir) = @_;
33   if ($file =~ /\.[pP][lL]$/) { 
34     local($aliasname) = $file; $aliasname =~ s/\.[Pp][lL]$//;
35     &make_perl_alias($aliasname, "$dir");
36   } elsif ($file =~ /\.[sS][hH]$/) { 
37     local($aliasname) = $file; $aliasname =~ s/\.[Ss][Hh]$//;
38     &make_bash_alias($aliasname, "$dir");
39   }
40 }
41
42 # makes an alias for a bash script given the alias name.
43 sub make_bash_alias {
44   local($aliasname) = shift(@_);
45   local($full_alias) = $aliasname;
46   $aliasname =~ s/^.*\/([^\/]*)/\1/;
47 #print "alias became $aliasname\n";
48   local($source_dir) = shift(@_);
49 #print "bash alias is $aliasname, dir is $source_dir\n";
50   print she "define_yeti_alias $aliasname=\"bash $source_dir/$full_alias.sh\"\n";
51 }
52
53 # makes an alias for a perl script given the alias name.
54 sub make_perl_alias {
55   local($aliasname) = shift(@_);
56   local($full_alias) = $aliasname;
57   $aliasname =~ s/^.*\/([^\/]*)/\1/;
58 #print "alias became $aliasname\n";
59   local($source_dir) = shift(@_);
60 #print "perl alias is $aliasname, dir is $source_dir\n";
61   print she "define_yeti_alias $aliasname=\"perl $source_dir/$full_alias.pl\"\n";
62 }
63
64 # given a directory, this returns an array of all the filenames found therein.
65 sub load_file_names {
66   local($path) = shift(@_);
67   opendir(that_dir, $path);
68   local(@those_files) = sort(readdir(that_dir));
69   return @those_files;
70 }
71
72 ##############
73
74 # The "common.alias" file is used in the generated aliases file as a base
75 # set of generally useful aliases.  We also add aliases for any script files
76 # (perl, bash, python, etc) that we find in the feisty meow script hierarchy.
77 # Any *.alias files found in the $FEISTY_MEOW_LOADING_DOCK/custom folder are
78 # loaded also.
79 sub rebuild_script_aliases {
80
81   if (length($SHELL_DEBUG)) {
82     print "rebuilding generated aliases file...\n";
83   }
84
85   # create our generated shells directory if it's not already.
86   if ( ! -d $FEISTY_MEOW_LOADING_DOCK ) {
87     mkdir $FEISTY_MEOW_LOADING_DOCK;
88     if (length($SHELL_DEBUG)) {
89       print "made FEISTY_MEOW_LOADING_DOCK at '$FEISTY_MEOW_LOADING_DOCK'\n";
90     }
91   }
92
93   # test if we can use color in ls...
94   $test_color=` ls --help 2>&1 | grep -i color `;
95
96   # this is an array of files from which to draw alias definitions.
97   @ALIAS_DEFINITION_FILES = ("$FEISTY_MEOW_SCRIPTS/core/common.alias");
98
99   # if custom aliases files exist, add them to the list.
100   foreach $i (&glob_list("$FEISTY_MEOW_LOADING_DOCK/custom/*.alias")) {
101     if (-f $i) { push(@ALIAS_DEFINITION_FILES, $i); }
102   }
103   if (length($SHELL_DEBUG)) {
104     print "using these alias files:\n";
105     foreach $i (@ALIAS_DEFINITION_FILES) {
106       local $base_of_dir = &basename(&dirname($i));
107       local $basename = &basename($i);
108       print "  $base_of_dir/$basename\n";
109     }
110   }
111
112   # write the aliases for sh and bash scripts.
113
114   local $GENERATED_ALIAS_FILE = "$FEISTY_MEOW_LOADING_DOCK/fmc_core_and_custom_aliases.sh";
115   if (length($SHELL_DEBUG)) {
116     print "writing generated aliases in $GENERATED_ALIAS_FILE...\n";
117   }
118
119 #hmmm: perhaps a good place for a function to create the header,
120 #      given the appropriate comment code.
121
122   open GENOUT, ">$GENERATED_ALIAS_FILE" or die "cannot open $GENERATED_ALIAS_FILE";
123
124   print GENOUT "##\n";
125   print GENOUT "## generated file: $GENERATED_ALIAS_FILE\n";
126   print GENOUT "## please do not edit.\n";
127   print GENOUT "##\n";
128
129   if (length($test_color)) {
130     print GENOUT "export color_add=--color=auto\n";
131   } else {
132     print GENOUT "export color_add=\n";
133   }
134
135   # plow in the full set of aliases into the file.
136   foreach $i (@ALIAS_DEFINITION_FILES) {
137     open CURR_ALIASER, "<$i" or die "cannot open current alias file $i";
138     foreach $line (<CURR_ALIASER>) {
139       print GENOUT "$line";
140     }
141   }
142
143   close GENOUT;
144
145   if (length($SHELL_DEBUG)) {
146     print("done rebuilding generated aliases file.\n");
147   }
148 }
149
150 ##############
151
152 # make sure we know where to store the files we're creating.
153 if ( ! length("$FEISTY_MEOW_LOADING_DOCK") ) {
154   print "\
155 The FEISTY_MEOW_LOADING_DOCK variable is not defined.  This must point to\
156 the location where the generated scripts are stored.  You may still need to\
157 configure the feisty meow script system with something like:\
158   bash ~/feisty_meow/scripts/core/reconfigure_feisty_meow.sh\
159 Please see http://feistymeow.org for more details.\n";
160   exit 1;
161 #really need to use better exit codes.
162 }
163
164 $FEISTY_MEOW_LOADING_DOCK =~ s/\\/\//g;
165 $FEISTY_MEOW_SCRIPTS =~ s/\\/\//g;
166 $FEISTY_MEOW_APEX =~ s/\\/\//g;
167
168 # create our generated shells directory if it's not already there.
169 if (! -d $FEISTY_MEOW_LOADING_DOCK) {
170   mkdir $FEISTY_MEOW_LOADING_DOCK;
171 }
172
173 ##############
174
175 # set the executable bit for binaries for just this current user.
176 if (-d $FEISTY_MEOW_BINARIES) {
177   system("chmod -R u+x \"$FEISTY_MEOW_BINARIES\"/*");
178 }
179
180 # generate the first set of alias files that are defined in the core
181 # and custom scripts directories.
182 &rebuild_script_aliases;
183
184 # trash the old versions.
185 unlink("$FEISTY_MEOW_LOADING_DOCK/fmc_aliases_for_scripts.sh");
186
187 if (length($SHELL_DEBUG)) {
188   printf "writing $FEISTY_MEOW_LOADING_DOCK/fmc_aliases_for_scripts.sh...\n";
189 }
190
191 # open the alias files to be created.
192 open(she, ">> $FEISTY_MEOW_LOADING_DOCK/fmc_aliases_for_scripts.sh");
193
194 # find the list of files in the scripts directory.
195 #opendir(scripts, "$FEISTY_MEOW_SCRIPTS");
196 #@shell_files = sort(readdir(scripts));
197 #print "scripts: @shell_files\n";
198
199 @shell_files = (&load_file_names("$FEISTY_MEOW_SCRIPTS"),
200    &load_file_names("$FEISTY_MEOW_LOADING_DOCK/custom/scripts"));
201
202 # construct aliases for items in the scripts directory.
203 foreach $file (@shell_files) {
204   # test to see what type of item we got.
205   if ($file =~ '^\.$'
206       || $file =~ '^\.\.$'
207       || $file =~ '^.svn$'
208       || $file =~ '^.git$'
209       || $file =~ /\/\.$/
210       || $file =~ /\/\.\.$/
211       || $file =~ /\/\.svn$/
212       || $file =~ /\/\.git$/
213 || $file =~ /^.*\/customize\/.*$/
214       ) {
215     # just skip this item; it's a special directory.
216 print "skipping name: $file\n";
217   } elsif (-d "$FEISTY_MEOW_SCRIPTS/$file"
218
219 && ! $file =~ /^.*\/customize\/.*$/
220
221 ) {
222     # if we see a subdirectory in the scripts folder, we add all the
223     # scripts in it as aliases.  we recurse only one level.
224
225 print "adding script dir in: $file\n";
226
227     opendir(subdir, "$FEISTY_MEOW_SCRIPTS/$file");
228     @subdir_files = sort(readdir(subdir));
229     foreach $subfile (@subdir_files) {
230       push(@shell_files, "$file/$subfile");
231     }
232   } elsif (-f "$FEISTY_MEOW_LOADING_DOCK/custom/scripts/$file") {
233     # if we see a file in the auto-generated area that comes from the
234     # customized scripts folder, we add it as an alias.
235     make_alias($file, "$FEISTY_MEOW_LOADING_DOCK/custom/scripts/");
236 print "added custom script file: $FEISTY_MEOW_LOADING_DOCK/custom/scripts/$file\n";
237   } else {
238
239
240     if ( ! $file =~ /^.*\/customize\/.*$/ ) {
241
242 print "adding regular file in: $file\n";
243
244       # if it's a regular file, we'll try to make an alias for it.  the function
245       # will only fire if the ending is appropriate for the script languages we use.
246       &make_alias($file, "$FEISTY_MEOW_SCRIPTS");
247
248     }
249
250   }
251 }
252
253 # open the source repository's script directory to find scripts in there.
254 local($build_shell_path) = "$BUILD_TOP/scripts/generator";
255 @build_shells = &load_file_names("$build_shell_path");
256 #opendir(build_shells_dir, $build_shell_path);
257 #@build_shell_files = sort(readdir(build_shells_dir));
258 #if (scalar(@build_shell_files) > 0) {
259 #  print "build shell folders: @build_shell_files\n";
260 #}
261 foreach $file (@build_shells) {
262   &make_alias($file, "$build_shell_path");
263 }
264
265 close(she);
266