5 # Name : generate_aliases
6 # Author : Chris Koeritz
7 # Rights : Copyright (C) 1996-$now by Author
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.
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".
24 require "filename_helper.pl";
26 use Env qw(FEISTY_MEOW_BINARIES BUILD_TOP FEISTY_MEOW_APEX FEISTY_MEOW_LOADING_DOCK FEISTY_MEOW_SCRIPTS DEBUG_FEISTY_MEOW );
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.
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");
42 # makes an alias for a bash script given the alias name.
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";
53 # makes an alias for a perl script given the alias name.
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";
66 # The "common.alias" file is used in the generated aliases file as a base
67 # set of generally useful aliases. We also add aliases for any script files
68 # (perl, bash, python, etc) that we find in the feisty meow script hierarchy.
69 # Any *.alias files found in the $FEISTY_MEOW_LOADING_DOCK/custom folder are
71 sub rebuild_script_aliases {
73 if (length($DEBUG_FEISTY_MEOW)) {
74 print "rebuilding generated aliases file...\n";
77 # create our generated shells directory if it's not already.
78 if ( ! -d $FEISTY_MEOW_LOADING_DOCK ) {
79 mkdir $FEISTY_MEOW_LOADING_DOCK;
80 if (length($DEBUG_FEISTY_MEOW)) {
81 print "made FEISTY_MEOW_LOADING_DOCK at '$FEISTY_MEOW_LOADING_DOCK'\n";
85 # test if we can use color in ls...
86 # $test_color=` ls --help 2>&1 | grep -i color `;
88 # this is an array of files from which to draw alias definitions.
89 @ALIAS_DEFINITION_FILES = ("$FEISTY_MEOW_SCRIPTS/core/common.alias");
91 # if custom aliases files exist, add them to the list.
92 foreach $i (&glob_list("$FEISTY_MEOW_LOADING_DOCK/custom/*.alias")) {
93 if (-f $i) { push(@ALIAS_DEFINITION_FILES, $i); }
95 if (length($DEBUG_FEISTY_MEOW)) {
96 print "using these alias files:\n";
97 foreach $i (@ALIAS_DEFINITION_FILES) {
98 local $base_of_dir = &basename(&dirname($i));
99 local $basename = &basename($i);
100 print " $base_of_dir/$basename\n";
104 # write the aliases for sh and bash scripts.
106 local $GENERATED_ALIAS_FILE = "$FEISTY_MEOW_LOADING_DOCK/fmc_core_and_custom_aliases.sh";
107 if (length($DEBUG_FEISTY_MEOW)) {
108 print "writing generated aliases in $GENERATED_ALIAS_FILE...\n";
111 #hmmm: perhaps a good place for a function to create the header,
112 # given the appropriate comment code.
114 open GENOUT, ">$GENERATED_ALIAS_FILE" or die "cannot open $GENERATED_ALIAS_FILE";
117 print GENOUT "## generated file: $GENERATED_ALIAS_FILE\n";
118 print GENOUT "## please do not edit.\n";
121 # if (length($test_color)) {
122 # print GENOUT "export color_add='--color=auto'\n";
124 # print GENOUT "export color_add=\n";
127 # plow in the full set of aliases into the file.
128 foreach $i (@ALIAS_DEFINITION_FILES) {
129 open CURR_ALIASER, "<$i" or die "cannot open current alias file $i";
130 foreach $line (<CURR_ALIASER>) {
131 print GENOUT "$line";
137 if (length($DEBUG_FEISTY_MEOW)) {
138 print("done rebuilding generated aliases file.\n");
144 # make sure we know where to store the files we're creating.
145 if ( ! length("$FEISTY_MEOW_LOADING_DOCK") ) {
147 The FEISTY_MEOW_LOADING_DOCK variable is not defined. This must point to\
148 the location where the generated scripts are stored. You may still need to\
149 configure the feisty meow script system with something like:\
150 bash ~/feisty_meow/scripts/core/reconfigure_feisty_meow.sh\
151 Please see http://feistymeow.org for more details.\n";
153 #really need to use better exit codes.
156 $FEISTY_MEOW_LOADING_DOCK =~ s/\\/\//g;
157 $FEISTY_MEOW_SCRIPTS =~ s/\\/\//g;
158 $FEISTY_MEOW_APEX =~ s/\\/\//g;
160 # create our generated shells directory if it's not already there.
161 if (! -d $FEISTY_MEOW_LOADING_DOCK) {
162 mkdir $FEISTY_MEOW_LOADING_DOCK;
167 # set the executable bit for binaries for just this current user.
168 if (-d $FEISTY_MEOW_BINARIES) {
169 system("chmod -R u+x \"$FEISTY_MEOW_BINARIES\"/*");
172 # generate the first set of alias files that are defined in the core
173 # and custom scripts directories.
174 &rebuild_script_aliases;
176 # trash the old versions.
177 unlink("$FEISTY_MEOW_LOADING_DOCK/fmc_aliases_for_scripts.sh");
179 if (length($DEBUG_FEISTY_MEOW)) {
180 printf "writing $FEISTY_MEOW_LOADING_DOCK/fmc_aliases_for_scripts.sh...\n";
183 # open the alias files to be created.
184 open(she, ">> $FEISTY_MEOW_LOADING_DOCK/fmc_aliases_for_scripts.sh");
186 # find the list of files in the scripts directory.
187 #opendir(scripts, "$FEISTY_MEOW_SCRIPTS");
188 #@shell_files = sort(readdir(scripts));
189 #print "scripts: @shell_files\n";
191 @shell_files = (&find_files("$FEISTY_MEOW_SCRIPTS"),
192 &find_files("$FEISTY_MEOW_LOADING_DOCK/custom/scripts"),
193 &find_files(recursive_find_directories("$FEISTY_MEOW_LOADING_DOCK/custom/scripts")));
195 #printf "found all these files: @shell_files\n";
197 # construct aliases for items in the scripts directory.
198 foreach $file (@shell_files) {
199 # test to see what type of item we got.
205 || $file =~ /\/\.\.$/
206 || $file =~ /\/\.svn$/
207 || $file =~ /\/\.git$/
208 || $file =~ /\/customize\//
209 #hmmm: would be nice to have this name in a symbol somewhere instead of having "customize" everywhere.
211 # just skip this item; it's a special directory.
212 #print "skipping name: $file\n";
213 } elsif (-d "$FEISTY_MEOW_SCRIPTS/$file") {
214 # if we see a subdirectory in the scripts folder, we add all the
215 # scripts in it as aliases. we recurse only one level. we also don't use
216 # our customize directory as aliases, since those are processed way differently.
217 #print "adding script dir in: $file\n";
218 opendir(subdir, "$FEISTY_MEOW_SCRIPTS/$file");
219 @subdir_files = sort(readdir(subdir));
220 foreach $subfile (@subdir_files) {
221 push(@shell_files, "$file/$subfile");
223 } elsif (-f "$FEISTY_MEOW_LOADING_DOCK/custom/scripts/$file") {
224 # if we see a file in the auto-generated area that comes from the
225 # customized scripts folder, we add it as an alias.
226 make_alias($file, "$FEISTY_MEOW_LOADING_DOCK/custom/scripts/");
227 #print "added custom script file: $FEISTY_MEOW_LOADING_DOCK/custom/scripts/$file\n";
229 # last ditch effort to make sense of the file; just go ahead and make an alias unless
230 # the file is part of our customization scheme.
231 if ( ! ($file =~ /customize/) ) {
232 #print "adding regular file in: $file\n";
233 # if it's a regular file, we'll try to make an alias for it. the function
234 # will only fire if the ending is appropriate for the script languages we use.
235 &make_alias($file, "$FEISTY_MEOW_SCRIPTS");
237 #print "omitting file in: $file\n";
242 # open the source repository's script directory to find scripts in there.
243 local($build_shell_path) = "$BUILD_TOP/scripts/generator";
244 @build_shells = &find_files("$build_shell_path");
245 #opendir(build_shells_dir, $build_shell_path);
246 #@build_shell_files = sort(readdir(build_shells_dir));
247 #if (scalar(@build_shell_files) > 0) {
248 # print "build shell folders: @build_shell_files\n";
250 foreach $file (@build_shells) {
251 &make_alias($file, "$build_shell_path");