3 // Marek Habersack (mhabersack@novell.com)
5 // (C) 2007 Novell, Inc
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
29 using System.Collections;
40 XmlNamespaceManager nsmgr;
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 ();
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);
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];
93 string curPath = folders.Count > 0 ? (string) folders.Peek () : null;
94 if (curPath == null || curPath.Length == 0)
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)
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);
123 File.Copy (src, dst, true);
126 void ProcessNode (XmlNode node)
128 if (node.NodeType != XmlNodeType.Element)
133 ProcessFolder (node);
142 void InstallProject (XmlDocument doc)
144 XmlNode project = doc.SelectSingleNode ("//def:VSTemplate[@Type='Project']/def:TemplateContent/def:Project", nsmgr);
146 throw new ApplicationException ("Missing project contents in the template file");
148 foreach (XmlNode child in project.ChildNodes)
155 public static void Main (string[] args)
160 App app = new App (args [0], args [1]);
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);
173 Console.WriteLine ("Usage: installvst <VSTemplateFile> <DestinationPath>\n");
174 Environment.Exit (1);