added python as possible script type for aliases
[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 DEBUG_FEISTY_MEOW HOME );
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][yY]$/) { 
34     local($aliasname) = $file; $aliasname =~ s/\.[Pp][yY]$//;
35     &make_python_alias($aliasname, "$dir");
36   } elsif ($file =~ /\.[sS][hH]$/) { 
37     local($aliasname) = $file; $aliasname =~ s/\.[Ss][Hh]$//;
38     &make_bash_alias($aliasname, "$dir");
39   } elsif ($file =~ /\.[pP][lL]$/) { 
40     local($aliasname) = $file; $aliasname =~ s/\.[Pp][lL]$//;
41     &make_perl_alias($aliasname, "$dir");
42   }
43 }
44
45 # makes an alias for a bash script given the alias name.
46 sub make_bash_alias {
47   local($aliasname) = shift(@_);
48   local($full_alias) = $aliasname;
49 #print "full alias is $full_alias\n";
50   $aliasname =~ s/^.*\/([^\/]*)/\1/;
51 #print "alias became $aliasname\n";
52   print she "define_yeti_alias $aliasname=\"bash $full_alias.sh\"\n";
53 }
54
55 # makes an alias for a python script given the alias name.
56 #hmmm: don't love that we're hardcoding python3 in here, but apparently some systems don't have a 'python' command despite having python installed.
57 sub make_python_alias {
58   local($aliasname) = shift(@_);
59   local($full_alias) = $aliasname;
60   $aliasname =~ s/^.*\/([^\/]*)/\1/;
61 #print "alias became $aliasname\n";
62   print she "define_yeti_alias $aliasname=\"python3 $full_alias.py\"\n";
63 }
64
65 # makes an alias for a perl script given the alias name.
66 sub make_perl_alias {
67   local($aliasname) = shift(@_);
68   local($full_alias) = $aliasname;
69   $aliasname =~ s/^.*\/([^\/]*)/\1/;
70 #print "alias became $aliasname\n";
71   print she "define_yeti_alias $aliasname=\"perl $full_alias.pl\"\n";
72 }
73
74 ##############
75
76 # The "common.alias" file is used in the generated aliases file as a base
77 # set of generally useful aliases.  We also add aliases for any script files
78 # (perl, bash, python, etc) that we find in the feisty meow script hierarchy.
79 # Any *.alias files found in the $FEISTY_MEOW_LOADING_DOCK/custom folder are
80 # loaded also.
81 sub rebuild_script_aliases {
82
83   if (length($DEBUG_FEISTY_MEOW)) {
84     print "rebuilding generated aliases file...\n";
85   }
86
87   # create our generated shells directory if it's not already.
88   if ( ! -d $FEISTY_MEOW_LOADING_DOCK ) {
89     mkdir $FEISTY_MEOW_LOADING_DOCK;
90     if (length($DEBUG_FEISTY_MEOW)) {
91       print "made FEISTY_MEOW_LOADING_DOCK at '$FEISTY_MEOW_LOADING_DOCK'\n";
92     }
93   }
94
95   # test if we can use color in ls...
96 #  $test_color=` ls --help 2>&1 | grep -i color `;
97
98   # this is an array of files from which to draw alias definitions.
99   @ALIAS_DEFINITION_FILES = ("$FEISTY_MEOW_SCRIPTS/core/common.alias");
100
101   # if custom aliases files exist, add them to the list.
102 #hmmm: would be nice to have this name in a symbol somewhere instead of having "custom" or "customize" everywhere.
103   foreach $i (&glob_list("$FEISTY_MEOW_LOADING_DOCK/custom/*.alias")) {
104     if (-f $i) { push(@ALIAS_DEFINITION_FILES, $i); }
105   }
106   if (length($DEBUG_FEISTY_MEOW)) {
107     print "using these alias files:\n";
108     foreach $i (@ALIAS_DEFINITION_FILES) {
109       local $base_of_dir = &basename(&dirname($i));
110       local $basename = &basename($i);
111       print "  $base_of_dir/$basename\n";
112     }
113   }
114
115   # write the aliases for sh and bash scripts.
116   local $GENERATED_ALIAS_FILE = "$FEISTY_MEOW_LOADING_DOCK/fmc_core_and_custom_aliases.sh";
117   if (length($DEBUG_FEISTY_MEOW)) {
118     print "writing generated aliases in $GENERATED_ALIAS_FILE...\n";
119   }
120
121 #hmmm: perhaps a good place for a function to create the header,
122 #      given the appropriate comment code.
123
124   open GENOUT, ">$GENERATED_ALIAS_FILE" or die "cannot open $GENERATED_ALIAS_FILE";
125
126   print GENOUT "##\n";
127   print GENOUT "## generated file: $GENERATED_ALIAS_FILE\n";
128   print GENOUT "## please do not edit.\n";
129   print GENOUT "##\n";
130
131 #  if (length($test_color)) {
132 #    print GENOUT "export color_add='--color=auto'\n";
133 #  } else {
134 #    print GENOUT "export color_add=\n";
135 #  }
136
137   # plow in the full set of aliases into the file.
138   foreach $i (@ALIAS_DEFINITION_FILES) {
139     open CURR_ALIASER, "<$i" or die "cannot open current alias file $i";
140     foreach $line (<CURR_ALIASER>) {
141       print GENOUT "$line";
142     }
143   }
144
145   close GENOUT;
146
147   if (length($DEBUG_FEISTY_MEOW)) {
148     print("done rebuilding generated aliases file.\n");
149   }
150 }
151
152 ##############
153
154 # make sure we know where to store the files we're creating.
155 if ( ! length("$FEISTY_MEOW_LOADING_DOCK") ) {
156   print "\
157 The FEISTY_MEOW_LOADING_DOCK variable is not defined.  This must point to\
158 the location where the generated scripts are stored.  You may still need to\
159 configure the feisty meow script system with something like:\
160   bash /opt/feistymeow.org/feisty_meow/scripts/core/reconfigure_feisty_meow.sh\
161 Please see http://feistymeow.org for more details.\n";
162   exit 1;
163 #really need to use better exit codes.
164 }
165
166 ##############
167
168 $FEISTY_MEOW_LOADING_DOCK =~ s/\\/\//g;
169 $FEISTY_MEOW_SCRIPTS =~ s/\\/\//g;
170 $FEISTY_MEOW_APEX =~ s/\\/\//g;
171
172 ##############
173
174 # create our generated shells directory if it's not already there.
175 if (! -d $FEISTY_MEOW_LOADING_DOCK) {
176   mkdir $FEISTY_MEOW_LOADING_DOCK;
177 }
178
179 ##############
180
181 # set the executable bit for binaries for just this current user.
182 if (-d $FEISTY_MEOW_BINARIES) {
183   system("find \"$FEISTY_MEOW_BINARIES\" -type f -exec chmod u+x \"{}\" ';'");
184 }
185
186 ##############
187
188 # generate the first set of alias files that are defined in the core
189 # and custom scripts directories.
190 &rebuild_script_aliases;
191
192 ##############
193
194 # trash the old versions.
195 unlink("$FEISTY_MEOW_LOADING_DOCK/fmc_aliases_for_scripts.sh");
196
197 if (length($DEBUG_FEISTY_MEOW)) {
198   printf "writing $FEISTY_MEOW_LOADING_DOCK/fmc_aliases_for_scripts.sh...\n";
199 }
200
201 ##############
202
203 # open the alias files to be created.
204 open(she, ">> $FEISTY_MEOW_LOADING_DOCK/fmc_aliases_for_scripts.sh");
205
206 # find the list of files in the scripts directory.
207 @shell_files = (find_files(recursive_find_directories("$FEISTY_MEOW_SCRIPTS")),
208     find_files("$FEISTY_MEOW_LOADING_DOCK/custom/scripts"),
209     find_files(recursive_find_directories("$FEISTY_MEOW_LOADING_DOCK/custom/scripts")));
210
211 # strip out the customization files, since they are added in on demand only.
212 #print "before filtering list: @shell_files\n";
213 @shell_files = grep ! /\/customize\//, @shell_files;
214 #print "after filtering list: @shell_files\n";
215
216 #printf "found all these files in main script dirs:\n";
217 #printf "  @shell_files\n";
218
219 # construct aliases for items in the scripts directory.
220 foreach $file (@shell_files) {
221   # test to see what type of item we got.
222   if ($file =~ '^\.$'
223       || $file =~ '^\.\.$'
224       || $file =~ '^.svn$'
225       || $file =~ '^.git$'
226       || $file =~ /\/\.$/
227       || $file =~ /\/\.\.$/
228       || $file =~ /\/\.svn$/
229       || $file =~ /\/\.git$/
230       ) {
231     # just skip this item; it's a special directory or a file we don't want to include.
232     print "skipping name: $file\n";
233   } else {
234      &make_alias($file, "");
235   }
236 }
237
238 close(she);
239
240 ##############
241
242 # prepare a finalizer chunk that is the last thing to load.
243
244 open(she, ">> $FEISTY_MEOW_LOADING_DOCK/fmc_ending_sentinel.sh");
245
246 # write in our sentinel alias that says alias loading was handled.
247 print she "define_yeti_alias CORE_ALIASES_LOADED=true\n";
248
249 close(she);
250
251 ##############
252
253 1;
254