root/lang/csharp/Misuzilla.Build.Tasks.Hash/Misuzilla.Build.Tasks.Hash/HashTask.cs

Revision 194, 1.8 kB (checked in by mayuki, 16 months ago)

lang/csharp/Misuzilla.Build.Tasks.Hash: 生成するファイルの拡張子を小文字にした。

  • Property svn:keywords set to Id
Line 
1// $Id$
2using System;
3using System.Collections.Generic;
4using System.Text;
5using Microsoft.Build.Framework;
6using System.Security.Cryptography;
7using System.IO;
8
9namespace Misuzilla.Build.Tasks.Hash
10{
11    public abstract class HashTask : Microsoft.Build.Utilities.Task
12    {
13        private ITaskItem[] _files;
14
15        public abstract HashAlgorithm HashAlgorithm { get; }
16        public abstract String HashAlgorithmName { get; }
17
18        [Required]
19        public ITaskItem[] Files
20        {
21            get { return _files; }
22            set { _files = value; }
23        }
24
25        public override bool Execute()
26        {
27            foreach (ITaskItem item in _files)
28            {
29                if (item.ItemSpec.Length > 0)
30                {
31                    try
32                    {
33                        Log.LogMessage("Generate " + Path.GetFileName(item.ItemSpec + "." + HashAlgorithmName.ToLower()));
34                        WriteHash(item.ItemSpec);
35                    }
36                    catch (IOException e)
37                    {
38                        Log.LogError(e.Message);
39                        return false;
40                    }
41                }
42            }
43            return true;
44        }
45
46        private void WriteHash(String path)
47        {
48            using (FileStream fsTarget = new FileStream(path, FileMode.Open, FileAccess.Read))
49            using (StreamWriter swHash = new StreamWriter(path + "." + HashAlgorithmName.ToLower(), false))
50            {
51                Byte[] hash = HashAlgorithm.ComputeHash(fsTarget);
52                swHash.Write("{0}({1})= ", HashAlgorithmName, Path.GetFileName(path));
53                foreach (Byte b in hash)
54                    swHash.Write("{0:x2}", b);
55            }
56        }
57    }
58}
Note: See TracBrowser for help on using the browser.