using System; using System.Collections.Generic; using System.IO; using System.Text; using System.Xml; namespace sharpreleaser { public class Project { private string name; private string version; private string fileName; private string sourceFolder; private string destinationFolder; private List commands; public string Name { get { return name; } set { name = value; } } public string Version { get { return version; } set { version = value; } } public string FileName { get { return fileName; } set { fileName = value; } } public string SourceFolder { get { return sourceFolder; } set { sourceFolder = value; } } public string DestinationFolder { get { return destinationFolder; } set { destinationFolder = value; } } public List Commands { get { return commands; } set { commands = value; } } public Project() { name = "untitled"; version = "0.0.1"; fileName = "{name}-{version}.zip"; sourceFolder = string.Empty; destinationFolder = string.Empty; commands = new List(); } public Project(string name, string version, string filename, string sourceFolder, string destinationFolder) { this.name = name; this.version = version; this.fileName = filename; this.sourceFolder = sourceFolder; this.destinationFolder = destinationFolder; commands = new List(); } public static Project Read(string FileName) { XmlDocument document = new XmlDocument(); document.Load(FileName); Project project = null; foreach (XmlNode node in document.ChildNodes) { if (node.Name == "project") { project = ReadProject(node); } } return project; } private static Project ReadProject(XmlNode rootNode) { Project project = new Project(); foreach (XmlNode node in rootNode.ChildNodes) { switch (node.Name) { case "name": project.Name = node.InnerText; break; case "version": project.Version = node.InnerText; break; case "fileName": project.FileName = node.InnerText; break; case "sourceFolder": project.SourceFolder = node.InnerText; break; case "destinationFolder": project.DestinationFolder = node.InnerText; break; case "commands": project.Commands = ReadCommands(node); break; } } return project; } private static List ReadCommands(XmlNode rootNode) { List commands = new List(); foreach (XmlNode node in rootNode.ChildNodes) { if (node.Name == "command") { Command command = ReadCommand(node); commands.Add(command); } } return commands; } private static Command ReadCommand(XmlNode node) { Command command = null; string type = node.Attributes["type"].Value; switch (type) { case "DOS": DosCommand dosCommand = new DosCommand(); dosCommand.CommandText = node.InnerText; command = dosCommand; break; case "DELETE": DeleteCommand deleteCommand = new DeleteCommand(); deleteCommand.Recursive = node.Attributes["recursive"] != null ? bool.Parse(node.Attributes["recursive"].Value) : false; deleteCommand.CommandText = node.InnerText; command = deleteCommand; break; case "CREATE": CreateCommand createCommand = new CreateCommand(); createCommand.CommandText = node.InnerText; command = createCommand; break; case "COPY": CopyCommand copyCommand = new CopyCommand(); string filter = node.Attributes["filter"].Value; bool recursive = bool.Parse(node.Attributes["recursive"].Value); string destination = node.Attributes["destination"].Value; copyCommand.Filter = filter; copyCommand.Recursive = recursive; copyCommand.Destination = destination; command = copyCommand; break; case "ZIP": ZipCommand zipCommand = new ZipCommand(); zipCommand.Filter = node.Attributes["filter"].Value; zipCommand.Source = node.Attributes["source"].Value; zipCommand.CommandText = node.InnerText; command = zipCommand; break; case "FTP": FtpCommand ftpCommand = new FtpCommand(); string address = node.Attributes["address"].Value; string username = node.Attributes["username"].Value; string password = node.Attributes["password"].Value; ftpCommand.Address = address; ftpCommand.Username = username; ftpCommand.Password = password; ftpCommand.FileName = node.InnerText; command = ftpCommand; break; default: string commandText = node.InnerText; command = new Command(Command.CommandType.DOS, commandText); break; } string onError = node.Attributes["onError"] != null ? node.Attributes["onError"].Value : string.Empty; if (onError == "break") command.OnError = Command.FlowActions.Break; return command; } public void Write(string FileName) { XmlTextWriter writer = new XmlTextWriter(FileName, Encoding.Unicode); writer.Formatting = Formatting.Indented; writer.WriteStartDocument(); writer.WriteStartElement("project"); writer.WriteElementString("name", name); writer.WriteElementString("version", version); writer.WriteElementString("filename", fileName); writer.WriteElementString("sourceFolder", sourceFolder); writer.WriteElementString("destinationFolder", destinationFolder); writer.WriteStartElement("commands"); // foreach (Command command in commands) { writer.WriteStartElement("command"); writer.WriteAttributeString("type", command.Type.ToString()); //writer.WriteElementString("command", command.CommandText); writer.WriteString(command.CommandText); writer.WriteEndElement(); } writer.WriteEndElement(); // writer.WriteEndElement(); writer.Flush(); writer.Close(); } public string ParseVariables(string input) { string[] tokens = { "{name}", "{version}", "{filename}", "{sourceFolder}", "{destinationFolder}", "{temp}", "{programFiles}", "{myDocuments}" }; int substitutions = 0; foreach (string token in tokens) { if (input.Contains(token)) substitutions++; } if (substitutions == 0) { return input; } string output = input; // Project variables output = output.Replace("{name}", Name); output = output.Replace("{version}", Version); output = output.Replace("{filename}", FileName); output = output.Replace("{sourceFolder}", SourceFolder); output = output.Replace("{destinationFolder}", DestinationFolder); // System variables output = output.Replace("{temp}", Path.GetTempPath()); output = output.Replace("{programFiles}", Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles)); output = output.Replace("{myDocuments}", Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)); // Recursively parse variables output = ParseVariables(output); return output; } } }