| 1 |
{ |
| 2 |
Copyright 2014-2024 Michalis Kamburelis. |
| 3 |
|
| 4 |
This file is part of "Castle Game Engine". |
| 5 |
|
| 6 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 7 |
included in this distribution, for details about the copyright. |
| 8 |
|
| 9 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 |
|
| 13 |
---------------------------------------------------------------------------- |
| 14 |
} |
| 15 |
|
| 16 |
{ Packaging data in archives. } |
| 17 |
unit ToolPackage; |
| 18 |
|
| 19 |
{$I castleconf.inc} |
| 20 |
|
| 21 |
interface |
| 22 |
|
| 23 |
uses CastleUtils, CastleInternalAutoGenerated, |
| 24 |
ToolPackageFormat, ToolManifest, ToolArchitectures; |
| 25 |
|
| 26 |
type |
| 27 |
{ Package a project to a directory. } |
| 28 |
TPackageDirectory = class |
| 29 |
private |
| 30 |
TemporaryDir: String; |
| 31 |
FPath: String; |
| 32 |
FTopDirectoryName: String; |
| 33 |
|
| 34 |
{ Absolute path (ends with path delimiter) under which you should |
| 35 |
store your files. They will end up being packaged, |
| 36 |
under TopDirectoryName. } |
| 37 |
property Path: String read FPath; |
| 38 |
property TopDirectoryName: String read FTopDirectoryName; |
| 39 |
public |
| 40 |
{ Architecture of the binaries in final package. |
| 41 |
Right now only used by package format = pfDeb. } |
| 42 |
Cpu: TCpu; |
| 43 |
|
| 44 |
{ Manifest of project used to make this. |
| 45 |
Right now only used by package format = pfDeb. } |
| 46 |
Manifest: TCastleManifest; |
| 47 |
|
| 48 |
{ Create a package. |
| 49 |
|
| 50 |
@param(ATopDirectoryName is the name of the main directory that will |
| 51 |
be visible in the archive, it's usually just a project name.) } |
| 52 |
constructor Create(const ATopDirectoryName: String); |
| 53 |
destructor Destroy; override; |
| 54 |
|
| 55 |
{ Add file to the package. |
| 56 |
|
| 57 |
@param SourceFileName Filename existing on disk right now, must be an absolute filename. |
| 58 |
|
| 59 |
@param DestinationFileName Name in package, must be relative within package. |
| 60 |
|
| 61 |
@param MakeExecutable Set the Unix executable bit on given file. } |
| 62 |
procedure Add(const SourceFileName, DestinationFileName: String; |
| 63 |
const MakeExecutable: Boolean = false); |
| 64 |
|
| 65 |
{ Generate auto_generated/CastleDataInformation.xml file inside |
| 66 |
DataName subdirectory of the archive. } |
| 67 |
procedure AddDataInformation(const DataName: String); |
| 68 |
|
| 69 |
{ Create final archive. |
| 70 |
It will be placed within PackageOutputPath. |
| 71 |
PackageFileName should contain only the file name, with an extension |
| 72 |
(like .zip or .deb) but without any directory parts. } |
| 73 |
procedure Make(const PackageOutputPath: String; const PackageFileName: String; |
| 74 |
const PackageFormat: TPackageFormatNoDefault); |
| 75 |
end; |
| 76 |
|
| 77 |
{ Generate auto_generated/CastleDataInformation.xml file inside |
| 78 |
CurrentDataPath, if it exists. |
| 79 |
CurrentDataPath may but doesn't have to end with PathDelim. } |
| 80 |
procedure GenerateDataInformation(const CurrentDataPath: String); |
| 81 |
|
| 82 |
implementation |
| 83 |
|
| 84 |
uses SysUtils, Process, |
| 85 |
CastleFilesUtils, CastleLog, CastleFindFiles, CastleUriUtils, |
| 86 |
CastleStringUtils, CastleInternalDirectoryInformation, |
| 87 |
ToolCommonUtils, ToolUtils, ToolDebian; |
| 88 |
|
| 89 |
{ TPackageDirectory ---------------------------------------------------------- } |
| 90 |
|
| 91 |
constructor TPackageDirectory.Create(const ATopDirectoryName: String); |
| 92 |
begin |
| 93 |
inherited Create; |
| 94 |
FTopDirectoryName := ATopDirectoryName; |
| 95 |
|
| 96 |
TemporaryDir := CreateTemporaryDir; |
| 97 |
|
| 98 |
FPath := InclPathDelim(TemporaryDir) + TopDirectoryName; |
| 99 |
CheckForceDirectories(FPath); |
| 100 |
FPath += PathDelim; |
| 101 |
end; |
| 102 |
|
| 103 |
destructor TPackageDirectory.Destroy; |
| 104 |
begin |
| 105 |
RemoveNonEmptyDir(TemporaryDir, true); |
| 106 |
inherited; |
| 107 |
end; |
| 108 |
|
| 109 |
procedure TPackageDirectory.Make(const PackageOutputPath: String; |
| 110 |
const PackageFileName: String; const PackageFormat: TPackageFormatNoDefault); |
| 111 |
var |
| 112 |
FullPackageFileName: String; |
| 113 |
|
| 114 |
{ Run zip/tar.gz/other command that makes archive called PackageFileName |
| 115 |
(should be a filename, without directory part, like 'myproject.zip' or 'myproject.tar.gz') |
| 116 |
in current directory, wherever it is run (we will run it in a temp directory). |
| 117 |
|
| 118 |
We will move the resulting file to FullPackageFileName |
| 119 |
(which should be absolute filename now). } |
| 120 |
procedure PackageCommand(const PackagingExeName: String; const PackagingParameters: array of String); |
| 121 |
var |
| 122 |
ProcessOutput, CommandExe: String; |
| 123 |
ProcessExitStatus: Integer; |
| 124 |
begin |
| 125 |
CommandExe := FindExe(PackagingExeName); |
| 126 |
if CommandExe = '' then |
| 127 |
raise Exception.CreateFmt('Cannot find "%s" program on $PATH. Make sure it is installed, and available on $PATH', [ |
| 128 |
PackagingExeName |
| 129 |
]); |
| 130 |
MyRunCommandIndir(TemporaryDir, CommandExe, |
| 131 |
PackagingParameters, |
| 132 |
ProcessOutput, ProcessExitStatus); |
| 133 |
|
| 134 |
if Verbose then |
| 135 |
begin |
| 136 |
Writeln('Executed package process, output:'); |
| 137 |
Writeln(ProcessOutput); |
| 138 |
end; |
| 139 |
|
| 140 |
if ProcessExitStatus <> 0 then |
| 141 |
raise Exception.CreateFmt('Package process exited with error, status %d', [ProcessExitStatus]); |
| 142 |
|
| 143 |
CheckRenameFile(InclPathDelim(TemporaryDir) + PackageFileName, FullPackageFileName); |
| 144 |
end; |
| 145 |
|
| 146 |
var |
| 147 |
PackageIsSingleFile: Boolean; |
| 148 |
begin |
| 149 |
FullPackageFileName := CombinePaths(PackageOutputPath, PackageFileName); |
| 150 |
|
| 151 |
PackageIsSingleFile := PackageFormat in [pfZip, pfTarGz, pfDeb]; |
| 152 |
|
| 153 |
{ Clean previous package file/directory. } |
| 154 |
if PackageIsSingleFile then |
| 155 |
DeleteFile(FullPackageFileName) |
| 156 |
else |
| 157 |
if DirectoryExists(FullPackageFileName) then |
| 158 |
RemoveNonEmptyDir(FullPackageFileName); |
| 159 |
|
| 160 |
{ Do the package-format-specific job. } |
| 161 |
case PackageFormat of |
| 162 |
pfZip : |
| 163 |
//PackageCommand('zip', ['-q', '-r', PackageFileName, TopDirectoryName]); |
| 164 |
// Better use internal zip, that doesn't require any tool installed: |
| 165 |
ZipDirectory(FullPackageFileName, Path); |
| 166 |
pfTarGz : PackageCommand('tar', ['czf', PackageFileName, TopDirectoryName]); |
| 167 |
pfDirectory: |
| 168 |
begin |
| 169 |
if DirectoryExists(FullPackageFileName) then |
| 170 |
RemoveNonEmptyDir(FullPackageFileName); |
| 171 |
CopyDirectory(Path, FullPackageFileName); |
| 172 |
end; |
| 173 |
pfDeb: PackageDebian(Path, PackageOutputPath, PackageFileName, Cpu, Manifest); |
| 174 |
else raise EInternalError.Create('TPackageDirectory.Make PackageFormat?'); |
| 175 |
end; |
| 176 |
|
| 177 |
{ Report success. } |
| 178 |
if PackageIsSingleFile then |
| 179 |
Writeln('Created package ' + PackageFileName + ', size: ', SizeToStr(FileSize(FullPackageFileName))) |
| 180 |
else |
| 181 |
Writeln('Created directory ' + PackageFileName); |
| 182 |
end; |
| 183 |
|
| 184 |
procedure TPackageDirectory.Add(const SourceFileName, DestinationFileName: String; |
| 185 |
const MakeExecutable: Boolean); |
| 186 |
begin |
| 187 |
SmartCopyFile(SourceFileName, Path + DestinationFileName); |
| 188 |
WritelnVerbose('Package file: ' + DestinationFileName); |
| 189 |
|
| 190 |
if MakeExecutable then |
| 191 |
begin |
| 192 |
{ For OSes where chmod matters, make sure to set it before packing } |
| 193 |
WritelnVerbose('Setting Unix executable permissions: ' + DestinationFileName); |
| 194 |
DoMakeExecutable(Path + DestinationFileName); |
| 195 |
end; |
| 196 |
end; |
| 197 |
|
| 198 |
procedure TPackageDirectory.AddDataInformation(const DataName: String); |
| 199 |
begin |
| 200 |
GenerateDataInformation(Path + DataName); |
| 201 |
end; |
| 202 |
|
| 203 |
{ global --------------------------------------------------------------------- } |
| 204 |
|
| 205 |
procedure GenerateDataInformation(const CurrentDataPath: String); |
| 206 |
var |
| 207 |
DataInformationDir, DataInformationFileName: String; |
| 208 |
DataInformation: TDirectoryInformation; |
| 209 |
DirsCount, FilesCount, FilesSize: QWord; |
| 210 |
begin |
| 211 |
if DirectoryExists(CurrentDataPath) then |
| 212 |
begin |
| 213 |
DataInformationDir := InclPathDelim(CurrentDataPath) + 'auto_generated'; |
| 214 |
CheckForceDirectories(DataInformationDir); |
| 215 |
DataInformationFileName := DataInformationDir + PathDelim + 'CastleDataInformation.xml'; |
| 216 |
{ Do not include CastleDataInformation.xml itself on a list of existing files, |
| 217 |
since we don't know it's size yet. } |
| 218 |
DeleteFile(DataInformationFileName); |
| 219 |
|
| 220 |
DataInformation := TDirectoryInformation.Create; |
| 221 |
try |
| 222 |
DataInformation.Generate(FilenameToUriSafe(CurrentDataPath)); |
| 223 |
DataInformation.SaveToFile(FilenameToUriSafe(DataInformationFileName)); |
| 224 |
|
| 225 |
DataInformation.Sum(DirsCount, FilesCount, FilesSize); |
| 226 |
Writeln('Generated CastleDataInformation.xml.'); |
| 227 |
Writeln(Format('Project data contains %d directories, %d files, total (uncompressed) size %s.', |
| 228 |
[DirsCount, FilesCount, SizeToStr(FilesSize)])); |
| 229 |
finally FreeAndNil(DataInformation) end; |
| 230 |
end; |
| 231 |
end; |
| 232 |
|
| 233 |
end. |