~raof/do-plugins/bansheeplugin

« back to all changes in this revision

Viewing changes to File/src/Do/Do.FilesAndFolders/FileTransformation.cs

  • Committer: Alex Launi
  • Date: 2009-01-12 22:08:01 UTC
  • mfrom: (276.29.21 plugins-trunk)
  • Revision ID: alex.launi@gmail.com-20090112220801-te84p7e7av19gt1j
merge trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* FileTransformation.cs
 
2
 *
 
3
 * GNOME Do is the legal property of its developers. Please refer to the
 
4
 * COPYRIGHT file distributed with this source distribution.
 
5
 *
 
6
 * This program is free software: you can redistribute it and/or modify
 
7
 * it under the terms of the GNU General Public License as published by
 
8
 * the Free Software Foundation, either version 3 of the License, or
 
9
 * (at your option) any later version.
 
10
 *
 
11
 * This program is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 * GNU General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU General Public License
 
17
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
18
 */
 
19
 
 
20
using System;
 
21
using System.IO;
 
22
using System.Linq;
 
23
 
 
24
using Do.Platform;
 
25
 
 
26
namespace Do.FilesAndFolders
 
27
{
 
28
 
 
29
        public delegate bool IncludeFile (string source);
 
30
        public delegate void TransformFile (string source, string destination);
 
31
 
 
32
        /// <summary>
 
33
        /// A class representing a recursive file transformation.
 
34
        /// </summary>
 
35
        public class FileTransformation
 
36
        {
 
37
 
 
38
                static bool DefaultIncludeFile (string source)
 
39
                {
 
40
                        return !". ..".Contains (Path.GetFileName (source));
 
41
                }
 
42
                
 
43
                public TransformFile Transformation { get; private set; }
 
44
                public IncludeFile Include { get; private set; }
 
45
                
 
46
                public FileTransformation (TransformFile transform) :
 
47
                        this (transform, DefaultIncludeFile)
 
48
                {
 
49
                }
 
50
 
 
51
                public FileTransformation (TransformFile transform, IncludeFile include)
 
52
                {
 
53
                        Transformation = transform;
 
54
                        Include = include;
 
55
                }
 
56
 
 
57
                /// <summary>
 
58
                /// Recursively perform transformation an all files at or below source
 
59
                /// to destination.
 
60
                /// </summary>
 
61
                /// <param name="source">
 
62
                /// A <see cref="System.String"/> source file or directory to transform.
 
63
                /// </param>
 
64
                /// <param name="destination">
 
65
                /// A <see cref="System.String"/> destination file or directory to transform to.
 
66
                /// </param>
 
67
                public void Transform (string source, string destination)
 
68
                {
 
69
                        string target = Path.Combine (destination, Path.GetFileName (source));
 
70
                        Transformation (source, target);
 
71
                        if (Directory.Exists (source)) {
 
72
                                Directory.GetFileSystemEntries (source)
 
73
                                        .Where (f => Include (f))
 
74
                                        .ForEach (f => Transform (f, target));
 
75
                        }
 
76
                }
 
77
        }
 
78
}