Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
Program.cs
Go to the documentation of this file.
1 
2 using System;
3 using System.IO;
4 using Mono.Options;
5 using System.Reflection;
6 using System.Text;
7 using System.Diagnostics;
8 using SiliconStudio.Core.Diagnostics;
9 
10 namespace SiliconStudio.BuildEngine
11 {
12  class Program
13  {
14  private static string FormatLog(LogMessage message)
15  {
16  var builder = new StringBuilder();
17  TimeSpan timestamp = DateTime.Now - Process.GetCurrentProcess().StartTime;
18  builder.Append((timestamp.TotalMilliseconds * 0.001).ToString("0.000 "));
19  builder.Append(message.Module);
20  builder.Append(": ");
21  builder.Append(message.Text);
22  return builder.ToString();
23  }
24 
25  private static int Main(string[] args)
26  {
27  var exeName = Path.GetFileName(Assembly.GetExecutingAssembly().Location);
28  var showHelp = false;
29  var options = new BuilderOptions(Logger.GetLogger("BuildEngine"));
30 
31  var p = new OptionSet
32  {
33  "Copyright (C) 2011-2013 Silicon Studio Corporation. All Rights Reserved",
34  "Paradox Build Tool - Version: "
35  +
36  String.Format(
37  "{0}.{1}.{2}",
38  typeof(Program).Assembly.GetName().Version.Major,
39  typeof(Program).Assembly.GetName().Version.Minor,
40  typeof(Program).Assembly.GetName().Version.Build) + string.Empty,
41  string.Format("Usage: {0} [options]* inputfile -o outputfile", exeName),
42  string.Empty,
43  "=== Options ===",
44  string.Empty,
45  { "h|help", "Show this message and exit", v => showHelp = v != null },
46  { "v|verbose", "Show more verbose progress logs", v => options.Verbose = v != null },
47  { "d|debug", "Show debug logs (imply verbose)", v => options.Debug = v != null },
48  { "c|clean", "Clean the command cache, forcing to rebuild everything at the next build.", v => options.BuilderMode = Builder.Mode.Clean },
49  { "cd|clean-delete", "Clean the command cache and delete output objects", v => options.BuilderMode = Builder.Mode.CleanAndDelete },
50  { "b|build-path=", "Build path", v => options.BuildDirectory = v },
51  { "mdb|metadata-database=", "Optional ; indicate the directory containing the Metadata database, if used.", v => { if (!string.IsNullOrEmpty(v)) options.MetadataDatabaseDirectory = v; } },
52  { "o|output-path=", "Optional ; indicate an output path to copy the built assets in.", v => options.OutputDirectory = v },
53  { "cfg|config=", "Configuration name", v => options.Configuration = v },
54  { "log", "Enable file logging", v => options.EnableFileLogging = v != null },
55  { "log-file=", "Log build in a custom file.", v =>
56  {
57  options.EnableFileLogging = v != null;
58  options.CustomLogFileName = v;
59  } },
60  { "monitor-pipe=", "Monitor pipe.", v =>
61  {
62  if (!string.IsNullOrEmpty(v))
63  options.MonitorPipeNames.Add(v);
64  } },
65  { "slave=", "Slave pipe", v => options.SlavePipe = v }, // Benlitz: I don't think this should be documented
66  { "s|sourcebase=", "Optional ; Set the base directory for the source files. Not required if all source paths are absolute", v => options.SourceBaseDirectory = v },
67  { "a|append", "If set, the existing asset mappings won't be deleted.", v => options.Append = v != null },
68  { "t|threads=", "Number of threads to create. Default value is the number of hardware threads available.", v => options.ThreadCount = int.Parse(v) },
69  { "p|plugin=", "Add plugin directory.", v =>
70  {
71  if (!string.IsNullOrEmpty(v))
72  options.Plugins.AddPluginFolder(v);
73  } },
74  { "test=", "Run a test session.", v => options.TestName = v }
75  };
76 
77  TextWriterLogListener fileLogListener = null;
78 
79  // Output logs to the console with colored messages
80  if (options.SlavePipe == null)
81  {
82  var consoleLogListener = new ConsoleLogListener { TextFormatter = FormatLog };
83  GlobalLogger.MessageLogged += consoleLogListener;
84  }
85 
86  // Setting up plugin manager
87  options.Plugins.AddPluginFolder(Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) ?? "", "BuildPlugins"));
88  options.Plugins.Register();
89 
90  BuildResultCode exitCode;
91 
92  try
93  {
94  options.InputFiles = p.Parse(args);
95 
96  // Also write logs from master process into a file
97  if (options.SlavePipe == null)
98  {
99  if (options.EnableFileLogging)
100  {
101  string logFileName = options.CustomLogFileName;
102  if (string.IsNullOrEmpty(logFileName))
103  {
104  string inputName = "NoInput";
105  if (options.InputFiles.Count > 0)
106  inputName = Path.GetFileNameWithoutExtension(options.InputFiles[0]);
107 
108  logFileName = "Logs/Build-" + inputName + "-" + DateTime.Now.ToString("yy-MM-dd-HH-mm") + ".txt";
109  }
110 
111  string dirName = Path.GetDirectoryName(logFileName);
112  if (dirName != null)
113  Directory.CreateDirectory(dirName);
114 
115  fileLogListener = new TextWriterLogListener(new FileStream(logFileName, FileMode.Create)) { TextFormatter = FormatLog };
116  GlobalLogger.MessageLogged += fileLogListener;
117  }
118  options.Logger.Info("BuildEngine arguments: " + string.Join(" ", args));
119  options.Logger.Info("Starting builder.");
120  }
121 
122  if (showHelp)
123  {
124  p.WriteOptionDescriptions(Console.Out);
125  exitCode = BuildResultCode.Successful;
126  }
127  else if (!string.IsNullOrEmpty(options.TestName))
128  {
129  var test = new TestSession();
130  test.RunTest(options.TestName, options.Logger);
131  exitCode = BuildResultCode.Successful;
132  }
133  else
134  {
135  exitCode = BuildEngineCommands.Build(options);
136  }
137  }
138  catch (OptionException e)
139  {
140  options.Logger.Error("{0}", e);
141  exitCode = BuildResultCode.CommandLineError;
142  }
143  catch (Exception e)
144  {
145  options.Logger.Error("{0}", e);
146  exitCode = BuildResultCode.BuildError;
147  }
148  finally
149  {
150  if (fileLogListener != null)
151  fileLogListener.LogWriter.Close();
152 
153  }
154  return (int)exitCode;
155  }
156  }
157 }
System.IO.FileMode FileMode
Definition: ScriptSync.cs:33
A LogListener implementation redirecting its output to a TextWriter.
Base implementation for ILogger.
Definition: Logger.cs:10
A base log message used by the logging infrastructure.
Definition: LogMessage.cs:13
A LogListener implementation redirecting its output to the default OS console. If console is not supp...