From e88048069795b11fe2af1536c7849521eae01018 Mon Sep 17 00:00:00 2001 From: Tim Clifford Date: Thu, 14 Oct 2021 20:58:45 +0100 Subject: [PATCH] Convert to C# and add Github Action to compile it Co-authored-by: Henry Franks --- .github/workflows/build.yml | 31 +++++ .gitignore | 4 + README.md | 8 +- charon_diff.sh | 1 - compare.swb | 224 ------------------------------------ compare/Marshal2.cs | 55 +++++++++ compare/Program.cs | 117 +++++++++++++++++++ compare/compare.csproj | 17 +++ keys/hades | 38 ++++++ keys/hades.pub | 1 + setup.sh | 35 ++++-- solidworks_driver.sh | 44 +++---- 12 files changed, 308 insertions(+), 267 deletions(-) create mode 100644 .github/workflows/build.yml create mode 100644 .gitignore delete mode 100644 compare.swb create mode 100644 compare/Marshal2.cs create mode 100644 compare/Program.cs create mode 100644 compare/compare.csproj create mode 100644 keys/hades create mode 100644 keys/hades.pub diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..396202a --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,31 @@ +name: Charon Build + +on: + push: + tags: + - "v*" + +jobs: + build: + runs-on: windows-latest + + steps: + - uses: webfactory/ssh-agent@v0.5.3 + with: + ssh-private-key: ${{ secrets.DEPLOY_KEY }} + - name: Prepare + run: | + git clone https://github.com/smh-my-head/charon + git clone git@github.com:smh-my-head/charon-build-assets + mkdir charon/compare/build + cp charon-build-assets/* charon/compare/build + + - name: Build + run: | + cd charon/compare + dotnet.exe publish -r win-x64 -o build --nologo --configuration "Release" /p:PublishSingleFile=true /p:IncludeNativeLibrariesForSelfExtract=true + - name: Release + uses: softprops/action-gh-release@v1 + with: + files: | + charon/compare/build/compare.exe diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ca0763a --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +/test +/compare/bin +/compare/obj +/compare/build diff --git a/README.md b/README.md index 55c9f7f..c04f00e 100644 --- a/README.md +++ b/README.md @@ -1,14 +1,10 @@ # Charon The ferryman to Hell (aka CAD version control). Provides integration with git -for SolidWorks parts and assemblies. +for SolidWorks parts and assemblies. Fully integrated with [Hades](https://github.com/smh-my-head/hades) ## Usage -Add this repo as a submodule and then run `./charon/setup.sh` from Git Bash or -MSYS2. You can then use git as normal, but `git diff` and `git merge` will have -added sugar. If you use Linux, check out -[hades](https://github.com/smh-my-head/hades), as the two projects are fully -integrated. +See the CUSF wiki page on [Charon](https://wiki.cusf.co.uk/Charon). You will also need to enable *SOLIDWORKS Utilities* at startup, you can do this by navigating (in SolidWorks) to *Tools->Add-Ins* and checking the box for diff --git a/charon_diff.sh b/charon_diff.sh index 514ba12..c9a5319 100755 --- a/charon_diff.sh +++ b/charon_diff.sh @@ -64,7 +64,6 @@ if ! $driver --check-health; then echo "File $1 differs" else echo "Opening $prog_name to diff $1..." - echo "$prog_name must exit fully before this diff can exit" $driver $left_filename $right_filename fi diff --git a/compare.swb b/compare.swb deleted file mode 100644 index d307258..0000000 --- a/compare.swb +++ /dev/null @@ -1,224 +0,0 @@ -' ************************************************** -' * SOLIDWORKS MACRO OF ETERNAL PAIN * -' * "I'M CRYING BLOOD" - H. FRANKS, SATISFIED USER * -' ************************************************** -' vi: ft=basic -' -' Copyright (C) 2021 Tim Clifford, Henry Franks -' -' This program is free software: you can redistribute it and/or modify -' it under the terms of the GNU Affero General Public License as -' published by the Free Software Foundation, either version 3 of the -' License, or (at your option) any later version. -' -' This program is distributed in the hope that it will be useful, -' but WITHOUT ANY WARRANTY; without even the implied warranty of -' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -' GNU Affero General Public License for more details. -' -' You should have received a copy of the GNU Affero General Public License -' along with this program. If not, see . - -Option Explicit - -' This is needed for the command line stuff -' I've found that moving it breaks everything so... don't do that? -' Also, solidworks is REALLY funny about where we break lines here -' In conclusion: don't touch -Declare PtrSafe Function lstrcpyn_long_string _ - Lib "kernel32" Alias "lstrcpynA" ( _ - ByVal DestString As String, _ - ByVal SourcePointer As Long, _ - ByVal MaxLen As Long _ - ) _ - As Long - -Declare PtrSafe Function lstrlen_long _ - Lib "kernel32" Alias "lstrlenA" ( _ - ByVal SourcePointer As Long _ - ) _ - As Long - -Declare PtrSafe Function GetCommandLine_long _ - Lib "kernel32" Alias "GetCommandLineA" () _ - As Long - -' Entry point -Sub main() - - ' Declare all our objects here - ' Note that they're generic objects and not solidworks-specific types - ' This is due to something called 'late linking', which - ' we need if we want to keep the files in plaintext. - ' Comments after generic types show the relevant bound types - Dim Args() As String - Dim swApp As Object ' SldWorks.SldWorks - Dim swUtil As Object ' SWUtilities.gtcocswUtilities - Dim swUtilCompGeom As Object ' SWUtilities.gtcocswCompareGeometry - Dim longStatus As Long ' gtError_e - Dim volStatus As Long ' gtVolDiffStatusOptionType_e - Dim faceStatus As Long ' gtVolDiffStatusOptionType_e - - ' Get Solidworks and the Utilites add-on (make sure that's installed) - Set swApp = CreateObject("SldWorks.Application") - Set swUtil = swApp.GetAddInObject("Utilities.UtilitiesApp") - - ' Check if Utilities is disabled - If swUtil Is Nothing Then - MsgBox "Cannot open comparison - please make sure" _ - + "Solidworks Utilities is enabled at startup" - Exit Sub - End If - ' This gets the solidworks compare interface, don't ask me how - Set swUtilCompGeom = swUtil.GetToolInterface(2, longStatus) - - ' Check for errors raised getting the compare tool - ' If so, CheckErr creates a popup box, then we exit - If CheckErr(longStatus) Then - Exit Sub - End If - - ' We split our args based on a somewhat arbitrary delimiter, - ' but notably one that cannot appear in any file paths - Args = Split(GetCommandLine, " /*") - - ' If file paths aren't correctly specified, warn the user and exit - If UBound(Args) <> 2 Then - MsgBox "Incorrect number of arguments" - Exit Sub - End If - - ' Run the actual geometry compare - ' We can't put the comments on each line because of how VBA parses line - ' breaks, so we call the function basically as follows: - ' - ' Function CompareGeometry3( - ' ref file (+ empty config) - ' mod file (+ empty config) - ' gtGdfVolumeCompare [gtGdfOperationOption_e] - ' gtResultShowUI [gtResultOptions_e] - ' empty report path - ' don't add to binder - ' don't overwrite - ' status variables - ' ) - longStatus = swUtilCompGeom.CompareGeometry3( _ - Args(1), "", Args(2), "", _ - 1, 1, "", False, False, _ - volStatus, faceStatus _ - ) - - ' Create a popup box if this throws an error - CheckErr longStatus - - ' This will probably only happen in the case longStatus = -1 (unknown) - ' But we'll check regardless of the longStatus code - If volStatus <> 0 And volStatus <> 1 Then - MsgBox "Volume comparison error: " _ - + Str(volStatus) + " - " + diffStatusToString(volStatus) - End If - - ' Close the geometry compare tool - ' You may think that leaving the tool open will give us a nice visual - ' diff like the when it's used manually. This is not the case. - ' Closing the compare tool stops the program from hanging. - If longStatus = 0 Then - ' If we haven't had an error so far, try closing - ' the tool and return an error if it raises one - CheckErr swUtilCompGeom.Close() - Else - ' If we've already thrown an error, this will probably throw - ' error 7007 (gtErrCompareGeometryNotExecuted), in which case - ' there's no point in us bothering the user further - swUtilCompGeom.Close - End If - -End Sub - - -Function CheckErr(status) as Boolean - ' Generic catch-all error message that will eventually - ' make its way to my inbox, whether I like it or not - - ' Check if we have a nonzero error code - ' Also exclude status code 15 (gtErrIncorrectReportPath) - ' We also return whether or not this triggers a reasonable error as a bool - If status <> 0 And status <> 15 Then - MsgBox "Comparison failed with exit code " + Str(status) _ - + ". Please open an issue at " _ - + "https://github.com/smh-my-head/charon" - CheckErr = True ' Assign return value - End If - - ' Default return value is False (so don't bother setting CheckErr = False) -End Function - - -Function diffStatusToString(status As Long) As String - ' Get a string for our gtVolDiffStatusOptionType_e error code - ' Diff status codes are fairly limited in number so we can easily - ' create a switch like this. Creating a similar function for - ' gtError_e would be a fool's errand. - - Select Case status - Case 0 - diffStatusToString = "Succeeded" - Case 1 - diffStatusToString = "Not performed" - Case 2 - diffStatusToString = "Canceled" - Case 3 - diffStatusToString = "Failed" - Case 4 - diffStatusToString = "Identical parts" - Case 5 - diffStatusToString = "Different parts" - Case 6 - diffStatusToString = "No solid body found" - Case 7 - diffStatusToString = "Already saved" - Case Else - diffStatusToString = "Undefined" - End Select - -End Function - - -' ***************************************************************************** -' * COMMAND LINE PARSER * -' ***************************************************************************** -' * Reference: https://forum.solidworks.com/docs/DOC-3861 * -' ***************************************************************************** - -Function GetCommandLine() As String - ' Get a pointer to process commandline as Long - Dim ptrCommandLine As Long - ptrCommandLine = GetCommandLine_long - If ptrCommandLine > 0 Then ' GetCommandLine ok - Dim Buffer As String, BufferLength As Long - ' Get a length of the command line using Long pointer - ' Returns the length without the zero terminating char - BufferLength = lstrlen_long(ptrCommandLine) - - If BufferLength > 0 Then - ' Allocate a space for the command line - Buffer = Space(BufferLength + 1) - - ' Copy the command line to a buffer using Long + String pointers. - ptrCommandLine = lstrcpyn_long_string( _ - Buffer, _ - ptrCommandLine, _ - BufferLength + 1 _ - ) - - ' The copy SHOULD be OK. Zero means some error, not enough of space - If ptrCommandLine > 0 Then ' lstrcpyn - ' Remove the zero terminator from the copied string - Dim PosZero As Long - PosZero = InStr(Buffer, Chr$(0)) - If PosZero > 0 Then Buffer = Left(Buffer, PosZero - 1) - GetCommandLine = Buffer - End If - End If - End If -End Function diff --git a/compare/Marshal2.cs b/compare/Marshal2.cs new file mode 100644 index 0000000..56f0c3a --- /dev/null +++ b/compare/Marshal2.cs @@ -0,0 +1,55 @@ +// https://stackoverflow.com/a/65496277 +using System; +using System.Runtime.InteropServices; +using System.Runtime.Versioning; +using System.Security; + +public static class Marshal2 +{ + internal const String OLEAUT32 = "oleaut32.dll"; + internal const String OLE32 = "ole32.dll"; + + [System.Security.SecurityCritical] // auto-generated_required + public static Object GetActiveObject(String progID) + { + Object obj = null; + Guid clsid; + + // Call CLSIDFromProgIDEx first then fall back on CLSIDFromProgID if + // CLSIDFromProgIDEx doesn't exist. + try + { + CLSIDFromProgIDEx(progID, out clsid); + } + // catch + catch (Exception) + { + CLSIDFromProgID(progID, out clsid); + } + + GetActiveObject(ref clsid, IntPtr.Zero, out obj); + return obj; + } + + //[DllImport(Microsoft.Win32.Win32Native.OLE32, PreserveSig = false)] + [DllImport(OLE32, PreserveSig = false)] + [ResourceExposure(ResourceScope.None)] + [SuppressUnmanagedCodeSecurity] + [System.Security.SecurityCritical] // auto-generated + private static extern void CLSIDFromProgIDEx([MarshalAs(UnmanagedType.LPWStr)] String progId, out Guid clsid); + + //[DllImport(Microsoft.Win32.Win32Native.OLE32, PreserveSig = false)] + [DllImport(OLE32, PreserveSig = false)] + [ResourceExposure(ResourceScope.None)] + [SuppressUnmanagedCodeSecurity] + [System.Security.SecurityCritical] // auto-generated + private static extern void CLSIDFromProgID([MarshalAs(UnmanagedType.LPWStr)] String progId, out Guid clsid); + + //[DllImport(Microsoft.Win32.Win32Native.OLEAUT32, PreserveSig = false)] + [DllImport(OLEAUT32, PreserveSig = false)] + [ResourceExposure(ResourceScope.None)] + [SuppressUnmanagedCodeSecurity] + [System.Security.SecurityCritical] // auto-generated + private static extern void GetActiveObject(ref Guid rclsid, IntPtr reserved, [MarshalAs(UnmanagedType.Interface)] out Object ppunk); + +} diff --git a/compare/Program.cs b/compare/Program.cs new file mode 100644 index 0000000..e67df9a --- /dev/null +++ b/compare/Program.cs @@ -0,0 +1,117 @@ +// ************************************************** +// * SOLIDWORKS ADD-IN OF ETERNAL PAIN * +// * "I'M CRYING BLOOD" - H. FRANKS, SATISFIED USER * +// ************************************************** +// vi: ft=basic +// +// ============================================================================= +// +// Copyright (C) 2021 Tim Clifford, Henry Franks +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . +// +// ============================================================================= + +using System; +using System.IO; +using System.Runtime.InteropServices; + +using SolidWorks.Interop.sldworks; +using SolidWorks.Interop.swconst; +using static Marshal2; + +namespace Charon +{ + class Program + { + static void Main(string[] args) + { + ISldWorks swApp; + + try { + var progId = "SldWorks.Application"; + swApp = Marshal2.GetActiveObject(progId) as ISldWorks; + } catch (COMException e) { + swApp = new SldWorks(); + swApp.Visible = true; + } + + // If file paths aren't correctly specified, warn the user and exit + if (args.Length != 2) { + Console.Error.Write("Incorrect number of arguments"); + return; + } + + string ext = Path.GetExtension(args[0]); + if (ext != Path.GetExtension(args[1])) { + Console.Error.Write("File types don't match"); + return; + } + + int filetype_int; + + switch (ext) { + case ".SLDPRT": + filetype_int = 1; // swDocPART + break; + + case ".SLDASM": + filetype_int = 2; // swDocASSEMBLY + break; + + default: + filetype_int = 0; // swDocNONE + break; + } + + int openDocOptions = 1; // swOpenDocOptions_Silent + int fileError = 0; + int fileWarning = 0; + + // open the second window first to tile properly + swApp.OpenDoc6( + args[1], // FileName + filetype_int, // Type + openDocOptions, // Options + "", // Configuration + ref fileError, // Errors + ref fileWarning // Warnings + ); + if (fileError != 0) { + Console.Error.Write("Encountered error opening file: " + + fileError.ToString()); + } else if (fileWarning != 0) { + Console.WriteLine("Encountered warning opening file: " + + fileWarning.ToString()); + } + swApp.OpenDoc6( + args[0], // FileName + filetype_int, // Type + openDocOptions, // Options + "", // Configuration + ref fileError, // Errors + ref fileWarning // Warnings + ); + if (fileError != 0) { + Console.Error.Write("Encountered error opening file: " + + fileError.ToString()); + } else if (fileWarning != 0) { + Console.WriteLine("Encountered warning opening file: " + + fileWarning.ToString()); + } + // Arrange Windows to vertical tiling + swApp.ArrangeWindows(2); + } + } +} diff --git a/compare/compare.csproj b/compare/compare.csproj new file mode 100644 index 0000000..722c5b0 --- /dev/null +++ b/compare/compare.csproj @@ -0,0 +1,17 @@ + + + + Exe + net5.0 + + + + + C:\Program Files\SOLIDWORKS Corp\SOLIDWORKS\api\redist\SolidWorks.Interop.sldworks.dll + + + C:\Program Files\SOLIDWORKS Corp\SOLIDWORKS\api\redist\SolidWorks.Interop.swconst.dll + + + + diff --git a/keys/hades b/keys/hades new file mode 100644 index 0000000..13daeb2 --- /dev/null +++ b/keys/hades @@ -0,0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s2Q5S2OK4DJ8TexRmKphluqUzKyeYrrkTDKJtSvfG5Pn+PZWhVYo8YKW+FtKzS4EIRU9Dz +PfRV7te0j22tLY4y+KRMNO9n19YCoXeQAAAMEAwSurIbAL4Bp2U+Tu7xWcosNI7kXhDYFj +gAbWCNcxACSWINXt2jhzj7WyE59Zc1+17EaqYQFr3Krbq0Me2Dz0T9BH9T2bigf5Sr4XIu +nPStuhe6qaN0+1nG6xDgMTNiNHCSglNkPI5rpwcQsNzg62nOthhga+PAVo3laaSNBxzrDf +U5uneNzUVJKQbqUKYGARpB8AM0usyPd6Y20SwJv6Xc9OBKcYM2XAsynancdYwAidaKRCp0 +5FcCR+DP66X9A7AAAAEEhhZGVzL0NoYXJvbiBrZXkBAg== +-----END OPENSSH PRIVATE KEY----- diff --git a/keys/hades.pub b/keys/hades.pub new file mode 100644 index 0000000..921cd14 --- /dev/null +++ b/keys/hades.pub @@ -0,0 +1 @@ +ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQChcTMHPrvrZXHWXrjmwpKgmrmqZLzMcDBguC2W8Iy0pQoTzFQDzoWU+1sCztwaDHZw2r4EVGtqdPal1IRxkz7wGYqiTftYpnXn6du2IbhZ1sepZbSo2sfVGY4DkzCpeCERYKqloXgjXBZqlrIdrta4OpaE2P4njsE0nb19gS/jCrlvXNn8cOS/nKBOks4ChqKD3jWSwDPZacS8Msvoss80kxIX80DgYR6bgwr48fpceHRNuzYWIY8pi9rUpqyX8Kfb3/2qoZJh7qwqpyjOYtSZgiqCHCWV5dQIoA3Ahb1SXuqq63O7oPt6K/hxhks5hmnw474gJesSV4UeTTfuneOoMgkbPY8/u1CI1VgznAjbbCK9brrlpZo9aIq7VmmV1qyQHAqd4yLCsp5uNwap9yhQFKvUbpzIED78BfN88SHjk/8rhGyN3gaYL+LyYiyZAUPoOrUCAhOdFnSNGvA7Jwrva2ch59G+64ef3TM/SpIVxsLyms+E33KtFpUEwfysuOM= Hades/Charon key diff --git a/setup.sh b/setup.sh index 81c3237..04b9a92 100755 --- a/setup.sh +++ b/setup.sh @@ -1,6 +1,6 @@ #!/bin/sh -# SolidWorks Git Tools +# Charon # Copyright (C) 2021 Tim Clifford, Henry Franks # # This program is free software: you can redistribute it and/or modify @@ -16,15 +16,30 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . +BASE_DIR="$(realpath "$(dirname "$(realpath $0)")/..")" + +# Download the Charon exe matching this version +cd $BASE_DIR/charon +version="$(git describe --tags --always HEAD)" +if echo $version | grep -q '^v'; then + wget "https://github.com/smh-my-head/charon/releases/download/$version/compare.exe" +else + # just get latest + wget "$(curl --silent \ + "https://api.github.com/repos/smh-my-head/charon/releases/latest" \ + | perl -ne 'print if s/.*"browser_download_url": "(.*?)".*/\1/')" +fi +cd - >/dev/null + # Set cat as the default pager so that we can more easily read/write text git config core.pager cat # Remove existing sld config if it exists and replace it with the latest config header_start="\ -### --------------- BEGIN AUTO ADDED SOLIDWORKS TOOLS --------------------- ###\ +### ---------------------------- BEGIN CHARON ----------------------------- ###\ " header_end="\ -### ---------------- END AUTO ADDED SOLIDWORKS TOOLS ---------------------- ###\ +### ----------------------------- END CHARON ------------------------------ ###\ " setup_config() { # args (header_start, header_end, file to add, file to add to) @@ -37,7 +52,7 @@ $(cat "$3") $2" >> $4 elif cat $4 | grep -q "$1"; then - echo "Found existing solidworks config in $4, replacing..." + echo "Found existing charon config in $4, replacing..." # This is cursed, but at least it's not vba... f="\ $(grep -B9999 "$1" $4 | grep -v "$1") @@ -58,15 +73,15 @@ $2" >> $4 setup_config \ "$header_start" "$header_end" \ - "charon/gitconfig" \ - ".git/config" + "$BASE_DIR/charon/gitconfig" \ + "$BASE_DIR/.git/config" setup_config \ "$header_start" "$header_end" \ - "charon/gitattributes" \ - ".gitattributes" + "$BASE_DIR/charon/gitattributes" \ + "$BASE_DIR/.gitattributes" setup_config \ "$header_start" "$header_end" \ - "charon/gitignore" \ - ".gitignore" + "$BASE_DIR/charon/gitignore" \ + "$BASE_DIR/.gitignore" diff --git a/solidworks_driver.sh b/solidworks_driver.sh index 41d41e1..149367b 100755 --- a/solidworks_driver.sh +++ b/solidworks_driver.sh @@ -20,6 +20,7 @@ # attempt to determine the OS and type of file path +BASE_DIR="$(dirname "$(realpath $0)")" usage() { cat << EOF @@ -27,42 +28,39 @@ Usage: $0 [OPTION]... FILE1 FILE2 Opens a comparison between the two files in SolidWorks. -h, --help display this help and exit - -c, --check-health check if a virtual machine is accessible + -c, --check-health check if a virtual machine satisfies the preconditions If you are using a VM, this will make some assumptions -(which are the defaults of solidworks-vm) +(which are the defaults of hades) 1. if you are using qemu and require the vm to access the host, there is a network drive at //10.0.2.4/qemu which mounts $HOME 2. If you require ssh access to the guest, MSYS2 is accessible at - archuser@localhost:6969 -3. The password of the guest is 'a'. (This should not be a security issue, as - no ports on the guest should be exposed) + hades@localhost:2200 +3. The keys for the guest are at keys/hades and keys/hades.pub EOF } +chmod 600 $BASE_DIR/keys/hades check_vm() { + # Check if there is a VM and it satisfies the conditions Charon + # requires + # check vm is running if ! pgrep qemu >/dev/null 2>&1; then echo "No virtual machine running" exit 1 fi - # check we have sshpass - if ! sshpass -h >/dev/null 2>&1; then - echo "sshpass is required for scripting ssh, please install it" - exit 1 - fi - # check vm is accessible over ssh - if ! sshpass -p 'a' ssh -p 6969 archuser@localhost 'exit 0'; then + if ! ssh -p 2200 hades@localhost -i $BASE_DIR/keys/hades 'exit 0'; then echo "Cannot access VM over ssh" exit 1 fi # check vm has mounted host - if ! sshpass -p 'a' ssh -p 6969 archuser@localhost \ + if ! ssh -p 2200 hades@localhost -i $BASE_DIR/keys/hades \ 'ls //10.0.2.4/qemu >/dev/null' then echo "VM does not appear to have mounted the host on //10.0.2.4/qemu" @@ -78,7 +76,7 @@ check_vm() { echo "when using it on Linux" exit 1 fi - if ! sshpass -p 'a' ssh -p 6969 archuser@localhost \ + if ! ssh -p 2200 hades@localhost -i $BASE_DIR/keys/hades \ ls "$(echo $fpath | sed "s|$HOME|//10.0.2.4/qemu|")" >/dev/null then echo "VM does not appear to have mounted the host at $HOME" @@ -150,7 +148,7 @@ case "$(uname)" in | sed "s|^$HOME|//10.0.2.4/qemu|;s|/|\\\\|g")" right_filename="$(echo "$(realpath "$right_filename")" \ | sed "s|^$HOME|//10.0.2.4/qemu|;s|/|\\\\|g")" - macro_filename="$(echo "$(realpath "charon/compare.swb")" \ + comp_filename="$(echo "$(realpath "$BASE_DIR/charon/compare.exe")" \ | sed "s|^$HOME|//10.0.2.4/qemu|;s|/|\\\\|g")" ;; @@ -162,25 +160,19 @@ case "$(uname)" in # assume the files are already a path accessible to windows left_filename="$( realpath $left_filename | xargs cygpath -w)" right_filename="$(realpath $right_filename | xargs cygpath -w)" - macro_filename="charon/compare.swb" + comp_filename="$( realpath "$BASE_DIR\\charon\\compare.exe" \ + | xargs cygpath -w)" ;; esac if $ssh_required; then # note that MSYS2 replaces - with / # (and / with /c/msys2/ or wherever it's installed) - sshpass -p 'a' ssh -p 6969 archuser@localhost "\ + ssh -p 2200 hades@localhost -i $BASE_DIR/keys/hades "\ schtasks -create -f -tn CharonSolidWorks -tr \ - 'C:\\Program Files\\SOLIDWORKS Corp\\SOLIDWORKS\\SLDWORKS.exe \ - /m $macro_filename /*$left_filename /*$right_filename' \ + '$comp_filename $left_filename $right_filename' \ -sc ONCE -st 00:00 schtasks -run -tn CharonSolidWorks" - read -p "Press Enter to kill SolidWorks and continue" - sshpass -p 'a' ssh -p 6969 archuser@localhost "\ - schtasks -end -tn CharonSolidWorks" else - /c/Program\ Files/SOLIDWORKS\ Corp/SOLIDWORKS/SLDWORKS.exe \ - //m sldworks-git-tools/compare.swb \ - /*$left_filename \ - /*$right_filename + $comp_filename $left_filename $right_filename fi