public void SerializeDataToStream(object target, Stream outgoingData)
        {
            try
            {
                if (target != null)
                {
                    var xml = SPMeta2Model.ToXML(((ModelNode)target));

                    var writer = new StreamWriter(outgoingData);
                    writer.WriteLine(xml);
                    writer.Flush();
                }

                if (ShouldThrowException)
                {
                    HadException = true;
                    throw new ApplicationException("ShouldThrowException = true");
                }
            }
            catch (Exception)
            {
                throw;
                // TODO
            }
        }
Exemple #2
0
        protected virtual Stream CreateSPFFieldSettingsPackage(ModelNode model, Action <SolutionPackageBase> action)
        {
            // save solution to XML file
            var xmlContext = SPMeta2Model.ToXML(model);

            // pack model
            var solutionPackage = GetSolutionPackageTemplate();

            // update version and add model
            action(solutionPackage);

            // create ModelContainerBase, put serialized model there
            var modelContainer = new ModelContainerBase
            {
                Model = Encoding.UTF8.GetBytes(xmlContext),
            };

            solutionPackage.AddModel(modelContainer);

            // pack to NuGet package
            var packageService        = new SPMeta2SolutionPackageService();
            var solutionPackageStream = packageService.Pack(solutionPackage);

            return(solutionPackageStream);
        }
        protected virtual void EnsureSerializationSupport(IEnumerable <ModelNode> models)
        {
            foreach (var model in models)
            {
                var xml = SPMeta2Model.ToXML(model);
                var xmlModelInstance = SPMeta2Model.FromXML(xml);

                var json = SPMeta2Model.ToJSON(model);
                var jsonModelInstance = SPMeta2Model.FromJSON(json);
            }
        }
        public virtual List <ModelNode> GetSerializedAndRestoredModels(IEnumerable <ModelNode> models)
        {
            var result = new List <ModelNode>();

            foreach (var model in models)
            {
                var xml = SPMeta2Model.ToXML(model);
                var xmlModelInstance = SPMeta2Model.FromXML(xml);

                var json = SPMeta2Model.ToJSON(model);
                var jsonModelInstance = SPMeta2Model.FromJSON(json);

                result.Add(xmlModelInstance);
                result.Add(jsonModelInstance);
            }

            return(result);
        }
        public void CanSerialize_SiteModelToXMLAndBack()
        {
            var orginalModel = SPMeta2Model.NewSiteModel(site =>
            {
            });

            TraceUtils.WithScope(trace =>
            {
                var modelString = SPMeta2Model.ToXML(orginalModel);
                Assert.IsFalse(string.IsNullOrEmpty(modelString));

                trace.WriteLine("XML");
                trace.WriteLine(modelString);

                var deserializedModel = SPMeta2Model.FromXML(modelString);
                Assert.IsNotNull(deserializedModel);
            });
        }
Exemple #6
0
        protected virtual SolutionPackageBase CreateNewSolutionPackage(
            NuGetSolutionPackageService service,
            Action <SolutionPackageBase> action,
            RegressinModelLevel modelLevel)
        {
            var knownPackageType = false;

            SolutionPackageBase solutionPackage = null;

            if (service is SPMeta2SolutionPackageService)
            {
                var m2package = new SolutionPackageBase();

                m2package.Name  = "SPMeta2 CI Package Name";
                m2package.Title = "SPMeta2 CI Package Title";

                m2package.Description = "SPMeta2 CI Package description";
                m2package.Id          = "MetaPack.SPMeta2.CI";
                m2package.Authors     = "SubPoint Solutions Authors";
                m2package.Company     = "SubPoint Solutions Company";
                m2package.Version     = "1.0.0.0";
                m2package.Owners      = "SubPoint Solutions Owners";

                m2package.ReleaseNotes = "ReleaseNotes";
                m2package.Summary      = "Summary";
                m2package.ProjectUrl   = "https://github.com/SubPointSolutions/metapack";
                m2package.IconUrl      = "https://github.com/SubPointSolutions/metapack/metapack.png";
                m2package.LicenseUrl   = "https://opensource.org/licenses/MIT";

                m2package.Copyright = "Some copyright here";
                m2package.Tags      = "CI SPMeta2 MetaPack Tags";

                var models = new List <ModelNode>();

                switch (modelLevel)
                {
                case RegressinModelLevel.Farm:
                    models.Add(SPMeta2Model.NewFarmModel(farm => { }));
                    break;

                case RegressinModelLevel.WebApplication:
                    models.Add(SPMeta2Model.NewWebApplicationModel(webApp => { }));
                    break;

                case RegressinModelLevel.Site:
                    models.Add(SPMeta2Model.NewSiteModel(site => { }));
                    models.Add(SPMeta2Model.NewWebModel(web => { }));
                    break;

                case RegressinModelLevel.Web:
                    models.Add(SPMeta2Model.NewWebModel(web => { }));
                    break;

                case RegressinModelLevel.Subweb:
                    models.Add(SPMeta2Model.NewWebModel(web => { }));
                    break;

                default:
                    throw new NotImplementedException(string.Format(
                                                          "Unsupported model level:[{0}] for model genaration",
                                                          modelLevel));
                }

                var index = 0;

                foreach (var model in models)
                {
                    index++;

                    var xmlContext = SPMeta2Model.ToXML(model);

                    var modelContainer = new ModelContainerBase
                    {
                        Model = Encoding.UTF8.GetBytes(xmlContext),
                    };

                    modelContainer.AdditionalOptions.Add(new OptionValue
                    {
                        Name  = DefaultOptions.Model.Order.Id,
                        Value = index.ToString()
                    });

                    m2package.AddModel(modelContainer);
                }

                m2package.AdditionalOptions.Add(new OptionValue
                {
                    Name  = DefaultOptions.SolutionToolPackage.PackageId.Id,
                    Value = "MetaPack.SPMeta2"
                });

                solutionPackage = m2package;
            }

            if (service is SharePointPnPSolutionPackageService)
            {
                var pnpPackage = new SolutionPackageBase();

                pnpPackage.Name  = "SharePointPnP CI Package Name";
                pnpPackage.Title = "SharePointPnP Package Title";

                pnpPackage.Description = "SPMeta2 CI Package description";
                pnpPackage.Id          = "MetaPack.SharePointPnP.CI";
                pnpPackage.Authors     = "SubPoint Solutions Authors";
                pnpPackage.Company     = "SubPoint Solutions Company";
                pnpPackage.Version     = "1.0.0.0";
                pnpPackage.Owners      = "SubPoint Solutions Owners";

                pnpPackage.ReleaseNotes = "ReleaseNotes";
                pnpPackage.Summary      = "Summary";
                pnpPackage.ProjectUrl   = "https://github.com/SubPointSolutions/metapack";
                pnpPackage.IconUrl      = "https://github.com/SubPointSolutions/metapack/metapack.png";
                pnpPackage.LicenseUrl   = "https://opensource.org/licenses/MIT";

                pnpPackage.Copyright = "Some copyright here";
                pnpPackage.Tags      = "CI SPMeta2 MetaPack Tags";


                // TODO
                // Zip up and set the model type
                var asmFolder = Path.GetDirectoryName(GetType().Assembly.Location);

                var foldersPath       = Path.Combine(asmFolder, @"Data/PnPTemplates/Folders");
                var openXmlFolderPath = Path.Combine(asmFolder, @"Data/PnPTemplates/OpenXML");

                foreach (var templateFolder in Directory.GetDirectories(foldersPath))
                {
                    // package up into zip
                    var templateFolderZipFile = GetTempZipFilePath();
                    ZipFile.CreateFromDirectory(templateFolder, templateFolderZipFile);

                    var modelContainer = new ModelContainerBase
                    {
                        Model = System.IO.File.ReadAllBytes(templateFolderZipFile),
                    };

                    modelContainer.AdditionalOptions.Add(new OptionValue
                    {
                        Name  = DefaultOptions.Model.Type.Id,
                        Value = "SharePointPnP.FolderZip"
                    });

                    pnpPackage.AddModel(modelContainer);
                }



                var openXmlPackages = Directory.GetFiles(openXmlFolderPath, "*.pnp");

                foreach (var file in openXmlPackages)
                {
                    var modelContainer = new ModelContainerBase
                    {
                        Model = System.IO.File.ReadAllBytes(file),
                    };

                    modelContainer.AdditionalOptions.Add(new OptionValue
                    {
                        Name  = DefaultOptions.Model.Type.Id,
                        Value = "SharePointPnP.OpenXml"
                    });

                    pnpPackage.AddModel(modelContainer);
                }

                pnpPackage.AdditionalOptions.Add(new OptionValue
                {
                    Name  = DefaultOptions.SolutionToolPackage.PackageId.Id,
                    Value = "MetaPack.SharePointPnP"
                });

                solutionPackage = pnpPackage;
            }

            if (solutionPackage == null)
            {
                throw new NotImplementedException(string.Format(
                                                      "Unknown service type:[{0}]", service.GetType()));
            }

            if (action != null)
            {
                action(solutionPackage);
            }

            return(solutionPackage);
        }
Exemple #7
0
        public void Create_Package_SPMeta2()
        {
            // Follows NuGet spec design - https://docs.nuget.org/ndocs/schema/nuspec
            // Solution package is a container for SERIALIZED models.
            var solutionPackage = new SolutionPackageBase();

            solutionPackage.Name  = "Contoso Intranet SPMeta2 - Site Fields";
            solutionPackage.Title = "Contoso Intranet SPMeta2 - Site Fields";

            solutionPackage.Description = "Contains site level fields for Contoso intranet";
            solutionPackage.Id          = "Contoso.Intranet.SiteFields.SPMeta2";
            solutionPackage.Authors     = "SubPoint Solutions";
            solutionPackage.Company     = "SubPoint Solutions";
            solutionPackage.Version     = "1.0.0.0";
            solutionPackage.Owners      = "SubPoint Solutions";

            solutionPackage.ReleaseNotes = "Initial set of the site fields for Contoso Intranet";
            solutionPackage.Summary      = "All site fields required for Contoso intranet";
            solutionPackage.ProjectUrl   = "https://github.com/SubPointSolutions/DefinitelyPacked";
            solutionPackage.IconUrl      = "https://github.com/SubPointSolutions/metapack/metapack.png";
            solutionPackage.LicenseUrl   = "https://opensource.org/licenses/MIT";

            solutionPackage.Copyright = "All yours";
            solutionPackage.Tags      = "MetaPack SPMeta2 SiteFields Taxonomy";

            // here are all your SPMeta2 models
            var models = new List <ModelNode>();

            // create a new ModelContainerBase for every model
            // and then add to solution package
            // you can put "Order" option to control deployment order of the models

            for (var index = 0; index < models.Count; index++)
            {
                var model      = models[index];
                var xmlContext = SPMeta2Model.ToXML(model);

                // create ModelContainerBase, put serialized model there
                var modelContainer = new ModelContainerBase
                {
                    Model = Encoding.UTF8.GetBytes(xmlContext),
                };

                // add sort order to control deployment order of the models
                modelContainer.AdditionalOptions.Add(new OptionValue
                {
                    Name  = DefaultOptions.Model.Order.Id,
                    Value = index.ToString()
                });

                // add model container to solution
                solutionPackage.AddModel(modelContainer);
            }

            // flag a provider which will be used for solution package deployment
            solutionPackage.AdditionalOptions.Add(new OptionValue
            {
                Name  = DefaultOptions.SolutionToolPackage.PackageId.Id,
                Value = "MetaPack.SPMeta2"
            });

            var solutionPackageService = new SPMeta2SolutionPackageService();

            // save your NuGet solution package as stream
            var nuGetPackageStream = solutionPackageService.Pack(solutionPackage, null);

            // or save it straight to file, for instance, on shared folder
            solutionPackageService.PackToFile(solutionPackage, "Contoso.Intranet.SiteFields.SPMeta2.nupkg");

            // or push it straight to NuGet gallery you've got - http://NuGet.org or http://MyGet.org
            // follow instructions on how obtain Url/Key for a specific NuGet Gallery
            var nuGetGallery_ApiUrl = "";
            var nuGetGallery_ApiKey = "";

            solutionPackageService.Push(solutionPackage, nuGetGallery_ApiUrl, nuGetGallery_ApiKey);
        }
        protected virtual IEnumerable<Stream> CreateMetaPackPackages()
        {
            var result = new List<Stream>();

            // pack model
            var solutionPackage = GetSolutionPackageTemplate();

            // update version and add model
            solutionPackage.Version = "0.1.0-beta1";

            var assemblyDirectoryPath = Path.GetDirectoryName(GetType().Assembly.Location);
            var jQueryDirectoryPath = Path.Combine(assemblyDirectoryPath, "jQuery");

            if (!Directory.Exists(jQueryDirectoryPath))
                throw new Exception(string.Format("Cannot find directory:[{0}]", jQueryDirectoryPath));

            var jQueryVersionFolderPaths = Directory.GetDirectories(jQueryDirectoryPath);

            foreach (var versionFolderPath in jQueryVersionFolderPaths)
            {
                var jQueryVersion = Path.GetFileName(versionFolderPath);

                var versionFolderName = (new DirectoryInfo(versionFolderPath)).Name;
                var filePaths = Directory.GetFiles(versionFolderPath, "*.*");

                // create model
                var webModel = SPMeta2Model.NewWebModel(web =>
                {
                    web.AddHostList(BuiltInListDefinitions.StyleLibrary, list =>
                    {
                        list.AddFolder(new FolderDefinition { Name = "jQuery" }, jQueryFolder =>
                        {
                            jQueryFolder.AddFolder(new FolderDefinition { Name = versionFolderName }, folder =>
                            {
                                foreach (var filePath in filePaths)
                                {
                                    folder.AddModuleFile(new ModuleFileDefinition
                                    {
                                        FileName = Path.GetFileName(filePath),
                                        Content = File.ReadAllBytes(filePath)
                                    });
                                }
                            });
                        });
                    });
                });

                // save solution to XML file
                var xmlContext = SPMeta2Model.ToXML(webModel);



                // create ModelContainerBase, put serialized model there
                var modelContainer = new ModelContainerBase
                {
                    Model = Encoding.UTF8.GetBytes(xmlContext),
                };

                modelContainer.AdditionalOptions.Add(new OptionValue
                {
                    Name = "_metapack.model.id",
                    Value = string.Format("jQuery.{0}", jQueryVersion)
                });

                solutionPackage.AddModel(modelContainer);
            }

            // pack to NuGet package
            var packageService = new SPMeta2SolutionPackageService();
            var solutionPackageStream = packageService.Pack(solutionPackage);

            // add to result strems
            result.Add(solutionPackageStream);

            return result;
        }