This commit is contained in:
Robert Muehsig
2017-10-15 00:08:15 +02:00
parent d099363fb9
commit f3050b9dc4
9 changed files with 122 additions and 14 deletions

View File

@@ -51,16 +51,16 @@ namespace ElectronNET.CLI.Commands
Directory.CreateDirectory(tempPath);
}
EmbeddedFileHelper.DeployEmbeddedFile(tempPath, "main.js");
EmbeddedFileHelper.DeployEmbeddedFile(tempPath, "package.json");
EmbeddedFileHelper.DeployEmbeddedFile(tempPath, "package-lock.json");
EmbeddedFileHelper.DeployEmbeddedFile(tempPath, "main.js", "ElectronHost.");
EmbeddedFileHelper.DeployEmbeddedFile(tempPath, "package.json", "ElectronHost.");
EmbeddedFileHelper.DeployEmbeddedFile(tempPath, "package-lock.json", "ElectronHost.");
string hostApiFolder = Path.Combine(tempPath, "api");
if (Directory.Exists(hostApiFolder) == false)
{
Directory.CreateDirectory(hostApiFolder);
}
EmbeddedFileHelper.DeployEmbeddedFile(hostApiFolder, "ipc.js", "api.");
EmbeddedFileHelper.DeployEmbeddedFile(hostApiFolder, "ipc.js", "ElectronHost.api.");
Console.WriteLine("Start npm install...");
ProcessHelper.CmdExecute("npm install", tempPath);

View File

@@ -0,0 +1,93 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace ElectronNET.CLI.Commands
{
public class InitCommand : ICommand
{
public const string COMMAND_NAME = "init";
public const string COMMAND_DESCRIPTION = "Creates the needed Electron.NET config for your Electron Application.";
public const string COMMAND_ARGUMENTS = "<Path> from ASP.NET Core Project.";
public static IList<CommandOption> CommandOptions { get; set; } = new List<CommandOption>();
private const string ConfigName = "electronnet.json";
public Task<bool> ExecuteAsync()
{
return Task.Run(() =>
{
var currentDirectory = Directory.GetCurrentDirectory();
Console.WriteLine("Adding our config file to your project...");
var targetFilePath = Path.Combine(currentDirectory, ConfigName);
if (File.Exists(targetFilePath))
{
Console.WriteLine("Config file already in your project.");
return false;
}
// Deploy config file
EmbeddedFileHelper.DeployEmbeddedFile(currentDirectory, ConfigName);
// search .csproj
Console.WriteLine($"Search your .csproj to add the needed {ConfigName}...");
var projectFile = Directory.EnumerateFiles(currentDirectory, "*.csproj", SearchOption.TopDirectoryOnly).FirstOrDefault();
// update config file with the name of the csproj
// ToDo: If the csproj name != application name, this will fail
string text = File.ReadAllText(targetFilePath);
text = text.Replace("{{executable}}", Path.GetFileNameWithoutExtension(projectFile));
File.WriteAllText(targetFilePath, text);
Console.WriteLine($"Found your .csproj: {projectFile} - check for existing config or update it.");
using (var stream = File.Open(projectFile, FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
var xmlDocument = XDocument.Load(stream);
var projectElement = xmlDocument.Descendants("Project").FirstOrDefault();
if (projectElement == null || projectElement.Attribute("Sdk")?.Value != "Microsoft.NET.Sdk.Web")
{
Console.WriteLine($"Project file is not a compatible type of 'Microsoft.NET.Sdk.Web'. Your project: {projectElement?.Attribute("Sdk")?.Value}");
return false;
}
if (xmlDocument.ToString().Contains($"Content Update=\"{ConfigName}\""))
{
Console.WriteLine($"{ConfigName} already in csproj.");
return false;
}
Console.WriteLine($"{ConfigName} will be added to csproj.");
string itemGroupXmlString = "<ItemGroup>" +
"<Content Update=\"" + ConfigName + "\">" +
"<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>" +
"</Content>" +
"</ItemGroup>";
var newItemGroupForConfig = XElement.Parse(itemGroupXmlString);
xmlDocument.Root.Add(newItemGroupForConfig);
stream.SetLength(0);
stream.Position = 0;
xmlDocument.Save(stream);
Console.WriteLine($"{ConfigName} added in csproj - happy electronizing!");
}
return true;
});
}
}
}

View File

@@ -49,16 +49,16 @@ namespace ElectronNET.CLI.Commands
string tempBinPath = Path.Combine(tempPath, "bin");
ProcessHelper.CmdExecute($"dotnet publish -r win10-x64 --output \"{tempBinPath}\"", aspCoreProjectPath);
EmbeddedFileHelper.DeployEmbeddedFile(tempPath, "main.js");
EmbeddedFileHelper.DeployEmbeddedFile(tempPath, "package.json");
EmbeddedFileHelper.DeployEmbeddedFile(tempPath, "package-lock.json");
EmbeddedFileHelper.DeployEmbeddedFile(tempPath, "main.js", "ElectronHost.");
EmbeddedFileHelper.DeployEmbeddedFile(tempPath, "package.json", "ElectronHost.");
EmbeddedFileHelper.DeployEmbeddedFile(tempPath, "package-lock.json", "ElectronHost.");
string hostApiFolder = Path.Combine(tempPath, "api");
if (Directory.Exists(hostApiFolder) == false)
{
Directory.CreateDirectory(hostApiFolder);
}
EmbeddedFileHelper.DeployEmbeddedFile(hostApiFolder, "ipc.js", "api.");
EmbeddedFileHelper.DeployEmbeddedFile(hostApiFolder, "ipc.js", "ElectronHost.api.");
Console.WriteLine("Start npm install...");
ProcessHelper.CmdExecute("npm install", tempPath);

View File

@@ -15,6 +15,7 @@
<ItemGroup>
<None Remove="ElectronHost\package-lock.json" />
<None Remove="ElectronHost\package.json" />
<None Remove="electronnet.json" />
</ItemGroup>
<ItemGroup>
@@ -24,6 +25,7 @@
<ItemGroup>
<EmbeddedResource Include="..\ElectronNET.Host\main.js" Link="ElectronHost\main.js" />
<EmbeddedResource Include="electronnet.json" />
</ItemGroup>
<ItemGroup>

View File

@@ -19,7 +19,7 @@ namespace ElectronNET.CLI
{
using (var fileStream = File.Create(Path.Combine(targetPath, file)))
{
var streamFromEmbeddedFile = GetTestResourceFileStream("ElectronHost." + namespacePath + file);
var streamFromEmbeddedFile = GetTestResourceFileStream(namespacePath + file);
streamFromEmbeddedFile.CopyTo(fileStream);
}
}

View File

@@ -4,9 +4,11 @@ using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Xml.Linq;
namespace ElectronNET.CLI
{
class Program
{
static void Main(string[] args)
@@ -28,6 +30,9 @@ namespace ElectronNET.CLI
case BuildCommand.COMMAND_NAME:
command = new BuildCommand();
break;
case InitCommand.COMMAND_NAME:
command = new InitCommand();
break;
case "--help":
case "--h":
case "help":

View File

@@ -0,0 +1,3 @@
{
"executable": "{{executable}}"
}

View File

@@ -1,14 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<RuntimeIdentifiers>win10-x64</RuntimeIdentifiers>
</PropertyGroup>
<ItemGroup>
<Folder Include="wwwroot\" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="ElectronNET.API" Version="1.0.0" />
<PackageReference Include="Microsoft.AspNetCore" Version="2.0.0" />
@@ -20,8 +18,12 @@
<PackageReference Include="Microsoft.VisualStudio.Web.BrowserLink" Version="2.0.0" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.0.0" />
</ItemGroup>
<ItemGroup>
<DotNetCliToolReference Include="ElectronNET.CLI" Version="*" />
</ItemGroup>
</Project>
<ItemGroup>
<Content Update="electronnet.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
</Project>

View File

@@ -0,0 +1,3 @@
{
"executable": "ElectronNET.WebApp"
}