OLD | NEW |
| (Empty) |
1 /* | |
2 Copyright 2011 Google Inc | |
3 | |
4 Licensed under the Apache License, Version 2.0 (the "License"); | |
5 you may not use this file except in compliance with the License. | |
6 You may obtain a copy of the License at | |
7 | |
8 http://www.apache.org/licenses/LICENSE-2.0 | |
9 | |
10 Unless required by applicable law or agreed to in writing, software | |
11 distributed under the License is distributed on an "AS IS" BASIS, | |
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 See the License for the specific language governing permissions and | |
14 limitations under the License. | |
15 */ | |
16 | |
17 using System.IO; | |
18 using Google.Apis.Samples.Helper; | |
19 | |
20 namespace Google.Build.Utils | |
21 { | |
22 /// <summary> | |
23 /// Directory helper utils. | |
24 /// </summary> | |
25 public static class DirUtils | |
26 { | |
27 /// <summary> | |
28 /// Removes all files and subdirectories of the specified directory. | |
29 /// </summary> | |
30 public static void ClearDir(string dir) | |
31 { | |
32 CommandLine.WriteAction("Clearing '" Path.GetFileName(dir) "' ..
."); | |
33 Directory.Delete(dir, true); | |
34 Directory.CreateDirectory(dir); | |
35 } | |
36 | |
37 /// <summary> | |
38 /// Clears or creates the specified directory. | |
39 /// </summary> | |
40 public static void ClearOrCreateDir(string dir) | |
41 { | |
42 if (Directory.Exists(dir)) | |
43 { | |
44 ClearDir(dir); | |
45 } | |
46 else | |
47 { | |
48 Directory.CreateDirectory(dir); | |
49 CommandLine.WriteAction("Creating '" Path.GetFileName(dir) "' ..
."); | |
50 } | |
51 } | |
52 | |
53 /// <summary> | |
54 /// Retrieves the relative path from the parent to the child. | |
55 /// </summary> | |
56 public static string GetRelativePath(string child, string parent) | |
57 { | |
58 string absoluteChild = Path.GetFullPath(child); | |
59 string absoluteParent = Path.GetFullPath(parent); | |
60 | |
61 if (!absoluteChild.StartsWith(absoluteParent Path.DirectorySeparat
orChar)) | |
62 { | |
63 return absoluteChild; // Nothing in common. | |
64 } | |
65 | |
66 return absoluteChild.Substring(absoluteParent.Length 1); | |
67 } | |
68 | |
69 /// <summary> | |
70 /// Copies a single file to the specified destination. | |
71 /// </summary> | |
72 /// <param name="sourceFile">The source file.</param> | |
73 /// <param name="destFolder">The destination folder.</param> | |
74 public static void CopyFile(string sourceFile, string destFolder) | |
75 { | |
76 CommandLine.WriteAction( | |
77 "Copying '" Path.GetFileName(sourceFile) "' to '" Path.Get
FileName(destFolder) "/' ..."); | |
78 | |
79 if (!Directory.Exists(destFolder)) | |
80 { | |
81 Directory.CreateDirectory(destFolder); | |
82 } | |
83 | |
84 string destFile = Path.Combine(destFolder, Path.GetFileName(sourceFi
le)); | |
85 File.Copy(sourceFile, destFile, true); | |
86 } | |
87 | |
88 /// <summary> | |
89 /// Copies all files and directories from the source to the destination. | |
90 /// </summary> | |
91 public static void CopyFiles(string source, string dest) | |
92 { | |
93 CommandLine.WriteAction( | |
94 "Copying '" Path.GetFileName(source) "' to '" Path.GetFile
Name(dest) "' ..."); | |
95 | |
96 foreach (var file in Directory.GetFiles(source, "*", SearchOption.Al
lDirectories)) | |
97 { | |
98 string fileDest = Path.Combine(dest, GetRelativePath(file, sourc
e)); | |
99 string dir = Path.GetDirectoryName(fileDest); | |
100 | |
101 if (!string.IsNullOrEmpty(dir) && !Directory.Exists(dir)) | |
102 { | |
103 Directory.CreateDirectory(dir); | |
104 } | |
105 | |
106 File.Copy(file, fileDest, true); | |
107 } | |
108 } | |
109 } | |
110 } | |
OLD | NEW |