| |
| |
|
|
| using System.IO; |
| using System.Management.Automation; |
| using System.Management.Automation.Internal; |
| using System.Text; |
|
|
| using Microsoft.PowerShell.Commands.Internal.Format; |
|
|
| namespace Microsoft.PowerShell.Commands |
| { |
| internal static class InputFileOpenModeConversion |
| { |
| internal static FileMode Convert(OpenMode openMode) |
| { |
| return SessionStateUtilities.GetFileModeFromOpenMode(openMode); |
| } |
| } |
|
|
| |
| |
| |
| [Cmdlet(VerbsData.Out, "File", SupportsShouldProcess = true, DefaultParameterSetName = "ByPath", HelpUri = "https://go.microsoft.com/fwlink/?LinkID=2096621")] |
| public class OutFileCommand : FrontEndCommandBase |
| { |
| |
| |
| |
| |
| public OutFileCommand() |
| { |
| this.implementation = new OutputManagerInner(); |
| } |
|
|
| #region Command Line Parameters |
|
|
| |
| |
| |
| [Alias("Path")] |
| [Parameter(Mandatory = true, Position = 0, ParameterSetName = "ByPath")] |
| public string FilePath |
| { |
| get { return _fileName; } |
|
|
| set { _fileName = value; } |
| } |
|
|
| private string _fileName; |
|
|
| |
| |
| |
| [Parameter(Mandatory = true, ValueFromPipelineByPropertyName = true, ParameterSetName = "ByLiteralPath")] |
| [Alias("PSPath", "LP")] |
| public string LiteralPath |
| { |
| get |
| { |
| return _fileName; |
| } |
|
|
| set |
| { |
| _fileName = value; |
| _isLiteralPath = true; |
| } |
| } |
|
|
| private bool _isLiteralPath = false; |
|
|
| |
| |
| |
| [Parameter(Position = 1)] |
| [ArgumentToEncodingTransformation] |
| [ArgumentEncodingCompletions] |
| [ValidateNotNullOrEmpty] |
| public Encoding Encoding |
| { |
| get |
| { |
| return _encoding; |
| } |
|
|
| set |
| { |
| EncodingConversion.WarnIfObsolete(this, value); |
| _encoding = value; |
| } |
| } |
|
|
| private Encoding _encoding = Encoding.Default; |
|
|
| |
| |
| |
| [Parameter] |
| public SwitchParameter Append |
| { |
| get { return _append; } |
|
|
| set { _append = value; } |
| } |
|
|
| private bool _append; |
|
|
| |
| |
| |
| [Parameter] |
| public SwitchParameter Force |
| { |
| get { return _force; } |
|
|
| set { _force = value; } |
| } |
|
|
| private bool _force; |
|
|
| |
| |
| |
| [Parameter] |
| [Alias("NoOverwrite")] |
| public SwitchParameter NoClobber |
| { |
| get { return _noclobber; } |
|
|
| set { _noclobber = value; } |
| } |
|
|
| private bool _noclobber; |
|
|
| |
| |
| |
| [ValidateRange(2, int.MaxValue)] |
| [Parameter] |
| public int Width |
| { |
| get { return (_width != null) ? _width.Value : 0; } |
|
|
| set { _width = value; } |
| } |
|
|
| private int? _width = null; |
|
|
| |
| |
| |
| [Parameter] |
| public SwitchParameter NoNewline |
| { |
| get |
| { |
| return _suppressNewline; |
| } |
|
|
| set |
| { |
| _suppressNewline = value; |
| } |
| } |
|
|
| private bool _suppressNewline = false; |
|
|
| #endregion |
|
|
| |
| |
| |
| protected override void BeginProcessing() |
| { |
| |
| OutputManagerInner outInner = (OutputManagerInner)this.implementation; |
|
|
| |
| |
| outInner.LineOutput = InstantiateLineOutputInterface(); |
|
|
| if (_sw == null) |
| { |
| return; |
| } |
|
|
| |
| base.BeginProcessing(); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| private LineOutput InstantiateLineOutputInterface() |
| { |
| string action = StringUtil.Format(FormatAndOut_out_xxx.OutFile_Action); |
| if (ShouldProcess(FilePath, action)) |
| { |
| PathUtils.MasterStreamOpen( |
| this, |
| FilePath, |
| Encoding, |
| false, |
| Append, |
| Force, |
| NoClobber, |
| out _fs, |
| out _sw, |
| out _readOnlyFileInfo, |
| _isLiteralPath |
| ); |
| } |
| else |
| return null; |
|
|
| |
| int computedWidth = int.MaxValue; |
|
|
| if (_width != null) |
| { |
| |
| computedWidth = _width.Value; |
| } |
|
|
| |
| TextWriterLineOutput twlo = new(_sw, computedWidth, _suppressNewline); |
|
|
| |
| return (LineOutput)twlo; |
| } |
|
|
| |
| |
| |
| protected override void ProcessRecord() |
| { |
| _processRecordExecuted = true; |
| if (_sw == null) |
| { |
| return; |
| } |
|
|
| |
| |
| base.ProcessRecord(); |
| _sw.Flush(); |
| } |
|
|
| |
| |
| |
| protected override void EndProcessing() |
| { |
| |
| |
| |
| |
| |
| |
| if (!_processRecordExecuted) |
| { |
| return; |
| } |
|
|
| if (_sw == null) |
| { |
| return; |
| } |
|
|
| |
| |
| base.EndProcessing(); |
|
|
| _sw.Flush(); |
|
|
| CleanUp(); |
| } |
|
|
| |
| |
| |
| protected override void InternalDispose() |
| { |
| base.InternalDispose(); |
| CleanUp(); |
| } |
|
|
| private void CleanUp() |
| { |
| if (_fs != null) |
| { |
| _fs.Dispose(); |
| _fs = null; |
| } |
|
|
| |
| if (_readOnlyFileInfo != null) |
| { |
| _readOnlyFileInfo.Attributes |= FileAttributes.ReadOnly; |
| _readOnlyFileInfo = null; |
| } |
| } |
|
|
| |
| |
| |
| private FileStream _fs; |
|
|
| |
| |
| |
| private StreamWriter _sw = null; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| private bool _processRecordExecuted = false; |
|
|
| |
| |
| |
| private FileInfo _readOnlyFileInfo = null; |
| } |
| } |
|
|