示例#1
0
        private Folder LookupFolderByPath(string path, bool create = false)
        {
            NuGetLogUtils.Verbose("Fetching folders for path: " + path);

            Folder result = null;

            WithSPContext(context =>
            {
                var web = context.Web;

                var nuGetFolderUrl  = UrlUtility.CombineUrl(Root, LibraryUrl);
                var targetFolderUrl = UrlUtility.CombineUrl(nuGetFolderUrl, path);



                try
                {
                    result = web.GetFolderByServerRelativeUrl(targetFolderUrl);

                    context.Load(result);
                    context.ExecuteQuery();

                    if (result.ServerObjectIsNull.HasValue && result.ServerObjectIsNull.Value)
                    {
                        throw new Exception("ServerObjectIsNull is null. Folrder does not exist.");
                    }
                }
                catch (Exception ex)
                {
                    if (create)
                    {
                        var rootFolder = web.GetFolderByServerRelativeUrl(nuGetFolderUrl);
                        rootFolder.Folders.Add(path);

                        context.ExecuteQuery();

                        result = web.GetFolderByServerRelativeUrl(targetFolderUrl);
                        context.ExecuteQuery();
                    }
                    else
                    {
                        result = null;
                    }
                }
            });

            NuGetLogUtils.Verbose("Fetched folders for path: " + path);

            return(result);
        }
示例#2
0
        private void EnsureLibrarySchema(Web web)
        {
            NuGetLogUtils.Verbose(string.Format("Ensuring metapack library schema version:[{0}]", metaPackShemaVersion));

            var context = web.Context;

            var list = WebExtensions.LoadListByUrl(web, MetaPackConsts.SharePointLibraryUrl);

            var rootFolder = list.RootFolder;

            context.Load(rootFolder);
            context.Load(rootFolder, f => f.Properties);

            context.ExecuteQuery();

            var properties = rootFolder.Properties;

            var currentSchemaObj   = properties.FieldValues.ContainsKey(schemaKey) ? properties[schemaKey] : null;;
            var currentSchemaValue = currentSchemaObj != null?currentSchemaObj.ToString() : string.Empty;

            var currentSchema = new System.Version(0, 0, 0, 0);

            if (!string.IsNullOrEmpty(currentSchemaValue))
            {
                try
                {
                    currentSchema = new Version(currentSchemaValue);
                    NuGetLogUtils.Verbose(string.Format("Current schema value:[{0}]", currentSchema));
                }
                catch (Exception e)
                {
                    NuGetLogUtils.Verbose(string.Format("Can't parse schema version value:[{0}]", currentSchemaValue));
                }
            }

            if (currentSchema < metaPackShemaVersion)
            {
                NuGetLogUtils.Verbose(string.Format("Schema update is required. Required schema version is :[{0}]", metaPackShemaVersion));

                UpdateToVersion(list, metaPackShemaVersion);
            }
            else
            {
                NuGetLogUtils.Verbose(string.Format("No schema update is required"));
            }
        }
示例#3
0
        protected virtual void EnsureMetapackLibrary()
        {
            NuGetLogUtils.Verbose("Detecting MetaPack library on target site...");

            var web  = _context.Web;
            var list = WebExtensions.LoadListByUrl(web, MetaPackConsts.SharePointLibraryUrl);

            if (list == null)
            {
                NuGetLogUtils.Verbose(string.Format("MetaPack library does not exist. Creating..."));

                var newList = web.Lists.Add(new ListCreationInformation
                {
                    TemplateType = (int)ListTemplateType.DocumentLibrary,
                    Title        = MetaPackConsts.SharePointLibraryTitle,
                    Url          = MetaPackConsts.SharePointLibraryUrl,
                    Description  = "MetaPack Gallery library. Stores NuGet packages with SharePoint solutions."
                });

                newList.Hidden        = true;
                newList.OnQuickLaunch = false;
                newList.NoCrawl       = true;

                newList.Update();
                _context.ExecuteQuery();

                // ensure schema

                NuGetLogUtils.Info("MetaPack library created");
                EnsureLibrarySchema(web);
            }
            else
            {
                NuGetLogUtils.Verbose("MetaPack library exists");
                EnsureLibrarySchema(web);
            }
        }
示例#4
0
        public Stream OpenFile(string path)
        {
            NuGetLogUtils.Verbose("Opening file for path: " + path);

            Stream result = null;

            WithSPContext(context =>
            {
                var web = context.Web;

                if (!web.IsPropertyAvailable("ServerRelativeUrl"))
                {
                    context.Load(web, w => w.ServerRelativeUrl);
                    context.ExecuteQuery();
                }

                var rootFolderUrl = UrlUtility.CombineUrl(web.ServerRelativeUrl, LibraryUrl).ToLower();

                // /sites/ci-121/metapack-gallery/metapack.sharepointpnp.ci.1.2017.0414.0657/metapack.sharepointpnp.ci.1.2017.0414.0657.nupkg
                var folderRelatedFileUrl = path;

                if (folderRelatedFileUrl.Contains(rootFolderUrl))
                {
                    folderRelatedFileUrl = folderRelatedFileUrl.Split(new string[] { rootFolderUrl }, StringSplitOptions.None)[1];
                }

                var fileUrl = UrlUtility.CombineUrl(rootFolderUrl, folderRelatedFileUrl);

                // try file item first, shadow this for optimization

                NuGetLogUtils.Verbose("Opening file for path: " + fileUrl);
                var file = web.GetFileByServerRelativeUrl(fileUrl);
                var item = file.ListItemAllFields;

                context.Load(item);
                context.ExecuteQuery();

                if (item == null || (item.ServerObjectIsNull.HasValue && item.ServerObjectIsNull.Value))
                {
                    throw new MetaPackException(string.Format(
                                                    "Cannot find file by server relative path:[{0}]",
                                                    fileUrl));
                }

                var nuspecXml = (string)item["PackageNuspecXml"];

                var useNuspecXml = true;

                // faking nuget package here to improve performance
                // PackageNuspecXml stores nuget package manifest
                // in that case, NuGet does not download the whole package from SharePoint!
                if (!string.IsNullOrEmpty(nuspecXml) && useNuspecXml)
                {
                    NuGetLogUtils.Verbose("Using PackageNuspecXml...");

                    var nuspecXmlDoc     = XDocument.Parse(nuspecXml);
                    var packagingService = new FakeNuGetSolutionPackageService();

                    using (var stream = new MemoryStream())
                    {
                        using (var writer = new StreamWriter(stream))
                        {
                            var fakeSolutionPackageBase = (new Core.Packaging.SolutionPackageBase
                            {
                                Authors = GetNuGetPackageMetadataValue(nuspecXmlDoc, "Authors"),
                                Company = GetNuGetPackageMetadataValue(nuspecXmlDoc, "Company"),
                                Copyright = GetNuGetPackageMetadataValue(nuspecXmlDoc, "Copyright"),
                                Description = GetNuGetPackageMetadataValue(nuspecXmlDoc, "Description"),
                                IconUrl = GetNuGetPackageMetadataValue(nuspecXmlDoc, "IconUrl"),
                                Id = GetNuGetPackageMetadataValue(nuspecXmlDoc, "Id"),
                                LicenseUrl = GetNuGetPackageMetadataValue(nuspecXmlDoc, "LicenseUrl"),
                                Name = GetNuGetPackageMetadataValue(nuspecXmlDoc, "Name"),
                                Owners = GetNuGetPackageMetadataValue(nuspecXmlDoc, "Owners"),
                                ProjectUrl = GetNuGetPackageMetadataValue(nuspecXmlDoc, "ProjectUrl"),
                                ReleaseNotes = GetNuGetPackageMetadataValue(nuspecXmlDoc, "ReleaseNotes"),
                                Summary = GetNuGetPackageMetadataValue(nuspecXmlDoc, "Summary"),
                                Tags = GetNuGetPackageMetadataValue(nuspecXmlDoc, "Tags"),
                                Title = GetNuGetPackageMetadataValue(nuspecXmlDoc, "Title"),
                                Version = GetNuGetPackageMetadataValue(nuspecXmlDoc, "Version")
                            });

                            // TODO, dependencies

                            result = packagingService.Pack(fakeSolutionPackageBase, null);
                        }
                    }

                    return;
                }

                NuGetLogUtils.Verbose("Using raw file content...");

                // fallback on the actual file
                // that's slow, NuGet download the whole package to get the metadata
                var q = file.OpenBinaryStream();

                context.Load(file);
                context.ExecuteQuery();

                result = q.Value;

                var memoryStream = new MemoryStream();

                CopyStream(q.Value, memoryStream);
                memoryStream.Position = 0;

                result = memoryStream;
            });

            NuGetLogUtils.Verbose("Opened file for path: " + path);

            return(result);
        }
示例#5
0
        public IEnumerable <string> GetFiles(string path, string filter, bool recursive)
        {
            return(GetShadowFiles(path, filter, recursive));

            var hasFolderMatch = Regex.IsMatch(path.ToLower(), WildCardToRegular(filter.ToLower().Replace(".nupkg", string.Empty)).ToLower());

            if (!hasFolderMatch)
            {
                return(Enumerable.Empty <string>());
            }

            NuGetLogUtils.Verbose(string.Format("Fetching files. Path:[{0}] Filter:[{1}] Recursive:[{2}]", path, filter, recursive));

            if (recursive)
            {
                throw new NotSupportedException("recursive = true");
            }

            var isRootFolder = string.IsNullOrEmpty(path);

            path = path.ToLower();
            var files = new List <VirtualFile>();

            WithSPContext(context =>
            {
                var nuGetFolderUrl = UrlUtility.CombineUrl(Root, LibraryUrl);

                if (!isRootFolder)
                {
                    nuGetFolderUrl = UrlUtility.CombineUrl(nuGetFolderUrl, path);
                }

                var web    = context.Web;
                var exists = false;

                var folder = web.GetFolderByServerRelativeUrl(nuGetFolderUrl);

                try
                {
                    context.ExecuteQuery();
                    exists = true;
                }
                catch (Exception ex)
                {
                }

                if (exists)
                {
                    files = new List <VirtualFile>();

                    if (!_cachedPathToFiles.ContainsKey(path))
                    {
                        _cachedPathToFiles.Add(path, files);
                    }
                    else
                    {
                        files = _cachedPathToFiles[path];
                    }

                    var items = folder.Files;

                    context.Load(items);
                    context.ExecuteQuery();

                    foreach (var f in items)
                    {
                        files.Add(new VirtualFile
                        {
                            Path         = f.Name,
                            LastModified = f.TimeLastModified,
                            RelativePath = Path.Combine(path, f.Name)
                        });
                    }
                }
                else
                {
                }
            });

            var result = new List <string>();

            foreach (var file in files)
            {
                if (string.IsNullOrEmpty(filter))
                {
                    result.Add(file.Path);
                }
                else
                {
                    var fileName  = file.Path;
                    var fileMatch = Regex.IsMatch(fileName.ToLower(), WildCardToRegular(filter).ToLower());

                    if (fileMatch)
                    {
                        result.Add(fileName);
                    }
                }
            }

            // make relative path
            if (result.Any())
            {
                result = result.Select(p => Path.Combine(path, p)).ToList();
            }

            NuGetLogUtils.Verbose("Fetched files for path with filter: " + filter + " : " + path);

            return(result);
        }
示例#6
0
        private void AddFileToSharePoint(Folder rootFolder, string filePath, Stream fileStream)
        {
            NuGetLogUtils.Verbose(string.Format("Saved file [{0}] to SharePoint", filePath));

            MemoryStream clonedNuPkgStream    = null;
            MemoryStream clonedZipNuPkgStream = null;

            try
            {
                if (filePath.ToLower().EndsWith(".nupkg"))
                {
                    clonedNuPkgStream    = new MemoryStream();
                    clonedZipNuPkgStream = new MemoryStream();

                    CopyStream(fileStream, clonedNuPkgStream);
                    fileStream.Position = 0;

                    CopyStream(fileStream, clonedZipNuPkgStream);
                    fileStream.Position = 0;

                    clonedNuPkgStream.Position    = 0;
                    clonedZipNuPkgStream.Position = 0;
                }
                else
                {
                    return;
                }

                var context = rootFolder.Context;

                var fileName       = Path.GetFileName(filePath);
                var fileFolderPath = Path.GetDirectoryName(filePath);

                InvalidatePathToFilesCache(fileFolderPath);

                var fileFolder = EnsureFolder(context, rootFolder, fileFolderPath);

                var file = fileFolder.Files.Add(new FileCreationInformation
                {
                    ContentStream = fileStream,
                    Overwrite     = true,
                    Url           = fileName
                });

                context.ExecuteQuery();

                if (clonedNuPkgStream != null)
                {
                    NuGetLogUtils.Verbose("Saving .nupkg metadata to SharePoint...");

                    var zipPackage = new ZipPackage(clonedNuPkgStream);
                    var zipArchive = new System.IO.Compression.ZipArchive(clonedZipNuPkgStream);

                    NuGetLogUtils.Info("Fetching nuspec file...");

                    var manifestFileName = string.Format("{0}.nuspec", zipPackage.Id);
                    var manifestFile     = zipArchive.GetEntry(manifestFileName);

                    var nuspecXmlValue = string.Empty;

                    using (var stramReader = new StreamReader(manifestFile.Open()))
                    {
                        nuspecXmlValue = stramReader.ReadToEnd();
                    }

                    NuGetLogUtils.Verbose("Updating nuspec and version values...");
                    if (!string.IsNullOrEmpty(nuspecXmlValue))
                    {
                        // update properties
                        var fileItem = file.ListItemAllFields;

                        context.Load(fileItem);

                        fileItem.ParseAndSetFieldValue("PackageNuspecXml", nuspecXmlValue);
                        fileItem.ParseAndSetFieldValue("PackageVersion", zipPackage.Version.ToString());
                        fileItem.ParseAndSetFieldValue("PackageId", zipPackage.Id);

                        fileItem.Update();
                        context.ExecuteQuery();
                    }
                }

                NuGetLogUtils.Verbose(string.Format("Saved file [{0}] to SharePoint", filePath));
            }
            finally
            {
                if (clonedNuPkgStream != null)
                {
                    clonedNuPkgStream.Dispose();
                    clonedNuPkgStream = null;
                }

                if (clonedZipNuPkgStream != null)
                {
                    clonedZipNuPkgStream.Dispose();
                    clonedZipNuPkgStream = null;
                }
            }
        }