Skip to content
This repository has been archived by the owner on Sep 4, 2024. It is now read-only.

Try avoiding emitting assembly to run setup process - rebased #170

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions Mono.Addins.SetupProcess/Mono.Addins.SetupProcess.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\TargetFrameworks.props" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>8.0.30703</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{303D2716-94AA-4067-8111-F478D26DFAFE}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>Mono.Addins.SetupProcess</RootNamespace>
<AssemblyName>Mono.Addins.SetupProcess</AssemblyName>
<SignAssembly>True</SignAssembly>
<AssemblyOriginatorKeyFile>..\mono-addins.snk</AssemblyOriginatorKeyFile>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>..\bin</OutputPath>
<DefineConstants>DEBUG;</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<ExternalConsole>true</ExternalConsole>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<Optimize>true</Optimize>
<OutputPath>..\bin</OutputPath>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<ExternalConsole>true</ExternalConsole>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
</ItemGroup>
<ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SetupProcessTool.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Mono.Addins\Mono.Addins.csproj">
<Project>{91DD5A2D-9FE3-4C3C-9253-876141874DAD}</Project>
<Name>Mono.Addins</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
</Project>
20 changes: 20 additions & 0 deletions Mono.Addins.SetupProcess/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.Reflection;
using System.Runtime.CompilerServices;

// Information about this assembly is defined by the following
// attributes.
//
// change them to the information which is associated with the assembly
// you compile.

[assembly: AssemblyTitle("Mono.Addins.SetupProcess")]
[assembly: AssemblyCopyright("Copyright (C) 2007 Novell, Inc (http://www.novell.com)")]

// The assembly version has following format :
//
// Major.Minor.Build.Revision
//
// You can specify all values by your own or you can build default build and revision
// numbers with the '*' character (the default):

[assembly: AssemblyVersion("1.0.0.0")]
75 changes: 75 additions & 0 deletions Mono.Addins.SetupProcess/SetupProcessTool.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
//
// SetupProcessTool.cs
//
// Author:
// Marius Ungureanu <[email protected]>
//
// Copyright (c) 2019 Microsoft Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using Mono.Addins.Database;

namespace Mono.Addins.SetupProcess
{
class SetupProcessTool
{
public static int Main (string [] args)
{
ProcessProgressStatus monitor = new ProcessProgressStatus (int.Parse (args [0]));

try {
string registryPath = Console.In.ReadLine ();
string startupDir = Console.In.ReadLine ();
string addinsDir = Console.In.ReadLine ();
string databaseDir = Console.In.ReadLine ();

AddinDatabase.RunningSetupProcess = true;
AddinRegistry reg = new AddinRegistry (registryPath, startupDir, addinsDir, databaseDir);

switch (args [1]) {
case "scan": {
string folder = args.Length > 2 ? args [2] : null;
if (folder.Length == 0) folder = null;

var context = new ScanOptions ();
context.Read (Console.In);
reg.ScanFolders (monitor, folder, context);
break;
}
case "pre-scan": {
string folder = args.Length > 2 ? args [2] : null;
if (folder.Length == 0) folder = null;
var recursive = bool.Parse (Console.In.ReadLine ());
reg.GenerateScanDataFilesInProcess (monitor, folder, recursive);
break;
}
case "get-desc":
var outFile = Console.In.ReadLine ();
reg.ParseAddin (monitor, args [2], outFile);
break;
}
} catch (Exception ex) {
monitor.ReportError ("Unexpected error in setup process", ex);
return 1;
}
return 0;
}
}
}
12 changes: 12 additions & 0 deletions Mono.Addins.sln
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Mono.Addins.MSBuild", "Mono
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Mono.Addins.GuiGtk3", "Mono.Addins.GuiGtk3\Mono.Addins.GuiGtk3.csproj", "{410A7DC9-E7DA-43E6-B592-93E2A344B660}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Mono.Addins.SetupProcess", "Mono.Addins.SetupProcess\Mono.Addins.SetupProcess.csproj", "{303D2716-94AA-4067-8111-F478D26DFAFE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
DebugWin32|Any CPU = DebugWin32|Any CPU
Expand Down Expand Up @@ -209,6 +211,16 @@ Global
{FEC19BDA-4904-4005-8C09-68E82E8BEF6A}.DebugWin32|Any CPU.Build.0 = Debug|Any CPU
{FEC19BDA-4904-4005-8C09-68E82E8BEF6A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FEC19BDA-4904-4005-8C09-68E82E8BEF6A}.Release|Any CPU.Build.0 = Release|Any CPU
{303D2716-94AA-4067-8111-F478D26DFAFE}.DebugWin32|Any CPU.ActiveCfg = Debug|Any CPU
{303D2716-94AA-4067-8111-F478D26DFAFE}.DebugWin32|Any CPU.Build.0 = Debug|Any CPU
{303D2716-94AA-4067-8111-F478D26DFAFE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{303D2716-94AA-4067-8111-F478D26DFAFE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{303D2716-94AA-4067-8111-F478D26DFAFE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{303D2716-94AA-4067-8111-F478D26DFAFE}.Release|Any CPU.Build.0 = Release|Any CPU
{303D2716-94AA-4067-8111-F478D26DFAFE}.DebugNoGui|Any CPU.ActiveCfg = Debug|Any CPU
{303D2716-94AA-4067-8111-F478D26DFAFE}.DebugNoGui|Any CPU.Build.0 = Debug|Any CPU
{303D2716-94AA-4067-8111-F478D26DFAFE}.DebugGtk3|Any CPU.ActiveCfg = Debug|Any CPU
{303D2716-94AA-4067-8111-F478D26DFAFE}.DebugGtk3|Any CPU.Build.0 = Debug|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{F109148D-849E-4044-8700-5E8EA0AB2476} = {7EFC0684-310E-417D-B8BD-5584C3F34BD5}
Expand Down
4 changes: 4 additions & 0 deletions Mono.Addins/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
"0400000100010079159977d2d03a8e6bea7a2e74e8d1afcc93e8851974952bb480a12c9134474d04062447c37e0e68c080536fcf3c" +
"3fbe2ff9c979ce998475e506e8ce82dd5b0f350dc10e93bf2eeecf874b24770c5081dbea7447fddafa277b22de47d6ffea449674a4" +
"f9fccf84d15069089380284dbdd35f46cdff12a1bd78e4ef0065d016df")]
[assembly: InternalsVisibleTo ("Mono.Addins.SetupProcess, PublicKey=00240000048000009400000006020000002400005253413100" +
"0400000100010079159977d2d03a8e6bea7a2e74e8d1afcc93e8851974952bb480a12c9134474d04062447c37e0e68c080536fcf3c" +
"3fbe2ff9c979ce998475e506e8ce82dd5b0f350dc10e93bf2eeecf874b24770c5081dbea7447fddafa277b22de47d6ffea449674a4" +
"f9fccf84d15069089380284dbdd35f46cdff12a1bd78e4ef0065d016df")]
[assembly: InternalsVisibleTo("UnitTests, PublicKey=00240000048000009400000006020000002400005253413100" +
"0400000100010079159977d2d03a8e6bea7a2e74e8d1afcc93e8851974952bb480a12c9134474d04062447c37e0e68c080536fcf3c" +
"3fbe2ff9c979ce998475e506e8ce82dd5b0f350dc10e93bf2eeecf874b24770c5081dbea7447fddafa277b22de47d6ffea449674a4" +
Expand Down
94 changes: 8 additions & 86 deletions Mono.Addins/Mono.Addins.Database/SetupProcess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,15 @@ internal static void ExecuteCommand (IProgressStatus monitor, string registryPat
sb.Append (" \"").Append (arg1).Append ("\"");

Process process = new Process ();

string asm = null;

string thisAsmDir = Path.GetDirectoryName (typeof (SetupProcess).Assembly.Location);
string asm;
if (Util.IsMono)
asm = Path.Combine (thisAsmDir, "Mono.Addins.SetupProcess.exe");
else
asm = Path.Combine (thisAsmDir, "Mono.Addins.SetupProcess.dll");

try {
asm = CreateHostExe ();
if (!Util.IsMono)
process.StartInfo = new ProcessStartInfo (asm, sb.ToString ());
else {
Expand Down Expand Up @@ -107,91 +112,8 @@ internal static void ExecuteCommand (IProgressStatus monitor, string registryPat
} catch (Exception ex) {
Console.WriteLine (ex);
throw;
} finally {
if (asm != null) {
try {
File.Delete (asm);
} catch { }
}
}
}

public static int Main (string [] args)
{
ProcessProgressStatus monitor = new ProcessProgressStatus (int.Parse (args [0]));

try {
string registryPath = Console.In.ReadLine ();
string startupDir = Console.In.ReadLine ();
string addinsDir = Console.In.ReadLine ();
string databaseDir = Console.In.ReadLine ();

AddinDatabase.RunningSetupProcess = true;
AddinRegistry reg = new AddinRegistry (registryPath, startupDir, addinsDir, databaseDir);

switch (args [1]) {
case "scan": {
string folder = args.Length > 2 ? args [2] : null;
if (folder.Length == 0) folder = null;

var context = new ScanOptions ();
context.Read (Console.In);
reg.ScanFolders (monitor, folder, context);
break;
}
case "pre-scan": {
string folder = args.Length > 2 ? args [2] : null;
if (folder.Length == 0) folder = null;
var recursive = bool.Parse (Console.In.ReadLine ());
reg.GenerateScanDataFilesInProcess (monitor, folder, recursive);
break;
}
case "get-desc":
var outFile = Console.In.ReadLine ();
reg.ParseAddin (monitor, args [2], args [3]);
break;
}
} catch (Exception ex) {
monitor.ReportError ("Unexpected error in setup process", ex);
return 1;
}
return 0;
}

static string CreateHostExe ()
{
string file;
string id;
string fullFile;
do {
id = Guid.NewGuid ().ToString ().Replace ('-','_');
file = id + ".exe";
fullFile = Path.Combine (Path.GetTempPath (), file);
} while (File.Exists (fullFile));

AssemblyName aname = new AssemblyName ();
aname.Name = id;
AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly (aname, AssemblyBuilderAccess.Save, Path.GetTempPath ());
ModuleBuilder mb = ab.DefineDynamicModule (aname.Name, file);
TypeBuilder tb = mb.DefineType ("App", TypeAttributes.Public|TypeAttributes.Class);

MethodBuilder fb = tb.DefineMethod("Main",
MethodAttributes.Public |
MethodAttributes.Static,
typeof(int), new Type[] { typeof(string[]) });

MethodInfo mi = typeof(SetupProcess).GetMethod ("Main", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);

ILGenerator ilg = fb.GetILGenerator();
ilg.Emit (OpCodes.Ldarg_0);
ilg.EmitCall (OpCodes.Call, mi, null);
ilg.Emit (OpCodes.Ret);

tb.CreateType();
ab.SetEntryPoint (fb, PEFileKinds.WindowApplication);
ab.Save (file);
return fullFile;
}
}

class ProcessFailedException: Exception
Expand Down
4 changes: 4 additions & 0 deletions Test/UnitTests/UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@
<Project>{A85C9721-C054-4BD8-A1F3-0227615F0A36}</Project>
<Name>Mono.Addins.Setup</Name>
</ProjectReference>
<ProjectReference Include="..\..\Mono.Addins.SetupProcess\Mono.Addins.SetupProcess.csproj">
<Project>{303D2716-94AA-4067-8111-F478D26DFAFE}</Project>
<Name>Mono.Addins.SetupProcess</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Compile Include="TestExtensions.cs" />
Expand Down