예제 #1
0
        public static PSObject ReadPSGetResourceInfo(string filePath)
        {
            if (PSResourceInfo.TryRead(filePath, out PSResourceInfo psGetInfo, out string errorMsg))
            {
                return(PSObject.AsPSObject(psGetInfo));
            }

            throw new PSInvalidOperationException(errorMsg);
        }
예제 #2
0
        public static bool TryConvert(
            IPackageSearchMetadata metadataToParse,
            out PSResourceInfo psGetInfo,
            string repositoryName,
            ResourceType?type,
            out string errorMsg)
        {
            psGetInfo = null;
            errorMsg  = String.Empty;

            if (metadataToParse == null)
            {
                errorMsg = "TryConvertPSResourceInfo: Invalid IPackageSearchMetadata object. Object cannot be null.";
                return(false);
            }

            try
            {
                psGetInfo = new PSResourceInfo(
                    additionalMetadata: null,
                    author: ParseMetadataAuthor(metadataToParse),
                    companyName: null,
                    copyright: null,
                    dependencies: ParseMetadataDependencies(metadataToParse),
                    description: ParseMetadataDescription(metadataToParse),
                    iconUri: ParseMetadataIconUri(metadataToParse),
                    includes: null,
                    installedDate: null,
                    installedLocation: null,
                    isPrelease: ParseMetadataIsPrerelease(metadataToParse),
                    licenseUri: ParseMetadataLicenseUri(metadataToParse),
                    name: ParseMetadataName(metadataToParse),
                    packageManagementProvider: null,
                    powershellGetFormatVersion: null,
                    prereleaseLabel: ParsePrerelease(metadataToParse),
                    projectUri: ParseMetadataProjectUri(metadataToParse),
                    publishedDate: ParseMetadataPublishedDate(metadataToParse),
                    releaseNotes: null,
                    repository: repositoryName,
                    repositorySourceLocation: null,
                    tags: ParseMetadataTags(metadataToParse),
                    type: ParseMetadataType(metadataToParse, repositoryName, type),
                    updatedDate: null,
                    version: ParseMetadataVersion(metadataToParse));

                return(true);
            }
            catch (Exception ex)
            {
                errorMsg = string.Format(
                    CultureInfo.InvariantCulture,
                    @"TryReadPSGetInfo: Cannot parse PSResourceInfo from IPackageSearchMetadata with error: {0}",
                    ex.Message);
                return(false);
            }
        }
예제 #3
0
        public static bool GetVersionForInstallPath(
            string installedPkgPath,
            bool isModule,
            PSCmdlet cmdletPassedIn,
            out NuGetVersion pkgNuGetVersion)
        {
            // this method returns false if the PSGetModuleInfo.xml or {pkgName}_InstalledScriptInfo.xml file
            // could not be parsed properly, or the version from it could not be parsed into a NuGetVersion.
            // In this case the caller method (i.e GetHelper.FilterPkgPathsByVersion()) should skip the current
            // installed package path or reassign NuGetVersion variable passed in to a non-null value as it sees fit.

            // for Modules, installedPkgPath will look like this:
            // ./PowerShell/Modules/test_module/3.0.0
            // for Scripts, installedPkgPath will look like this:
            // ./PowerShell/Scripts/test_script.ps1
            string pkgName = isModule ? String.Empty : Utils.GetInstalledPackageName(installedPkgPath);

            string packageInfoXMLFilePath = isModule ? Path.Combine(installedPkgPath, "PSGetModuleInfo.xml") : Path.Combine((new DirectoryInfo(installedPkgPath).Parent).FullName, "InstalledScriptInfos", $"{pkgName}_InstalledScriptInfo.xml");

            if (!PSResourceInfo.TryRead(packageInfoXMLFilePath, out PSResourceInfo psGetInfo, out string errorMsg))
            {
                cmdletPassedIn.WriteVerbose(String.Format(
                                                "The {0} file found at location: {1} cannot be parsed due to {2}",
                                                isModule ? "PSGetModuleInfo.xml" : $"{pkgName}_InstalledScriptInfo.xml",
                                                packageInfoXMLFilePath,
                                                errorMsg));
                pkgNuGetVersion = null;
                return(false);
            }

            string version         = psGetInfo.Version.ToString();
            string prereleaseLabel = psGetInfo.PrereleaseLabel;

            if (!NuGetVersion.TryParse(
                    value: String.IsNullOrEmpty(prereleaseLabel) ? version : GetNormalizedVersionString(version, prereleaseLabel),
                    version: out pkgNuGetVersion))
            {
                cmdletPassedIn.WriteVerbose(String.Format("Leaf directory in path '{0}' cannot be parsed into a version.", installedPkgPath));
                return(false);
            }

            return(true);
        }
예제 #4
0
        /// <summary>
        /// Reads a PSGet resource xml (PowerShell serialized) file and returns
        /// a PSResourceInfo object containing the file contents.
        /// </summary>
        public static bool TryRead(
            string filePath,
            out PSResourceInfo psGetInfo,
            out string errorMsg)
        {
            psGetInfo = null;
            errorMsg  = string.Empty;

            if (string.IsNullOrWhiteSpace(filePath))
            {
                errorMsg = "TryReadPSGetInfo: Invalid file path. Filepath cannot be empty or whitespace.";
                return(false);
            }

            try
            {
                // Read and deserialize information xml file.
                var psObjectInfo = (PSObject)PSSerializer.Deserialize(
                    System.IO.File.ReadAllText(
                        filePath));

                var     additionalMetadata = GetProperty <Dictionary <string, string> >(nameof(PSResourceInfo.AdditionalMetadata), psObjectInfo);
                Version version            = GetVersionInfo(psObjectInfo, additionalMetadata, out string prereleaseLabel);

                psGetInfo = new PSResourceInfo(
                    additionalMetadata: additionalMetadata,
                    author: GetStringProperty(nameof(PSResourceInfo.Author), psObjectInfo),
                    companyName: GetStringProperty(nameof(PSResourceInfo.CompanyName), psObjectInfo),
                    copyright: GetStringProperty(nameof(PSResourceInfo.Copyright), psObjectInfo),
                    dependencies: GetDependencies(GetProperty <ArrayList>(nameof(PSResourceInfo.Dependencies), psObjectInfo)),
                    description: GetStringProperty(nameof(PSResourceInfo.Description), psObjectInfo),
                    iconUri: GetProperty <Uri>(nameof(PSResourceInfo.IconUri), psObjectInfo),
                    includes: new ResourceIncludes(GetProperty <Hashtable>(nameof(PSResourceInfo.Includes), psObjectInfo)),
                    installedDate: GetProperty <DateTime>(nameof(PSResourceInfo.InstalledDate), psObjectInfo),
                    installedLocation: GetStringProperty(nameof(PSResourceInfo.InstalledLocation), psObjectInfo),
                    isPrelease: GetProperty <bool>(nameof(PSResourceInfo.IsPrerelease), psObjectInfo),
                    licenseUri: GetProperty <Uri>(nameof(PSResourceInfo.LicenseUri), psObjectInfo),
                    name: GetStringProperty(nameof(PSResourceInfo.Name), psObjectInfo),
                    packageManagementProvider: GetStringProperty(nameof(PSResourceInfo.PackageManagementProvider), psObjectInfo),
                    powershellGetFormatVersion: GetStringProperty(nameof(PSResourceInfo.PowerShellGetFormatVersion), psObjectInfo),
                    prereleaseLabel: prereleaseLabel,
                    projectUri: GetProperty <Uri>(nameof(PSResourceInfo.ProjectUri), psObjectInfo),
                    publishedDate: GetProperty <DateTime>(nameof(PSResourceInfo.PublishedDate), psObjectInfo),
                    releaseNotes: GetStringProperty(nameof(PSResourceInfo.ReleaseNotes), psObjectInfo),
                    repository: GetStringProperty(nameof(PSResourceInfo.Repository), psObjectInfo),
                    repositorySourceLocation: GetStringProperty(nameof(PSResourceInfo.RepositorySourceLocation), psObjectInfo),
                    tags: Utils.GetStringArray(GetProperty <ArrayList>(nameof(PSResourceInfo.Tags), psObjectInfo)),
                    type: Enum.TryParse(
                        GetProperty <string>(nameof(PSResourceInfo.Type), psObjectInfo) ?? nameof(ResourceType.Module),
                        out ResourceType currentReadType)
                                    ? currentReadType : ResourceType.Module,
                    updatedDate: GetProperty <DateTime>(nameof(PSResourceInfo.UpdatedDate), psObjectInfo),
                    version: version);

                return(true);
            }
            catch (Exception ex)
            {
                errorMsg = string.Format(
                    CultureInfo.InvariantCulture,
                    @"TryReadPSGetInfo: Cannot read the PowerShellGet information file with error: {0}",
                    ex.Message);

                return(false);
            }
        }
예제 #5
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="name">Name of the command or DSC resource</param>
 /// <param name="parentResource">the parent module resource the command or dsc resource belongs to</param>
 public PSCommandResourceInfo(string name, PSResourceInfo parentResource)
 {
     Name           = name;
     ParentResource = parentResource;
 }