Search code examples
c#.netprocessdebdpkg

How to create a deb package with Process .NET?



I would like to create a deb package programmatically inside a c# method.
I did a method to create a zip package using 7z.
You just have to install 7z and setup a ENV variable.
private string GeneratePackageZip(string tempDirectory, string directoryPrefix, int presetId)
{
    try
    {
        _logger.LogInformation("Creating zip archive");

        string packageSavePath = Path.Combine(_storagePath, PackageDirectory, $"{directoryPrefix}{presetId}", _packageFileName);

        if (File.Exists(packageSavePath))
        {
            File.Delete(packageSavePath);
        }

        // adds all files and subfolders from folder subdir to archive archive.zip.
        // 7z a -t7z Files.7z *.txt -r
        var process = new Process
        {
            StartInfo =
            {
                FileName = "7z",
                Arguments = $"a -t7z \"{packageSavePath}\" \"{tempDirectory}/*\"",
                UseShellExecute = false,
                CreateNoWindow = true
            }
        };
        process.Start();
        process.WaitForExit();

        if (Directory.Exists(tempDirectory))
        {
            Directory.Delete(tempDirectory, true);
        }

        return packageSavePath;
    }
    catch (Exception ex)
    {
        _logger.LogError(ex, "Failed to generate 7zip archive");
        Directory.Delete(_packageWorkingDirectory, true);
        throw;
    }
}

And I wanted to do the same with dpkg-deb to create a deb package.
But dpkg-deb cannot be install on windows.

private string GeneratePackageDeb(string tempDirectory, string directoryPrefix, int id)
{
    try
    {
        _logger.LogInformation("Creating deb package");

        string packageSavePath = Path.Combine(_storagePath, PackageDirectory, $"{directoryPrefix}{id}", _packageFileName);

        if (File.Exists(packageSavePath))
        {
            File.Delete(packageSavePath);
        }

        var process = new Process
        {
            StartInfo =
            {
                FileName = "dpkg-deb",
                Arguments = $"-b \"{packageSavePath}\" \"{tempDirectory}/*\"",
                UseShellExecute = false,
                CreateNoWindow = true
            }
        };

        process.Start();
        process.WaitForExit();

        if (Directory.Exists(tempDirectory))
        {
            Directory.Delete(tempDirectory, true);
        }

        return packageSavePath;
    }
    catch (Exception ex)
    {
        _logger.LogError(ex, "Failed to generate Deb");
        Directory.Delete(_packageWorkingDirectory, true);
        throw;
    }
}

How can I do that ? Is there a way to do it or is it a dead end ?
I thought of https://github.com/quamotion/dotnet-packaging to do it.
By creating a .csproj that would contains the binary I need.
But I need a very specific debian-binary. Will I be able to control what I have inside of it?

Thanks for any help given.


Solution

  • My solution was to use wsl.

    private string GeneratePackageDeb(string tempDirectory, string packageDebName)
    {
        try
        {
            _logger.LogInformation("Creating deb package");
    
            string packageSavePath = Path.Combine(tempDirectory, packageDebName + ".deb");
            string directoryDebPath = Path.Combine(tempDirectory, packageDebName);
    
            if (File.Exists(packageSavePath))
            {
                File.Delete(packageSavePath);
            }
    
            // Execute wsl command:
            using (var proc = new Process
            {
                StartInfo = new ProcessStartInfo
                {
                    FileName = @"cmd.exe",
                    UseShellExecute = false,
                    RedirectStandardOutput = true,
                    RedirectStandardInput = true,
                    CreateNoWindow = true,
                }
            })
            {
                proc.Start();
                // --nocheck because " dpkg-deb: error: control directory has bad permissions 777 (must be >=0755 and <=0775) "
                proc.StandardInput.WriteLine("wsl " + "--cd " + tempDirectory + " dpkg-deb -b --nocheck " + packageDebName);
                System.Threading.Thread.Sleep(500); // give some time for command to execute
                proc.StandardInput.Flush();
                proc.StandardInput.Close();
                proc.WaitForExit(5000); // wait up to 5 seconds for command to execute
                Console.WriteLine(proc.StandardOutput.ReadToEnd());
            }
    
            if (Directory.Exists(directoryDebPath))
            {
                Directory.Delete(directoryDebPath, true);
            }
    
            return packageSavePath;
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "Failed to generate Deb");
            Directory.Delete(_packageWorkingDirectory, true);
            throw;
        }
    }