Ejemplo n.º 1
0
		private static int Main(string[] args)
		{
			ComputeTemplatePath();

			bool overrideFile = false;
			List<string> files = new List<string>();
			foreach (var arg in args)
			{
				if (IsOverrideArg(arg))
				{
					overrideFile = true;
				}
				else
				{
					FileInfo xml = new FileInfo(arg);
					if (!xml.Exists)
					{
						Console.WriteLine("File not found: " + xml.FullName);
						return -1;
					}
					files.Add(arg);
				}
			}

			if (files.Count < 1)
			{
				Console.WriteLine("Model# "
				                  + System.Reflection.Assembly.GetExecutingAssembly().GetName().Version);
				Console.WriteLine("");
				Console.WriteLine("Use:");
				Console.WriteLine("    ModelSharp [-override] <model.ms>");
				return 1;
			}

			int ret = 0;
			foreach (var file in files)
			{
				var modelProcessor = new ModelProcessor(templatesPath, file, overrideFile);
				modelProcessor.Logger = new Logger();

				var result = modelProcessor.Process();
				if (!result.Success)
					ret = -1;
			}

			if (ret != 0)
				Console.WriteLine("Finished with error(s)");
			return ret;
		}
Ejemplo n.º 2
0
		private void GenerateCode(object sender, GenerationEventArgs e)
		{
			e.OutputFileExtension = ".dot";

			string templatesDir = GetInstallPath("templates");
			if (!Directory.Exists(templatesDir))
			{
				e.GenerateError("Missing templates from install: " + templatesDir);
				return;
			}

			string libDll = GetInstallPath(GetInstallPath(@"lib\ModelSharp.Lib.dll"));
			if (!Directory.Exists(templatesDir))
			{
				e.GenerateError("Missing dll from install: " + libDll);
				return;
			}

			string tempFile = Path.GetTempFileName();
			string tempDir = null;
			try
			{
				tempDir = Directory.CreateDirectory(tempFile + "_dir").FullName;

				var modelProcessor = new ModelProcessor(templatesDir, e.InputFilePath, true);
				modelProcessor.BaseOutputPath = tempDir;
				modelProcessor.GlobalConfig.ProjectNamespace = e.Namespace;

				var result = modelProcessor.Process();

				// Output to console
//				foreach (var msg in result.Messages)
//				{
//					string txt;
//					if (msg.Line > 0)
//						txt = String.Format("[{0}] [{1:0000} : {2:000}] {3}",
//						                                msg.Error ? "ERROR" : "INFO ", msg.Line, msg.Column,
//						                                msg.Description);
//					else
//						txt = String.Format("[{0}] {1}", msg.Error ? "ERROR" : "INFO ", msg.Description);
//
//					Console.WriteLine(txt); 
//				}

				if (!result.Success)
				{
					e.GenerateError("Error generating model");

					foreach (var msg in result.Messages)
					{
						if (msg.Error)
						{
							if (msg.Line > 0)
								e.GenerateError(msg.Description, msg.Line - 1, msg.Column);
							else
								e.GenerateError(msg.Description);
						}
					}
					return;
				}

				//string filename = Path.Combine(tempDir, names.First());
				//foreach (var line in File.ReadLines(filename))
				//{
				//    e.OutputCode.AppendLine(line);
				//}

				string inputDir = new FileInfo(e.InputFilePath).DirectoryName;

				MergeFiles(result.NotToChangeFilenames, tempDir, inputDir, false);
				MergeFiles(result.EditableFilenames, tempDir, inputDir, true);

				SetSubItems(e, inputDir, result.NotToChangeFilenames);
				AddToProject(e, inputDir, result.EditableFilenames);

				EnsureDllIsInProject(e, libDll);
			}
			finally
			{
				if (tempDir != null)
				{
					foreach (var file in Directory.GetFiles(tempDir))
					{
						if (file == "." || file == "..")
							continue;

						DeleteFile(Path.Combine(tempDir, file), e);
					}

					DeleteDir(tempDir, e);
				}

				DeleteFile(tempFile, e);
			}
		}