17 using RPlugin.Properties;
19 using System.Diagnostics;
21 using System.Reflection;
24 namespace RPlugin.Core
29 internal static string Temp;
40 Temp = Path.Combine(Path.GetTempPath(), Properties.Settings.Default.TempPath);
41 Cache = Path.Combine(Temp, Properties.Settings.Default.TempCache);
42 Scripts = Path.Combine(Temp, Properties.Settings.Default.TempScripts);
44 Directory.CreateDirectory(Temp);
45 Directory.CreateDirectory(Cache);
46 Directory.CreateDirectory(Scripts);
48 foreach (
string resourceName
in Assembly.GetExecutingAssembly().GetManifestResourceNames())
50 if (resourceName.EndsWith(
".R"))
52 ExtractResource(Scripts, resourceName);
63 RScriptResult RCheck = RunScript(RScriptPath, Path.Combine(Scripts, Settings.Default.Resources +
"PackageInstaller.R"),
"0 " + String.Join(
" ", args));
67 throw new Exception(
"There was an error in the application:\n" + RCheck.
Error);
70 if (!String.IsNullOrWhiteSpace(RCheck.
Output.Trim()))
72 RScriptResult RInstall = RunScriptAdmin(RScriptPath, Path.Combine(Scripts, Settings.Default.Resources +
"PackageInstaller.R"),
"1 " + RCheck.
Output.Trim());
76 throw new Exception(
"There was an error installing packages.");
91 ProcessStartInfo info =
new ProcessStartInfo
93 FileName = RScriptPath,
94 Arguments =
"--verbose \"" + Path.Combine(Scripts, script) +
"\" " + String.Join(
" ", args),
95 UseShellExecute =
false,
96 WindowStyle = ProcessWindowStyle.Hidden,
97 CreateNoWindow =
true,
98 RedirectStandardError =
true,
99 RedirectStandardOutput =
true,
102 Process R =
new Process();
107 R.BeginOutputReadLine();
108 R.BeginErrorReadLine();
126 ProcessStartInfo info =
new ProcessStartInfo
128 FileName =
"cmd.exe",
129 Arguments =
"/C \"call \"" + RScriptPath +
"\" \"" + Path.Combine(Scripts, script) +
"\" " + String.Join(
" ", args) +
"\"",
130 UseShellExecute =
true,
133 Process RAdmin =
new Process();
134 RAdmin.StartInfo = info;
136 RAdmin.WaitForExit();
154 if (Directory.Exists(path))
156 DirectoryInfo dInfo =
new DirectoryInfo(path);
157 foreach (FileInfo file
in dInfo.GetFiles())
163 catch (Exception) { }
165 foreach (DirectoryInfo dir
in dInfo.GetDirectories())
169 ClearCache(dir.FullName);
171 catch (Exception) { }
173 if (!path.Equals(Cache))
177 Directory.Delete(path);
179 catch (Exception) { }
193 string filename = Path.Combine(Cache, name);
194 if (File.Exists(filename) && !overwrite)
196 throw new Exception(
"Filename collision in cache: " + filename);
200 return File.Create(filename);
211 string dirname = Path.Combine(Cache, name);
212 if (Directory.Exists(dirname) && checkExists)
218 return Directory.CreateDirectory(dirname);
229 File.WriteAllText(Path.Combine(dir, ResourceName),
230 new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream(ResourceName)).ReadToEnd());
240 return (
new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream(ResourceName))).ReadToEnd();
252 throw new Exception(
"Setup has not been run.");