update version number
[mcs.git] / tools / installvst / installvst.cs
bloba98193ba4ef5c7c78da1a2450cf96c6f1db1dc81
1 //
2 // Authors:
3 // Marek Habersack (mhabersack@novell.com)
4 //
5 // (C) 2007 Novell, Inc
6 //
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 using System;
29 using System.Collections;
30 using System.IO;
31 using System.Xml;
33 public class App
35 string templateFile;
36 string targetDir;
37 string targetPath;
38 string sourceDir;
40 XmlNamespaceManager nsmgr;
42 Stack folders;
44 public App (string templateFile, string targetDir)
46 this.templateFile = templateFile;
47 this.targetDir = targetDir;
49 this.sourceDir = Path.GetDirectoryName (templateFile);
50 if (this.sourceDir == null || this.sourceDir.Length == 0)
51 this.sourceDir = Directory.GetCurrentDirectory ();
54 public void Run ()
56 if (templateFile == null || templateFile.Length == 0 ||
57 targetDir == null || targetDir.Length == 0)
58 throw new ApplicationException ("Missing or invalid installation data");
60 XmlDocument doc = new XmlDocument ();
61 doc.Load (templateFile);
62 nsmgr = new XmlNamespaceManager (doc.NameTable);
63 nsmgr.AddNamespace ("def", "http://schemas.microsoft.com/developer/vstemplate/2005");
65 ReadTemplateData (doc);
66 InstallProject (doc);
69 void ReadTemplateData (XmlDocument doc)
71 XmlNode defaultName = doc.SelectSingleNode ("//def:VSTemplate[@Type='Project']/def:TemplateData/def:DefaultName", nsmgr);
72 if (defaultName == null)
73 throw new ApplicationException ("Input file is not a VisualStudio Template");
74 string folderName = defaultName.InnerText;
75 targetPath = Path.Combine (targetDir, folderName);
77 if (!Directory.Exists (targetPath))
78 Directory.CreateDirectory (targetPath);
80 folders = new Stack ();
83 string SafeGetAttribute (XmlNode node, string name)
85 XmlAttribute attr = node.Attributes [name];
86 if (attr != null)
87 return attr.Value;
88 return String.Empty;
91 string GetCurPath ()
93 string curPath = folders.Count > 0 ? (string) folders.Peek () : null;
94 if (curPath == null || curPath.Length == 0)
95 return targetPath;
96 return curPath;
99 void ProcessFolder (XmlNode node)
101 string curPath = GetCurPath ();
102 string folderPath = Path.Combine (curPath, SafeGetAttribute (node, "Name"));
103 if (!Directory.Exists (folderPath))
104 Directory.CreateDirectory (folderPath);
105 folders.Push (folderPath);
106 foreach (XmlNode child in node.ChildNodes)
107 ProcessNode (child);
108 folders.Pop ();
111 void ProcessItem (XmlNode node)
113 string curPath = GetCurPath ();
114 string srcName = node.InnerText;
115 string targetName = SafeGetAttribute (node, "TargetFileName");
116 string src = Path.Combine (sourceDir, srcName);
117 string dst = Path.Combine (curPath, targetName.Length > 0 ? targetName : srcName);
119 if (!File.Exists (src)) {
120 Console.WriteLine ("Warning: source file {0} does not exist.", src);
121 return;
123 File.Copy (src, dst, true);
126 void ProcessNode (XmlNode node)
128 if (node.NodeType != XmlNodeType.Element)
129 return;
131 switch (node.Name) {
132 case "Folder":
133 ProcessFolder (node);
134 break;
136 case "ProjectItem":
137 ProcessItem (node);
138 break;
142 void InstallProject (XmlDocument doc)
144 XmlNode project = doc.SelectSingleNode ("//def:VSTemplate[@Type='Project']/def:TemplateContent/def:Project", nsmgr);
145 if (project == null)
146 throw new ApplicationException ("Missing project contents in the template file");
148 foreach (XmlNode child in project.ChildNodes)
149 ProcessNode (child);
153 public class AppMain
155 public static void Main (string[] args)
157 if (args.Length < 2)
158 Usage ();
160 App app = new App (args [0], args [1]);
162 try {
163 app.Run ();
164 } catch (Exception ex) {
165 Console.WriteLine ("Failed to install template {0} in {1}\n{2}\n", args [0], args [1], ex.Message);
166 Console.WriteLine ("Exception: {0}", ex);
167 Environment.Exit (1);
171 static void Usage ()
173 Console.WriteLine ("Usage: installvst <VSTemplateFile> <DestinationPath>\n");
174 Environment.Exit (1);