예제 #1
0
        private bool BuildProject(ITaskItem project)
        {
            using (Process proc = new Process())
            {
                // start changing properties
                if (!string.IsNullOrEmpty(project.GetMetadata("ChgPropVBP")))
                {
                    this.LogTaskMessage("START - Changing Properties VBP");

                    VBPProject projectVBP = new VBPProject(project.ItemSpec);
                    if (projectVBP.Load())
                    {
                        string[] linesProperty = project.GetMetadata("ChgPropVBP").Split(Separator);
                        string[] keyProperty = new string[linesProperty.Length];
                        string[] valueProperty = new string[linesProperty.Length];
                        int index;

                        for (index = 0; index <= linesProperty.Length - 1; index++)
                        {
                            if (linesProperty[index].IndexOf("=", StringComparison.OrdinalIgnoreCase) != -1)
                            {
                                keyProperty[index] = linesProperty[index].Substring(0, linesProperty[index].IndexOf("=", StringComparison.OrdinalIgnoreCase));
                                valueProperty[index] = linesProperty[index].Substring(linesProperty[index].IndexOf("=", StringComparison.OrdinalIgnoreCase) + 1);
                            }

                            if (!string.IsNullOrEmpty(keyProperty[index]) && !string.IsNullOrEmpty(valueProperty[index]))
                            {
                                this.LogTaskMessage(keyProperty[index] + " -> New value: " + valueProperty[index]);
                                projectVBP.SetProjectProperty(keyProperty[index], valueProperty[index], false);
                            }
                        }

                        projectVBP.Save();
                    }

                    this.LogTaskMessage("END - Changing Properties VBP");
                }

                // end changing properties
                proc.StartInfo.FileName = this.VB6Path;
                proc.StartInfo.UseShellExecute = false;
                proc.StartInfo.RedirectStandardOutput = true;
                proc.StartInfo.RedirectStandardError = true;
                if (string.IsNullOrEmpty(project.GetMetadata("OutDir")))
                {
                    proc.StartInfo.Arguments = @"/MAKE /OUT " + @"""" + project.ItemSpec + ".log" + @""" " + @"""" + project.ItemSpec + @"""";
                }
                else
                {
                    if (!Directory.Exists(project.GetMetadata("OutDir")))
                    {
                        Directory.CreateDirectory(project.GetMetadata("OutDir"));
                    }

                    proc.StartInfo.Arguments = @"/MAKE /OUT " + @"""" + project.ItemSpec + ".log" + @""" " + @"""" + project.ItemSpec + @"""" + " /outdir " + @"""" + project.GetMetadata("OutDir") + @"""";
                }

                // start the process
                this.LogTaskMessage("Running " + proc.StartInfo.FileName + " " + proc.StartInfo.Arguments);

                proc.Start();

                string outputStream = proc.StandardOutput.ReadToEnd();
                if (outputStream.Length > 0)
                {
                    this.LogTaskMessage(outputStream);
                }

                string errorStream = proc.StandardError.ReadToEnd();
                if (errorStream.Length > 0)
                {
                    Log.LogError(errorStream);
                }

                proc.WaitForExit();
                if (proc.ExitCode != 0)
                {
                    Log.LogError("Non-zero exit code from VB6.exe: " + proc.ExitCode);
                    try
                    {
                        using (FileStream myStreamFile = new FileStream(project.ItemSpec + ".log", FileMode.Open))
                        {
                            System.IO.StreamReader myStream = new System.IO.StreamReader(myStreamFile);
                            string myBuffer = myStream.ReadToEnd();
                            Log.LogError(myBuffer);
                        }
                    }
                    catch (Exception ex)
                    {
                        Log.LogError(string.Format(CultureInfo.CurrentUICulture, "Unable to open log file: '{0}'. Exception: {1}", project.ItemSpec + ".log", ex.Message));
                    }

                    return false;
                }

                return true;
            }
        }
예제 #2
0
        private bool BuildProject(ITaskItem project)
        {
            using (Process proc = new Process())
            {
                if (!string.IsNullOrEmpty(project.GetMetadata("ChgPropVBP")))
                {
                    this.LogTaskMessage("START - Changing Properties VBP");

                    VBPProject projectVBP = new VBPProject(project.ItemSpec);
                    if (projectVBP.Load())
                    {
                        string[] linesProperty = project.GetMetadata("ChgPropVBP").Split(Separator);
                        string[] keyProperty = new string[linesProperty.Length];
                        string[] valueProperty = new string[linesProperty.Length];
                        int index;

                        for (index = 0; index <= linesProperty.Length - 1; index++)
                        {
                            if (linesProperty[index].IndexOf("=", StringComparison.OrdinalIgnoreCase) != -1)
                            {
                                keyProperty[index] = linesProperty[index].Substring(0, linesProperty[index].IndexOf("=", StringComparison.OrdinalIgnoreCase));
                                valueProperty[index] = linesProperty[index].Substring(linesProperty[index].IndexOf("=", StringComparison.OrdinalIgnoreCase) + 1);
                            }

                            if (!string.IsNullOrEmpty(keyProperty[index]) && !string.IsNullOrEmpty(valueProperty[index]))
                            {
                                this.LogTaskMessage(keyProperty[index] + " -> New value: " + valueProperty[index]);
                                projectVBP.SetProjectProperty(keyProperty[index], valueProperty[index], false);
                            }
                        }

                        projectVBP.Save();
                    }

                    this.LogTaskMessage("END - Changing Properties VBP");
                }

                FileInfo artifactFileInfo = null;
                if (this.IfModificationExists)
                {
                    this.LogTaskMessage("START - Checking for modified files");
                    bool doBuild = false;
                    VBPProject projectVBP = new VBPProject(project.ItemSpec);
                    if (projectVBP.Load())
                    {
                        FileInfo projectFileInfo = new FileInfo(projectVBP.ProjectFile);
                        artifactFileInfo = projectVBP.ArtifactFile;
                        this.LogTaskMessage(string.Format(CultureInfo.CurrentCulture, "artifactFile '{0}', LastWrite: {1}'", artifactFileInfo.FullName, artifactFileInfo.LastWriteTime));

                        if (projectFileInfo.LastWriteTime > artifactFileInfo.LastWriteTime)
                        {
                            this.LogTaskMessage(MessageImportance.High, string.Format("File '{0}' is newer then '{1}'", projectFileInfo.Name, artifactFileInfo.Name));
                            doBuild = true;
                        }
                        else
                        {
                            foreach (var file in projectVBP.GetFiles())
                            {
                                this.LogTaskMessage(string.Format("File '{0}', LastWrite: {1}'", file.FullName, file.LastWriteTime));

                                if (file.LastWriteTime > artifactFileInfo.LastWriteTime)
                                {
                                    this.LogTaskMessage(MessageImportance.High, string.Format(CultureInfo.CurrentCulture, "File '{0}' is newer then '{1}'", file.Name, artifactFileInfo.Name));
                                    doBuild = true;
                                    break;
                                }
                            }
                        }
                    }

                    if (!doBuild)
                    {
                        this.LogTaskMessage(MessageImportance.High, "Build skipped, because no modifications exists.");
                        return true;
                    }

                    this.LogTaskMessage("END - Checking for modified files");
                }

                proc.StartInfo.FileName = this.VB6Path;
                proc.StartInfo.UseShellExecute = false;
                proc.StartInfo.RedirectStandardOutput = true;
                proc.StartInfo.RedirectStandardError = true;
                if (string.IsNullOrEmpty(project.GetMetadata("OutDir")))
                {
                    proc.StartInfo.Arguments = @"/MAKE /OUT " + @"""" + project.ItemSpec + ".log" + @""" " + @"""" + project.ItemSpec + @"""";
                }
                else
                {
                    if (!Directory.Exists(project.GetMetadata("OutDir")))
                    {
                        Directory.CreateDirectory(project.GetMetadata("OutDir"));
                    }

                    proc.StartInfo.Arguments = @"/MAKE /OUT " + @"""" + project.ItemSpec + ".log" + @""" " + @"""" + project.ItemSpec + @"""" + " /outdir " + @"""" + project.GetMetadata("OutDir") + @"""";
                }

                // start the process
                this.LogTaskMessage("Running " + proc.StartInfo.FileName + " " + proc.StartInfo.Arguments);

                proc.Start();

                string outputStream = proc.StandardOutput.ReadToEnd();
                if (outputStream.Length > 0)
                {
                    this.LogTaskMessage(outputStream);
                }

                string errorStream = proc.StandardError.ReadToEnd();
                if (errorStream.Length > 0)
                {
                    Log.LogError(errorStream);
                }

                proc.WaitForExit();
                if (proc.ExitCode != 0)
                {
                    Log.LogError("Non-zero exit code from VB6.exe: " + proc.ExitCode);
                    try
                    {
                        using (FileStream myStreamFile = new FileStream(project.ItemSpec + ".log", FileMode.Open))
                        {
                            System.IO.StreamReader myStream = new System.IO.StreamReader(myStreamFile);
                            string myBuffer = myStream.ReadToEnd();
                            Log.LogError(myBuffer);
                        }
                    }
                    catch (Exception ex)
                    {
                        Log.LogError(string.Format(CultureInfo.CurrentUICulture, "Unable to open log file: '{0}'. Exception: {1}", project.ItemSpec + ".log", ex.Message));
                    }

                    return false;
                }

                if (artifactFileInfo != null)
                {
                    var myNow = DateTime.Now;
                    artifactFileInfo.LastWriteTime = myNow;
                    artifactFileInfo.LastAccessTime = myNow;
                }

                return true;
            }
        }