Exemplo n.º 1
0
        public TaskResult ValidatePatchProcess()
        {
            var result = new TaskResult()
            {
                IsValid = true
            };

            // pull request merged?
            if (!AssemblyHelper.PullRequest.Merged)
            {
                result.AddError(String.Format("Pull Request {0} has not been merged yet.", AssemblyHelper.PullRequest.Id));
                // no further validation may be done. Bail...
                return(result);
            }

            try
            {
                // repo at the correct version?
                if (!ValidatePatchVersion())
                {
                    result.AddError(String.Format("Repo version, {0} - {1}, does match patch target version(s), {2} - {3}.", BranchVersionHelper.CurrentBranch.Name, RepoVersion.ToString(), this.Branch.Name, PatchTargetVersion.ToString()));
                }
                //if (PatchTargetVersion.Build == -1 && PatchTargetVersion.Revision == -1)
                //{
                //    if (RepoVersion.Major != PatchTargetVersion.Major || RepoVersion.Minor != PatchTargetVersion.Minor)
                //    {
                //        result.AddError(String.Format("Repo version, {0}, does match patch target version, {1}.", RepoVersion.ToString(), PatchTargetVersion.ToString()));
                //        // no further validation may be done. Bail...
                //        return result;
                //    }
                //    else
                //    {
                //        PatchTargetVersion = RepoVersion;
                //    }
                //}

                _sourceAssemblies = GetSourceAssemblies();
                // assembly exists?
                foreach (var assemblyFileName in _sourceAssemblies)
                {
                    if (!File.Exists(assemblyFileName.FullName))
                    {
                        result.AddError(String.Format("Built assembly {0} is not found.", assemblyFileName.FullName));
                    }
                    else
                    {
                        // versions correct?
                        var    versInfo       = FileVersionInfo.GetVersionInfo(assemblyFileName.FullName);
                        String fileVersion    = versInfo.FileVersion;
                        String productVersion = versInfo.ProductVersion;

                        //example for own display version string, built of the four version parts:
                        String myVers = String.Format("V{0}.{1}.{2} build {3}", versInfo.FileMajorPart, versInfo.FileMinorPart,
                                                      versInfo.FileBuildPart, versInfo.FilePrivatePart);

                        Console.WriteLine("{0}: File Version:{1}; Product Version:{2}; myVers:{3}; IsDebug:{4}", assemblyFileName.FullName, fileVersion, productVersion, myVers, versInfo.IsDebug);
                        if (RepoVersion.ToString() != fileVersion)
                        {
                            result.AddError(String.Format("Built assembly {0} is at version {1}, patching to version {2}.", assemblyFileName, fileVersion, RepoVersion.ToString()));
                        }

                        // built since request made?
                        var      binAssemblyFileInfo     = new FileInfo(assemblyFileName.FullName);
                        DateTime binAssemblyLastModified = binAssemblyFileInfo.LastWriteTime;
                        if (BuildTimestampLimit > binAssemblyLastModified)
                        {
                            result.AddError(String.Format("Built assembly {0} is out of date. Last build was {1}, build cutoff timestamp is {2}.", assemblyFileName.FullName, binAssemblyLastModified, BuildTimestampLimit));
                        }

                        // debug?
                        if (versInfo.IsDebug)
                        {
                            result.AddError(String.Format("Built assembly {0} is a DEBUG assembly.", assemblyFileName.FullName));
                        }

                        var targetAssembly = Path.Combine(PatchFolder, assemblyFileName.Name);
                        if (File.Exists(targetAssembly))
                        {
                            result.AddDebug(String.Format("Assembly {0} exists in patch folder.", targetAssembly));
                            // target assembly not newer than source assembly?
                            var patchFolderAssemblyFileInfo = new FileInfo(targetAssembly);

                            if (binAssemblyLastModified < patchFolderAssemblyFileInfo.LastWriteTime)
                            {
                                result.AddError(String.Format("Built assembly {0} is out of date. Assembly currently in Patch folder has more recent build timestamp, {1}.", assemblyFileName.Name, binAssemblyLastModified, patchFolderAssemblyFileInfo.LastWriteTime));
                            }
                            // target assembly build timestamp same as source? (same build time and version)
                            if (binAssemblyLastModified == patchFolderAssemblyFileInfo.LastWriteTime)
                            {
                                result.AddError(String.Format("Assembly {0} has the same build timestamp in Patch and Bin folders.", assemblyFileName.Name));
                            }
                        }

                        // confirm commit(s) in current branch.


                        if (!Directory.Exists(AssemblyBackupFolder))
                        {
                            var backupInfo = Directory.CreateDirectory(AssemblyBackupFolder);
                            if (null == backupInfo)
                            {
                                result.AddError(String.Format("Error creating backup directory {0}.", AssemblyBackupFolder));
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                result.AddError(ex.ToString());
            }
            return(result);
        }