5 # Name : generate_aliases
6 # Author : Chris Koeritz
7 # Rights : Copyright (C) 1996-$now by Author
11 # This script generates YETI alias files. Alias files contain a list of
12 # definitions for command aliases that are written in the specified shell
13 # dialect (such as bash or perl) and which are additionally tailored for the
14 # 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 require "importenv.pl";
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 "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 "alias $aliasname=\"perl $source_dir/$full_alias.pl\"\n";
64 # given a directory, this returns an array of all the filenames found therein.
66 local($path) = shift(@_);
67 opendir(that_dir, $path);
68 local(@those_files) = sort(readdir(that_dir));
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_GENERATED/custom folder are
79 sub rebuild_script_aliases {
81 if (length($SHELL_DEBUG)) {
82 print "rebuiling generated aliases file...\n";
85 # create our generated shells directory if it's not already.
86 if ( ! -d $FEISTY_MEOW_GENERATED ) {
87 mkdir $FEISTY_MEOW_GENERATED;
88 print "made gener dir $FEISTY_MEOW_GENERATED\n";
91 # test if we can use color in ls...
92 $test_color=` ls --help 2>&1 | grep -i color `;
94 # this is an array of files from which to draw alias definitions.
95 @ALIAS_DEFINITION_FILES = ("$FEISTY_MEOW_SCRIPTS/core/common.alias");
97 # if custom aliases files exist, add them to the list.
98 foreach $i (&glob_list("$FEISTY_MEOW_GENERATED/custom/*.alias")) {
99 if (-f $i) { push(@ALIAS_DEFINITION_FILES, $i); }
101 print "alias files:\n";
102 foreach $i (@ALIAS_DEFINITION_FILES) {
103 local $base_of_dir = &basename(&dirname($i));
104 local $basename = &basename($i);
105 print " $base_of_dir/$basename\n";
108 # write the aliases for sh and bash scripts.
110 local $GENERATED_ALIAS_FILE = "$FEISTY_MEOW_GENERATED/fmc_core_and_custom_aliases.sh";
111 print "writing generated aliases in $GENERATED_ALIAS_FILE...\n";
113 #hmmm: perhaps a good place for a function to create the header,
114 # given the appropriate comment code.
116 open GENOUT, ">$GENERATED_ALIAS_FILE" or die "cannot open $GENERATED_ALIAS_FILE";
119 print GENOUT "## generated file: $GENERATED_ALIAS_FILE\n";
120 print GENOUT "## please do not edit.\n";
123 if (length($test_color)) {
124 print GENOUT "color_add=--color=auto\n";
126 print GENOUT "color_add=\n";
129 # plow in the full set of aliases into the file.
130 foreach $i (@ALIAS_DEFINITION_FILES) {
131 open CURR_ALIASER, "<$i" or die "cannot open current alias file $i";
132 foreach $line (<CURR_ALIASER>) {
133 print GENOUT "$line";
139 if (length($SHELL_DEBUG)) {
140 print("done rebuiling generated aliases file.\n");
146 # make sure we know where to store the files we're creating.
147 if ( ! length("$FEISTY_MEOW_GENERATED") ) {
149 The FEISTY_MEOW_GENERATED variable is not defined. This must point to the location where\n\
150 the generated scripts are stored. Perhaps you still need to run\n\
151 bootstrap_shells.sh and set up some environment variables. Please see\n\
152 http://yeticode.org for more details.\n";
154 #really need to use better exit codes.
157 $FEISTY_MEOW_GENERATED =~ s/\\/\//g;
158 $FEISTY_MEOW_SCRIPTS =~ s/\\/\//g;
159 $FEISTY_MEOW_DIR =~ s/\\/\//g;
161 # create our generated shells directory if it's not already there.
162 if (! -d $FEISTY_MEOW_GENERATED) {
163 mkdir $FEISTY_MEOW_GENERATED;
168 # set the executable bit for yeti binaries for just this current user.
170 system("chmod -R u+x \"$BINDIR\"/*");
173 # generate the first set of alias files that are defined in the core
174 # and custom scripts directories.
175 &rebuild_script_aliases;
177 # trash the old versions.
178 unlink("$FEISTY_MEOW_GENERATED/fmc_aliases_for_scripts.sh");
180 printf "writing $FEISTY_MEOW_GENERATED/fmc_aliases_for_scripts.sh...\n";
182 # open the alias files to be created.
183 open(she, ">> $FEISTY_MEOW_GENERATED/fmc_aliases_for_scripts.sh");
185 #print "os is $OS\n";
187 # find the list of files in the scripts directory.
188 #opendir(scripts, "$FEISTY_MEOW_SCRIPTS");
189 #@shell_files = sort(readdir(scripts));
190 #print "yeti scripts: @shell_files\n";
192 @shell_files = &load_file_names("$FEISTY_MEOW_SCRIPTS");
194 # construct aliases for items in the scripts directory.
195 foreach $file (@shell_files) {
196 # test to see what type of item we got.
202 || $file =~ /\/\.\.$/
203 || $file =~ /\/\.svn$/
204 || $file =~ /\/\.git$/
206 # just skip this item; it's a special directory.
207 } elsif (-d "$FEISTY_MEOW_SCRIPTS/$file") {
208 # if we see a subdirectory in the scripts folder, we add all the
209 # scripts in it as aliases. we recurse only one level.
210 opendir(subdir, "$FEISTY_MEOW_SCRIPTS/$file");
211 @subdir_files = sort(readdir(subdir));
212 foreach $subfile (@subdir_files) {
213 push(@shell_files, "$file/$subfile");
216 # if it's a regular file, we'll try to make an alias for it. the function
217 # will only fire if the ending is appropriate for the script languages we use.
218 &make_alias($file, "$FEISTY_MEOW_SCRIPTS");
222 # open the source repository's script directory to find scripts in there.
223 local($build_shell_path) = "$BUILD_TOP/scripts/generator";
224 @build_shells = &load_file_names("$build_shell_path");
225 #opendir(build_shells_dir, $build_shell_path);
226 #@build_shell_files = sort(readdir(build_shells_dir));
227 #if (scalar(@build_shell_files) > 0) {
228 # print "build shell folders: @build_shell_files\n";
230 foreach $file (@build_shells) {
231 &make_alias($file, "$build_shell_path");