PowerShellでTodo管理 その3

全ソース

TodoProvider.cs

using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Management.Automation;
using System.Management.Automation.Provider;
using System.Collections.ObjectModel;
using System.Diagnostics;

namespace PSTodo {
    /// <summary>
    /// Todo機能を提供するプロバイダクラス
    /// </summary>
    [CmdletProvider("Todo", ProviderCapabilities.ShouldProcess)]
    public class TodoProvider : NavigationCmdletProvider {
        public TodoProvider() { }
        
        private void Serialize(TodoItem todoItem, string path) {
            using(FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write)) {
                BinaryFormatter formatter = new BinaryFormatter();

                formatter.Serialize(fs, todoItem);
            }
        }
        private TodoItem Deserialize(string path) {
            using(FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read)) {
                BinaryFormatter formatter = new BinaryFormatter();

                return (TodoItem)formatter.Deserialize(fs);
            }
        }

        protected override Collection<PSDriveInfo> InitializeDefaultDrives() {
            string rootPath = Path.Combine(
                Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "PSTodo"
            );
            Directory.CreateDirectory(rootPath);

            Collection<PSDriveInfo> driveInfos = new Collection<PSDriveInfo>();
            driveInfos.Add(
                new PSDriveInfo("Todo", base.ProviderInfo, rootPath, "", base.Credential)
            );
            return driveInfos;
        }
        protected override void GetChildItems(string path, bool recurse) {
            foreach(string fileName in Directory.GetFiles(path)) {
                WriteItemObject(
                    Deserialize(fileName), fileName, false
                );
            }
        }
        protected override bool IsItemContainer(string path) {
            return Directory.Exists(path);
        }
        protected override bool ItemExists(string path) {
            return true;
        }
        protected override void NewItem(string path, string itemTypeName, object newItemValue) {
            TodoItem todoItem = new TodoItem(Path.GetFileName(path));
            todoItem.Description = newItemValue != null ? newItemValue.ToString() : string.Empty;

            Serialize(todoItem, path);

            WriteItemObject(todoItem, path, false);
        }
        protected override void RemoveItem(string path, bool recurse) {
            File.Delete(path);
        }
        protected override bool HasChildItems(string path) {
            return Directory.Exists(path) && Directory.GetFiles(path).Length > 0;
        }
        protected override bool IsValidPath(string path) {
            return true;
        }
    }
}

TodoSnapIn.cs
using System;
using System.Management.Automation;
using System.ComponentModel;
using System.Diagnostics;

namespace PSTodo.Configuration {
    /// <summary>
    /// SnapInをインストールするクラス
    /// </summary>
    [RunInstaller(true)]
    public class TodoSnapIn : PSSnapIn {
        public TodoSnapIn() { }

        public override string Description {
            get { return "PowerShellにTodo機能を追加します。"; }
        }
        public override string Name {
            get { return "PSTodo"; }
        }
        public override string Vendor {
            get { return "coma2n"; }
        }
    }
}
TodoItem.cs

using System;
using System.Diagnostics;

namespace PSTodo {
    /// <summary>
    /// Todoアイテムの情報を格納するクラス
    /// </summary>
    [Serializable]
    [DebuggerStepThrough]
    public class TodoItem {
        private string title;
        private string description;
        private bool isComplete;

        /// <summary>
        /// タイトルを取得、設定します。
        /// </summary>
        public string Title {
            get { return title; }
            set { title = value; }
        }
        /// <summary>
        /// 詳細を取得、設定します。
        /// </summary>
        public string Description {
            get { return description; }
            set { description = value; }
        }
        /// <summary>
        /// 完了したかどうかを取得、設定します。
        /// </summary>
        public bool IsComplete {
            get { return isComplete; }
            set { isComplete = value; }
        }

        public TodoItem() { }
        public TodoItem(string title) {
            this.title = title;
        }

        public override string ToString() {
            return string.Format("Title = {0}", Title);
        }
    }
}