示例#1
0
	  private void CompileClojureScript(string filePath, string inputFileContents, Action<string> outputResult)
		{
			new System.Threading.Thread(() =>
			{
				outputResult("/* compiling ... */");

				string runtimeDir = string.Format("{0}\\{1}-{2}", EnvironmentVariables.VsClojureRuntimesDir, Constants.CLOJURESCRIPT, Constants.VERSION);
				List<string> paths = Directory.GetFiles(string.Format("{0}\\lib\\", runtimeDir), "*.jar", SearchOption.AllDirectories).ToList();
				paths.Add(string.Format("{0}\\src\\clj", runtimeDir));
				paths.Add(string.Format("{0}\\src\\cljs", runtimeDir));
				paths.Add(string.Format("{0}\\lib", runtimeDir));

				string classPath = paths.Aggregate((x, y) => x + ";" + y);
				string compilerPath = String.Format("{0}\\bin\\cljsc.clj", runtimeDir);

				string inputFileName = Path.GetTempFileName();
				using (StreamWriter outfile = new StreamWriter(inputFileName))
				{
					outfile.Write(inputFileContents);
				}

				string workingDirectory = GetTempDirectory();

				Process newProcess = new Process();
				newProcess.StartInfo.UseShellExecute = false;
				newProcess.StartInfo.RedirectStandardOutput = true;
				newProcess.StartInfo.RedirectStandardError = true;
				newProcess.StartInfo.CreateNoWindow = true;
				newProcess.StartInfo.FileName = "java";
				const string optimizations = OPTIMIZE_COMPILED_JAVASCRIPT ? "{:optimizations :advanced}" : "";
				string arguments = string.Format("-server -cp \"{0}\" clojure.main \"{1}\" \"{2}\" {3}", classPath, compilerPath, inputFileName, optimizations);
				newProcess.StartInfo.Arguments = arguments;
				newProcess.StartInfo.WorkingDirectory = workingDirectory;

				string standardOutput = "";
				string standardError = "";
				lock (filesBeingCompiledLock)
				{
					if (filesBeingCompiled.ContainsKey(filePath))
					{
						Process oldProcess = filesBeingCompiled[filePath];
						try
						{
							oldProcess.Kill();
						}
						catch { }
					}

					filesBeingCompiled[filePath] = newProcess;

					IntPtr oldWow64Redirection = new IntPtr();
					Win32Api.Wow64DisableWow64FsRedirection(ref oldWow64Redirection);

					try
					{
						newProcess.Start();
					}
					catch (Exception e)
					{
						standardError = e.Message + Environment.NewLine + "Ensure you have the latest version of Java and the JDK installed from Oracle.com" + Environment.NewLine + "Ensure the directory containing java is on the path environment variable (usually C:\\Program Files (x86)\\Java\\jre7\\bin)";
					}

					Win32Api.Wow64RevertWow64FsRedirection(oldWow64Redirection);
				}

				if (string.IsNullOrWhiteSpace(standardError))
				{
					standardOutput = newProcess.StandardOutput.ReadToEnd();
					standardError = newProcess.StandardError.ReadToEnd();

					newProcess.WaitForExit();
				}

				_errorListHelper.ClearErrors(filePath);

				if (!string.IsNullOrWhiteSpace(standardError))
				{
					standardError = string.Format("/*{0}{1}{0}*/{0}", Environment.NewLine, standardError);

					List<string> compilerErrors = new Regex("^Exception in thread \"main\" [^:]*:(.*)compiling:\\(.:[^:]*:([0-9]*):([0-9]*)\\)", RegexOptions.Multiline).Matches(standardError, 0).Cast<Match>().Select(x => string.Format("({0}, {1}, {0}, {1}): {2}", x.Groups[2].Value, x.Groups[3].Value, x.Groups[1].Value)).ToList();
					compilerErrors.AddRange(new Regex("^Caused by:[^:]*:(.*)", RegexOptions.Multiline).Matches(standardError, 0).Cast<Match>().Select(x => x.Groups[1].Value).ToList());

					foreach (string compilerError in compilerErrors)
					{
						_errorListHelper.Write(TaskCategory.BuildCompile, TaskErrorCategory.Error, compilerError, filePath);
					}
				}

				if (!OPTIMIZE_COMPILED_JAVASCRIPT && !string.IsNullOrWhiteSpace(standardOutput))
				{
					string outDirectory = workingDirectory + "\\out";
					if (Directory.Exists(outDirectory))
					{
						string outputFile = Directory.GetFiles(outDirectory, "*.js", SearchOption.TopDirectoryOnly).FirstOrDefault();
						string outputFileContent = !string.IsNullOrWhiteSpace(outputFile) ? File.ReadAllText(outputFile) : "";
						standardOutput = string.Format("/*{0}{1}{0}*/{0}{2}", Environment.NewLine, standardOutput, outputFileContent);
					}
					else
					{
						standardOutput = string.Format("/*{0}{1}{0}*/{0}", Environment.NewLine, standardOutput);
					}
				}

				if (!string.IsNullOrWhiteSpace(standardError) || !string.IsNullOrWhiteSpace(standardOutput))
				{
					outputResult(string.Format("{0}{1}", standardError, standardOutput));
				}
			}).Start();
		}