OSDN Git Service

Initial commit
[speare/speare.git] / rename.pl
1 #!/usr/bin/perl -w
2
3 # A MATLAB helper script for Speare code editor.
4 # Copyright (c) 2019 sevenuc.com. All rights reserved.
5
6 # THIS FILE IS PART OF SPEARE CODE EDITOR. WITHOUT THE
7 # WRITTEN PERMISSION OF THE AUTHOR THIS FILE MAY NOT
8 # BE USED FOR ANY COMMERCIAL PRODUCT.
9
10 # More info: 
11 #    http://sevenuc.com/en/Speare.html
12 # Contact:
13 #    Sevenuc support <info@sevenuc.com>
14 # Issue report and requests pull:
15 #    https://github.com/chengdu/Speare
16
17 use strict;
18 use warnings;
19 use Cwd;
20 use File::Basename;
21 use utf8;
22
23 binmode STDIN, ':utf8';
24 binmode STDOUT, ':utf8';
25 binmode STDERR, ':utf8';
26
27 sub scanDirectory{
28     my $workdir = shift @_;
29     chdir($workdir) or die "Unable to enter dir $workdir:$!\n";
30     my ($startdir) = &cwd;
31     opendir(DIR, $workdir) or die "Unable to open $workdir:$!\n"; # "."
32     my @names = readdir(DIR) or die "Unable to read $workdir:$!\n";
33     closedir(DIR);
34
35     foreach my $name (@names){
36         next if ($name eq ".");
37         next if ($name eq "..");
38         my $filepath = $workdir."/".$name;
39         if (-d $filepath) {
40             &scanDirectory($filepath);
41             next;
42         }
43         my ($basename, $parentdir, $extension) = fileparse($filepath, qr/\.[^.]*$/);
44         #if ($name =~ /\.m$/) {
45         if ($extension eq ".m") {
46           print($filepath, "\n");
47           my $newpath = $workdir."/".$basename.".mat"; # .m --> .mat
48           my $command = "mv \"$filepath\" \"$newpath\"";
49           system($command);
50         }
51         chdir($startdir) or 
52            die "Unable to change to dir $startdir:$!\n";
53     }
54 }
55
56 # To prevent conflict with objective-c files,
57 # MATLAB source code file extension name must be renamed from .m to .mat.
58 if ($#ARGV + 1 == 1) {
59   my $srcdir = $ARGV[0];
60   print("Rename source files in ".$srcdir." ...\n");
61   &scanDirectory($srcdir);
62 }else{
63   print("Usage: perl rename.pl directory.\n");
64 }
65
66