PageRenderTime 50ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/Releases/4.0.4.0/Main/Framework/VisualStudio/VB6.cs

#
C# | 231 lines | 169 code | 19 blank | 43 comment | 17 complexity | 922d8774875f8e77e5fe9c06f1b9b47d MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. //-----------------------------------------------------------------------
  2. // <copyright file="VB6.cs">(c) http://www.codeplex.com/MSBuildExtensionPack. This source is subject to the Microsoft Permissive License. See http://www.microsoft.com/resources/sharedsource/licensingbasics/sharedsourcelicenses.mspx. All other rights reserved.</copyright>
  3. //-----------------------------------------------------------------------
  4. namespace MSBuild.ExtensionPack.VisualStudio
  5. {
  6. using System;
  7. using System.Diagnostics;
  8. using System.Globalization;
  9. using System.IO;
  10. using System.Linq;
  11. using Microsoft.Build.Framework;
  12. using MSBuild.ExtensionPack.VisualStudio.Extended;
  13. /// <summary>
  14. /// <b>Valid TaskActions are:</b>
  15. /// <para><i>Build</i> (<b>Required: </b> Projects <b>Optional: </b>VB6Path, StopOnError)</para>
  16. /// <para><b>Remote Execution Support:</b> NA</para>
  17. /// <para/>
  18. /// </summary>
  19. /// <example>
  20. /// <code lang="xml"><![CDATA[
  21. /// <Project ToolsVersion="4.0" DefaultTargets="Default" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  22. /// <PropertyGroup>
  23. /// <TPath>$(MSBuildProjectDirectory)\..\MSBuild.ExtensionPack.tasks</TPath>
  24. /// <TPath Condition="Exists('$(MSBuildProjectDirectory)\..\..\Common\MSBuild.ExtensionPack.tasks')">$(MSBuildProjectDirectory)\..\..\Common\MSBuild.ExtensionPack.tasks</TPath>
  25. /// </PropertyGroup>
  26. /// <Import Project="$(TPath)"/>
  27. /// <ItemGroup>
  28. /// <ProjectsToBuild Include="C:\MyVB6Project.vbp">
  29. /// <OutDir>c:\output</OutDir>
  30. /// <!-- Note the special use of ChgPropVBP metadata to change project properties at Build Time -->
  31. /// <ChgPropVBP>RevisionVer=4;CompatibleMode="0"</ChgPropVBP>
  32. /// </ProjectsToBuild>
  33. /// <ProjectsToBuild Include="C:\MyVB6Project2.vbp"/>
  34. /// </ItemGroup>
  35. /// <Target Name="Default">
  36. /// <!-- Build a collection of VB6 projects -->
  37. /// <MSBuild.ExtensionPack.VisualStudio.VB6 TaskAction="Build" Projects="@(ProjectsToBuild)"/>
  38. /// </Target>
  39. /// </Project>
  40. /// ]]></code>
  41. /// </example>
  42. [HelpUrl("http://www.msbuildextensionpack.com/help/4.0.4.0/html/c68d1d6c-b0bc-c944-e1a2-1ad4f0c28d3c.htm")]
  43. public class VB6 : BaseTask
  44. {
  45. private const string BuildTaskAction = "Build";
  46. private const char Separator = ';';
  47. [DropdownValue(BuildTaskAction)]
  48. public override string TaskAction
  49. {
  50. get { return base.TaskAction; }
  51. set { base.TaskAction = value; }
  52. }
  53. /// <summary>
  54. /// Sets the VB6Path. Default is [Program Files]\Microsoft Visual Studio\VB98\VB6.exe
  55. /// </summary>
  56. [TaskAction(BuildTaskAction, false)]
  57. public string VB6Path { get; set; }
  58. /// <summary>
  59. /// Set to true to stop processing when a project in the Projects collection fails to compile. Default is false.
  60. /// </summary>
  61. [TaskAction(BuildTaskAction, false)]
  62. public bool StopOnError { get; set; }
  63. /// <summary>
  64. /// Sets the projects. Use an 'OutDir' metadata item to specify the output directory. The OutDir will be created if it does not exist.
  65. /// </summary>
  66. [Required]
  67. [TaskAction(BuildTaskAction, true)]
  68. public ITaskItem[] Projects { get; set; }
  69. protected override void InternalExecute()
  70. {
  71. if (!this.TargetingLocalMachine())
  72. {
  73. return;
  74. }
  75. if (string.IsNullOrEmpty(this.VB6Path))
  76. {
  77. string programFilePath = Environment.GetEnvironmentVariable("ProgramFiles");
  78. if (string.IsNullOrEmpty(programFilePath))
  79. {
  80. Log.LogError("Failed to read a value from the ProgramFiles Environment Variable");
  81. return;
  82. }
  83. if (File.Exists(programFilePath + @"\Microsoft Visual Studio\VB98\VB6.exe"))
  84. {
  85. this.VB6Path = programFilePath + @"\Microsoft Visual Studio\VB98\VB6.exe";
  86. }
  87. else
  88. {
  89. Log.LogError(string.Format(CultureInfo.CurrentCulture, "VB6.exe was not found in the default location. Use VB6Path to specify it. Searched at: {0}", programFilePath + @"\Microsoft Visual Studio\VB98\VB6.exe"));
  90. return;
  91. }
  92. }
  93. switch (this.TaskAction)
  94. {
  95. case "Build":
  96. this.Build();
  97. break;
  98. default:
  99. this.Log.LogError(string.Format(CultureInfo.CurrentCulture, "Invalid TaskAction passed: {0}", this.TaskAction));
  100. return;
  101. }
  102. }
  103. private void Build()
  104. {
  105. if (this.Projects == null)
  106. {
  107. Log.LogError("The collection passed to Projects is empty");
  108. return;
  109. }
  110. this.LogTaskMessage(string.Format(CultureInfo.CurrentCulture, "Building Projects Collection: {0} projects", this.Projects.Length));
  111. if (this.Projects.Any(project => !this.BuildProject(project) && this.StopOnError))
  112. {
  113. this.LogTaskMessage("BuildVB6 Task Execution Failed [" + DateTime.Now.ToString("HH:MM:ss", CultureInfo.CurrentCulture) + "] Stopped by StopOnError set on true");
  114. return;
  115. }
  116. this.LogTaskMessage("BuildVB6 Task Execution Completed [" + DateTime.Now.ToString("HH:MM:ss", CultureInfo.CurrentCulture) + "]");
  117. return;
  118. }
  119. private bool BuildProject(ITaskItem project)
  120. {
  121. using (Process proc = new Process())
  122. {
  123. // start changing properties
  124. if (!string.IsNullOrEmpty(project.GetMetadata("ChgPropVBP")))
  125. {
  126. this.LogTaskMessage("START - Changing Properties VBP");
  127. VBPProject projectVBP = new VBPProject(project.ItemSpec);
  128. if (projectVBP.Load())
  129. {
  130. string[] linesProperty = project.GetMetadata("ChgPropVBP").Split(Separator);
  131. string[] keyProperty = new string[linesProperty.Length];
  132. string[] valueProperty = new string[linesProperty.Length];
  133. int index;
  134. for (index = 0; index <= linesProperty.Length - 1; index++)
  135. {
  136. if (linesProperty[index].IndexOf("=", StringComparison.OrdinalIgnoreCase) != -1)
  137. {
  138. keyProperty[index] = linesProperty[index].Substring(0, linesProperty[index].IndexOf("=", StringComparison.OrdinalIgnoreCase));
  139. valueProperty[index] = linesProperty[index].Substring(linesProperty[index].IndexOf("=", StringComparison.OrdinalIgnoreCase) + 1);
  140. }
  141. if (!string.IsNullOrEmpty(keyProperty[index]) && !string.IsNullOrEmpty(valueProperty[index]))
  142. {
  143. this.LogTaskMessage(keyProperty[index] + " -> New value: " + valueProperty[index]);
  144. projectVBP.SetProjectProperty(keyProperty[index], valueProperty[index], false);
  145. }
  146. }
  147. projectVBP.Save();
  148. }
  149. this.LogTaskMessage("END - Changing Properties VBP");
  150. }
  151. // end changing properties
  152. proc.StartInfo.FileName = this.VB6Path;
  153. proc.StartInfo.UseShellExecute = false;
  154. proc.StartInfo.RedirectStandardOutput = true;
  155. proc.StartInfo.RedirectStandardError = true;
  156. if (string.IsNullOrEmpty(project.GetMetadata("OutDir")))
  157. {
  158. proc.StartInfo.Arguments = @"/MAKE /OUT " + @"""" + project.ItemSpec + ".log" + @""" " + @"""" + project.ItemSpec + @"""";
  159. }
  160. else
  161. {
  162. if (!Directory.Exists(project.GetMetadata("OutDir")))
  163. {
  164. Directory.CreateDirectory(project.GetMetadata("OutDir"));
  165. }
  166. proc.StartInfo.Arguments = @"/MAKE /OUT " + @"""" + project.ItemSpec + ".log" + @""" " + @"""" + project.ItemSpec + @"""" + " /outdir " + @"""" + project.GetMetadata("OutDir") + @"""";
  167. }
  168. // start the process
  169. this.LogTaskMessage("Running " + proc.StartInfo.FileName + " " + proc.StartInfo.Arguments);
  170. proc.Start();
  171. string outputStream = proc.StandardOutput.ReadToEnd();
  172. if (outputStream.Length > 0)
  173. {
  174. this.LogTaskMessage(outputStream);
  175. }
  176. string errorStream = proc.StandardError.ReadToEnd();
  177. if (errorStream.Length > 0)
  178. {
  179. Log.LogError(errorStream);
  180. }
  181. proc.WaitForExit();
  182. if (proc.ExitCode != 0)
  183. {
  184. Log.LogError("Non-zero exit code from VB6.exe: " + proc.ExitCode);
  185. try
  186. {
  187. using (FileStream myStreamFile = new FileStream(project.ItemSpec + ".log", FileMode.Open))
  188. {
  189. System.IO.StreamReader myStream = new System.IO.StreamReader(myStreamFile);
  190. string myBuffer = myStream.ReadToEnd();
  191. Log.LogError(myBuffer);
  192. }
  193. }
  194. catch (Exception ex)
  195. {
  196. Log.LogError(string.Format(CultureInfo.CurrentUICulture, "Unable to open log file: '{0}'. Exception: {1}", project.ItemSpec + ".log", ex.Message));
  197. }
  198. return false;
  199. }
  200. return true;
  201. }
  202. }
  203. }
  204. }