Upgrade to ExtJS 4.0.1 - Released 05/18/2011
[extjs.git] / jsbuilder / src / Filesystem.js
1 Filesystem = {
2     exists : function(path) {
3         return system.exists(path);
4     },
5
6     getFullPath: function(path) {
7         var currentPath = system.setcwd(path);
8         return system.setcwd(currentPath);
9     },
10
11     getPath: function(path){
12         return path.replace(/\//g, Fs.sep);
13     },
14
15     mkdir: function(path) {
16         if (Platform.isWindows) {
17             system.mkdir(path);
18         }
19         else {
20             Cmd.execute('mkdir -p ' + path);
21         }
22         return this.getFullPath(path);
23     },
24
25     readFile : function(file) {
26         if (!Fs.exists(file)) {
27             Logger.log("[ERROR] File '" + file + "' does not exist or is not readable!");
28             return '';
29         }
30
31         file = new Stream(file);
32         var contents = file.readFile();
33         file.close();
34
35         return contents;
36     },
37
38     writeFile: function(file, contents) {
39         file = new Stream(file, 'w');
40         file.writeln(contents);
41         file.close();
42
43         return contents;
44     },
45
46     copy: function(src, dest) {
47         src = Fs.getPath(src);
48         dest = Fs.getPath(dest);
49
50         if (Platform.isWindows) {
51             if (Fs.endsWith(src, Fs.sep)) {
52                 src = src.slice(0, -1); // cut off any trailing \
53             }
54
55             /**
56              * Check if we're copying a single file. This isn't bulletproof, however xcopy
57              * will prompt regarding if the item is a directory or file, with no way to
58              * suppress the prompt. As such, this will catch a majority of scenarios
59              * and actually make the build work!
60              */
61             var isFile = /\.[0-9a-z]{2,4}$/i;
62             if (isFile.test(src)) {
63                 system.copy(src, dest);
64             } else {
65                 Cmd.execute('xcopy ' + src + ' ' + dest + ' /E /Y /I');
66             }
67         }
68         else {
69             try {
70                 // q: quiet
71                 // r: recursive
72                 // u: only update if newer
73                 // p: keep permissions
74                 // L: copy the contents of symlinks
75                 Cmd.execute('rsync -qrupL ' + src + ' ' + dest);
76             }
77             catch(e) {
78                 Cmd.execute('cp -Rpf ' + src + ' ' + dest);
79             }
80         }
81     },
82
83     endsWith: function(str, last){
84         return str.lastIndexOf(last) == str.length - 1;
85     },
86
87     split: function(file) {
88         var split = [];
89         if (!Fs.exists(file)) {
90             return split;
91         }
92         file = new Stream(file);
93         while (!file.eof) {
94             split.push(file.readln().trim());
95         }
96         return split;
97     },
98
99     remove: function(file) {
100         if (Platform.isWindows) {
101             system.remove(file);
102         } else {
103             Cmd.execute('rm -Rf "' + file + '"');
104         }
105     }
106 };
107
108 // Create short alias
109 Fs = Filesystem;
110
111 Fs.sep = (Fs.getFullPath('.')[0] == '/') ? '/': '\\';
112 Fs.fileWorkingDir = Fs.getFullPath('.');