1 | use File::Basename;
|
---|
2 |
|
---|
3 | #envi messages
|
---|
4 | sub envi_message {
|
---|
5 | my($message) = @_;
|
---|
6 | if ( $envi_verbose ) {
|
---|
7 | print "envi message: $message\n";
|
---|
8 | }
|
---|
9 | }
|
---|
10 |
|
---|
11 | #TASK FUNCTIONS
|
---|
12 | sub task_location {
|
---|
13 |
|
---|
14 | if ( !exists $_[0] ) { return ""; }
|
---|
15 |
|
---|
16 | local $task = $_[0];
|
---|
17 |
|
---|
18 | local @locations = ( "$ENV{'HOME'}/.envi/tasks", "$ENV{'ENVI_HOME'}/etc/tasks" );
|
---|
19 | foreach (@locations) {
|
---|
20 | #try for basic task with one name
|
---|
21 | if ( -f "$_/$task/task.pl" ) {
|
---|
22 | return "$_/$task";
|
---|
23 | #try for link to another task
|
---|
24 | } elsif ( -f "$_/$task" ) {
|
---|
25 | open (TASKFILE, "$_/$task");
|
---|
26 | $line = <TASKFILE>;
|
---|
27 | chomp $line;
|
---|
28 | close (TASKFILE);
|
---|
29 | if ( $line =~ "alias-for:(.*)" ) {
|
---|
30 | return task_location($1);
|
---|
31 | }
|
---|
32 | #try for a built-in official task alias
|
---|
33 | } else {
|
---|
34 | @taskdirs = glob "$_/*";
|
---|
35 | foreach $dir ( @taskdirs ) {
|
---|
36 | if ( -f "$dir/aliases.txt" ) {
|
---|
37 | open (ALIASES, "$dir/aliases.txt");
|
---|
38 | while ( $line = <ALIASES> ) {
|
---|
39 | chomp $line;
|
---|
40 | if ( $line eq $task ) {
|
---|
41 | my $resolved = basename($dir);
|
---|
42 | return task_location($resolved);
|
---|
43 | }
|
---|
44 | }
|
---|
45 | }
|
---|
46 | }
|
---|
47 | }
|
---|
48 |
|
---|
49 | }
|
---|
50 |
|
---|
51 | }
|
---|
52 |
|
---|
53 | sub task_exists {
|
---|
54 | if ( ! exists $_[0] || !task_location($_[0]) ) {
|
---|
55 | return 0;
|
---|
56 | }
|
---|
57 | return 1;
|
---|
58 | }
|
---|
59 |
|
---|
60 | sub task_canonical_name {
|
---|
61 | if ( ! exists $_[0] || !task_location($_[0]) ) {
|
---|
62 | return;
|
---|
63 | }
|
---|
64 | return basename(task_location($_[0]));
|
---|
65 | }
|
---|
66 |
|
---|
67 | sub run_task {
|
---|
68 | if ( ! exists $_[0] || !task_exists($_[0])) {
|
---|
69 | return;
|
---|
70 | }
|
---|
71 |
|
---|
72 | local $task = $_[0];
|
---|
73 |
|
---|
74 | #let rt.pl take over from here, so a new environment context created for the
|
---|
75 | #run of the task, which will be destroyed when rt.pl exits
|
---|
76 | local $command = "perl \"$ENV{'ENVI_HOME'}/lib/rt.pl\" \"$task\"";
|
---|
77 | for my $a ( @ARGV ) {
|
---|
78 | $command .= " \"$a\"";
|
---|
79 | }
|
---|
80 |
|
---|
81 | #envi_message( "rt.pl command: $command" );
|
---|
82 | system( $command );
|
---|
83 |
|
---|
84 | }
|
---|
85 |
|
---|
86 | sub user_task_environment_script {
|
---|
87 | if ( ! exists $_[0] || !task_location($_[0]) ) { return ""; }
|
---|
88 | return "$ENV{'HOME'}/.envi/tasks/" . task_canonical_name($_[0]) . "/environment.pl";
|
---|
89 | }
|
---|
90 |
|
---|
91 | sub system_task_environment_script {
|
---|
92 | if ( ! exists $_[0] || !task_location($_[0]) ) { return ""; }
|
---|
93 | return "$ENV{'ENVI_HOME'}/etc/tasks/" . task_canonical_name($_[0]) . "/environment.pl";
|
---|
94 | }
|
---|
95 |
|
---|
96 | sub list_tasks() {
|
---|
97 |
|
---|
98 | find_tasks( "$ENV{'HOME'}/.envi/tasks/*" );
|
---|
99 | find_tasks( "$ENV{'ENVI_HOME'}/etc/tasks/*" );
|
---|
100 | }
|
---|
101 |
|
---|
102 |
|
---|
103 | #PLAYLIST FUNCTIONS
|
---|
104 | sub playlist_location {
|
---|
105 |
|
---|
106 | if ( !exists $_[0] ) { return ""; }
|
---|
107 |
|
---|
108 | local $playlist = $_[0];
|
---|
109 | local $v = "";
|
---|
110 |
|
---|
111 | if ( -f ($v = "$ENV{'HOME'}/.envi/playlists/$playlist") ) {
|
---|
112 | return $v;
|
---|
113 | }
|
---|
114 |
|
---|
115 | if ( -f ($v = "$ENV{'ENVI_HOME'}/etc/playlists/$playlist") ) {
|
---|
116 | return $v;
|
---|
117 | }
|
---|
118 |
|
---|
119 | return "";
|
---|
120 | }
|
---|
121 |
|
---|
122 | sub playlist_exists {
|
---|
123 | if ( ! exists $_[0] || !playlist_location($_[0]) ) {
|
---|
124 | return 0;
|
---|
125 | }
|
---|
126 | return 1;
|
---|
127 | }
|
---|
128 |
|
---|
129 | sub run_playlist {
|
---|
130 | if ( ! exists $_[0] || !playlist_exists($_[0])) {
|
---|
131 | return;
|
---|
132 | }
|
---|
133 |
|
---|
134 | local $playlist = $_[0];
|
---|
135 | local $playlist_loc = playlist_location($playlist);
|
---|
136 |
|
---|
137 | #parse arguments
|
---|
138 | local $ls=0;
|
---|
139 | for my $arg ( @ARGV ) {
|
---|
140 | #list tasks
|
---|
141 | if ( $arg =~ "ls|tasks" ) {
|
---|
142 | $ls=1;
|
---|
143 | } else {
|
---|
144 | die "Invalid argument: $arg\n";
|
---|
145 | }
|
---|
146 | shift(@ARGV);
|
---|
147 | }
|
---|
148 |
|
---|
149 | local $task_cmd = "";
|
---|
150 | open( FILE, "< $playlist_loc" ) or die "can't open $playlist_loc : $!";
|
---|
151 | while( $task_cmd = <FILE> ) {
|
---|
152 | chomp($task_cmd);
|
---|
153 | if ( $task_cmd ne "" && substr($task_cmd,0,1) ne "#" ) {
|
---|
154 | #show the task command
|
---|
155 | if ( $ls ) {
|
---|
156 | print "$task_cmd\n";
|
---|
157 | #run the task
|
---|
158 | } else {
|
---|
159 | envi_message("Running '$task_cmd'");
|
---|
160 | system( "$ENV{'ENVI_HOME'}/bin/envi.pl $task_cmd" );
|
---|
161 | }
|
---|
162 | }
|
---|
163 | }
|
---|
164 | }
|
---|
165 |
|
---|
166 | sub list_playlists {
|
---|
167 | local_names( "$ENV{'HOME'}/.envi/playlists/*" );
|
---|
168 | local_names( "$ENV{'ENVI_HOME'}/etc/playlists/*" );
|
---|
169 | }
|
---|
170 |
|
---|
171 | sub local_names {
|
---|
172 | if ( ! exists $_[0] ) { return; }
|
---|
173 | @files = <$_[0]>;
|
---|
174 | foreach $file (@files) {
|
---|
175 | print basename($file) . "\n";
|
---|
176 | }
|
---|
177 | }
|
---|
178 |
|
---|
179 | sub find_tasks {
|
---|
180 | if ( ! exists $_[0] ) { return; }
|
---|
181 | @dirs = <$_[0]>;
|
---|
182 | foreach $dir (@dirs) {
|
---|
183 | #try task with aliases
|
---|
184 | if ( -f "$dir/aliases.txt" ) {
|
---|
185 | open (ALIASES, "$dir/aliases.txt");
|
---|
186 | while ( $line = <ALIASES> ) {
|
---|
187 | chomp $line;
|
---|
188 | print "$line\n";
|
---|
189 | }
|
---|
190 | #task without aliases
|
---|
191 | } else {
|
---|
192 | print basename($dir) . "\n";
|
---|
193 | }
|
---|
194 | }
|
---|
195 | }
|
---|
196 |
|
---|
197 | sub source_script {
|
---|
198 | #TODO: allow multiline environment variables
|
---|
199 | if ( ! exists $_[0] || ! exists $_[1] ) { return; }
|
---|
200 | if ( ! -f "$_[1]" ) { return; }
|
---|
201 | my $rnd = rand();
|
---|
202 | my $cmd = undef;
|
---|
203 | if ( $^O eq "MSWin32" ) {
|
---|
204 | my $subcommand = '"' . $ENV{'ENVI_HOME'} . '\\lib\\source.' . $_[0] . '" "' . $_[1] . '" "' . $rnd . '"';
|
---|
205 | if ( $ENV{'ENVI_HOME'} =~ " " || $_[1] =~ " " ) {
|
---|
206 | $cmd = 'cmd.exe /v:on /c ' . $subcommand;
|
---|
207 | } else {
|
---|
208 | $cmd = 'cmd.exe /v:on /c "' . $subcommand . '"';
|
---|
209 | }
|
---|
210 | } else {
|
---|
211 | $cmd = '"' . $ENV{'ENVI_HOME'} . '/lib/source.' . $_[0] . '" "' . $_[1] . '" "' . $rnd . '"';
|
---|
212 | }
|
---|
213 | open(SRC, "$cmd|") or die("Couldn't run command $cmd: $!\n");
|
---|
214 | my $reading = 0;
|
---|
215 | while ( my $line=<SRC> ) {
|
---|
216 | chomp($line);
|
---|
217 | if ( $reading ) {
|
---|
218 | my ($name, $value) = split(/=/,$line,2);
|
---|
219 | if ( $name !~ /^\_/ ) {
|
---|
220 | $ENV{$name} = $value;
|
---|
221 | }
|
---|
222 | } elsif ( $line eq "-- ENVIRONMENT \"$rnd\" --" ) {
|
---|
223 | $reading = 1;
|
---|
224 | }
|
---|
225 | }
|
---|
226 | close(SRC);
|
---|
227 | }
|
---|
228 |
|
---|
229 | sub source_bash_script {
|
---|
230 | if ( ! exists $_[0] ) { return; }
|
---|
231 | source_script("bash","$_[0]");
|
---|
232 | }
|
---|
233 |
|
---|
234 | sub source_batch_script {
|
---|
235 | if ( ! exists $_[0] ) { return; }
|
---|
236 | source_script("bat","$_[0]");
|
---|
237 | }
|
---|
238 |
|
---|
239 |
|
---|