TemporaryDirectory will deleted at the end of process, Path of NSIS is besite of base directory.

This commit is contained in:
Holger Börchers 2018-08-02 10:23:45 +02:00
parent f8ca13d7e6
commit d85858b0c1
6 changed files with 70 additions and 50 deletions

View File

@ -0,0 +1,28 @@
using System;
using System.Threading;
namespace KattekerCreator.Helper
{
public class TemporaryDirectory : IDisposable
{
public string Path { get; }
public TemporaryDirectory(string path)
{
Path = path;
}
void IDisposable.Dispose()
{
try
{
Thread.Sleep(100);
System.IO.Directory.Delete(Path, true);
}
catch
{
//ignore.
}
}
}
}

View File

@ -19,7 +19,7 @@ namespace KattekerCreator.Helper
return false; return false;
} }
public static string CreateTempDirectory() public static TemporaryDirectory CreateTempDirectory()
{ {
string result; string result;
do do
@ -29,7 +29,7 @@ namespace KattekerCreator.Helper
} while (Directory.Exists(result)); } while (Directory.Exists(result));
Directory.CreateDirectory(result); Directory.CreateDirectory(result);
return result; return new TemporaryDirectory(result);
} }
public static bool IsFilesizeWrong(string filename) public static bool IsFilesizeWrong(string filename)

View File

@ -52,6 +52,7 @@
<Compile Include="ApplicationArguments.cs" /> <Compile Include="ApplicationArguments.cs" />
<Compile Include="AssemblyFileInfo.cs" /> <Compile Include="AssemblyFileInfo.cs" />
<Compile Include="Helper\Log.cs" /> <Compile Include="Helper\Log.cs" />
<Compile Include="Helper\TemporaryDirectory.cs" />
<Compile Include="IconExtractor\IconExtractor.cs" /> <Compile Include="IconExtractor\IconExtractor.cs" />
<Compile Include="IconExtractor\IconUtil.cs" /> <Compile Include="IconExtractor\IconUtil.cs" />
<Compile Include="IconExtractor\NativeMethods.cs" /> <Compile Include="IconExtractor\NativeMethods.cs" />

View File

@ -10,11 +10,12 @@ namespace KattekerCreator
{ {
public class Program public class Program
{ {
private const string Executable = @"C:\Program Files (x86)\NSIS\makensis.exe"; //private const string MakeNsis = @"C:\Program Files (x86)\NSIS\makensis.exe";
private readonly string _baseDirectory = AppDomain.CurrentDomain.BaseDirectory; private readonly string _baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
private string MakeNsis => Path.Combine(_baseDirectory, "NSIS", "makensis.exe");
private ApplicationArguments _appArguments; private ApplicationArguments _appArguments;
private AssemblyFileInfo _assemblyFileInfo; private AssemblyFileInfo _assemblyFileInfo;
private string _tempDir; private TemporaryDirectory _tempDir;
private Releases _releases; private Releases _releases;
private static int Main(string[] args) private static int Main(string[] args)
@ -50,13 +51,14 @@ namespace KattekerCreator
_appArguments = parser.GetObject(); _appArguments = parser.GetObject();
//Create tempdir //Create tempdir
_tempDir = Utils.CreateTempDirectory(); using (_tempDir = Utils.CreateTempDirectory())
{
_releases = new Releases(_appArguments.OutputDir); _releases = new Releases(_appArguments.OutputDir);
parser.ShowProgramArguments(); parser.ShowProgramArguments();
//Modify AppStub //Modify AppStub
var appStubFile = ModifyAppStub(); var appStubFile = ModifyAppStub();
//Acquire infos from Executable. //Acquire infos from Executable.
_assemblyFileInfo = new AssemblyFileInfo(_appArguments, _tempDir); _assemblyFileInfo = new AssemblyFileInfo(_appArguments, _tempDir.Path);
var configFile = CreateConfigFile(); var configFile = CreateConfigFile();
//Generate NSIS-Script //Generate NSIS-Script
@ -76,6 +78,7 @@ namespace KattekerCreator
CopyAsSetup(setupFilePath, releaseEntry); CopyAsSetup(setupFilePath, releaseEntry);
return 0; return 0;
} }
}
private void CopyAsSetup(string setupFilePath, ReleaseEntry releaseEntry) private void CopyAsSetup(string setupFilePath, ReleaseEntry releaseEntry)
{ {
@ -117,15 +120,15 @@ namespace KattekerCreator
File.Copy(setupFilePath, Path.Combine(_appArguments.OutputDir, setupFile), true); File.Copy(setupFilePath, Path.Combine(_appArguments.OutputDir, setupFile), true);
} }
private static string CompileSetupScript(string templateFile) private string CompileSetupScript(string templateFile)
{ {
if (!File.Exists(Executable)) throw new FileNotFoundException("NSIS not installed properly."); if (!File.Exists(MakeNsis)) throw new FileNotFoundException("NSIS not installed properly.");
int exitcode; int exitcode;
using (var p = new Process()) using (var p = new Process())
{ {
p.StartInfo = new ProcessStartInfo p.StartInfo = new ProcessStartInfo
{ {
FileName = Executable, FileName = MakeNsis,
Arguments = $"\"{templateFile}\"", Arguments = $"\"{templateFile}\"",
UseShellExecute = false UseShellExecute = false
}; };
@ -133,16 +136,16 @@ namespace KattekerCreator
p.Start(); p.Start();
p.WaitForExit(); p.WaitForExit();
exitcode = p.ExitCode; exitcode = p.ExitCode;
Log.WriteInfoLine($"{Path.GetFileName(Executable)} has exited with Exitcode: {exitcode} (Took: {sw.ElapsedMilliseconds}ms)"); Log.WriteInfoLine($"{Path.GetFileName(MakeNsis)} has exited with Exitcode: {exitcode} (Took: {sw.ElapsedMilliseconds}ms)");
} }
if (exitcode != 0) throw new Exception($"{Path.GetFileName(Executable)} has exited with Exitcode: {exitcode}"); if (exitcode != 0) throw new Exception($"{Path.GetFileName(MakeNsis)} has exited with Exitcode: {exitcode}");
return Path.ChangeExtension(templateFile, "exe"); return Path.ChangeExtension(templateFile, "exe");
} }
private string CreateConfigFile() private string CreateConfigFile()
{ {
var pathToFile = Path.Combine(_tempDir, Constants.KattekerConfig); var pathToFile = Path.Combine(_tempDir.Path, Constants.KattekerConfig);
var kattekerConfig = new KattekerConfig var kattekerConfig = new KattekerConfig
{ {
Publish = _appArguments.PublishDir ?? _appArguments.OutputDir, Publish = _appArguments.PublishDir ?? _appArguments.OutputDir,
@ -155,7 +158,7 @@ namespace KattekerCreator
private string ModifyAppStub() private string ModifyAppStub()
{ {
var baseFile = new FileInfo(Path.Combine(_baseDirectory, Constants.ExecutionStub)); var baseFile = new FileInfo(Path.Combine(_baseDirectory, Constants.ExecutionStub));
var targetFile = baseFile.CopyTo(Path.Combine(_tempDir, Constants.ExecutionStub)); var targetFile = baseFile.CopyTo(Path.Combine(_tempDir.Path, Constants.ExecutionStub));
Utils.CopyResources(_appArguments.ProgramFile, targetFile.FullName); Utils.CopyResources(_appArguments.ProgramFile, targetFile.FullName);
return targetFile.FullName; return targetFile.FullName;
} }
@ -166,7 +169,7 @@ namespace KattekerCreator
{ {
try try
{ {
var outFile = Path.Combine(_tempDir, GenerateFilename(_assemblyFileInfo.ProductName, _assemblyFileInfo.AssemblyVersion.ToString(), "nsi")); var outFile = Path.Combine(_tempDir.Path, GenerateFilename(_assemblyFileInfo.ProductName, _assemblyFileInfo.AssemblyVersion.ToString(), "nsi"));
var setupTmpl = new SetupTmpl var setupTmpl = new SetupTmpl
{ {
Executable = _assemblyFileInfo.FileInfo.Name, Executable = _assemblyFileInfo.FileInfo.Name,

View File

@ -60,33 +60,21 @@
<Content Include="testdata\Example.exe"> <Content Include="testdata\Example.exe">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content> </Content>
<Content Include="testdata\Example.pdb">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="testdata\Example.vshost.exe"> <Content Include="testdata\Example.vshost.exe">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content> </Content>
<Content Include="testdata\Katteker.dll"> <Content Include="testdata\Katteker.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content> </Content>
<Content Include="testdata\Katteker.pdb">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="testdata\Katteker.UserInterface.dll"> <Content Include="testdata\Katteker.UserInterface.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content> </Content>
<Content Include="testdata\Katteker.UserInterface.pdb">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="testdata\Katteker.UserInterface.xml"> <Content Include="testdata\Katteker.UserInterface.xml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content> </Content>
<Content Include="testdata\Microsoft.Practices.ServiceLocation.dll"> <Content Include="testdata\Microsoft.Practices.ServiceLocation.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content> </Content>
<Content Include="testdata\Microsoft.Practices.ServiceLocation.pdb">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="testdata\Microsoft.Practices.ServiceLocation.xml"> <Content Include="testdata\Microsoft.Practices.ServiceLocation.xml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content> </Content>

View File

@ -32,6 +32,6 @@ using System.Runtime.InteropServices;
// Sie können alle Werte angeben oder Standardwerte für die Build- und Revisionsnummern verwenden, // Sie können alle Werte angeben oder Standardwerte für die Build- und Revisionsnummern verwenden,
// indem Sie "*" wie unten gezeigt eingeben: // indem Sie "*" wie unten gezeigt eingeben:
// [assembly: AssemblyVersion("1.0.*")] // [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.4")] [assembly: AssemblyVersion("1.1.0")]
[assembly: AssemblyFileVersion("1.0.4")] [assembly: AssemblyFileVersion("1.1.0")]
[assembly: InternalsVisibleTo("Katteker.Test")] [assembly: InternalsVisibleTo("Katteker.Test")]