Exemple #1
0
        /// <summary>
        /// Updates .ps1 file.
        /// Caller must check that the file to update doesn't have a signature or if it does permission to remove signature has been granted
        /// as this method will remove original signature, as updating would have invalidated it.
        /// </summary>
        internal static bool TryUpdateScriptFileContents(
            PSScriptFileInfo scriptInfo,
            out string[] updatedPSScriptFileContents,
            out ErrorRecord[] errors,
            string version,
            Guid guid,
            string author,
            string companyName,
            string copyright,
            string[] tags,
            Uri licenseUri,
            Uri projectUri,
            Uri iconUri,
            ModuleSpecification[] requiredModules,
            string[] externalModuleDependencies,
            string[] requiredScripts,
            string[] externalScriptDependencies,
            string releaseNotes,
            string privateData,
            string description)
        {
            updatedPSScriptFileContents = Utils.EmptyStrArray;
            List <ErrorRecord> errorsList = new List <ErrorRecord>();
            bool successfullyUpdated      = true;

            if (scriptInfo == null)
            {
                throw new ArgumentNullException(nameof(scriptInfo));
            }

            if (!scriptInfo.ScriptMetadataComment.UpdateContent(
                    version: version,
                    guid: guid,
                    author: author,
                    companyName: companyName,
                    copyright: copyright,
                    tags: tags,
                    licenseUri: licenseUri,
                    projectUri: projectUri,
                    iconUri: iconUri,
                    externalModuleDependencies: externalModuleDependencies,
                    requiredScripts: requiredScripts,
                    externalScriptDependencies: externalScriptDependencies,
                    releaseNotes: releaseNotes,
                    privateData: privateData,
                    out ErrorRecord metadataUpdateError))
            {
                errorsList.Add(metadataUpdateError);
                successfullyUpdated = false;
            }

            if (!scriptInfo.ScriptHelpComment.UpdateContent(
                    description: description,
                    out ErrorRecord helpUpdateError))
            {
                errorsList.Add(helpUpdateError);
                successfullyUpdated = false;
            }

            // this doesn't produce errors, as ModuleSpecification creation is already validated before param passed in
            // and user can't update endOfFileContents
            scriptInfo.ScriptRequiresComment.UpdateContent(requiredModules: requiredModules);

            if (!successfullyUpdated)
            {
                errors = errorsList.ToArray();
                return(successfullyUpdated);
            }

            // create string contents for .ps1 file
            if (!scriptInfo.TryCreateScriptFileInfoString(
                    psScriptFileContents: out updatedPSScriptFileContents,
                    errors: out ErrorRecord[] createUpdatedFileContentErrors))
            {
                errorsList.AddRange(createUpdatedFileContentErrors);
                successfullyUpdated = false;
            }

            errors = errorsList.ToArray();
            return(successfullyUpdated);
        }
Exemple #2
0
        /// <summary>
        /// Tests .ps1 file for validity
        /// </summary>
        internal static bool TryTestPSScriptFile(
            string scriptFileInfoPath,
            out PSScriptFileInfo parsedScript,
            out ErrorRecord[] errors,
            // this is for Uri errors, which aren't required by script but we check if those in the script aren't valid Uri's.
            out string[] verboseMsgs)
        {
            verboseMsgs = Utils.EmptyStrArray;
            List <ErrorRecord> errorsList = new List <ErrorRecord>();

            parsedScript = null;

            List <string> psScriptInfoCommentContent = new List <string>();
            List <string> helpInfoCommentContent     = new List <string>();
            List <string> requiresCommentContent     = new List <string>();

            string[] remainingFileContent = Utils.EmptyStrArray;

            // Parse .ps1 contents out of file into list objects
            if (!TryParseScriptFileContents(
                    scriptFileInfoPath: scriptFileInfoPath,
                    psScriptInfoCommentContent: ref psScriptInfoCommentContent,
                    helpInfoCommentContent: ref helpInfoCommentContent,
                    requiresCommentContent: ref requiresCommentContent,
                    remainingFileContent: ref remainingFileContent,
                    out ErrorRecord parseError))
            {
                errors = new ErrorRecord[] { parseError };
                return(false);
            }

            // Populate PSScriptFileInfo object by first creating instances for the property objects
            // i.e (PSScriptMetadata, PSScriptHelp, PSScriptRequires, PSScriptContents)
            if (!TryPopulateScriptClassesWithParsedContent(
                    psScriptInfoCommentContent: psScriptInfoCommentContent,
                    helpInfoCommentContent: helpInfoCommentContent,
                    requiresCommentContent: requiresCommentContent,
                    remainingFileContent: remainingFileContent,
                    currentMetadata: out PSScriptMetadata currentMetadata,
                    currentHelpInfo: out PSScriptHelp currentHelpInfo,
                    currentRequiresComment: out PSScriptRequires currentRequiresComment,
                    currentEndOfFileContents: out PSScriptContents currentEndOfFileContents,
                    errors: out errors,
                    out verboseMsgs))
            {
                return(false);
            }

            // Create PSScriptFileInfo instance with script metadata class instances (PSScriptMetadata, PSScriptHelp, PSScriptRequires, PSScriptContents)
            try
            {
                parsedScript = new PSScriptFileInfo(
                    scriptMetadataComment: currentMetadata,
                    scriptHelpComment: currentHelpInfo,
                    scriptRequiresComment: currentRequiresComment,
                    scriptRemainingContent: currentEndOfFileContents);
            }
            catch (Exception e)
            {
                var message = String.Format("PSScriptFileInfo object could not be created from passed in file due to {0}", e.Message);
                var ex      = new ArgumentException(message);
                var PSScriptFileInfoObjectNotCreatedFromFileError = new ErrorRecord(ex, "PSScriptFileInfoObjectNotCreatedFromFileError", ErrorCategory.ParserError, null);
                errors = new ErrorRecord[] { PSScriptFileInfoObjectNotCreatedFromFileError };
                return(false);
            }

            errors = errorsList.ToArray();
            return(true);
        }