Forráskód Böngészése

PowerShell script that runs WinSCP scripting commands can be generated on Generate Code dialog

Source commit: 51b98c02b5f2a14481e0501b19c46b8df612f4a8
Martin Prikryl 7 éve
szülő
commit
9b70af411e

+ 8 - 3
source/core/Common.cpp

@@ -3415,9 +3415,14 @@ UnicodeString __fastcall RtfRemoveHyperlinks(UnicodeString Text)
   return Text;
   return Text;
 }
 }
 //---------------------------------------------------------------------
 //---------------------------------------------------------------------
-UnicodeString __fastcall RtfEscapeParam(UnicodeString Param)
+UnicodeString __fastcall RtfEscapeParam(UnicodeString Param, bool PowerShellEscape)
 {
 {
   const UnicodeString Quote(L"\"");
   const UnicodeString Quote(L"\"");
+  UnicodeString Escape(Quote);
+  if (PowerShellEscape)
+  {
+    Escape = "`" + Escape + "`";
+  }
   // Equivalent of EscapeParam, except that it does not double quotes in HYPERLINK.
   // Equivalent of EscapeParam, except that it does not double quotes in HYPERLINK.
   // See also RtfRemoveHyperlinks.
   // See also RtfRemoveHyperlinks.
   int Index = 1;
   int Index = 1;
@@ -3440,8 +3445,8 @@ UnicodeString __fastcall RtfEscapeParam(UnicodeString Param)
       }
       }
       else
       else
       {
       {
-        Param.Insert(Quote, P1);
-        Index = P1 + (Quote.Length() * 2);
+        Param.Insert(Escape, P1);
+        Index = P1 + (Escape.Length() + Quote.Length());
       }
       }
     }
     }
   }
   }

+ 1 - 1
source/core/Common.h

@@ -255,7 +255,7 @@ UnicodeString __fastcall RtfSwitch(
   const UnicodeString & Name, const UnicodeString & Link, const UnicodeString & Value, bool Rtf = true);
   const UnicodeString & Name, const UnicodeString & Link, const UnicodeString & Value, bool Rtf = true);
 UnicodeString __fastcall RtfSwitch(
 UnicodeString __fastcall RtfSwitch(
   const UnicodeString & Name, const UnicodeString & Link, int Value, bool Rtf = true);
   const UnicodeString & Name, const UnicodeString & Link, int Value, bool Rtf = true);
-UnicodeString __fastcall RtfEscapeParam(UnicodeString Param);
+UnicodeString __fastcall RtfEscapeParam(UnicodeString Param, bool PowerShellEscape);
 UnicodeString __fastcall RtfRemoveHyperlinks(UnicodeString Text);
 UnicodeString __fastcall RtfRemoveHyperlinks(UnicodeString Text);
 UnicodeString __fastcall ScriptCommandLink(const UnicodeString & Command);
 UnicodeString __fastcall ScriptCommandLink(const UnicodeString & Command);
 UnicodeString __fastcall AssemblyBoolean(TAssemblyLanguage Language, bool Value);
 UnicodeString __fastcall AssemblyBoolean(TAssemblyLanguage Language, bool Value);

+ 53 - 5
source/forms/GenerateUrl.cpp

@@ -240,7 +240,7 @@ UnicodeString __fastcall RtfCommandlineSwitch(const UnicodeString & Switch, cons
 //---------------------------------------------------------------------------
 //---------------------------------------------------------------------------
 static UnicodeString __fastcall QuoteStringParam(UnicodeString S)
 static UnicodeString __fastcall QuoteStringParam(UnicodeString S)
 {
 {
-  return AddQuotes(RtfEscapeParam(S));
+  return AddQuotes(RtfEscapeParam(S, false));
 }
 }
 //---------------------------------------------------------------------------
 //---------------------------------------------------------------------------
 // Keep in sync with .NET Session.EscapeFileMask
 // Keep in sync with .NET Session.EscapeFileMask
@@ -366,6 +366,8 @@ UnicodeString __fastcall TGenerateUrlDialog::GenerateScript(UnicodeString & Scri
   Commands.push_back(UnicodeString());
   Commands.push_back(UnicodeString());
   Commands.push_back(RtfScriptCommand(L"exit"));
   Commands.push_back(RtfScriptCommand(L"exit"));
 
 
+  UnicodeString ComExeName = ChangeFileExt(ExeName, L".com");
+
   if (ScriptFormatCombo->ItemIndex == sfScriptFile)
   if (ScriptFormatCombo->ItemIndex == sfScriptFile)
   {
   {
     for (TCommands::const_iterator I = Commands.begin(); I != Commands.end(); I++)
     for (TCommands::const_iterator I = Commands.begin(); I != Commands.end(); I++)
@@ -395,8 +397,6 @@ UnicodeString __fastcall TGenerateUrlDialog::GenerateScript(UnicodeString & Scri
   }
   }
   else if (ScriptFormatCombo->ItemIndex == sfBatchFile)
   else if (ScriptFormatCombo->ItemIndex == sfBatchFile)
   {
   {
-    UnicodeString ComExeName = ChangeFileExt(ExeName, L".com");
-
     Result =
     Result =
       RtfScriptPlaceholder(L"@echo off") + RtfPara +
       RtfScriptPlaceholder(L"@echo off") + RtfPara +
       RtfPara +
       RtfPara +
@@ -420,7 +420,7 @@ UnicodeString __fastcall TGenerateUrlDialog::GenerateScript(UnicodeString & Scri
         }
         }
         else
         else
         {
         {
-          Result += RtfEscapeParam(ReplaceStr(Command, L"%", L"%%"));
+          Result += RtfEscapeParam(ReplaceStr(Command, L"%", L"%%"), false);
         }
         }
         Result += L"\"";
         Result += L"\"";
       }
       }
@@ -458,11 +458,54 @@ UnicodeString __fastcall TGenerateUrlDialog::GenerateScript(UnicodeString & Scri
         }
         }
         else
         else
         {
         {
-          Result += RtfEscapeParam(Command);
+          Result += RtfEscapeParam(Command, false);
+        }
+        Result += L"\"";
+      }
+    }
+  }
+  else if (ScriptFormatCombo->ItemIndex == sfPowerShell)
+  {
+    Result =
+      RtfText(L"& \"" + ComExeName + "\" `") + RtfPara +
+      RtfText(L"  ") + LogParameter + L" " + IniParameter + RtfText(L" `") + RtfPara +
+      RtfText(L"  ") + CommandParameter;
+
+    for (TCommands::const_iterator I = Commands.begin(); I != Commands.end(); I++)
+    {
+      UnicodeString Command = *I;
+      if (!Command.IsEmpty())
+      {
+        Result += RtfText(L" `") + RtfPara + RtfText(L"    \"");
+        if (Command[1] == L'#')
+        {
+          Command.Delete(1, 1);
+          Result += RtfScriptPlaceholder(Command.TrimLeft());
+        }
+        else
+        {
+          Command = ReplaceStr(Command, L"`", L"``");
+          Command = ReplaceStr(Command, L"$", L"`$");
+          Result += RtfEscapeParam(Command, true);
         }
         }
         Result += L"\"";
         Result += L"\"";
       }
       }
     }
     }
+
+    Result +=
+      RtfPara +
+      RtfPara +
+      RtfText(L"$winscpResult = $LastExitCode") + RtfPara +
+      RtfKeyword(L"if") + RtfText(L" ($winscpResult -eq 0)") + RtfPara +
+      RtfText(L"{") + RtfPara +
+      RtfText(L"  ") + RtfKeyword(L"Write-Host") + L" " + RtfString(L"\"Success\"") + RtfPara +
+      RtfText(L"}") + RtfPara +
+      RtfText(L"else") + RtfPara +
+      RtfText(L"{") + RtfPara +
+      RtfText(L"  ") + RtfKeyword(L"Write-Host") + L" " + RtfString(L"\"Error\"") + RtfPara +
+      RtfText(L"}") + RtfPara +
+      RtfPara +
+      RtfKeyword(L"exit") + RtfText(L" $winscpResult") + RtfPara;
   }
   }
 
 
   return Result;
   return Result;
@@ -714,6 +757,11 @@ void __fastcall TGenerateUrlDialog::UpdateControls()
 
 
         ScriptDescription = FMTLOAD(GENERATE_URL_COMMANDLINE_DESC, (FORMAT("\"%s\"", (Application->ExeName)))) + L"\n";
         ScriptDescription = FMTLOAD(GENERATE_URL_COMMANDLINE_DESC, (FORMAT("\"%s\"", (Application->ExeName)))) + L"\n";
       }
       }
+      else if (ScriptFormatCombo->ItemIndex == sfPowerShell)
+      {
+        WordWrap = false;
+        FixedWidth = true;
+      }
 
 
       if (HostKeyUnknown)
       if (HostKeyUnknown)
       {
       {

+ 4 - 3
source/forms/GenerateUrl.dfm

@@ -126,7 +126,7 @@ object GenerateUrlDialog: TGenerateUrlDialog
       object ScriptFormatCombo: TComboBox
       object ScriptFormatCombo: TComboBox
         Left = 112
         Left = 112
         Top = 5
         Top = 5
-        Width = 103
+        Width = 121
         Height = 21
         Height = 21
         Style = csDropDownList
         Style = csDropDownList
         TabOrder = 0
         TabOrder = 0
@@ -134,7 +134,8 @@ object GenerateUrlDialog: TGenerateUrlDialog
         Items.Strings = (
         Items.Strings = (
           'Script file'
           'Script file'
           'Batch file'
           'Batch file'
-          'Command-line')
+          'Command-line'
+          'PowerShell script')
       end
       end
     end
     end
     object AssemblySheet: TTabSheet
     object AssemblySheet: TTabSheet
@@ -160,7 +161,7 @@ object GenerateUrlDialog: TGenerateUrlDialog
       object AssemblyLanguageCombo: TComboBox
       object AssemblyLanguageCombo: TComboBox
         Left = 112
         Left = 112
         Top = 5
         Top = 5
-        Width = 103
+        Width = 121
         Height = 21
         Height = 21
         Style = csDropDownList
         Style = csDropDownList
         TabOrder = 0
         TabOrder = 0

+ 1 - 1
source/windows/WinConfiguration.h

@@ -8,7 +8,7 @@
 //---------------------------------------------------------------------------
 //---------------------------------------------------------------------------
 enum TEditor { edInternal, edExternal, edOpen };
 enum TEditor { edInternal, edExternal, edOpen };
 enum TGenerateUrlCodeTarget { guctUrl, guctScript, guctAssembly };
 enum TGenerateUrlCodeTarget { guctUrl, guctScript, guctAssembly };
-enum TScriptFormat { sfScriptFile, sfBatchFile, sfCommandLine };
+enum TScriptFormat { sfScriptFile, sfBatchFile, sfCommandLine, sfPowerShell };
 enum TLocaleFlagOverride { lfoLanguageIfRecommended, lfoLanguage, lfoAlways, lfoNever };
 enum TLocaleFlagOverride { lfoLanguageIfRecommended, lfoLanguage, lfoAlways, lfoNever };
 //---------------------------------------------------------------------------
 //---------------------------------------------------------------------------
 #define C(Property) (Property != rhc.Property) ||
 #define C(Property) (Property != rhc.Property) ||