Ejemplo n.º 1
0
        public bool SavePackage(PackageDefinition definition)
        {
            if (definition == null)
            {
                throw new ArgumentNullException(nameof(definition));
            }

            var packagesXml = EnsureStorage(out var packagesFile);

            if (packagesXml?.Root == null)
            {
                return(false);
            }

            //ensure it's valid
            ValidatePackage(definition);

            if (definition.Id == default)
            {
                //need to gen an id and persist
                // Find max id
                var maxId = packagesXml.Root.Elements("package").Max(x => x.AttributeValue <int?>("id")) ?? 0;
                var newId = maxId + 1;
                definition.Id        = newId;
                definition.PackageId = definition.PackageId == default ? Guid.NewGuid() : definition.PackageId;
                var packageXml = _parser.ToXml(definition);
                packagesXml.Root.Add(packageXml);
            }
            else
            {
                //existing
                var packageXml = packagesXml.Root.Elements("package").FirstOrDefault(x => x.AttributeValue <int>("id") == definition.Id);
                if (packageXml == null)
                {
                    return(false);
                }

                var updatedXml = _parser.ToXml(definition);
                packageXml.ReplaceWith(updatedXml);
            }

            packagesXml.Save(packagesFile);

            return(true);
        }
Ejemplo n.º 2
0
        private void PackageLanguages(PackageDefinition definition, XContainer root)
        {
            var languages = new XElement("Languages");

            foreach (var langId in definition.Languages)
            {
                if (!int.TryParse(langId, NumberStyles.Integer, CultureInfo.InvariantCulture, out var outInt))
                {
                    continue;
                }
                var lang = _languageService.GetLanguageById(outInt);
                if (lang == null)
                {
                    continue;
                }
                languages.Add(_serializer.Serialize(lang));
            }
            root.Add(languages);
        }
Ejemplo n.º 3
0
        private void PackageDataTypes(PackageDefinition definition, XContainer root)
        {
            var dataTypes = new XElement("DataTypes");

            foreach (var dtId in definition.DataTypes)
            {
                if (!int.TryParse(dtId, NumberStyles.Integer, CultureInfo.InvariantCulture, out var outInt))
                {
                    continue;
                }
                var dataType = _dataTypeService.GetDataType(outInt);
                if (dataType == null)
                {
                    continue;
                }
                dataTypes.Add(_serializer.Serialize(dataType));
            }
            root.Add(dataTypes);
        }
Ejemplo n.º 4
0
        private void PackageDictionaryItems(PackageDefinition definition, XContainer root)
        {
            var rootDictionaryItems = new XElement("DictionaryItems");
            var items = new Dictionary <Guid, (IDictionaryItem dictionaryItem, XElement serializedDictionaryValue)>();

            foreach (var dictionaryId in definition.DictionaryItems)
            {
                if (!int.TryParse(dictionaryId, NumberStyles.Integer, CultureInfo.InvariantCulture, out var outInt))
                {
                    continue;
                }

                IDictionaryItem di = _languageService.GetDictionaryItemById(outInt);

                if (di == null)
                {
                    continue;
                }

                items[di.Key] = (di, _serializer.Serialize(di, false));
            }

            // organize them in hierarchy ...
            var itemCount = items.Count;
            var processed = new Dictionary <Guid, XElement>();

            while (processed.Count < itemCount)
            {
                foreach (Guid key in items.Keys.ToList())
                {
                    (IDictionaryItem dictionaryItem, XElement serializedDictionaryValue) = items[key];

                    if (!dictionaryItem.ParentId.HasValue)
                    {
                        // if it has no parent, its definitely just at the root
                        AppendDictionaryElement(rootDictionaryItems, items, processed, key, serializedDictionaryValue);
                    }
                    else
                    {
                        if (processed.ContainsKey(dictionaryItem.ParentId.Value))
                        {
                            // we've processed this parent element already so we can just append this xml child to it
                            AppendDictionaryElement(processed[dictionaryItem.ParentId.Value], items, processed, key, serializedDictionaryValue);
                        }
                        else if (items.ContainsKey(dictionaryItem.ParentId.Value))
                        {
                            // we know the parent exists in the dictionary but
                            // we haven't processed it yet so we'll leave it for the next loop
                            continue;
                        }
                        else
                        {
                            // in this case, the parent of this item doesn't exist in our collection, we have no
                            // choice but to add it to the root.
                            AppendDictionaryElement(rootDictionaryItems, items, processed, key, serializedDictionaryValue);
                        }
                    }
                }
            }

            root.Add(rootDictionaryItems);
Ejemplo n.º 5
0
        public string ExportPackage(PackageDefinition definition)
        {
            if (definition.Id == default)
            {
                throw new ArgumentException("The package definition does not have an ID, it must be saved before being exported");
            }
            if (definition.PackageId == default)
            {
                throw new ArgumentException("the package definition does not have a GUID, it must be saved before being exported");
            }

            //ensure it's valid
            ValidatePackage(definition);

            //Create a folder for building this package
            var temporaryPath = _hostingEnvironment.MapPathContentRoot(_tempFolderPath.EnsureEndsWith('/') + Guid.NewGuid());

            if (Directory.Exists(temporaryPath) == false)
            {
                Directory.CreateDirectory(temporaryPath);
            }

            try
            {
                //Init package file
                XDocument compiledPackageXml = CreateCompiledPackageXml(out XElement root);

                //Info section
                root.Add(GetPackageInfoXml(definition));

                PackageDocumentsAndTags(definition, root);
                PackageDocumentTypes(definition, root);
                PackageMediaTypes(definition, root);
                PackageTemplates(definition, root);
                PackageStylesheets(definition, root);
                PackageStaticFiles(definition.Scripts, root, "Scripts", "Script", _fileSystems.ScriptsFileSystem);
                PackageStaticFiles(definition.PartialViews, root, "PartialViews", "View", _fileSystems.PartialViewsFileSystem);
                PackageMacros(definition, root);
                PackageDictionaryItems(definition, root);
                PackageLanguages(definition, root);
                PackageDataTypes(definition, root);
                Dictionary <string, Stream> mediaFiles = PackageMedia(definition, root);

                string fileName;
                string tempPackagePath;
                if (mediaFiles.Count > 0)
                {
                    fileName        = "package.zip";
                    tempPackagePath = Path.Combine(temporaryPath, fileName);
                    using (FileStream fileStream = File.OpenWrite(tempPackagePath))
                        using (var archive = new ZipArchive(fileStream, ZipArchiveMode.Create, true))
                        {
                            ZipArchiveEntry packageXmlEntry = archive.CreateEntry("package.xml");
                            using (Stream entryStream = packageXmlEntry.Open())
                            {
                                compiledPackageXml.Save(entryStream);
                            }

                            foreach (KeyValuePair <string, Stream> mediaFile in mediaFiles)
                            {
                                var             entryPath  = $"media{mediaFile.Key.EnsureStartsWith('/')}";
                                ZipArchiveEntry mediaEntry = archive.CreateEntry(entryPath);
                                using (Stream entryStream = mediaEntry.Open())
                                    using (mediaFile.Value)
                                    {
                                        mediaFile.Value.Seek(0, SeekOrigin.Begin);
                                        mediaFile.Value.CopyTo(entryStream);
                                    }
                            }
                        }
                }
                else
                {
                    fileName        = "package.xml";
                    tempPackagePath = Path.Combine(temporaryPath, fileName);

                    using (FileStream fileStream = File.OpenWrite(tempPackagePath))
                    {
                        compiledPackageXml.Save(fileStream);
                    }
                }

                var directoryName = _hostingEnvironment.MapPathContentRoot(Path.Combine(_createdPackagesFolderPath, definition.Name.Replace(' ', '_')));
                Directory.CreateDirectory(directoryName);

                var finalPackagePath = Path.Combine(directoryName, fileName);

                if (File.Exists(finalPackagePath))
                {
                    File.Delete(finalPackagePath);
                }

                File.Move(tempPackagePath, finalPackagePath);

                definition.PackagePath = finalPackagePath;
                SavePackage(definition);

                return(finalPackagePath);
            }
            finally
            {
                // Clean up
                Directory.Delete(temporaryPath, true);
            }
        }