Just an idea. :)
I don't know if people are requesting it but I would personally find it useful.
+1 for that, it would be great to have PowerShell support.
A +1 from me too, since I just tried it, not realizing it wasn't supported. ;-)
I agree - er '+1` it'
+1!
+1
+1
+1
:+1: I'd love this
+1
Before any further action can be taken please indicate PowerShell code beautifier. Without a beautifer there is nothing to be done.
@prettydiff is this not what is needed?
https://atom.io/packages/language-powershell
@johnypony3 Probably not. That application looks like it provides language support to Atom Beautify, which means things like function/block folding and coloring code according to syntax rules. We will need something that can actually beautify the code, such as impose indentation and line breaks.
+1
+1
+1
+1
You know guys... instead of just posting "+1", there's an actual upvote on the top comment. Just click it. That way, we aren't receiving mass emails/notifications from everyone doing a +1.
Oh, and please... don't +1 this comment. That's just silly.
Thanks @MaximRouiller. Hopefully clicking the 👍 was the right thing.
I'm still wrapping my brain around the whole Github thing.
PLEASE DO NOT POST A +1 COMMENT
Please 👍 the Issue body at the top (see screenshot if you are confused). Thanks.

For those who want this feature to happen, the only way for it to progress is for a suitable third-party beautifier to be found. It looks like https://danspowershellstuff.blogspot.ca/2012/03/powershell-pretty-printer-code-cleaner.html may work however we need someone to test it. I recommend not assuming someone else will do it; if you are at all interested in contributing then please don't wait! It's better to have everyone providing feedback and ideas than no one 😜 .
What we really need is either:
1) A package written for Node.js published to npmjs.org
2) A command-line interface (CLI) program which can be used like powershell-format FILENAME --OPTION1 --OPTION2 VALUE2 for example
I would love to be able to test, but I'm not up to speed with node or making anything for atom. I can test the link I had previously posted outside of atom and report back on my experience with it if that helps.
Hey folks - Dan here - I created the PowerShell pretty printer / beautifier mentioned above. I'm planning on putting it up into GitHub in the next few weeks. It will take some time - lots of test cases to write, it's my first public project, etc. - but once it's in place I'd love to work with you to integrate it into atom-beautifier.
Note: unless you want a watered-down version, the cleanup work will have to be done from within PowerShell as the code will need access to the types, aliases, function names, etc. in memory. (It doesn't just change section indentation, it also changes stuff like "dir" to "Get-ChildItem").
I'll check back in once version 1.0 is up. I'll need some help as I've done very little with atom; also, I'll need some testers to help me fill out the various test cases.
Hey folks -
The PowerShell Beautifier is finally on GitHub: DTW-DanWard/PowerShell-Beautifier
Please note: I haven't made any specific changes to support atom-beautifier yet but I wrote up some notes about what needs to change here. But you can help out now by testing the Beautifier from within PowerShell itself and letting me know if you encounter any issues.
Please back up or commit your files first! Or run them on a temp copy of your files! I don't want you to lose any work or special formatting!
Also note: I have some tickets to add related to performance improvements and others tweaks. Those will be going in the next few days.
Let me know how it works out for you!
Great work, @DTW-DanWard!
Unfortunately, I do not develop on Windows (turns out PowerShell supports Mac & Linux!) I am not familiar with PowerShell so my support is limited. I will try my best to answer any questions and assist you or others to contribute to Atom-Beautify with support for this new beautifier. Thanks!
Any way to get @DTW-DanWard's code in Atom? The goal is to include it as an extension.
I believe @DTW-DanWard is trying to get some more testing done on his end before attempting to bring it into Atom as a beautifier.
@nemchik Makes sense.
I think that once we have code/pseudo code that represent the behavior of the beautifier, it would be relatively easier to port it to other languages (JavaScript).
I wonder if there is even something available on VS Code that could help @DTW-DanWard.
it would be relatively easier to port it to other languages (JavaScript).
If someone could port it over to JavaScript/Node.js then it would be even easier to add to Atom-Beautify, and more performant (no child process being created).
I wonder if there is even something available on VS Code
I am working on Unibeautify which will replace the core of Atom-Beautify. Unibeautify will support VSCode and more: https://github.com/Unibeautify/unibeautify/blob/master/ROADMAP.md
If we add this beautifier to Atom-Beautify, then I will personally migrate it to Unibeautify when I am ready.
I'm not sure how easy it will be to port it to JavaScript/Node.js. If we're talking about just whitespace cleaning (the PowerShell Beautifier does a lot more than that) you might get 80% coverage without too much effort but there are some funky things in PowerShell. For example, most of time variable names look pretty basic ($Message = 'Hello, world!') but PowerShell will allow you to use any character in a variable name - including newlines - if you use the curly brace notation. For example:
${This is
a horrible
variable name} = 'Hello, world!'
While that's freaky, it's also perfectly legitimate.
For that reason the PowerShell Beautifier does not attempt to parse the source code to be cleaned itself; it uses a built-in tokenizer API in PowerShell to break up the source and then rewrites the source back to file from the tokens, using rules about whether or not to add a space, indent within groups/sections, etc. It also replaces aliases with cmdlets (dir -> Get-ChildItem) at that time, along with fixing casing, etc.
Long story short: using the PS Beautifier in it's current form will require a child process.
@DTW-DanWard: Sounds like it will require Windows too?
@asbjornu supposedly there is a PowerShell repository for Linux. I have no idea if it'll work in this context.
The PowerShell repository says it supports cross-platform: https://github.com/PowerShell/PowerShell#-powershell
Welcome to the PowerShell GitHub Community! PowerShell is a cross-platform (Windows, Linux, and macOS) automation and configuration tool/framework that works well with your existing tools and is optimized for dealing with structured data (e.g. JSON, CSV, XML, etc.), REST APIs, and object models. It includes a command-line shell, an associated scripting language and a framework for processing cmdlets.
And I found a Docker image which uses Ubuntu: https://hub.docker.com/r/microsoft/powershell/~/dockerfile/
So users can either install PowerShell themselves or use the PowerShell Beautifier within Docker.
Thanks for the feedback, @DTW-DanWard. Good points.
Is the following how Atom-Beautify would call PowerShell Beautifier?
powershell.exe -NoProfile -Command "Import-Module <path to>\DTW.PS.Beautifier.psd1; Edit-DTWBeautifyScript -SourcePath <path to current file> <-WhichOptions>"
It looks like the file is fixed in place. Is there a way to output new changes to stdout instead of replacing the original file?
@Glavin001
It's close, the paths would need to be filled in, you should remove the parameter -WhichOptions (it's just an example) but there might be additional parameter options to add. More on that below.
There's 3 ways we can output formatted content:
1 Specify only -SourcePath and it rewrites in place.
2 Specify -DestinationPath and it will write there (you could pass in a temp path and switch the files behind the scene after).
3 I add a new parameter, something like -StandardOutput where the cleaned content is piped back to stdout.
There's one wrinkle with option 3: errors can occur in in the PowerShell beautify process and I was expecting to output information about these errors to you via stdout (while successful completions update at either SourcePath or DestinationPath). I'm not sure at the moment if I can output stderr information from within a PowerShell process to a host calling process; I will test that tonight. If that works then option 3 is viable. However, the type of error that can occur within PS Beautifier is common - syntax errors in a user's script. If a user has, say, an string that's not terminated, the Beautifier reports the specifics (line and character location, etc.).
@Glavin001 One other quick note: I'm guessing most (high 90%) of PowerShell development is still only happening on Windows machines. The PowerShell Core team is doing amazing work for cross-platform compatibility but it's not there yet. I have not attempted running the Beautifier in PS Core yet - getting it compatible with Core is on the roadmap - but I can't promise it will be ready in the next few days.
@Glavin001 I think we should take a two-pronged approach for the short-term:
I'll work on putting together that sample for you. In the meantime, anyone curious can test the Beautifier as-is.
Thanks for sharing this, Dan!
I tried it on Core (on MacOs X) yesterday; it certainly works but emits a
ton of red warnings from Get-Command not finding commands and properties
that aren't available. You could probably silence them with -ErrorAction
SilentlyContinue wherever Get-Command is used... but I'm not sure if
that's best... I don't immediately have an opinion about the best way to
handle missing commands and properties.
On Tue, Jun 13, 2017 at 5:21 AM, Dan Ward notifications@github.com wrote:
@Glavin001 https://github.com/glavin001 One other quick note: I'm
guessing most (high 90%) of PowerShell development is still only happening
on Windows machines. The PowerShell Core team is doing amazing work for
cross-platform compatibility but it's not there yet. I have not attempted
running the Beautifier in PS Core yet - getting it compatible with Core is
on the roadmap - but I can't promise it will be ready in the next few days.—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/Glavin001/atom-beautify/issues/333#issuecomment-308113646,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AArTCg6w-Mj4UDQwC-MhlYoTdiDrKF2Bks5sDozwgaJpZM4ESdJL
.
--
Nathan Vonnahme
CareEvolution http://careevolution.com/
[email protected]
It is CareEvolution's strict policy that no patient-identifying data should
*ever *be sent via unsecured email or in attachments. Need to send PHI?
Please ask me how to do so securely.
@n8v Thanks for testing on Core! Yeah, I'm sure there's a bit of work to make it Core-compatible. I will probably have to tweak some of the assumptions about Byte Order Marks and I need to make the line-endings environment-specific (right now it's Windows-standard CR LF). There could be more... with any luck I will have some free time this weekend to take a closer look. Once I have some specific tickets created we can move the conversation there.
@Glavin001 I've got two issues to fix (specifically 1 and 2 in my project) before we can integrate it with Atom-Beautify. But before then, can you test running the PowerShell below from within Atom-Beautify to make sure it works correctly? I want to make sure both stdout and stderr are getting passed back.
powershell.exe -NoProfile -NoLogo -Command "[Console]::Error.WriteLine('stderr!'); Write-Output 'stdout!';"
Standard output should have stdout! and standard error should have stderr! If this works I will make the changes to pass back the beautified script via stdout instead of updating the file at SourcePath. Also, if an error occurs (syntax error found in user script), I won't pass back anything to stdout but will return the error info (line number, character number, error message) in stderr - I'm hoping this can it presented to the user in a pop-up or some status area.
Also - thanks for your patience! I apologize if integrating PowerShell-Beautifier is more work than your usual language.
Hey @Glavin001 -
Just wrapping up some final testing but have one quick question. In the event of an error (syntax error in user script), stderr will have the error info; would you prefer that stdout be empty or have the original unmodified script in it?
FYI, updated version is Core (cross-platform) compatible; I've been testing on Windows and Ubuntu, @n8v has been testing on Mac.
Any updates on this?? Would love to be able to beautify my powershell scripts in Atom so I can stop having to jump between two different editors..
I believe I completed all required changes in PowerShell-Beautifier although it hasn't been tested yet within Atom-Beautify. At this point it requires someone from Atom-Beautify to incorporate the PowerShell-Beautifier.
@DTW-DanWard are there any settings for your beautifier? there are some things in your beautifier that I would love to be able to change.
Right now you can only specify the IndentType; I have some changes planned (see the current issues list) but feel free to add any specific requests. Ultimately there will be a json file with all of the configurable options and allow some of them to be overridden at the command-line.
Some of these changes will require a couple of major updates to the beautifier (changing to use abstract syntrax trees and a rewrite of the whitespace rules processing). Unfortunately I have little free time right now; just started a new job and the hours are a bit long. I'm hoping to spend some time on the beautifier towards the end of the year.
Ideally PowerShell-Beautifier would be a Docker image that could be run from a terminal on any non-Windows OS (just like these). That way it can be incorporated into Atom Beautify's automated testing on Travis and anyone with Docker installed has it as a fallback. However I don't know if it's possible to run as a Docker container. @DTW-DanWard Have you tried that all?
@DTW-DanWard I've started a Docker image to run this beautifier on my Mac (you can find the Dockerfile here). I'm able to run the Get-Help Edit-DTWBeautifyScript command and get the output, but when I try something like Edit-DTWBeautifyScript test.ps1 against https://github.com/DTW-DanWard/PowerShell-Beautifier/blob/master/test/1_Input_Bad/Case/Members.ps1, I get a bunch of errors. Here's the gist: https://gist.github.com/szeck87/402d76c04c26c25e8a2a5c262f34910f. Any insight?
@szeck87 I currently do most of my cross-OS testing using local Docker containers. When I do it I copy all the scripts onto the container then run it locally. I could create a container based on the latest Core container and put it on Hub and this would be ideal for automated testing. It might be kludgy for actual use; copying a file in and out of the container could easily be scripted but if someone is doing that manually it'll get tiring fast. But we'll see.
Regarding the bug you are getting: that's a new one. I'm not running the absolute latest Core so it's possible they tightened the API and the "byte" to encoding type cast is failing. I can take a look into that in the next few days.
@szeck87 I confirmed this is an issue with the latest PowerShell core. I created a ticket; we can move the conversation there: https://github.com/DTW-DanWard/PowerShell-Beautifier/issues/33
FYI, issue is now fixed (1.0.15)
Sounds good. If it works the way I hope, there won't be any need to move files in and out of the container.
I'll try and start working on Atom Beautify support next weekend.
@DTW-DanWard is there an easy way to get the version of powershell modules? @Glavin001 is there a way to "skip" version checking?
@szeck87
If the module is loaded into memory:
> (Get-Module DTW.PS.Beautifier).Version
Major Minor Build Revision
----- ----- ----- --------
1 0 15 -1
You can get individual properties off that version object:
> (Get-Module DTW.PS.Beautifier).Version.Build
15
Or just the whole thing as text:
> (Get-Module DTW.PS.Beautifier).Version.ToString()
1.0.15
If the module isn't loaded and you don't want to load it but rather parse the PSD1 file, this returns the text:
> (Select-String -Path .\DTW.PS.Beautifier.psd1 -Pattern "ModuleVersion += '(.*)'").Matches.Groups[1].Value
1.0.15
Or, if you want the version object so you don't have to parse the text for the build version
> [version]((Select-String -Path .\DTW.PS.Beautifier.psd1 -Pattern "ModuleVersion += '(.*)'").Matches.Groups[1].Value)
Major Minor Build Revision
----- ----- ----- --------
1 0 15 -1
Or more specifically the Build:
> ([version]((Select-String -Path .\DTW.PS.Beautifier.psd1 -Pattern "ModuleVersion += '(.*)'").Matches.Groups[1].Value)).Build
15
Thanks @DTW-DanWard. Once the psgallery update is in place, I can start doing some testing of what I have so far.
is there a way to "skip" version checking?
@szeck87 : I would suppose implement the version check to always return true.
I would very much like this feature.
It is not entirely clear to me what the remaining obstacles are. What exactly remains to be done?
Please Add Support
Most helpful comment
You know guys... instead of just posting "+1", there's an actual upvote on the top comment. Just click it. That way, we aren't receiving mass emails/notifications from everyone doing a +1.
Oh, and please... don't +1 this comment. That's just silly.