Skip to content

Commit

Permalink
Add support for post-install/remove scripts and custom deb packages (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
jkoritzinsky authored Nov 12, 2024
1 parent 8ff7332 commit a9ef866
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/Microsoft.DotNet.Build.Tasks.Installers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,15 @@ To add symlinks that should be installed on the system, add a `LinuxPackageSymli
> Symlinks added with `LinuxPackageSymlink` are relative to the filesystem root, not to the `LinuxInstallRoot` property.
> As the vast majority of symlinks in a package are from system locations to the install root, this provides an easier UX for defining symlinks.
Add a `LinuxPostInstallScript` item to specify an sh script that should be run after the package is installed.
Add a `LinuxPostRemoveScript` item to specify an sh script that should be run after the package is removed.

#### Deb package configuration

To add additional properties for the deb control file, add `DebControlProperty` items with the value of the field in the `Value` metadata.

To add additional files to the `control` tarball in the package, add `DebControlFile` items for each file.

#### Rpm package configuration

To specify directories owned by the package, add `RpmOwnedDirectory` items for each directory. These are provided automatically for any non-ToolPack packages produced by the Shared Framework SDK.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,26 @@
<FileWrites Include="@(_ControlFile)" />
</ItemGroup>

<Copy SourceFiles="@(DebControlFile)"
DestinationFiles="@(DebControlFile->'$(_LayoutControlRoot)%(Filename)%(Extension)')"
OverwriteReadOnlyFiles="True"
SkipUnchangedFiles="True"
UseHardlinksIfPossible="True" />

<Copy SourceFiles="@(LinuxPostInstallScript)"
DestinationFiles="$(_LayoutControlRoot)postinst"
OverwriteReadOnlyFiles="True"
SkipUnchangedFiles="True"
UseHardlinksIfPossible="True" />

<Copy SourceFiles="@(LinuxPostRemoveScript)"
DestinationFiles="$(_LayoutControlRoot)postrm"
OverwriteReadOnlyFiles="True"
SkipUnchangedFiles="True"
UseHardlinksIfPossible="True" />

<Exec Command="chmod ugo+x '$(_LayoutControlRoot)postinst' '$(_LayoutControlRoot)postrm'" />

<Exec Command="tar -C '$(_LayoutControlRoot)' -czf '$(_LayoutDirectory)/control.tar.gz' ."
IgnoreExitCode="true"
IgnoreStandardErrorWarningFormat="true" />
Expand Down Expand Up @@ -293,6 +313,11 @@
<Output TaskParameter="ConsoleOutput" ItemName="_RawFileKindInfo" />
</Exec>

<ItemGroup>
<_RpmScriptlet Include="@(LinuxPostInstallScript)" Kind="Postin" />
<_RpmScriptlet Include="@(LinuxPostRmScript)" Kind="Postun" />
</ItemGroup>

<CreateRpmPackage
OutputRpmPackagePath="$(_InstallerFile)"
Vendor=".NET Foundation"
Expand All @@ -312,6 +337,7 @@
Summary="$(_ShortDescription)"
Description="$(_PackageLongDescription)"
PackageUrl="https://github.com/dotnet/core"
Scripts="@(_RpmScriptlet)"
/>

<Message Text="$(MSBuildProjectName) -> $(_InstallerFile)" Importance="high" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ public sealed class CreateRpmPackage : BuildTask
public string Description { get; set; } = "";
[Required]
public string PackageUrl { get; set; } = "";
[Required]
public ITaskItem[] Scripts { get; set; } = [];

public override bool Execute()
{
Expand Down Expand Up @@ -109,6 +111,11 @@ public override bool Execute()

HashSet<string> ownedDirectories = new(OwnedDirectories.Select(d => d.ItemSpec));

foreach (ITaskItem script in Scripts)
{
builder.AddScript(script.GetMetadata("Kind"), File.ReadAllText(script.ItemSpec));
}

using (CpioReader reader = new(File.OpenRead(Payload), leaveOpen: false))
{
Dictionary<string, string> filePathToKind = RawPayloadFileKinds.Select(k => k.ItemSpec.Split(':')).ToDictionary(k => k[0], k => k[1].Trim());
Expand Down
13 changes: 13 additions & 0 deletions src/Microsoft.DotNet.Build.Tasks.Installers/src/RpmBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,13 @@ public void AddFile(CpioEntry file, string fileKind)
_files.Add((file, fileKind));
}

private readonly Dictionary<string, string> _scripts = [];

public void AddScript(string kind, string script)
{
_scripts.Add(kind, script);
}

public string Url { get; set; } = "";
public string Vendor { get; set; } = "";
public string License { get; set; } = "";
Expand Down Expand Up @@ -195,6 +202,12 @@ public RpmPackage Build()
entries.Add(new(RpmHeaderTag.Summary, RpmHeaderEntryType.I18NString, Summary));
entries.Add(new(RpmHeaderTag.Description, RpmHeaderEntryType.I18NString, Description));

foreach (var script in _scripts)
{
entries.Add(new((RpmHeaderTag)Enum.Parse(typeof(RpmHeaderTag), script.Key), RpmHeaderEntryType.String, "/bin/sh"));
entries.Add(new((RpmHeaderTag)Enum.Parse(typeof(RpmHeaderTag), $"{script.Key}prog"), RpmHeaderEntryType.String, script.Value));
}

MemoryStream cpioArchive = new();
using (CpioWriter writer = new(cpioArchive, leaveOpen: true))
using (SHA256 sha256 = SHA256.Create())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public enum RpmHeaderTag
Url = 1020,
OperatingSystem = 1021,
Architecture = 1022,
Postin = 1024,
Postun = 1026,
FileSizes = 1028,
FileModes = 1030,
DeviceFileIds = 1033,
Expand All @@ -53,6 +55,8 @@ public enum RpmHeaderTag
ChangelogTimestamp = 1080,
ChangelogName = 1081,
ChangelogText = 1082,
Postinprog = 1086,
Postunprog = 1088,
FileDevices = 1095,
FileInode = 1096,
FileLang = 1097,
Expand All @@ -76,4 +80,4 @@ public enum RpmHeaderTag
PayloadDigestAlgorithm = 5093,
UncompressedPayloadDigest = 5097,
}
}
}

0 comments on commit a9ef866

Please sign in to comment.