* Mono.Documentation/webdoc.cs: Add --use-system-sources option so
[mono.git] / mcs / tools / mdoc / Mono.Documentation / webdoc.cs
1 //
2 // webdoc.cs
3 //
4 // Author:
5 //   Jonathan Pryor  <jpryor@novell.com>
6 //
7 // Copyright (c) 2009 Novell, Inc. (http://www.novell.com)
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.
27 //
28
29 using System;
30 using System.Collections.Generic;
31 using System.Diagnostics;
32 using System.IO;
33 using System.Linq;
34 using System.Text;
35 using System.Web;
36
37 using Monodoc;
38 using Mono.Documentation;
39
40 using Mono.Options;
41 using Mono.Rocks;
42
43 using ICSharpCode.SharpZipLib.Zip;
44
45 namespace Mono.Documentation
46 {
47         public class MDocExportWebdocHtml : MDocCommand
48         {
49                 class Options {
50                         public Dictionary<string, List<string>> Formats = new Dictionary<string, List<string>>();
51                         public List<string> Sources = new List<string>();
52                         public bool UseSystemSources = true;
53                         public bool ForceUpdate = false;
54                         public string OutputDirectory = null;
55                 }
56
57                 public override void Run (IEnumerable<string> args)
58                 {
59                         var opts = new Options ();
60                         var formatOptions = MDocAssembler.CreateFormatOptions (this, opts.Formats);
61                         var options = new OptionSet () {
62                                 { "force-update",
63                                         "Always generate new files.  If not specified, will only generate " +
64                                         "files if the write time of the output directory is older than the " +
65                                         "write time of the source .tree/.zip files.",
66                                         v => opts.ForceUpdate = v != null },
67                                 formatOptions [0],
68                                 formatOptions [1],
69                                 { "o|out=",
70                                         "The {PREFIX} to place the generated files and directories.  " + 
71                                         "Default: \"`dirname FILE`/cache/\".\n" +
72                                         "Underneath {PREFIX}, `basename FILE .tree` directories will be " + 
73                                         "created which will contain the pre-generated HTML content.",
74                                         v => opts.OutputDirectory = v },
75                                 { "r=",
76                                         "A {SOURCE} file to use for reference purposes.\n" +
77                                         "Extension methods are searched for among all {SOURCE}s which are referenced.\n" +
78                                         "This option may be specified multiple times.",
79                                         v => opts.Sources.Add (v) },
80                                 { "use-system-sources",
81                                         "Use the system-wide .source files for reference purposes. " +
82                                         "Default is " + (opts.UseSystemSources ? "enabled" : "disabled") + ".",
83                                         v => opts.UseSystemSources = v != null },
84                         };
85                         Parse (options, args, "export-html-webdoc", 
86                                         "[OPTIONS]+ FILES",
87                                         "Export mdoc documentation within FILES to HTML for use by ASP.NET webdoc.\n\n" +
88                                         "FILES are .tree or .zip files as produced by 'mdoc assemble'.");
89                         if (opts.Formats.Values.All (files => files.Count == 0))
90                                 Error ("No files specified.");
91                         HelpSource.use_css = true;
92                         HelpSource.FullHtml = false;
93                         SettingsHandler.Settings.EnableEditing = false;
94                         foreach (var p in opts.Formats)
95                                 ProcessFiles (opts, p.Key, p.Value);
96                 }
97
98                 void ProcessFiles (Options opts, string format, List<string> files)
99                 {
100                         foreach (var basePath in 
101                                         files.Select (f => 
102                                                         Path.Combine (Path.GetDirectoryName (f), Path.GetFileNameWithoutExtension (f)))
103                                         .Distinct ()) {
104                                 string treeFile = basePath + ".tree";
105                                 string zipFile  = basePath + ".zip";
106                                 if (!Exists (treeFile) || !Exists (zipFile))
107                                         continue;
108                                 string outDir = opts.OutputDirectory != null 
109                                         ? Path.Combine (opts.OutputDirectory, Path.GetFileName (basePath))
110                                         : XmlDocUtils.GetCacheDirectory (basePath);
111                                 if (!opts.ForceUpdate && Directory.Exists (outDir) &&
112                                                         MaxWriteTime (treeFile, zipFile) < Directory.GetLastWriteTime (outDir))
113                                         continue;
114                                 Message (TraceLevel.Warning, "Processing files: {0}, {1}", treeFile, zipFile);
115                                 Directory.CreateDirectory (outDir);
116                                 ExtractZipFile (zipFile, outDir);
117                                 GenerateCache (opts, basePath, format, outDir);
118                         }
119                 }
120
121                 bool Exists (string file)
122                 {
123                         if (!File.Exists (file)) {
124                                         Message (TraceLevel.Error,
125                                                         "mdoc: Could not find file: {0}", file);
126                                         return false;
127                         }
128                         return true;
129                 }
130
131                 DateTime MaxWriteTime (params string[] files)
132                 {
133                         return files.Select (f => File.GetLastWriteTime (f)).Max ();
134                 }
135
136                 void ExtractZipFile (string zipFile, string outDir)
137                 {
138                         ZipInputStream zip = new ZipInputStream (File.OpenRead (zipFile));
139
140                         ZipEntry entry;
141                         while ((entry = zip.GetNextEntry ()) != null) {
142                                 string file = Path.Combine (outDir, entry.Name);
143                                 Directory.CreateDirectory (Path.GetDirectoryName (file));
144                                 using (var output = File.OpenWrite (file))
145                                         zip.WriteTo (output);
146                         }
147                 }
148
149                 void GenerateCache (Options opts, string basePath, string format, string outDir)
150                 {
151                         var hs = RootTree.GetHelpSource (format, basePath);
152                         if (hs == null) {
153                                 Error ("Unable to find a HelpSource for provider '{0}' and file '{1}.tree'.", format, basePath);
154                         }
155                         var tree = hs.Tree;
156                         RootTree docRoot = null;
157                         if (!opts.UseSystemSources)
158                                 docRoot = RootTree.LoadTree (null, null, opts.Sources);
159                         else {
160                                 docRoot = RootTree.LoadTree ();
161                                 foreach (var source in opts.Sources)
162                                         docRoot.AddSourceFile (source);
163                         }
164                         hs.RootTree = docRoot;
165                         string helpSourceName = Path.GetFileName (basePath);
166                         foreach (Node node in tree.TraverseDepthFirst<Node, Node> (t => t, t => t.Nodes.Cast<Node> ())) {
167                                 var url = node.URL;
168                                 Message (TraceLevel.Info, "\tProcessing URL: {0}", url);
169                                 if (string.IsNullOrEmpty (url))
170                                         continue;
171                                 var file = XmlDocUtils.GetCachedFileName (outDir, url);
172                                 using (var o = File.AppendText (file)) {
173                                         Node _;
174                                         string contents = hs.GetText (url, out _) ?? hs.RenderNamespaceLookup (url, out _);
175                                         o.Write (contents);
176                                 }
177                         }
178                 }
179         }
180 }