コード例 #1
0
        public override string Deploy(IList <IFileObject> fobjs)
        {
            ZipDirectoryHelper zdh    = Dir as ZipDirectoryHelper;
            string             logStr = string.Format("Deploying {0} to {1} \r\n Creating zip...\r\n", Tag, DeploymentDir);

            try
            {
                foreach (IFileObject fobj in fobjs)
                {
                    string fulldeployedFileName = DeploymentDir + fobj.StoreIndependentFileName;
                    if (!fobj.StoreIndependentFileName.StartsWith("$"))
                    {
                        using (Stream stream = fobj.FileStream)
                        {
                            //stream is null on deletes
                            if (stream != null)
                            {
                                using (ZipFile zip = zdh.Zip)
                                {
                                    string filename = fobj.StoreIndependentFileName;

                                    if (!zip.ContainsEntry(filename))
                                    {
                                        logStr += string.Format("\tadding file {0} \r\n", filename);
                                        ZipEntry ze = zip.AddEntry(filename, stream);
                                        zip.Save();
                                    }
                                    else
                                    {
                                        logStr += string.Format("\tupdating file {0} \r\n", filename);
                                        zip.RemoveEntry(filename);
                                        zip.AddEntry(filename, stream);
                                        zip.Save();
                                    }
                                }
                            }
                            else if (stream == null && File.Exists(fulldeployedFileName))
                            {
                                logStr += string.Format("{0} cannot be found in tfs. This might be due to a deletion in TFS", fulldeployedFileName);
                                //remove file from zip
                                using (ZipFile zip = zdh.Zip)
                                {
                                    if (zip.ContainsEntry(fobj.StoreIndependentFileName))
                                    {
                                        logStr += string.Format("removing file {0} \r\n", fobj.StoreIndependentFileName);
                                        zip.RemoveEntry(fobj.StoreIndependentFileName);
                                        zip.Save();
                                    }
                                }
                                File.Delete(fulldeployedFileName);
                            }
                            //zip.Save();
                        }
                        fobj.Close();
                    }
                }
                using (ZipFile zip = zdh.Zip)
                {
                    //if (!Directory.Exists(DeploymentDir)) { Directory.CreateDirectory(DeploymentDir); }// throw new Exception("Deployment failed because directory " + DeploymentDir + " does not exist. Create the directory and try again");

                    logStr += string.Format("Deploying to {0} \r\n", DeploymentDir);
                    zip.ExtractExistingFile = ExtractExistingFileAction.OverwriteSilently;

                    zip.ExtractAll(DeploymentDir, ExtractExistingFileAction.OverwriteSilently);
                }
            }
            catch (Exception e)
            {
                logStr += string.Format("\r\nException: {0} at {1}\r\n\t{2}\r\n", e.Message, e.Source, e.StackTrace);
                throw new Exception(e.Message);
            }
            finally
            {
                zdh.Zip.Dispose();
                zdh = null;
                //File.WriteAllText(string.Format(@"{0}\{1}.{2}", BuildDir, Tag + BuildNumber, "txt"), logStr);
            }/**/
            return(logStr);
        }
コード例 #2
0
        private string DeployListOf(IList <IFileObject> fobjs)
        {
            ZipDirectoryHelper zdh     = Dir as ZipDirectoryHelper;
            string             logStr  = string.Format("\r\n Deploying {0} to {1} \r\nPackaging to {2}\r\n", Tag, DeploymentDir, zdh.zipFile);
            string             logStr2 = string.Empty;

            try
            {
                foreach (IFileObject fobj in fobjs)
                {
                    string fulldeployedFileName = DeploymentDir + fobj.StoreIndependentFileName;
                    if (!fobj.StoreIndependentFileName.StartsWith("$"))
                    {
                        using (
                            Stream stream = fobj.FileStream
                            )
                        {
                            using (ZipFile zip = new ZipFile(zdh.zipFile))
                            {
                                ZipEntry ze = null;
                                //stream is null on deletes
                                if (stream != null)
                                {
                                    string filename = fobj.StoreIndependentFileName;
                                    if (!zip.ContainsEntry(filename))
                                    {
                                        logStr += string.Format("\t     adding file {0} \r\n", filename);
                                        ze      = zip.AddEntry(filename, stream);
                                    }
                                    else
                                    {
                                        logStr += string.Format("\t     updating file {0} \r\n", filename);
                                        //zip.RemoveEntry(filename);
                                        //zip.UpdateFile(filename);
                                        ze = zip.UpdateEntry(filename, stream);
                                        //zip.AddEntry(filename, stream);
                                    }
                                }
                                else if (stream == null && File.Exists(fulldeployedFileName))
                                {
                                    logStr += string.Format("\t     {0} cannot be found in tfs. This might be due to a deletion in TFS", fulldeployedFileName);
                                    //remove file from zip
                                    //using (ZipFile zip = zdh.Zip)
                                    //{
                                    if (zip.ContainsEntry(fobj.StoreIndependentFileName))
                                    {
                                        logStr += string.Format("\t     removing file {0} \r\n", fobj.StoreIndependentFileName);
                                        zip.RemoveEntry(fobj.StoreIndependentFileName);
                                        //zip.Save();
                                    }
                                    //}
                                    File.Delete(fulldeployedFileName);
                                }
                                zip.Save();
                                if (ze != null)
                                {
                                    ze.Extract(DeploymentDir, ExtractExistingFileAction.OverwriteSilently);
                                    logStr2 += string.Format("     \tsuccessfully deployed {0} to {1}", fobj.FullFileName, DeploymentDir + fobj.StoreIndependentFileName);
                                }
                            }
                            //
                        }
                        fobj.Close();
                    }
                }
            }
            catch (Exception e)
            {
                logStr += string.Format("\r\nException: {0} at {1}\r\n\t{2}\r\n", e.Message, e.Source, e.StackTrace);
                throw new Exception(e.Message);
            }
            finally
            {
                zdh.Zip.Dispose();
                zdh = null;
                File.WriteAllText(string.Format(@"{0}\{1}.{2}", BuildDir, Tag + BuildNumber, "txt"), logStr);
            }/**/
            logStr += logStr2;
            return(logStr);
        }