| 1 |
{ |
| 2 |
Copyright 2018-2020 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 |
{ Collect files to package (TBinaryPackageFiles, TSourcePackageFiles). } |
| 17 |
unit ToolPackageCollectFiles; |
| 18 |
|
| 19 |
interface |
| 20 |
|
| 21 |
uses CastleInternalAutoGenerated, CastleFindFiles, CastleStringUtils, CastleUtils, |
| 22 |
ToolManifest, ToolProject; |
| 23 |
|
| 24 |
type |
| 25 |
{ Collect list of files for binary package. |
| 26 |
Peforms all necessary inclusions / exclusions. } |
| 27 |
TBinaryPackageFiles = class |
| 28 |
strict private |
| 29 |
AutoGenerated: TAutoGenerated; |
| 30 |
Project: TCastleProject; |
| 31 |
{ For FindFilesCallback: TIncludePath instance that caused this inclusion, if any. } |
| 32 |
FindFilesCallback_InputPath: TIncludePath; |
| 33 |
procedure FindFilesCallback(const FileInfo: TFileInfo; var StopSearch: boolean); |
| 34 |
public |
| 35 |
// Set fields below before calling @link(Run). |
| 36 |
IncludePaths: TIncludePathList; |
| 37 |
ExcludePaths: TCastleStringList; |
| 38 |
OnlyData: Boolean; |
| 39 |
TargetPlatform: TCastlePlatform; |
| 40 |
{ Collected files to include in the package. |
| 41 |
Objects of this list, if non nil, are TIncludePath instances that caused inclusion |
| 42 |
of given file in the package. |
| 43 |
Note that they are valid only as long as IncludePaths are valid |
| 44 |
(which usually means: as long as TCastleManifest instance is valid). } |
| 45 |
CollectedFiles: TCastleStringList; |
| 46 |
constructor Create(const AProject: TCastleProject); |
| 47 |
destructor Destroy; override; |
| 48 |
procedure Run; |
| 49 |
end; |
| 50 |
|
| 51 |
{ Collect list of files for source code package. |
| 52 |
Peforms all necessary inclusions / exclusions. } |
| 53 |
TSourcePackageFiles = class |
| 54 |
strict private |
| 55 |
Project: TCastleProject; |
| 56 |
{ For FindFilesCallback: TIncludePath instance that caused this inclusion, if any. } |
| 57 |
FindFilesCallback_InputPath: TIncludePath; |
| 58 |
procedure FindFilesCallback(const FileInfo: TFileInfo; var StopSearch: boolean); |
| 59 |
public |
| 60 |
{ Collected files to include in the package. |
| 61 |
Objects of this list, if non nil, are TIncludePath instances that caused inclusion |
| 62 |
of given file in the package. } |
| 63 |
CollectedFiles: TCastleStringList; |
| 64 |
constructor Create(const AProject: TCastleProject); |
| 65 |
destructor Destroy; override; |
| 66 |
procedure Run; |
| 67 |
end; |
| 68 |
|
| 69 |
implementation |
| 70 |
|
| 71 |
uses SysUtils, StrUtils, |
| 72 |
CastleUriUtils, CastleLog, CastleFilesUtils, |
| 73 |
ToolCommonUtils, ToolArchitectures, ToolPackage; |
| 74 |
|
| 75 |
{ TBinaryPackageFiles ------------------------------------------------------- } |
| 76 |
|
| 77 |
constructor TBinaryPackageFiles.Create(const AProject: TCastleProject); |
| 78 |
var |
| 79 |
AutoGeneratedUrl: String; |
| 80 |
begin |
| 81 |
inherited Create; |
| 82 |
Project := AProject; |
| 83 |
|
| 84 |
CollectedFiles := TCastleStringList.Create; |
| 85 |
|
| 86 |
AutoGeneratedUrl := FilenameToUriSafe(Project.DataPath + TAutoGenerated.FileName); |
| 87 |
AutoGenerated := TAutoGenerated.Create; |
| 88 |
AutoGenerated.LoadFromFile(AutoGeneratedUrl); |
| 89 |
end; |
| 90 |
|
| 91 |
destructor TBinaryPackageFiles.Destroy; |
| 92 |
begin |
| 93 |
FreeAndNil(AutoGenerated); |
| 94 |
FreeAndNil(CollectedFiles); |
| 95 |
inherited; |
| 96 |
end; |
| 97 |
|
| 98 |
procedure TBinaryPackageFiles.Run; |
| 99 |
|
| 100 |
procedure Exclude(const PathMask: string; const Files: TCastleStringList); |
| 101 |
const |
| 102 |
IgnoreCase = true; |
| 103 |
var |
| 104 |
I: Integer; |
| 105 |
PathMaskSlashes, ItemSlashes: string; |
| 106 |
begin |
| 107 |
{ replace all backslashes with slashes, so that they are equal for comparison } |
| 108 |
PathMaskSlashes := StringReplace(PathMask, '\', '/', [rfReplaceAll]); |
| 109 |
I := 0; |
| 110 |
while I < Files.Count do |
| 111 |
begin |
| 112 |
ItemSlashes := StringReplace(Files[I], '\', '/', [rfReplaceAll]); |
| 113 |
if IsWild(ItemSlashes, PathMaskSlashes, IgnoreCase) then |
| 114 |
Files.Delete(I) else |
| 115 |
Inc(I); |
| 116 |
end; |
| 117 |
end; |
| 118 |
|
| 119 |
var |
| 120 |
I: Integer; |
| 121 |
FindOptions: TFindFilesOptions; |
| 122 |
FullPath: String; |
| 123 |
begin |
| 124 |
if Project.DataExists then |
| 125 |
begin |
| 126 |
FindFilesCallback_InputPath := nil; |
| 127 |
FindFiles(Project.DataPath, '*', false, @FindFilesCallback, [ffRecursive]); |
| 128 |
end; |
| 129 |
|
| 130 |
if not OnlyData then |
| 131 |
for I := 0 to IncludePaths.Count - 1 do |
| 132 |
begin |
| 133 |
FindFilesCallback_InputPath := IncludePaths[I]; |
| 134 |
|
| 135 |
if IncludePaths[I].Recursive then |
| 136 |
FindOptions := [ffRecursive] |
| 137 |
else |
| 138 |
{ not recursive, so that e.g. <include path="README.txt" /> |
| 139 |
or <include path="docs/README.txt" /> |
| 140 |
should not include *all* README.txt files inside. } |
| 141 |
FindOptions := []; |
| 142 |
FullPath := Project.Path + IncludePaths[I].Path; |
| 143 |
if IsSuffix('/', FullPath, false) or |
| 144 |
IsSuffix('\', FullPath, false) then |
| 145 |
begin |
| 146 |
WritelnWarning('Include path ends with slash or backslash, it would not match anything: "%s". Appending "*" at the end to match everything inside.', |
| 147 |
[FullPath]); |
| 148 |
FullPath := FullPath + '*'; |
| 149 |
end; |
| 150 |
|
| 151 |
FindFiles(FullPath, false, @FindFilesCallback, FindOptions); |
| 152 |
end; |
| 153 |
|
| 154 |
Exclude('*.xcf', CollectedFiles); |
| 155 |
Exclude('*.blend*', CollectedFiles); |
| 156 |
Exclude('*~', CollectedFiles); |
| 157 |
// Note: slash or backslash below doesn't matter, Exclude function converts them |
| 158 |
Exclude('*/.DS_Store', CollectedFiles); |
| 159 |
Exclude('*/thumbs.db', CollectedFiles); |
| 160 |
for I := 0 to ExcludePaths.Count - 1 do |
| 161 |
Exclude(ExcludePaths[I], CollectedFiles); |
| 162 |
|
| 163 |
{ Change to relative paths vs DataPath. |
| 164 |
We do it only at the end, this way inclusion/exclusion mechanism |
| 165 |
works the same, regardless of OnlyData. So e.g. these work the same: |
| 166 |
<exclude path="data/blahblah/*" /> |
| 167 |
or |
| 168 |
<exclude path="*/.svn/*" /> |
| 169 |
(even when "data/.svn" exists). } |
| 170 |
if OnlyData then |
| 171 |
for I := 0 to CollectedFiles.Count - 1 do |
| 172 |
CollectedFiles[I] := ExtractRelativePath(Project.DataPath, |
| 173 |
CombinePaths(Project.Path, CollectedFiles[I])); |
| 174 |
end; |
| 175 |
|
| 176 |
procedure TBinaryPackageFiles.FindFilesCallback(const FileInfo: TFileInfo; var StopSearch: boolean); |
| 177 |
|
| 178 |
{ Do we include given file (with a path relative to project root) |
| 179 |
based on auto-generated files suitable for each platform. } |
| 180 |
function IncludeDataForPlatform(const RelativeFileName: String): Boolean; |
| 181 |
var |
| 182 |
NameInData: String; |
| 183 |
begin |
| 184 |
Result := true; |
| 185 |
NameInData := StringReplace(RelativeFileName, '\', '/', [rfReplaceAll]); |
| 186 |
|
| 187 |
if IsPrefix('data/', NameInData, true) then |
| 188 |
NameInData := PrefixRemove('data/', NameInData, true) |
| 189 |
else |
| 190 |
Exit; // RelativeFileName is outside data/ so allowed |
| 191 |
|
| 192 |
if not AutoGenerated.IncludeDataForPlatform(TargetPlatform, NameInData) then |
| 193 |
begin |
| 194 |
WritelnVerbose(Format('Not packaging "%s" because it is not for the platform "%s"', [ |
| 195 |
RelativeFileName, |
| 196 |
PlatformToStr(TargetPlatform) |
| 197 |
])); |
| 198 |
Result := false; |
| 199 |
end; |
| 200 |
end; |
| 201 |
|
| 202 |
var |
| 203 |
RelativeFileName: String; |
| 204 |
begin |
| 205 |
{ Add relative paths to CollectedFiles, to make include/exclude |
| 206 |
only work looking at relative paths. } |
| 207 |
RelativeFileName := ExtractRelativePath(Project.Path, FileInfo.AbsoluteName); |
| 208 |
if IncludeDataForPlatform(RelativeFileName) then |
| 209 |
CollectedFiles.AddObject(RelativeFileName, FindFilesCallback_InputPath); |
| 210 |
end; |
| 211 |
|
| 212 |
{ TSourcePackageFiles ------------------------------------------------- } |
| 213 |
|
| 214 |
constructor TSourcePackageFiles.Create(const AProject: TCastleProject); |
| 215 |
begin |
| 216 |
inherited Create; |
| 217 |
Project := AProject; |
| 218 |
CollectedFiles := TCastleStringList.Create; |
| 219 |
end; |
| 220 |
|
| 221 |
destructor TSourcePackageFiles.Destroy; |
| 222 |
begin |
| 223 |
FreeAndNil(CollectedFiles); |
| 224 |
inherited; |
| 225 |
end; |
| 226 |
|
| 227 |
procedure TSourcePackageFiles.FindFilesCallback(const FileInfo: TFileInfo; var StopSearch: boolean); |
| 228 |
var |
| 229 |
RelativeFileName: String; |
| 230 |
begin |
| 231 |
if FileInfo.Directory then |
| 232 |
begin |
| 233 |
if { exclude version control dirs } |
| 234 |
SameFileName(FileInfo.Name, '.git') or |
| 235 |
SameFileName(FileInfo.Name, '.svn') or |
| 236 |
{ exclude various build tool output } |
| 237 |
SameFileName(FileInfo.Name, 'castle-engine-output') then |
| 238 |
Exit; |
| 239 |
{ recursively scan children } |
| 240 |
FindFiles(FileInfo.AbsoluteName, '*', true, @FindFilesCallback, []); |
| 241 |
end else |
| 242 |
begin |
| 243 |
{ add relative filename to CollectedFiles } |
| 244 |
RelativeFileName := ExtractRelativePath(Project.Path, FileInfo.AbsoluteName); |
| 245 |
|
| 246 |
{ Do not pack packages (binary or source) into the source package. |
| 247 |
The packages are not cleaned by DoClean, so they could otherwise |
| 248 |
be packed by accident. } |
| 249 |
if Project.PackageOutput(RelativeFileName) then |
| 250 |
Exit; |
| 251 |
|
| 252 |
CollectedFiles.AddObject(RelativeFileName, FindFilesCallback_InputPath); |
| 253 |
end; |
| 254 |
end; |
| 255 |
|
| 256 |
procedure TSourcePackageFiles.Run; |
| 257 |
begin |
| 258 |
{ Right now, TSourcePackageFiles.CollectedFiles always have object = nil, |
| 259 |
since there is no TIncludePath that caused them. } |
| 260 |
FindFilesCallback_InputPath := nil; |
| 261 |
|
| 262 |
{ Non-recursive FindFiles, we will make recursion manually inside FindFilesCallback } |
| 263 |
FindFiles(Project.Path, '*', true, @FindFilesCallback, []); |
| 264 |
end; |
| 265 |
|
| 266 |
end. |