Skip to content

Commit 28d5dac

Browse files
author
Vladimir Kotikov
committed
Add logic to enable package debugging mode on Windows
1 parent 983c6bc commit 28d5dac

File tree

5 files changed

+202
-3
lines changed

5 files changed

+202
-3
lines changed

debug-mode-plugin/plugin.xml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
Licensed to the Apache Software Foundation (ASF) under one
4+
or more contributor license agreements. See the NOTICE file
5+
distributed with this work for additional information
6+
regarding copyright ownership. The ASF licenses this file
7+
to you under the Apache License, Version 2.0 (the
8+
"License"); you may not use this file except in compliance
9+
with the License. You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing,
14+
software distributed under the License is distributed on an
15+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
KIND, either express or implied. See the License for the
17+
specific language governing permissions and limitations
18+
under the License.
19+
-->
20+
21+
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
22+
id="debug-mode-plugin"
23+
version="1.0.0-dev">
24+
25+
<name>Windows debug mode plugin</name>
26+
<description>Plugin to enable package debugging to allow app run without active local/remote desktop session</description>
27+
<license>Apache 2.0</license>
28+
29+
<platform name="windows">
30+
<hook type="after_plugin_install" src="scripts/enable-debug-mode.js" />
31+
<hook type="before_plugin_uninstall" src="scripts/disable-debug-mode.js" />
32+
</platform>
33+
34+
</plugin>
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<#
2+
Licensed to the Apache Software Foundation (ASF) under one
3+
or more contributor license agreements. See the NOTICE file
4+
distributed with this work for additional information
5+
regarding copyright ownership. The ASF licenses this file
6+
to you under the Apache License, Version 2.0 (the
7+
"License"); you may not use this file except in compliance
8+
with the License. You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing,
13+
software distributed under the License is distributed on an
14+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
KIND, either express or implied. See the License for the
16+
specific language governing permissions and limitations
17+
under the License.
18+
#>
19+
20+
param(
21+
[Parameter(Mandatory=$true, Position=0, ValueFromPipelineByPropertyName=$true)]
22+
[string] $ID <# package.appxmanifest//Identity@name #>
23+
)
24+
25+
$code = @"
26+
using System;
27+
using System.Runtime.InteropServices;
28+
namespace PackageDebug
29+
{
30+
public enum PACKAGE_EXECUTION_STATE
31+
{
32+
PES_UNKNOWN,
33+
PES_RUNNING,
34+
PES_SUSPENDING,
35+
PES_SUSPENDED,
36+
PES_TERMINATED
37+
}
38+
39+
[ComImport, Guid("B1AEC16F-2383-4852-B0E9-8F0B1DC66B4D")]
40+
public class PackageDebugSettings
41+
{
42+
}
43+
44+
[ComImport, Guid("F27C3930-8029-4AD1-94E3-3DBA417810C1"),InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
45+
public interface IPackageDebugSettings
46+
{
47+
int EnableDebugging([MarshalAs(UnmanagedType.LPWStr)] string packageFullName, [MarshalAs(UnmanagedType.LPWStr)] string debuggerCommandLine, IntPtr environment);
48+
int DisableDebugging([MarshalAs(UnmanagedType.LPWStr)] string packageFullName);
49+
int Suspend([MarshalAs(UnmanagedType.LPWStr)] string packageFullName);
50+
int Resume([MarshalAs(UnmanagedType.LPWStr)] string packageFullName);
51+
int TerminateAllProcesses([MarshalAs(UnmanagedType.LPWStr)] string packageFullName);
52+
int SetTargetSessionId(int sessionId);
53+
int EnumerageBackgroundTasks([MarshalAs(UnmanagedType.LPWStr)] string packageFullName,
54+
out uint taskCount, out int intPtr, [Out] string[] array);
55+
int ActivateBackgroundTask(IntPtr something);
56+
int StartServicing([MarshalAs(UnmanagedType.LPWStr)] string packageFullName);
57+
int StopServicing([MarshalAs(UnmanagedType.LPWStr)] string packageFullName);
58+
int StartSessionRedirection([MarshalAs(UnmanagedType.LPWStr)] string packageFullName, uint sessionId);
59+
int StopSessionRedirection([MarshalAs(UnmanagedType.LPWStr)] string packageFullName);
60+
int GetPackageExecutionState([MarshalAs(UnmanagedType.LPWStr)] string packageFullName,
61+
out PACKAGE_EXECUTION_STATE packageExecutionState);
62+
int RegisterForPackageStateChanges([MarshalAs(UnmanagedType.LPWStr)] string packageFullName,
63+
IntPtr pPackageExecutionStateChangeNotification, out uint pdwCookie);
64+
int UnregisterForPackageStateChanges(uint dwCookie);
65+
}
66+
67+
public class DebugTool
68+
{
69+
public static void EnableDebug(String packageFullName)
70+
{
71+
// Set debug mode for App and activate installed application
72+
var debugSettings = (IPackageDebugSettings)(new PackageDebugSettings());
73+
debugSettings.EnableDebugging(packageFullName, null, (IntPtr)null);
74+
}
75+
}
76+
}
77+
"@
78+
79+
Add-Type -TypeDefinition $code
80+
81+
$packageFullName = $(Get-AppxPackage $ID).PackageFullName
82+
Write-Host "Setting debug mode for application:" $ID
83+
[PackageDebug.DebugTool]::EnableDebug($packageFullName) | Out-Null
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
Licensed to the Apache Software Foundation (ASF) under one
3+
or more contributor license agreements. See the NOTICE file
4+
distributed with this work for additional information
5+
regarding copyright ownership. The ASF licenses this file
6+
to you under the Apache License, Version 2.0 (the
7+
"License"); you may not use this file except in compliance
8+
with the License. You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing,
13+
software distributed under the License is distributed on an
14+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
KIND, either express or implied. See the License for the
16+
specific language governing permissions and limitations
17+
under the License.
18+
*/
19+
20+
module.exports = function (context) {
21+
var path = require('path');
22+
var shell = context.requireCordovaModule('shelljs');
23+
24+
var libPath = path.resolve(context.opts.projectRoot, "platforms/windows/cordova/lib");
25+
var appUtilsPath = path.join(libPath, "WindowsStoreAppUtils.ps1");
26+
var appUtilsBackupPath = path.join(libPath, "WindowsStoreAppUtils.ps1.bak");
27+
var destScriptPath = path.join(libPath, "EnableDebuggingForPackage.ps1");
28+
29+
// Remove the patch and copu over backup StoreAppUtils script
30+
shell.rm("-f", destScriptPath);
31+
shell.cp("-f", appUtilsBackupPath, appUtilsPath);
32+
};
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
Licensed to the Apache Software Foundation (ASF) under one
3+
or more contributor license agreements. See the NOTICE file
4+
distributed with this work for additional information
5+
regarding copyright ownership. The ASF licenses this file
6+
to you under the Apache License, Version 2.0 (the
7+
"License"); you may not use this file except in compliance
8+
with the License. You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing,
13+
software distributed under the License is distributed on an
14+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
KIND, either express or implied. See the License for the
16+
specific language governing permissions and limitations
17+
under the License.
18+
*/
19+
20+
module.exports = function (context) {
21+
var path = require('path');
22+
var shell = context.requireCordovaModule('shelljs');
23+
24+
var libPath = path.resolve(context.opts.projectRoot, "platforms/windows/cordova/lib");
25+
var appUtilsPath = path.join(libPath, "WindowsStoreAppUtils.ps1");
26+
var appUtilsBackupPath = path.join(libPath, "WindowsStoreAppUtils.ps1.bak");
27+
var srcScriptPath = path.join(__dirname, "EnableDebuggingForPackage.ps1");
28+
var destScriptPath = path.join(libPath, "EnableDebuggingForPackage.ps1");
29+
30+
// copy over the patch
31+
shell.cp("-f", srcScriptPath, libPath);
32+
shell.cp("-f", appUtilsPath, appUtilsBackupPath);
33+
34+
// add extra code to patch
35+
shell.sed(
36+
"-i",
37+
/^\s*\$appActivator .*$/gim,
38+
"$&\n\n" +
39+
" # START ENABLE DEBUG MODE SECTION\n" +
40+
" powershell " + destScriptPath + " $$ID\n" +
41+
" $Ole32 = Add-Type -MemberDefinition '[DllImport(\"Ole32.dll\")]public static extern int CoAllowSetForegroundWindow(IntPtr pUnk, IntPtr lpvReserved);' -Name 'Ole32' -Namespace 'Win32' -PassThru\n" +
42+
" $Ole32::CoAllowSetForegroundWindow([System.Runtime.InteropServices.Marshal]::GetIUnknownForObject($appActivator), [System.IntPtr]::Zero)\n" +
43+
" # END ENABLE DEBUG MODE SECTION\n",
44+
appUtilsPath
45+
);
46+
};

lib/paramedic.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,13 @@ ParamedicRunner.prototype.installPlugins = function () {
109109
this.pluginsManager = new PluginsManager(this.tempFolder.name, this.storedCWD);
110110
this.pluginsManager.installPlugins(this.config.getPlugins());
111111
this.pluginsManager.installTestsForExistingPlugins();
112-
this.pluginsManager.installSinglePlugin('cordova-plugin-test-framework');
113-
this.pluginsManager.installSinglePlugin('cordova-plugin-device');
114-
this.pluginsManager.installSinglePlugin(path.join(__dirname, '../paramedic-plugin'));
112+
113+
var additionalPlugins = ['cordova-plugin-test-framework', 'cordova-plugin-device', path.join(__dirname, '../paramedic-plugin')];
114+
if (this.config.getPlatformId() === 'windows') {
115+
additionalPlugins.push(path.join(__dirname, '../debug-mode-plugin'));
116+
}
117+
118+
this.pluginsManager.installPlugins(additionalPlugins);
115119
};
116120

117121
ParamedicRunner.prototype.setUpStartPage = function () {

0 commit comments

Comments
 (0)