Ejemplo n.º 1
0
 internal void OnStatusChanged(StatusEventArgs e)
 {
     if (StatusChanged != null)
         StatusChanged.Invoke(this, e);
 }
Ejemplo n.º 2
0
        internal void ProcessNode(XmlNodeReader reader, DirectoryInfo targetDirectory)
        {
            StatusEventArgs e = new StatusEventArgs(this, StatusType.Parsing, 0);
            float step = 1f / _packageInstallXmlNode.ChildNodes.OfType<XmlNode>().Count();

            do
            {
                _repository.OnStatusChanged(e);
                Log.DebugFormat("ProcessNode({1}): {0}", reader.Name, ID);
                switch (reader.Name.ToLower())
                {
                    case "extract":
                        {
                            string file = VariableResolver.Expand(reader.GetAttribute("file"), this);
                            string targetpath = Path.Combine(targetDirectory.FullName, VariableResolver.Expand(reader.GetAttribute("targetpath"), this));

                            Log.InfoFormat("ProcessNode: Extracting {0} to {1}", file, targetpath);
                            Directory.CreateDirectory(Path.GetDirectoryName(targetpath));
                            _archive.ExtractFile(file, targetpath);
                        }
                        break;
                    case "extract-all":
                        {
                            string targetfolder = Path.Combine(targetDirectory.FullName, VariableResolver.Expand(reader.GetAttribute("targetfolder"), this));

                            Log.InfoFormat("ProcessNode: Extracting {0} to {1}", "everything", targetfolder);
                            _archive.ExtractAllFiles(targetfolder);
                        }
                        break;
                    case "extract-filter":
                        {
                            string filter = VariableResolver.Expand(reader.GetAttribute("filter"), this);
                            string targetfolder = Path.Combine(targetDirectory.FullName, VariableResolver.Expand(reader.GetAttribute("targetfolder"), this));

                            if (string.IsNullOrEmpty(filter))
                                filter = "*";

                            foreach (string entry in FileFilterUtil.FilterFiles(_archive.GetFileEntries(), filter))
                            {
                                string targetpath = Path.Combine(targetfolder.Replace('/', Path.DirectorySeparatorChar), entry.Replace('/', Path.DirectorySeparatorChar));
                                Log.InfoFormat("ProcessNode: Extracting {0} to {1}", entry, targetpath);
                                Directory.CreateDirectory(Path.GetDirectoryName(targetpath));
                                _archive.ExtractFile(entry, targetpath);
                            }
                            e.Progress += step;
                        }
                        break;
                    case "delete":
                        {
                            string targetpath = Path.Combine(targetDirectory.FullName, VariableResolver.Expand(reader.GetAttribute("targetpath"), this));

                            Log.InfoFormat("ProcessNode: Deleting {0}", targetpath);
                            File.Delete(targetpath);
                            e.Progress += step;
                        }
                        break;
                    case "delete-folder":
                        {
                            string filter = VariableResolver.Expand(reader.GetAttribute("filter"), this);
                            string folder = Path.Combine(targetDirectory.FullName, VariableResolver.Expand(reader.GetAttribute("targetfolder"), this));
                            var d = new DirectoryInfo(folder);
                            foreach (var file in d.GetFiles(string.IsNullOrEmpty(filter) ? "*" : filter, SearchOption.AllDirectories))
                            {
                                Log.InfoFormat("ProcessNode: Deleting {0}", file.FullName);
                                file.Delete();
                                if (!file.Directory.GetFiles().Any())
                                    file.Directory.Delete();
                            }
                            if (!d.GetFiles().Any())
                                d.Delete();
                            e.Progress += step;
                        }
                        break;
                    case "uninject":
                        {
                            string target = Path.Combine(targetDirectory.FullName, VariableResolver.Expand(reader.GetAttribute("targetpath"), this));
                            string filter = VariableResolver.Expand(reader.GetAttribute("filter"), this);

                            Directory.CreateDirectory(Path.GetDirectoryName(target));

                            ZipArchive targetZip;
                            if (File.Exists(target))
                                targetZip = ZipArchive.Open(target);
                            else
                            {
                                targetZip = ZipArchive.Create();
                                break;
                            }
                            targetZip.DeflateCompressionLevel = SharpCompress.Compressor.Deflate.CompressionLevel.BestCompression;

                            foreach (ZipArchiveEntry entry in from x in targetZip.Entries where FileFilterUtil.IsMatch(x.FilePath, filter) select x)
                            {
                                Log.InfoFormat("ProcessNode: Removing {0} from {1}", entry.FilePath, target);
                                targetZip.RemoveEntry(entry);
                            }

                            Log.InfoFormat("ProcessNode: Updating {0}", target);
                            targetZip.SaveTo(target + ".~", CompressionType.Deflate);
                            targetZip.Dispose();

                            File.Delete(target);
                            File.Move(target + ".~", target);
                            e.Progress += step;
                        }
                        break;
                    case "inject":
                        {
                            string target = Path.Combine(targetDirectory.FullName, VariableResolver.Expand(reader.GetAttribute("targetpath"), this));
                            string filter = VariableResolver.Expand(reader.GetAttribute("filter"), this);

                            Directory.CreateDirectory(Path.GetDirectoryName(target));

                            ZipArchive targetZip;
                            if (File.Exists(target))
                                targetZip = ZipArchive.Open(target);
                            else
                                targetZip = ZipArchive.Create();
                            targetZip.DeflateCompressionLevel = SharpCompress.Compressor.Deflate.CompressionLevel.BestCompression;

                            foreach (string entry in FileFilterUtil.FilterFiles(_archive.GetFileEntries(), filter))
                            {
                                if (entry.EndsWith("/") || entry.EndsWith(Path.DirectorySeparatorChar.ToString())) continue;

                                MemoryStream ms = new MemoryStream();
                                using (var s = _archive.OpenFile(entry))
                                {
                                    //s.CopyTo(ms);
                                    int i = 1;
                                    while (i > 0)
                                    {
                                        byte[] buffer = new byte[2048];
                                        i = s.Read(buffer, 0, buffer.Length);
                                        if (i > 0)
                                            ms.Write(buffer, 0, i);
                                    }
                                }
                                ms.Flush();
                                ms.Seek(0, SeekOrigin.Begin);
                                var entriesWhichAreSame = (from zipentry in targetZip.Entries where zipentry.FilePath.Replace(Path.DirectorySeparatorChar, '\\') == entry select zipentry).ToArray();
                                if (entriesWhichAreSame.Any())
                                    foreach (var zipentry in entriesWhichAreSame)
                                    {
                                        Log.InfoFormat("ProcessNode: Removing {0} from {1}", zipentry.FilePath, target);
                                        targetZip.RemoveEntry(zipentry);
                                    }
                                Log.InfoFormat("ProcessNode: Adding {0} to {1}", entry, target);
                                targetZip.AddEntry(entry, ms);
                            }

                            Log.InfoFormat("ProcessNode: Updating {0}", target);
                            targetZip.SaveTo(target + ".~", CompressionType.Deflate);
                            targetZip.Dispose();

                            File.Delete(target);
                            File.Move(target + ".~", target);
                            e.Progress += step;
                        }
                        break;
                    case "move":
                        {
                            string sourcePath = Path.Combine(targetDirectory.FullName, VariableResolver.Expand(reader.GetAttribute("path"), this));
                            string targetPath = Path.Combine(targetDirectory.FullName, VariableResolver.Expand(reader.GetAttribute("targetpath"), this));

                            Log.InfoFormat("ProcessNode: Moving {0} to {1}", sourcePath, targetPath);
                            Directory.CreateDirectory(Path.GetDirectoryName(targetPath));
                            File.Move(sourcePath, targetPath);
                            e.Progress += step;
                        }
                        break;
                    case "download":
                        {
                            string sourcePath = VariableResolver.Expand(reader.GetAttribute("url"), this);
                            string targetPath = Path.Combine(targetDirectory.FullName, VariableResolver.Expand(reader.GetAttribute("targetpath"), this));

                            e.Status = StatusType.Downloading;
                            _repository.OnStatusChanged(e);
                            Log.InfoFormat("ProcessNode: Downloading {0} to {1}", sourcePath, targetPath);
                            new System.Net.WebClient().DownloadFile(new Uri(sourcePath), targetPath);

                            e.Progress += step;
                        }
                        break;
                    default:
                        Log.WarnFormat("ProcessNode: Unknown step {0}", reader.Name);
                        break;
                }
            } while (reader.Read());
            _repository.OnStatusChanged(e);
        }