Skip to content

Commit 3676db8

Browse files
Merge branch 'master' into net5_compat
2 parents 613f0a0 + 2901118 commit 3676db8

30 files changed

+486
-1998
lines changed

ElectronNET.API/Entities/Blob.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
namespace ElectronNET.API.Entities
2+
{
3+
/// <summary>
4+
///
5+
/// </summary>
6+
public class Blob : IPostData
7+
{
8+
/// <summary>
9+
/// The object represents a Blob
10+
/// </summary>
11+
public string Type { get; } = "blob";
12+
13+
/// <summary>
14+
/// The UUID of the Blob being uploaded
15+
/// </summary>
16+
public string BlobUUID { get; set; }
17+
}
18+
}

ElectronNET.API/Entities/BrowserViewConstructorOptions.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,17 @@ public class BrowserViewConstructorOptions
99
/// See BrowserWindow.
1010
/// </summary>
1111
public WebPreferences WebPreferences { get; set; }
12+
13+
/// <summary>
14+
/// A proxy to set on creation in the format host:port.
15+
/// The proxy can be alternatively set using the BrowserView.WebContents.SetProxyAsync function.
16+
/// </summary>
17+
public string Proxy { get; set; }
18+
19+
/// <summary>
20+
/// The credentials of the Proxy in the format username:password.
21+
/// These will only be used if the Proxy field is also set.
22+
/// </summary>
23+
public string ProxyCredentials { get; set; }
1224
}
1325
}

ElectronNET.API/Entities/BrowserWindowOptions.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,5 +258,17 @@ public class BrowserWindowOptions
258258
/// Settings of web page's features.
259259
/// </summary>
260260
public WebPreferences WebPreferences { get; set; }
261+
262+
/// <summary>
263+
/// A proxy to set on creation in the format host:port.
264+
/// The proxy can be alternatively set using the BrowserWindow.WebContents.SetProxyAsync function.
265+
/// </summary>
266+
public string Proxy { get; set; }
267+
268+
/// <summary>
269+
/// The credentials of the Proxy in the format username:password.
270+
/// These will only be used if the Proxy field is also set.
271+
/// </summary>
272+
public string ProxyCredentials { get; set; }
261273
}
262274
}

ElectronNET.API/Entities/IPostData.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
namespace ElectronNET.API.Entities
2+
{
3+
/// <summary>
4+
/// Interface to use Electrons PostData Object
5+
/// </summary>
6+
public interface IPostData
7+
{
8+
/// <summary>
9+
/// One of the following:
10+
/// rawData - <see cref="UploadRawData"/> The data is available as a Buffer, in the rawData field.
11+
/// file - <see cref="UploadFile"/> The object represents a file. The filePath, offset, length and modificationTime fields will be used to describe the file.
12+
/// blob - <see cref="Blob"/> The object represents a Blob. The blobUUID field will be used to describe the Blob.
13+
/// </summary>
14+
public string Type { get; }
15+
}
16+
}

ElectronNET.API/Entities/LoadURLOptions.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,11 @@ public class LoadURLOptions
2626
/// Extra headers for the request.
2727
/// </summary>
2828
public string ExtraHeaders { get; set; }
29+
30+
/// <summary>
31+
/// PostData Object for the request.
32+
/// Can be <see cref="UploadRawData"/>, <see cref="UploadFile"/> or <see cref="Blob"/>
33+
/// </summary>
34+
public IPostData[] PostData { get; set; }
2935
}
3036
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
namespace ElectronNET.API.Entities
2+
{
3+
/// <summary>
4+
///
5+
/// </summary>
6+
public class UploadFile : IPostData
7+
{
8+
/// <summary>
9+
/// The object represents a file.
10+
/// </summary>
11+
public string Type { get; } = "file";
12+
13+
/// <summary>
14+
/// The path of the file being uploaded.
15+
/// </summary>
16+
public string FilePath { get; set; }
17+
18+
/// <summary>
19+
/// The offset from the beginning of the file being uploaded, in bytes. Defaults to 0.
20+
/// </summary>
21+
public long Offset { get; set; } = 0;
22+
23+
/// <summary>
24+
/// The length of the file being uploaded, <see cref="Offset"/>. Defaults to 0.
25+
/// If set to -1, the whole file will be uploaded.
26+
/// </summary>
27+
public long Length { get; set; } = 0;
28+
29+
/// <summary>
30+
/// The modification time of the file represented by a double, which is the number of seconds since the UNIX Epoch (Jan 1, 1970)
31+
/// </summary>
32+
public double ModificationTime { get; set; }
33+
}
34+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
namespace ElectronNET.API.Entities
2+
{
3+
/// <summary>
4+
///
5+
/// </summary>
6+
public class UploadRawData : IPostData
7+
{
8+
/// <summary>
9+
/// The data is available as a Buffer, in the rawData field.
10+
/// </summary>
11+
public string Type { get; } = "rawData";
12+
13+
/// <summary>
14+
/// The raw bytes of the post data in a Buffer.
15+
/// </summary>
16+
public byte[] Bytes { get; set; }
17+
}
18+
}

ElectronNET.API/IpcMain.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,45 @@ public void Send(BrowserWindow browserWindow, string channel, params object[] da
179179
}
180180
}
181181

182+
/// <summary>
183+
/// Send a message to the BrowserView renderer process asynchronously via channel, you can also send
184+
/// arbitrary arguments. Arguments will be serialized in JSON internally and hence
185+
/// no functions or prototype chain will be included. The renderer process handles it by
186+
/// listening for channel with ipcRenderer module.
187+
/// </summary>
188+
/// <param name="browserView">BrowserView with channel.</param>
189+
/// <param name="channel">Channelname.</param>
190+
/// <param name="data">Arguments data.</param>
191+
public void Send(BrowserView browserView, string channel, params object[] data)
192+
{
193+
List<JObject> jobjects = new List<JObject>();
194+
List<JArray> jarrays = new List<JArray>();
195+
List<object> objects = new List<object>();
196+
197+
foreach (var parameterObject in data)
198+
{
199+
if(parameterObject.GetType().IsArray || parameterObject.GetType().IsGenericType && parameterObject is IEnumerable)
200+
{
201+
jarrays.Add(JArray.FromObject(parameterObject, _jsonSerializer));
202+
} else if(parameterObject.GetType().IsClass && !parameterObject.GetType().IsPrimitive && !(parameterObject is string))
203+
{
204+
jobjects.Add(JObject.FromObject(parameterObject, _jsonSerializer));
205+
} else if(parameterObject.GetType().IsPrimitive || (parameterObject is string))
206+
{
207+
objects.Add(parameterObject);
208+
}
209+
}
210+
211+
if(jobjects.Count > 0 || jarrays.Count > 0)
212+
{
213+
BridgeConnector.Socket.Emit("sendToIpcRendererBrowserView", browserView.Id, channel, jarrays.ToArray(), jobjects.ToArray(), objects.ToArray());
214+
}
215+
else
216+
{
217+
BridgeConnector.Socket.Emit("sendToIpcRendererBrowserView", browserView.Id, channel, data);
218+
}
219+
}
220+
182221
private JsonSerializer _jsonSerializer = new JsonSerializer()
183222
{
184223
ContractResolver = new CamelCasePropertyNamesContractResolver(),

ElectronNET.API/WebContents.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,18 @@ public Task LoadURLAsync(string url, LoadURLOptions options)
258258
return taskCompletionSource.Task;
259259
}
260260

261+
/// <summary>
262+
/// Inserts CSS into the web page.
263+
/// See: https://www.electronjs.org/docs/api/web-contents#contentsinsertcsscss-options
264+
/// Works for both BrowserWindows and BrowserViews.
265+
/// </summary>
266+
/// <param name="isBrowserWindow">Whether the webContents belong to a BrowserWindow or not (the other option is a BrowserView)</param>
267+
/// <param name="path">Absolute path to the CSS file location</param>
268+
public void InsertCSS(bool isBrowserWindow, string path)
269+
{
270+
BridgeConnector.Socket.Emit("webContents-insertCSS", Id, isBrowserWindow, path);
271+
}
272+
261273
private JsonSerializer _jsonSerializer = new JsonSerializer()
262274
{
263275
ContractResolver = new CamelCasePropertyNamesContractResolver(),

ElectronNET.CLI/Commands/BuildCommand.cs

Lines changed: 60 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ public class BuildCommand : ICommand
2121
"Optional: '/absolute-path to specify and absolute path for output." + Environment.NewLine +
2222
"Optional: '/package-json' to specify a custom package.json file." + Environment.NewLine +
2323
"Optional: '/install-modules' to force node module install. Implied by '/package-json'" + Environment.NewLine +
24+
"Optional: '/Version' to specify the version that should be applied to both the `dotnet publish` and `electron-builder` commands. Implied by '/Version'" + Environment.NewLine +
25+
"Optional: '/p:[property]' or '/property:[property]' to pass in dotnet publish properties. Example: '/property:Version=1.0.0' to override the FileVersion" + Environment.NewLine +
2426
"Full example for a 32bit debug build with electron prune: build /target custom win7-x86;win32 /dotnet-configuration Debug /electron-arch ia32 /electron-params \"--prune=true \"";
2527

2628
public static IList<CommandOption> CommandOptions { get; set; } = new List<CommandOption>();
@@ -43,6 +45,7 @@ public BuildCommand(string[] args)
4345
private string _manifest = "manifest";
4446
private string _paramPublishReadyToRun = "PublishReadyToRun";
4547
private string _paramPublishSingleFile = "PublishSingleFile";
48+
private string _paramVersion = "Version";
4649

4750
public Task<bool> ExecuteAsync()
4851
{
@@ -53,6 +56,11 @@ public Task<bool> ExecuteAsync()
5356
SimpleCommandLineParser parser = new SimpleCommandLineParser();
5457
parser.Parse(_args);
5558

59+
//This version will be shared between the dotnet publish and electron-builder commands
60+
string version = null;
61+
if (parser.Arguments.ContainsKey(_paramVersion))
62+
version = parser.Arguments[_paramVersion][0];
63+
5664
if (!parser.Arguments.ContainsKey(_paramTarget))
5765
{
5866
Console.WriteLine($"Error: missing '{_paramTarget}' argument.");
@@ -95,28 +103,18 @@ public Task<bool> ExecuteAsync()
95103
string tempBinPath = Path.Combine(tempPath, "bin");
96104

97105
Console.WriteLine($"Build ASP.NET Core App for {platformInfo.NetCorePublishRid} under {configuration}-Configuration...");
106+
107+
var dotNetPublishFlags = GetDotNetPublishFlags(parser);
98108

99-
string publishReadyToRun = "/p:PublishReadyToRun=";
100-
if (parser.Arguments.ContainsKey(_paramPublishReadyToRun))
101-
{
102-
publishReadyToRun += parser.Arguments[_paramPublishReadyToRun][0];
103-
}
104-
else
105-
{
106-
publishReadyToRun += "true";
107-
}
108-
109-
string publishSingleFile = "/p:PublishSingleFile=";
110-
if (parser.Arguments.ContainsKey(_paramPublishSingleFile))
111-
{
112-
publishSingleFile += parser.Arguments[_paramPublishSingleFile][0];
113-
}
114-
else
115-
{
116-
publishSingleFile += "false";
117-
}
109+
var command =
110+
$"dotnet publish -r {platformInfo.NetCorePublishRid} -c \"{configuration}\" --output \"{tempBinPath}\" {string.Join(' ', dotNetPublishFlags.Select(kvp => $"{kvp.Key}={kvp.Value}"))} --self-contained";
111+
112+
// output the command
113+
Console.ForegroundColor = ConsoleColor.Green;
114+
Console.WriteLine(command);
115+
Console.ResetColor();
118116

119-
var resultCode = ProcessHelper.CmdExecute($"dotnet publish -r {platformInfo.NetCorePublishRid} -c \"{configuration}\" --output \"{tempBinPath}\" {publishReadyToRun} {publishSingleFile} --self-contained", Directory.GetCurrentDirectory());
117+
var resultCode = ProcessHelper.CmdExecute(command, Directory.GetCurrentDirectory());
120118

121119
if (resultCode != 0)
122120
{
@@ -194,7 +192,10 @@ public Task<bool> ExecuteAsync()
194192
manifestFileName = parser.Arguments[_manifest].First();
195193
}
196194

197-
ProcessHelper.CmdExecute($"node build-helper.js " + manifestFileName, tempPath);
195+
ProcessHelper.CmdExecute(
196+
string.IsNullOrWhiteSpace(version)
197+
? $"node build-helper.js {manifestFileName}"
198+
: $"node build-helper.js {manifestFileName} {version}", tempPath);
198199

199200
Console.WriteLine($"Package Electron App for Platform {platformInfo.ElectronPackerPlatform}...");
200201
ProcessHelper.CmdExecute($"npx electron-builder --config=./bin/electron-builder.json --{platformInfo.ElectronPackerPlatform} --{electronArch} -c.electronVersion=11.1.1 {electronParams}", tempPath);
@@ -204,5 +205,43 @@ public Task<bool> ExecuteAsync()
204205
return true;
205206
});
206207
}
208+
209+
private Dictionary<string, string> GetDotNetPublishFlags(SimpleCommandLineParser parser)
210+
{
211+
var dotNetPublishFlags = new Dictionary<string, string>
212+
{
213+
{"/p:PublishReadyToRun", parser.TryGet(_paramPublishReadyToRun, out var rtr) ? rtr[0] : "true"},
214+
{"/p:PublishSingleFile", parser.TryGet(_paramPublishSingleFile, out var psf) ? psf[0] : "true"},
215+
};
216+
217+
foreach (var parm in parser.Arguments.Keys.Where(key => key.StartsWith("p:") || key.StartsWith("property:")))
218+
{
219+
var split = parm.IndexOf('=');
220+
if (split < 0)
221+
{
222+
continue;
223+
}
224+
225+
var key = $"/{parm.Substring(0, split)}";
226+
// normalize the key
227+
if (key.StartsWith("/property:"))
228+
{
229+
key = key.Replace("/property:", "/p:");
230+
}
231+
232+
var value = parm.Substring(split + 1);
233+
234+
if (dotNetPublishFlags.ContainsKey(key))
235+
{
236+
dotNetPublishFlags[key] = value;
237+
}
238+
else
239+
{
240+
dotNetPublishFlags.Add(key, value);
241+
}
242+
}
243+
244+
return dotNetPublishFlags;
245+
}
207246
}
208247
}

0 commit comments

Comments
 (0)