Quellcode durchsuchen

start using modules as objects

Tobias Simetsreiter vor 4 Jahren
Ursprung
Commit
cf53893349

+ 3 - 0
Makefile

@@ -8,3 +8,6 @@ build:
 
 run: 
 	start dist/WinImager.exe
+
+ps:
+	start powershell -ExecutionPolicy ByPass

+ 8 - 2
WinImager/LibWinImager.psm1

@@ -1,7 +1,12 @@
 
 Add-Type -AssemblyName "System.Windows.Forms"
 
+$Forms = Import-Module -AsCustomObject -Name ($PSScriptRoot + "/lib/forms.psm1")
+$VBox  = Import-Module -AsCustomObject -Name ($PSScriptRoot + "/lib/VBox.psm1")
+$Wpf  = Import-Module -AsCustomObject -Name ($PSScriptRoot + "/lib/wpf.psm1")
+
 Import-Module -Name ($PSScriptRoot + "/lib/forms.psm1")
+
 $PSScriptRoot |Out-Host
 
 Function BecomeAdmin($script){
@@ -126,9 +131,9 @@ y
 Function GuiSaveFileName
 {  
     param(
-        $initialDirectory = "C:\",
         $title = "Save Image File",
-        $filter = "Images (*.img;*.afp;*.raw;*.usb;*.iso)| *.img;*.afp;*.raw;*.usb;*.iso"
+        $filter = "Images (*.img;*.afp;*.raw;*.usb;*.iso)| *.img;*.afp;*.raw;*.usb;*.iso",
+        $initialDirectory = "C:\"
     )
     $SaveFileDialog = New-Object System.Windows.Forms.SaveFileDialog
     $SaveFileDialog.initialDirectory = $initialDirectory
@@ -282,3 +287,4 @@ function Hide-Console
     [Console.Window]::ShowWindow($consolePtr, 0)
 }
 
+Export-ModuleMember -Function * -Variable Forms,VBox,Wpf

+ 10 - 6
WinImager/bin/ImageTruncate.ps1

@@ -1,28 +1,32 @@
 
-Import-Module -Name $env:PSLIB
+$Lib = Import-Module -Name $env:PSLIB -AsCustomObject
 
 Function Main()
 {
     
     "Select Image File..."
     
-    $outfilename = GuiSaveFileName
+    $outfilename = $Lib.GuiOpenFileName()
 
-    if (($outfilename -eq $null) -or ($outfilename -eq "")){
+    if ( -not $outfilename ){
         "No Target Selected"
         Break
     }
 
-    if (Test-Path $outfilename -PathType Leaf) {
-        Remove-Item $outfilename
+    $size = $Lib.Wpf.ByteSizeDialog()
+    if ( -not $size ){
+        "No Size Selected"
+        Break
     }
 
-    fsutil file seteof  .\FB_small.img ([Math]::Pow(2,2))
+    "fsutil file seteof  $outfilename $size"
+    fsutil file seteof  $outfilename "$size"
 
     if($LASTEXITCODE -eq 0){
         GuiMessageBox "Success" -Buttons "Ok"
     } else {
         GuiMessageBox "Error" -Buttons "Ok"
+        pause
     }
     Break
 }

+ 7 - 11
WinImager/bin/VBOX_ImageToVDI.ps1

@@ -1,20 +1,16 @@
 
-Import-Module -Name $env:PSLIB
+$Lib = Import-Module -AsCustomObject -Name $env:PSLIB
 
 Function Main()
 {
-    if ($env:VBOX_INSTALL_PATH -ne $null){
-        $VBOXMANAGE = $env:VBOX_INSTALL_PATH + "VBoxManage.exe"
-    } elseif ($env:VBOX_MSI_INSTALL_PATH -ne $null){
-        $VBOXMANAGE = $env:VBOX_MSI_INSTALL_PATH + "VBoxManage.exe"
-    } else {
+    if ($Lib.VBox.VBOXMANAGE -eq $null){
         "Virtualbox install Path not found!"
         Break
     }
     
     "Select Image File..."
     
-    $infilename = GuiOpenFileName
+    $infilename = $Lib.GuiOpenFileName()
 
     if (($infilename -eq $null) -or ($infilename -eq "")){
         "No source file elected"
@@ -22,7 +18,7 @@ Function Main()
     }
 
     "Select VDI File..."
-    $outfilename = GuiSaveFileName -filter "Virtualbox Images (*.vdi)| *.vdi"
+    $outfilename = $Lib.GuiSaveFileName("Output Virtualbox Image","Virtualbox Images (*.vdi)| *.vdi")
 
     if (($outfilename -eq $null) -or ($outfilename -eq "")){
         "No Target Selected"
@@ -33,12 +29,12 @@ Function Main()
         Remove-Item $outfilename
     }
 
-    &$VBOXMANAGE convertfromraw $infilename $outfilename --format VDI
+    &$Lib.VBox.VBOXMANAGE convertfromraw $infilename $outfilename --format VDI
 
     if($LASTEXITCODE -eq 0){
-        GuiMessageBox "Success" -Buttons "Ok"
+        $Lib.GuiMessageBox("Success", "Success", "Ok")
     } else {
-        GuiMessageBox "Error" -Buttons "Ok"
+        $Lib.GuiMessageBox("Error", "Error","Ok")
     }
     Break
 }

+ 6 - 13
WinImager/lib/forms.psm1

@@ -1,20 +1,8 @@
 
-class Forms {
-    [string]$Brand
-     static [void]GuiMessageBox(
-        $text,
-        $title = "Continue",
-        $buttons = "YesNo",
-        $level = "Info"){
-
-        [System.Windows.Forms.MessageBox]::Show($text, $title, $buttons, $level)
-     }
-}
-
 Function GuiMessageBox
 {
     param(
-        $text,
+        $text = "",
         $title = "Continue",
         $buttons = "YesNo",
         $level = "Info"
@@ -22,3 +10,8 @@ Function GuiMessageBox
     [System.Windows.Forms.MessageBox]::Show($text, $title, $buttons, $level)
 }
 
+$Forms = {
+    MessageBox = (Get-Command "GuiMessageBox")
+}
+
+Export-ModuleMember -Function * -Variable "Forms"

+ 11 - 0
WinImager/lib/vbox.psm1

@@ -0,0 +1,11 @@
+
+if ($env:VBOX_INSTALL_PATH -ne $null){
+    $VBOXMANAGE = $env:VBOX_INSTALL_PATH + "VBoxManage.exe"
+} elseif ($env:VBOX_MSI_INSTALL_PATH -ne $null){
+    $VBOXMANAGE = $env:VBOX_MSI_INSTALL_PATH + "VBoxManage.exe"
+} else {
+    $VBOXMANAGE = $null
+}
+
+
+Export-ModuleMember -Function * -Variable VBOXMANAGE

+ 150 - 0
WinImager/lib/wpf.psm1

@@ -0,0 +1,150 @@
+
+[xml]$XAML_TEXTDIALOG = @"
+<Window
+        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
+        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
+		xmlns:xc="clr-namespace:ExCastle.Wpf"
+        Title="Input" SizeToContent="WidthAndHeight" WindowStartupLocation="CenterScreen">
+    <Grid Margin="15">
+        <Grid.ColumnDefinitions>
+            <ColumnDefinition Width="Auto" />
+            <ColumnDefinition Width="*" />
+        </Grid.ColumnDefinitions>
+        <Grid.RowDefinitions>
+            <RowDefinition Height="Auto" />
+            <RowDefinition Height="Auto" />
+            <RowDefinition Height="Auto" />
+        </Grid.RowDefinitions>
+
+        <Label Name="lblQuestion" Grid.Column="1">Question:</Label>
+        <TextBox Name="txtAnswer" Grid.Column="1" Grid.Row="1" MinWidth="250">Answer</TextBox>
+
+        <WrapPanel Grid.Row="2" Grid.ColumnSpan="2" HorizontalAlignment="Right" Margin="0,15,0,0">
+            <Button IsDefault="True" Name="btnDialogOk" MinWidth="60" Margin="0,0,10,0">_Ok</Button>
+            <Button IsCancel="True" MinWidth="60">_Cancel</Button>
+        </WrapPanel>
+    </Grid>
+</Window>
+"@
+
+[xml]$XAML_BYTESIZEDIALOG = @"
+<Window
+        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
+        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
+		xmlns:xc="clr-namespace:ExCastle.Wpf"
+        Title="Input" SizeToContent="WidthAndHeight" WindowStartupLocation="CenterScreen">
+    <Grid Margin="15">
+        <Grid.ColumnDefinitions>
+            <ColumnDefinition Width="Auto" />
+            <ColumnDefinition Width="*" />
+        </Grid.ColumnDefinitions>
+        <Grid.RowDefinitions>
+            <RowDefinition Height="Auto" />
+            <RowDefinition Height="Auto" />
+            <RowDefinition Height="Auto" />
+        </Grid.RowDefinitions>
+
+       
+        <Label Name="lblQuestion" Grid.Column="1">Enter Size in:</Label>
+        <WrapPanel Grid.Row="1" Grid.ColumnSpan="2" HorizontalAlignment="Right" Margin="0,15,0,0">
+            <TextBox Name="txtAnswer" Grid.Column="1" Grid.Row="1" MinWidth="250">512</TextBox>
+            <ComboBox Name="sizeScale">
+                <ComboBoxItem>B</ComboBoxItem>
+                <ComboBoxItem>512B</ComboBoxItem>
+                <ComboBoxItem>KB</ComboBoxItem>
+                <ComboBoxItem IsSelected="True">MB</ComboBoxItem>
+                <ComboBoxItem>GB</ComboBoxItem>
+            </ComboBox>
+        </WrapPanel>
+
+        <WrapPanel Grid.Row="2" Grid.ColumnSpan="2" HorizontalAlignment="Right" Margin="0,15,0,0">
+            <Button IsDefault="True" Name="btnDialogOk" MinWidth="60" Margin="0,0,10,0">_Ok</Button>
+            <Button IsCancel="True" MinWidth="60">_Cancel</Button>
+        </WrapPanel>
+    </Grid>
+</Window>
+"@
+
+# $XAML = $XAML -replace 'mc:Ignorable="d"','' -replace "x:N",'N' -replace '^<Win.*', '<Window' #-replace wird benötigt, wenn XAML aus Visual Studio kopiert wird.
+#XAML laden
+[void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework')
+
+Function LoadWPF($XAML){
+	$app = @{
+		Form = [Windows.Markup.XamlReader]::Load( (New-Object System.Xml.XmlNodeReader $XAML) )
+		WPF = @{}
+	}
+	$xaml.SelectNodes("//*[@Name]") | %{$app.WPF | Add-Member -MemberType NoteProperty -Name $_.Name -Value $app.Form.FindName($_.Name)}
+	$app
+}
+
+Function ByteSizeDialog
+{
+    param(
+        [int]$default = 536870912
+    )
+	$MyApp = LoadWPF $XAML_BYTESIZEDIALOG
+    $MyApp.WPF.txtAnswer.SelectAll() | Out-Null
+    $MyApp.WPF.txtAnswer.Focus() | Out-Null
+
+	$MyApp.WPF.txtAnswer.Add_PreviewTextInput({
+        $event = $_
+        try{
+            $val = ([int]$_.Text)
+        } catch {
+            $event.Handled = $true
+        }
+    })
+
+    $values = @(
+        0,
+        9,
+        10,
+        20,
+        30
+    )
+
+	$MyApp.WPF.btnDialogOk.Add_Click({
+		$MyApp.Form.DialogResult = $true
+		$MyApp.Form.Close() | Out-Null
+	})
+
+	if ($MyApp.Form.ShowDialog()){
+		(([int]$MyApp.WPF.txtAnswer.Text) * [Math]::Pow(2,$values[$MyApp.WPF.SizeScale.selectedIndex]))
+    } else {
+        $false
+    }
+}
+
+Function TextDialog
+{
+    param(
+        $label = "Question:",
+        $default = "Some Default Text"
+    )
+
+	$MyApp = LoadWPF $XAML_TEXTDIALOG
+
+    $MyApp | Add-Member -NotePropertyName DialogResult -NotePropertyValue $false
+
+	$MyApp.WPF.txtAnswer.Text = $default
+	$MyApp.WPF.lblQuestion.Text = $label
+
+    $MyApp.WPF.txtAnswer.SelectAll()
+    $MyApp.WPF.txtAnswer.Focus()
+
+	# $MyApp.WPF.txtAnswer.Add_TextChanged({$this.Text | Out-Host})
+
+	$Form = $MyApp.Form
+	$MyApp.WPF.btnDialogOk.Add_Click({
+		$MyApp.Form.DialogResult = $true
+		$MyApp.Form.Close()
+	})
+	if ($MyApp.Form.ShowDialog()){
+		$MyApp.WPF.txtAnswer.Text
+    } else {
+        $false
+    }
+}
+
+Export-ModuleMember -Function *