All Projects → DTW-DanWard → Powershell Beautifier

DTW-DanWard / Powershell Beautifier

Licence: mit
A whitespace reformatter and code cleaner for Windows PowerShell and PowerShell Core

Programming Languages

powershell
5483 projects

Projects that are alternatives of or similar to Powershell Beautifier

Nginx Config Formatter
nginx config file formatter/beautifier written in Python.
Stars: ✭ 222 (+4.23%)
Mutual labels:  beautifier, formatter
stylus-supremacy
A Node.js script that helps formatting Stylus files.
Stars: ✭ 86 (-59.62%)
Mutual labels:  formatter, beautifier
Pp sql
Rails ActiveRecord SQL queries log beautifier
Stars: ✭ 223 (+4.69%)
Mutual labels:  beautifier, formatter
vscode-liquid
💧Liquid language support for VS Code
Stars: ✭ 137 (-35.68%)
Mutual labels:  formatter, beautifier
Typeset
自动修正中文、英文、代码混合排版中的全半角、空格等问题
Stars: ✭ 63 (-70.42%)
Mutual labels:  beautifier, formatter
Atom Beautify
📣 Help Wanted - Looking for Maintainer: https://github.com/Glavin001/atom-beautify/issues/2572 | 💄 Universal beautification package for Atom editor (⚠️ Currently migrating to https://github.com/Unibeautify/ and have very limited bandwidth for Atom-Beautify Issues. Thank you for your patience and understanding ❤️ )
Stars: ✭ 1,501 (+604.69%)
Mutual labels:  beautifier, formatter
Mbeautifier
MBeautifier is a MATLAB source code formatter, beautifier. It can be used directly in the MATLAB Editor and it is configurable.
Stars: ✭ 248 (+16.43%)
Mutual labels:  beautifier, formatter
Latexindent.pl
Perl script to add indentation (leading horizontal space) to LaTeX files. It can modify line breaks before, during and after code blocks; it can perform text wrapping and paragraph line break removal. It can also perform string-based and regex-based substitutions/replacements. The script is customisable through its YAML interface.
Stars: ✭ 415 (+94.84%)
Mutual labels:  beautifier, formatter
Unibeautify
One Beautifier to rule them all, One Beautifier to clean them, One Beautifier to bring them all and in the darkness sheen them
Stars: ✭ 466 (+118.78%)
Mutual labels:  beautifier, formatter
Plugin Php
Prettier PHP Plugin
Stars: ✭ 1,243 (+483.57%)
Mutual labels:  beautifier, formatter
Uncrustify
Code beautifier
Stars: ✭ 2,442 (+1046.48%)
Mutual labels:  beautifier, formatter
Jstoolnpp
A JavaScript (JSON) tool for Notepad++ (formerly JSMinNpp) and Visual Studio Code.
Stars: ✭ 186 (-12.68%)
Mutual labels:  formatter
Yapf
A formatter for Python files
Stars: ✭ 12,203 (+5629.11%)
Mutual labels:  formatter
Vim Autoformat
Provide easy code formatting in Vim by integrating existing code formatters.
Stars: ✭ 2,000 (+838.97%)
Mutual labels:  formatter
Spotless
Keep your code spotless
Stars: ✭ 2,285 (+972.77%)
Mutual labels:  formatter
Graphql Parser
A graphql query language and schema definition language parser and formatter for rust
Stars: ✭ 203 (-4.69%)
Mutual labels:  formatter
Stylefmt
stylefmt is a tool that automatically formats stylesheets.
Stars: ✭ 2,123 (+896.71%)
Mutual labels:  formatter
Pettier
Prettier config that randomizes options and arbitrarily switches between spaces and tabs 🙄
Stars: ✭ 149 (-30.05%)
Mutual labels:  formatter
Google Java Format Gradle Plugin
Stars: ✭ 147 (-30.99%)
Mutual labels:  formatter
Easymoney
Library for operating with monetary values in JavaScript and Typescript 💵
Stars: ✭ 145 (-31.92%)
Mutual labels:  formatter

PowerShell Beautifier

A whitespace reformatter and code cleaner for Windows PowerShell and PowerShell Core (all OSes).

Formatting Matters

Tabs or spaces; spaces or tabs? If spaces, how many? We sure do take whitespace seriously. But when writing commit-worthy PowerShell code, there's more than just whitespace to think about. Shouldn't you use cmdlet names instead of aliases? And shouldn't you have correct casing for cmdlets, methods and types? Especially if you want to support PowerShell Core...?

PowerShell Beautifier is a PowerShell command-line utility for cleaning and reformatting PowerShell script files, written in PowerShell. Sure, it will change all indentation to tabs or spaces for you - but it will do more than just that. A picture is worth 1KB words; here's a before/after in VS Code: Before and after - casing

Here are the changes in a diff viewer: Before and after - casing

Here's another diff viewer showing only alias-replacement and casing changes: Before and after - whitespace

The PowerShell Beautifier makes these changes:

  • properly indents code inside {}, [], () and $() groups
  • cleans/rearranges all whitespace within a line
  • replaces aliases with the command names: dir → Get-ChildItem
  • fixes command name casing: get-childitem → Get-ChildItem
  • fixes parameter name casing: -path → -Path
  • fixes [type] casing
    • changes built-in shortcut / type accelerator: [STRING] → [string]
    • [DSCLocalConfigurationManager] → [DscLocalConfigurationManager]
    • changes other types if in memory: [system.exception] → [System.Exception]
  • ensures file ends with a new line.

Release Testing

For each release the PowerShell Beautifier is tested on:

It is also occasionally tested natively on OSX, Ubuntu 16.04, Windows Server 2016 Core and Nano.

Table of Contents

Setup

  1. Download the PowerShell Beautifier utility. Clone it, zip it or get it from the PowerShell Gallery:
Install-Module -Name PowerShell-Beautifier
  1. Import the module. This takes a few seconds the first time but is fast thereafter.
Import-Module .\PowerShell-Beautifier.psd1
  1. Confirm it is loaded correctly:
Get-Help Edit-DTWBeautifyScript

NAME
    Edit-DTWBeautifyScript

SYNOPSIS
    Cleans PowerShell script: re-indents code with spaces or tabs, cleans
    and rearranges all whitespace within a line, replaces aliases with
    cmdlet names, replaces parameter names with proper casing, fixes case for
    [types], etc.


SYNTAX
    Edit-DTWBeautifyScript [-SourcePath] <String> [[-DestinationPath] <String>] [[-IndentType] <String>]
    [-StandardOutput] [[-NewLine] <String>] [<CommonParameters>]
    [<CommonParameters>]

...more text...

Usage

Before using this utility on any file, back up your file!

Commit your file, run a backup, run the beautify utility on a copy first, whatever you have to do! If you don't use the DestinationPath parameter, it will rewrite your file in place! I've run this utility on many script now but I don't know if something funky in your script might throw off the utility. Be safe and back it up first!

(FYI, the beautifier only rewrites your script at the end of processing if no errors occur. However better safe than sorry - back it up!)

So, assuming you've imported the module, how do you use it?

Run on single file updating in place with 2 spaces indentation

This rewrites the source file in place. Two spaces is the default indent step so IndentType is not specified.

Edit-DTWBeautifyScript C:\temp\MyFile.ps1

Run on single file but indent with tabs

This rewrites the source file in place using a tab at the indent step.

Edit-DTWBeautifyScript C:\temp\MyFile.ps1 -IndentType Tabs

Run on single file outputting to new file with 2 spaces indentation

This doesn't modify the source file; it outputs the clean version to a new file. Also uses the default indent step (2 spaces).

Edit-DTWBeautifyScript -SourcePath C:\temp\MyFile.ps1 -DestinationPath c:\temp\MyFile_AFTER.ps1

Run on multiple files in a directory structure

Time for the pipeline.

Get-ChildItem -Path c:\temp -Include *.ps1,*.psm1 -Recurse | Edit-DTWBeautifyScript

Note: if you don't include the file extension filtering you'll need some other way to ignore folders (i.e. ignore PSIsContainer -eq $true) as Edit-DTWBeautifyScript will error for those.

Get cleaned content via standard output rather than updating file

If you want to receive the beautified content via stdout (most likely if you are calling from an external editor), use the -StandardOutput (or -StdOut) parameter:

Edit-DTWBeautifyScript C:\temp\MyFile.ps1 -StandardOutput

When using -StandardOutput, the SourcePath is used for content but not updated, DestinationPath is ignored (if passed). If an error occurs (syntax error in user script), no content is returned via stdout but stderr will have a concise error that can be displayed to the user.

Use LF as newline no matter what the OS

By default, when writing the file the beautifier will use the host OS's style for newlines. To override this, use -NewLine and pass either CRLF or LF.

For example, to force newline endings to be LF

Edit-DTWBeautifyScript C:\temp\MyFile.ps1 -NewLine LF

Want to Know More

Contributing

There are several ways to contribute:

Credits

Dan Ward created the PowerShell Beautifier as a pet project back in 2011 but only recently added it to Github.

License

The PowerShell Beautifier is licensed under the MIT license.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].