Skip to content

Script file info cmdlets #602

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 91 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
91 commits
Select commit Hold shift + click to select a range
0d62ecb
parse psScriptInfo comment out to hashtable
anamnavi Jan 28, 2022
bd47e2b
merge with changes from master for Utils.cs
anamnavi Jan 31, 2022
7bd8c10
remove extra lines
anamnavi Jan 31, 2022
5db7d3b
set successfullyParsed variable
anamnavi Jan 31, 2022
77e29c6
change TestRequiredResourceFile.json path referenced to not be relative
anamnavi Jan 31, 2022
b74077b
skeleton for *-FileScriptInfo cmdlets
anamnavi Feb 4, 2022
92b0eaa
skeleton for PSSCriptFileInfo.cs
anamnavi Feb 4, 2022
b684762
PSScriptInfo comment of New-PSScriptFileInfo cmdlet works
anamnavi Feb 6, 2022
6851f09
RequiresString and CommentHelp string now print
anamnavi Feb 7, 2022
653d47c
move moduleSpecification parsing code from Utils to PSScriptFileInfo …
anamnavi Feb 7, 2022
da9eb75
commentHelpInfo string works properly now
anamnavi Feb 7, 2022
b90caf8
fix typo
anamnavi Feb 7, 2022
ddcab1a
ensure Path and PassThru work
anamnavi Feb 7, 2022
cacf746
clean up code
anamnavi Feb 8, 2022
7f023cb
test-scriptfileinfo now works; but PSScriptFileInfo constructor takes…
anamnavi Feb 9, 2022
262cc81
TestScriptFileInfo works and Utils creates ModuleSpecification objects
anamnavi Feb 9, 2022
3f4ed6f
Test-ScriptFileInfo now works
anamnavi Feb 9, 2022
04ace4b
add code for Update-ScriptFileInfo; still need to test
anamnavi Feb 10, 2022
015d4cd
add help docs; Update works except for RequiredModules population
anamnavi Feb 11, 2022
22156a2
add comemnts
anamnavi Feb 11, 2022
b441874
fix bug where update doesn't retain requiredModules
anamnavi Feb 15, 2022
cdd52d6
New-PSScriptFileInfo cmdlet: rename Path to FilePath and make FilePat…
anamnavi Feb 15, 2022
f4a205d
update help file for New-PSScriptFileInfo cmdlet
anamnavi Feb 15, 2022
b07fab2
Test-PSScriptFileInfo cmdlet: rename Path to FilePath
anamnavi Feb 15, 2022
ce1ae07
Update-PSScriptFileInfo cmdlet: change Path to FilePath
anamnavi Feb 15, 2022
4cb3e0f
gets end of file ast content; some duplication issues though still
anamnavi Feb 15, 2022
afaeac0
refactor update to pass in regalar argument for original script and o…
anamnavi Feb 16, 2022
70aba50
refactor udpate to use ref variable to pass PSScriptFileInfo
anamnavi Feb 16, 2022
792cc12
ensure update and test work and former validates as a separate step
anamnavi Feb 16, 2022
dbe1878
fix for why Test would fail on an updated script
anamnavi Feb 16, 2022
7adeccd
end of file contents are now sucessfully grabbed
anamnavi Feb 17, 2022
3c9867a
Merge branch 'master' of https://github.com/anamnavi/PowerShellGet in…
anamnavi Feb 17, 2022
92dfb7b
revert changes to InstallHelper; add that to separate PR
anamnavi Feb 17, 2022
73e0fe7
change how we write to file to use FileStream
anamnavi Feb 17, 2022
932dfae
clean up code in Test-PSScriptFileInfo
anamnavi Feb 17, 2022
8400d6f
use TryCreateUri instead of TryCreateUrl
anamnavi Feb 17, 2022
0d9c74a
rename PSScriptFileInfo static methods
anamnavi Feb 17, 2022
0fddba4
for Update-PSScriptFileInfo's errors make it clear which errors come …
anamnavi Feb 17, 2022
c5213dd
clean up New-PSScriptFileInfo
anamnavi Feb 17, 2022
9b131af
remove unneccessary changes from Utils
anamnavi Feb 17, 2022
200f7b0
Revert change to install test
anamnavi Feb 17, 2022
4aff970
add validation on Description property to not contain <# or #>
anamnavi Feb 17, 2022
3d8eb10
fix typo
anamnavi Feb 17, 2022
3a0eac6
refactor CreateModuleSpecification() to have out variable of type Mod…
anamnavi Feb 17, 2022
ac696ac
some changes to script file info cmdlets
anamnavi Mar 16, 2022
d49fba4
comment out buggy code
anamnavi Mar 30, 2022
20f3c3c
fix bug in .ps1 file creation that didn't recognize description as va…
anamnavi Mar 31, 2022
74528fa
add tests for New-PSScriptFileInfo
anamnavi Mar 31, 2022
c799147
Merge branch 'master' of https://github.com/anamnavi/PowerShellGet in…
anamnavi Apr 28, 2022
4f9810f
fix bug with RequiredMOdules ModuleSpecification(Hashtable) creation …
anamnavi Apr 28, 2022
adf3550
pull changes from master, resolve merge conflicts
anamnavi Jun 6, 2022
deeaddb
add tests for Test-PSScriptFileInfo
anamnavi Jun 6, 2022
2da70ca
clean up comments in test
anamnavi Jun 6, 2022
f58e6ce
clean up code and tests
anamnavi Jun 6, 2022
37aeb37
fix bug in test
anamnavi Jun 7, 2022
8e7b6c2
push changes temporarily
anamnavi Jun 8, 2022
b20f73d
remove succcesfullyParsed var
anamnavi Jun 9, 2022
a175c69
refactor TryParsePSScript() to use helpers
anamnavi Jun 13, 2022
4cd6fdd
fix bug with RequiredModules
anamnavi Jun 13, 2022
d7a9d5b
cmdlets should use new refactored TryParseScriptIntoPSScriptInfo method
anamnavi Jun 13, 2022
529b532
make certain methods private
anamnavi Jun 14, 2022
f1cad37
make method internal for update
anamnavi Jun 14, 2022
98ecc51
code review changes
anamnavi Jun 14, 2022
a61ba87
add RemoveSignature param to Update
anamnavi Jun 22, 2022
8fc0b9f
fix Description block newline padding bug
anamnavi Jun 22, 2022
d7d763b
add tests for RemoveSignature parameter
anamnavi Jun 22, 2022
f465015
add try catch around ModuleSpecification constructor call
anamnavi Jun 22, 2022
a50251c
clean up code comments
anamnavi Jun 22, 2022
3a6e0a8
use private setters
anamnavi Jun 22, 2022
0632927
incorporating CR feedback
anamnavi Jun 22, 2022
d1ee41e
Merge branch 'master' of https://github.com/anamnavi/PowerShellGet in…
anamnavi Jun 22, 2022
b758818
fix typo
anamnavi Jun 22, 2022
edd0699
make hastable keys insensitive case
anamnavi Jun 22, 2022
fed5232
remove help docs for now and include in a separate PR
anamnavi Jun 22, 2022
ea3b638
pull in changes from master including tests
anamnavi Jul 5, 2022
6d2d3c3
New-PSScriptFileInfo- add code to resolve non-existant (yet) relative…
anamnavi Jul 5, 2022
2bdc8d8
remove Force parameter from Update
anamnavi Jul 6, 2022
2aaa5c1
remoe unnecessary directives, remove todo comments from tests
anamnavi Jul 6, 2022
dae5675
fix bug with ReleaseNotes, PrivateData being on next line than .KEY
anamnavi Jul 7, 2022
59eab74
clean up code comments
anamnavi Jul 7, 2022
2490dd4
refactor commentLine parsing into helper method, and add . to /// lines
anamnavi Jul 8, 2022
bfcaa42
code should be in EndProcessing() not ProcessRecord()
anamnavi Jul 8, 2022
c21aecf
address PR feedback
anamnavi Jul 8, 2022
7600d14
address more PR feedback
anamnavi Jul 8, 2022
6bdbb86
fix bug with Description property being overriden in metadata hashtable
anamnavi Jul 8, 2022
e9765e0
fix todo
anamnavi Jul 8, 2022
bf9b21b
Pester TestDrive will clean up folders created there, don't do oursel…
anamnavi Jul 11, 2022
5d1f1a7
add test to Update to ensure old data is not overwritten by update
anamnavi Jul 11, 2022
025438f
add file line reading logic (WIP) for initial review
anamnavi Jul 12, 2022
7d7eea6
line by line parsing works as expected, and added error handling for …
anamnavi Jul 13, 2022
c12b11a
notes from design meeting
anamnavi Jul 13, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/PowerShellGet.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@
'Register-PSResourceRepository',
'Save-PSResource',
'Set-PSResourceRepository',
'New-PSScriptFileInfo',
'Publish-PSResource',
'Test-PSScriptFileInfo',
'Uninstall-PSResource',
'Unregister-PSResourceRepository',
'Update-PSResource')
'Update-PSResource',
'Update-PSScriptFileInfo')

VariablesToExport = 'PSGetPath'
AliasesToExport = @('inmo', 'fimo', 'upmo', 'pumo')
Expand Down
259 changes: 259 additions & 0 deletions src/code/NewPSScriptFileInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,259 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Collections;
using System.IO;
using System.Management.Automation;
using Microsoft.PowerShell.Commands;
using Microsoft.PowerShell.PowerShellGet.UtilClasses;

namespace Microsoft.PowerShell.PowerShellGet.Cmdlets
{
/// <summary>
/// Creates a new .ps1 file with script information required for publishing a script.
/// </summary>
[Cmdlet(VerbsCommon.New, "PSScriptFileInfo")]
public sealed class NewPSScriptFileInfo : PSCmdlet
{
#region Parameters

/// <summary>
/// The path the .ps1 script info file will be created at.
/// </summary>
[Parameter(Position = 0, Mandatory = true)]
[ValidateNotNullOrEmpty]
public string FilePath { get; set; }

/// <summary>
/// The version of the script.
/// </summary>
[Parameter]
[ValidateNotNullOrEmpty()]
public string Version { get; set; }

/// <summary>
/// The author of the script.
/// </summary>
[Parameter]
[ValidateNotNullOrEmpty()]
public string Author { get; set; }

/// <summary>
/// The description of the script.
/// </summary>
[Parameter(Mandatory = true)]
[ValidateNotNullOrEmpty()]
public string Description { get; set; }

/// <summary>
/// A unique identifier for the script. The GUID can be used to distinguish among scripts with the same name.
/// </summary>
[Parameter]
[ValidateNotNullOrEmpty()]
public Guid Guid { get; set; }

/// <summary>
/// The name of the company owning the script.
/// </summary>
[Parameter]
[ValidateNotNullOrEmpty()]
public string CompanyName { get; set; }

/// <summary>
/// The copyright statement for the script.
/// </summary>
[Parameter]
[ValidateNotNullOrEmpty()]
public string Copyright { get; set; }

/// <summary>
/// The list of modules required by the script.
/// </summary>
[Parameter]
[ValidateNotNullOrEmpty()]
public Hashtable[] RequiredModules { get; set; }

/// <summary>
/// The list of external module dependencies taken by this script.
/// </summary>
[Parameter]
[ValidateNotNullOrEmpty()]
public string[] ExternalModuleDependencies { get; set; }

/// <summary>
/// The list of scripts required by the script.
/// </summary>
[Parameter]
[ValidateNotNullOrEmpty()]
public string[] RequiredScripts { get; set; }

/// <summary>
/// The list of external script dependencies taken by this script.
/// </summary>
[Parameter]
[ValidateNotNullOrEmpty()]
public string[] ExternalScriptDependencies { get; set; }

/// <summary>
/// The tags associated with the script.
/// </summary>
[Parameter]
[ValidateNotNullOrEmpty()]
public string[] Tags { get; set; }

/// <summary>
/// The Uri for the project associated with the script.
/// </summary>
[Parameter]
[ValidateNotNullOrEmpty()]
public string ProjectUri { get; set; }

/// <summary>
/// The Uri for the license associated with the script.
/// </summary>
[Parameter]
[ValidateNotNullOrEmpty()]
public string LicenseUri { get; set; }

/// <summary>
/// The Uri for the icon associated with the script.
/// </summary>
[Parameter]
[ValidateNotNullOrEmpty()]
public string IconUri { get; set; }

/// <summary>
/// The release notes for the script.
/// </summary>
[Parameter]
[ValidateNotNullOrEmpty()]
public string ReleaseNotes { get; set; }

/// <summary>
/// The private data associated with the script.
/// </summary>
[Parameter]
[ValidateNotNullOrEmpty()]
public string PrivateData { get; set; }

/// <summary>
/// If used with Path parameter and .ps1 file specified at the path exists, it rewrites the file.
/// </summary>
[Parameter]
public SwitchParameter Force { get; set; }

#endregion

#region Methods

protected override void EndProcessing()
{
// validate Uri related parameters passed in as strings
Uri projectUri = null;
if (!String.IsNullOrEmpty(ProjectUri) && !Utils.TryCreateValidUri(uriString: ProjectUri,
cmdletPassedIn: this,
uriResult: out projectUri,
errorRecord: out ErrorRecord projectErrorRecord))
{
ThrowTerminatingError(projectErrorRecord);
}

Uri licenseUri = null;
if (!String.IsNullOrEmpty(LicenseUri) && !Utils.TryCreateValidUri(uriString: LicenseUri,
cmdletPassedIn: this,
uriResult: out licenseUri,
errorRecord: out ErrorRecord licenseErrorRecord))
{
ThrowTerminatingError(licenseErrorRecord);
}

Uri iconUri = null;
if (!String.IsNullOrEmpty(IconUri) && !Utils.TryCreateValidUri(uriString: IconUri,
cmdletPassedIn: this,
uriResult: out iconUri,
errorRecord: out ErrorRecord iconErrorRecord))
{
ThrowTerminatingError(iconErrorRecord);
}

if (!FilePath.EndsWith(".ps1", StringComparison.OrdinalIgnoreCase))
{
var exMessage = "Path needs to end with a .ps1 file. Example: C:/Users/john/x/MyScript.ps1";
var ex = new ArgumentException(exMessage);
var InvalidPathError = new ErrorRecord(ex, "InvalidPath", ErrorCategory.InvalidArgument, null);
ThrowTerminatingError(InvalidPathError);
}

var resolvedFilePath = SessionState.Path.GetUnresolvedProviderPathFromPSPath(FilePath);
if (String.IsNullOrEmpty(resolvedFilePath))
{
var exMessage = "Error: Could not resolve provided Path argument into a single path.";
var ex = new PSArgumentException(exMessage);
var InvalidPathArgumentError = new ErrorRecord(ex, "InvalidPathArgumentError", ErrorCategory.InvalidArgument, null);
ThrowTerminatingError(InvalidPathArgumentError);
}

if (File.Exists(resolvedFilePath) && !Force)
{
// .ps1 file at specified location already exists and Force parameter isn't used to rewrite the file
var exMessage = ".ps1 file at specified path already exists. Specify a different location or use -Force parameter to overwrite the .ps1 file.";
var ex = new ArgumentException(exMessage);
var ScriptAtPathAlreadyExistsError = new ErrorRecord(ex, "ScriptAtPathAlreadyExists", ErrorCategory.InvalidArgument, null);
ThrowTerminatingError(ScriptAtPathAlreadyExistsError);
}

ModuleSpecification[] validatedRequiredModuleSpecifications = new ModuleSpecification[]{};
if (RequiredModules != null && RequiredModules.Length > 0)
{
if (!Utils.TryCreateModuleSpecification(
moduleSpecHashtables: RequiredModules,
out validatedRequiredModuleSpecifications,
out ErrorRecord[] moduleSpecErrors))
{
foreach (ErrorRecord err in moduleSpecErrors)
{
WriteError(err);
}

return;
}
}

PSScriptFileInfo scriptInfo = new PSScriptFileInfo(
version: Version,
guid: Guid,
author: Author,
companyName: CompanyName,
copyright: Copyright,
tags: Tags,
licenseUri: licenseUri,
projectUri: projectUri,
iconUri: iconUri,
requiredModules: validatedRequiredModuleSpecifications,
externalModuleDependencies: ExternalModuleDependencies,
requiredScripts: RequiredScripts,
externalScriptDependencies: ExternalScriptDependencies,
releaseNotes: ReleaseNotes,
privateData: PrivateData,
description: Description,
endOfFileContents: String.Empty);

if (!scriptInfo.TryCreateScriptFileInfoString(
pSScriptFileString: out string psScriptFileContents,
errors: out ErrorRecord[] errors))
{
foreach (ErrorRecord err in errors)
{
WriteError(err);
}

return;
}

File.WriteAllText(resolvedFilePath, psScriptFileContents);
}

#endregion
}
}
Loading