GithubHelp home page GithubHelp logo

ironmansoftware / code-conversion Goto Github PK

View Code? Open in Web Editor NEW
118.0 7.0 20.0 71 KB

Code conversion command line tool for PowerShell and C#

License: Apache License 2.0

PowerShell 10.69% C# 89.31%
converter csharp powershell

code-conversion's Introduction

PowerShell to C# Code Conversion

Orginally a feature of PowerShell Pro Tools, this module provides the ability to convert between C# and PowerShell. No license required.

Installation

Install-Module CodeConversion

Usage

Convert from C# to PowerShell

Invoke-CodeConversion -CSharp 'class MyClass { 
    public object MyMethod()
    {
        return new Object();
    }
}'
<#
Outputs: 

function MyMethod
{
        return (New-Object -TypeName Object)
}

#>

Convert from PowerShell to C#

Invoke-CodeConversion -PowerShell 'Get-Process'
# Outputs: System.Diagnostics.Process.GetProcesses()

code-conversion's People

Contributors

adamdriscoll avatar skycommand avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

code-conversion's Issues

FileNotFoundException when CodeConversion.exe provided with -o argument

Directly invoking the executable, since the script passes the wrong parameters as mentioned in #12, still doesn't work, throwing a FileNotFoundException for the output file:

CodeConversion.exe -i .\MyModule.psm1 -o .\MyModule.cs

results in:

Unhandled exception. System.IO.FileNotFoundException: Could not find file 'V:\MyModule.cs'.
File name: 'V:\MyModule.cs'
   at Microsoft.Win32.SafeHandles.SafeFileHandle.CreateFile(String fullPath, FileMode mode, FileAccess access, FileShare share, FileOptions options)
   at Microsoft.Win32.SafeHandles.SafeFileHandle.Open(String fullPath, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize)
   at System.IO.Strategies.OSFileStreamStrategy..ctor(String path, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize)
   at System.IO.Strategies.FileStreamHelpers.ChooseStrategyCore(String path, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize)
   at System.IO.Strategies.FileStreamHelpers.ChooseStrategy(FileStream fileStream, String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, Int64 preallocationSize)
   at System.IO.StreamReader.ValidateArgsAndOpenPath(String path, Encoding encoding, Int32 bufferSize)
   at System.IO.File.InternalReadAllText(String path, Encoding encoding)
   at System.IO.File.ReadAllText(String path)
   at CodeConversion.Program.<>c.<Main>b__0_0(Options o) in C:\src\code-conversion\src\Program.cs:line 40
   at CommandLine.ParserResultExtensions.WithParsed[T](ParserResult`1 result, Action`1 action)
   at CodeConversion.Program.Main(String[] args) in C:\src\code-conversion\src\Program.cs:line 23

Don't work on macOS

Is it supposed to work on macOS? It outputs nothing:

$pwsh
PowerShell 7.2.4
Copyright (c) Microsoft Corporation.

https://aka.ms/powershell
Type 'help' to get help.

PS /Users/a> Install-Module CodeConversion

Untrusted repository
You are installing the modules from an untrusted repository. If you trust this repository, change its InstallationPolicy value by running the Set-PSRepository cmdlet. Are you sure
 you want to install the modules from 'PSGallery'?
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "N"): y

PS /Users/a> Invoke-CodeConversion -PowerShell 'Get-Process'
PS /Users/a>

ConvertTo-Powershell appears to use the wrong working path

Even though the prompt is at my home folder, it forces the working path to be c:\windows\system32.

PS C:\Users\Andrew.Stanton> ConvertTo-Powershell -CSharpFile .\source\repos\MochaResultTestRailUploader\MochaResultTestRailUploader\MochaTypes.cs
ConvertTo-PowerShell: File C:\WINDOWS\system32\source\repos\MochaResultTestRailUploader\MochaResultTestRailUploader\MochaTypes.cs not found.

If i give it the full path I get the error named in #1

CodeConversion v2.0.1 throwing errors when converting from C# to Powershell

Hello, I am trying to convert a ~100 line C# snippet to Powershell so that I don't have to use Add-Type to call the code however CodeConversion is erroring out. Below is the error I get:

   at CodeConverter.PowerShell.PowerShellCodeWriter.VisitObjectCreation(ObjectCreation node) in C:\src\code-conversion\src\PowerShell\PowerShellCodeWriter.cs:line 217
   at CodeConverter.Common.ObjectCreation.Accept(NodeVisitor visitor) in C:\src\code-conversion\src\Common\Ast.cs:line 681
   at CodeConverter.PowerShell.PowerShellCodeWriter.VisitVariableDeclarator(VariableDeclarator node) in C:\src\code-conversion\src\PowerShell\PowerShellCodeWriter.cs:line 377
   at CodeConverter.PowerShell.PowerShellCodeWriter.VisitVariableDeclaration(VariableDeclaration node) in C:\src\code-conversion\src\PowerShell\PowerShellCodeWriter.cs:line 366
   at CodeConverter.Common.VariableDeclaration.Accept(NodeVisitor visitor) in C:\src\code-conversion\src\Common\Ast.cs:line 1029
   at CodeConverter.Common.CStyleCodeWriter.VisitBlock(Block node) in C:\src\code-conversion\src\Common\CStyleCodeWriter.cs:line 83
   at CodeConverter.Common.Block.Accept(NodeVisitor visitor) in C:\src\code-conversion\src\Common\Ast.cs:line 241
   at CodeConverter.PowerShell.PowerShellCodeWriter.VisitMethodDeclaration(MethodDeclaration node) in C:\src\code-conversion\src\PowerShell\PowerShellCodeWriter.cs:line 198
   at CodeConverter.Common.MethodDeclaration.Accept(NodeVisitor visitor) in C:\src\code-conversion\src\Common\Ast.cs:line 637
   at CodeConverter.Common.NodeVisitor.VisitClassDeclaration(ClassDeclaration node) in C:\src\code-conversion\src\Common\NodeVisitor.cs:line 95
   at CodeConverter.Common.ClassDeclaration.Accept(NodeVisitor visitor) in C:\src\code-conversion\src\Common\Ast.cs:line 341
   at CodeConverter.Common.CStyleCodeWriter.VisitBlock(Block node) in C:\src\code-conversion\src\Common\CStyleCodeWriter.cs:line 83
   at CodeConverter.Common.Block.Accept(NodeVisitor visitor) in C:\src\code-conversion\src\Common\Ast.cs:line 241
   at CodeConverter.Common.CodeWriter.Write(Node ast) in C:\src\code-conversion\src\Common\CodeWriter.cs:line 33
   at CodeConverter.Converter.Convert(String code, Language from, Language to) in C:\src\code-conversion\src\Converter.cs:line 67
   at CodeConverter.Converter.Convert(String code, Language to) in C:\src\code-conversion\src\Converter.cs:line 46
   at CodeConversion.Program.<>c.<Main>b__0_0(Options o) in C:\src\code-conversion\src\Program.cs:line 52
   at CommandLine.ParserResultExtensions.WithParsed[T](ParserResult`1 result, Action`1 action)
   at CodeConversion.Program.Main(String[] args) in C:\src\code-conversion\src\Program.cs:line 23

And here is a link to the gist of the C# code so that I don't have a 100 line code block in the issue:

https://gist.githubusercontent.com/chrisdma/2ab5e03edc8b1f656922e7c2e5753808/raw/b9c9fd09f824736354df426d4660bdbca7b1c7c9/GetCredential.cs

Thanks for any assistance you can provide!

Chris

Incorrect parameter passing to executable from Invoke-CodeConversion

Invoke-CodeConversion -InputFile .\MyModule.psm1 -OutputFile MyModule.cs

The parameters the executable expects do not match.

The executable is expecting either -o or --OutputPath, but the script is explicitly sending --OutputFile.

But even directly invoking the executable with the correct parameters fails, which I'll post as another issue.

PowerShell > C# using 'File' parameter set fails (module v2.0.1)

When attempting to convert from PowerShell to C#, I'm running this:

$Params = @{
    InputFile  = "$env:SystemRoot\repo\Modules\test\classes\class_Options.ps1" 
    OutputFile =  "$env:SystemRoot\repo\Modules\test\classes\class_Options.cs"
}
Invoke-CodeConversion @Params

The CodeConverter outputs the below error. Curiously, the converter doesn't throw errors (i.e. it outputs to stdout instead of stderr), but I digress.

CodeConversion 1.0.0
Copyright (C) 2023 CodeConversion

ERROR(S):
  Option 'OutputFile' is unknown.
  Required option 'o, OutputPath' is missing.

  -i, --InputFile     Required. File to convert.

  -o, --OutputPath    Required. Output file.

  --PowerShell        Required. PowerShell script block to convert to C#

  --CSharp            Required. C# code to convert to PowerShell

  --help              Display this help screen.

  --version           Display version information.

I'm very inexperienced with .NET and C# (using this as a means to translate my PowerShell knowledge), but as best I can tell this is tracing to the Program.cs.

If I'm understanding it correctly, it's taking this line from /CodeConversion.psm1:

& $Path --InputFile $InputFile --OutputPath $OutputFile

which passes $OutputFile into the Options class in /src/Program.cs:

[Option('o', "OutputPath", Required = true, HelpText = "Output file.", SetName = "File")]
public string OutputFile { get; set; }

and somehow the $OutputFile parameter is not being passed correctly, causing a failure because the OutputPath option is required? If I'm understanding the context, the first error says the cmdlet is calling OutputFile as the option instead of OutputPath?

I'm not entirely sure if this is an actual bug or I am merely not using this correctly... But I'm also curious if I am correctly understanding the mechanics of this (testing my knowledge, if you will). Any help is appreciated.

Wrong output

input
Invoke-CodeConversion -CSharp 'namespace HelloWorld{class Hello { static void Main(string[] args){System.Console.WriteLine("Hello World!");}}}'

output

{
        param([string[]]$args)
        $System.Console.WriteLine($Hello)
}

CodeConversion does not work at all (in a Sandbox)

I started a fresh Sandbox and followed your instructions:

PS C:\Users\WDAGUtilityAccount> Install-Module CodeConversion

NuGet provider is required to continue
PowerShellGet requires NuGet provider version '2.8.5.201' or newer to interact with NuGet-based repositories. The NuGet provider must be available in 'C:\Program Files\PackageManagement\ProviderAssemblies' or 'C:\Users\WDAGUtilityAccount\AppData\Local\PackageManagement\ProviderAssemblies'. You can also install the NuGet provider by running
'Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force'. Do you want PowerShellGet to install and import the NuGet provider now?
[Y] Yes  [N] No  [S] Suspend  [?] Help (default is "Y"): Y

Untrusted repository
You are installing the modules from an untrusted repository. If you trust this repository, change its InstallationPolicy value by running the Set-PSRepository cmdlet. Are you sure you want to install the modules from 'PSGallery'?
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "N"): A
PS C:\Users\WDAGUtilityAccount> ConvertTo-PowerShell -Code 'System.Diagnostics.Process.GetProcesses()'
ConvertTo-PowerShell : A parameter cannot be found that matches parameter name 'Code'.
At line:1 char:22
+ ConvertTo-PowerShell -Code 'System.Diagnostics.Process.GetProcesses() ...
+                      ~~~~~
    + CategoryInfo          : InvalidArgument: (:) [ConvertTo-PowerShell], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,PowerShellToolsPro.Cmdlets.ConvertToPowerShellCommand

PS C:\Users\WDAGUtilityAccount> ConvertTo-CSharp -Code 'Get-Process'
ConvertTo-CSharp : A parameter cannot be found that matches parameter name 'Code'.
At line:1 char:18
+ ConvertTo-CSharp -Code 'Get-Process'
+                  ~~~~~
    + CategoryInfo          : InvalidArgument: (:) [ConvertTo-CSharp], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,PowerShellToolsPro.Cmdlets.ConvertToCSharpCommand

PS C:\Users\WDAGUtilityAccount>

Unfortunately it didn't work at all. Am I doing something wrong or is this a bug?

Could not load file or assembly 'Microsoft.CodeAnalysis.CSharp,

Hello,

I am unable to use the ConvertTo-PowerShell command with the 'CSharpFile' parameter.
It returns an error ConvertTo-PowerShell: Could not load file or assembly 'Microsoft.CodeAnalysis.CSharp, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' - The specified file could not be found.

image

I think the DLL should be in the repo.

Thank you.

Cordially,

Thomas.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.