| 1 |
{ |
| 2 |
Copyright 2022-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 |
{ Functionality useful for CGE tools and design-time operations. |
| 17 |
|
| 18 |
Practically, this is a shared code between |
| 19 |
|
| 20 |
@unorderedList( |
| 21 |
@item(CGE build tool, in tools/build-tool/) |
| 22 |
|
| 23 |
@item(CGE editor, in tools/castle-editor/) |
| 24 |
|
| 25 |
@item(Delphi design-time functionality, in castle_engine_design.dpk, |
| 26 |
in unit CastleInternalDelphiDesign.) |
| 27 |
) |
| 28 |
} |
| 29 |
unit CastleInternalTools; |
| 30 |
|
| 31 |
{$I castleconf.inc} |
| 32 |
|
| 33 |
interface |
| 34 |
|
| 35 |
uses Classes, Dom, |
| 36 |
CastleFindFiles; |
| 37 |
|
| 38 |
const |
| 39 |
{ Paths with units and include files that are for all OSes and all compilers. |
| 40 |
|
| 41 |
Note: |
| 42 |
|
| 43 |
- We don't bother trying to have separate include dirs (.inc) and units (.pas). |
| 44 |
We just pass the same paths for both includes and units, this is simpler. |
| 45 |
|
| 46 |
- We pass all paths, even system-specific, regardless of the target |
| 47 |
OS/architecture. |
| 48 |
|
| 49 |
We tried smarter approach in the past (such that you could have e.g. |
| 50 |
"windows/castle_system_specific.inc" and "unix/castle_system_specific.inc", |
| 51 |
and compiler recognized what to do on [$I castle_system_specific.inc] |
| 52 |
based on include paths)... |
| 53 |
but it was not friendly to Lazarus or Delphi packages, |
| 54 |
where it's easier to define the same include path for all platforms. |
| 55 |
|
| 56 |
So it is simpler to just name all includes and units differently, |
| 57 |
even across system-specific dirs. } |
| 58 |
|
| 59 |
EnginePaths: array [0..43] of String = ( |
| 60 |
'base', |
| 61 |
'common_includes', |
| 62 |
'base/android', |
| 63 |
'base/windows', |
| 64 |
'base/unix', |
| 65 |
'base_rendering', |
| 66 |
'base_rendering/dglopengl', |
| 67 |
'base_rendering/glsl/generated-pascal', |
| 68 |
'fonts', |
| 69 |
'window', |
| 70 |
'window/gtk', |
| 71 |
'window/windows', |
| 72 |
'window/unix', |
| 73 |
'window/deprecated_units', |
| 74 |
'images', |
| 75 |
'transform', |
| 76 |
'scene', |
| 77 |
'scene/glsl/generated-pascal', |
| 78 |
'scene/x3d', |
| 79 |
'scene/load', |
| 80 |
'scene/load/spine', |
| 81 |
'scene/load/md3', |
| 82 |
'scene/load/collada', |
| 83 |
'scene/load/pasgltf', |
| 84 |
'audio', |
| 85 |
'audio/fmod', |
| 86 |
'audio/openal', |
| 87 |
'audio/ogg_vorbis', |
| 88 |
'files', |
| 89 |
'files/indy', |
| 90 |
'castlescript', |
| 91 |
'ui', |
| 92 |
'ui/windows', |
| 93 |
'services', |
| 94 |
'physics', |
| 95 |
'physics/kraft', |
| 96 |
'deprecated_units', |
| 97 |
{ Vampyre Imaging Library } |
| 98 |
'vampyre_imaginglib/src/Source', |
| 99 |
'vampyre_imaginglib/src/Source/JpegLib', |
| 100 |
'vampyre_imaginglib/src/Source/ZLib', |
| 101 |
'vampyre_imaginglib/src/Extras/Extensions', |
| 102 |
'vampyre_imaginglib/src/Extensions/J2KObjects', |
| 103 |
'vampyre_imaginglib/src/Extensions/LibTiff', |
| 104 |
'vampyre_imaginglib/src/Extensions' |
| 105 |
); |
| 106 |
|
| 107 |
{ Additional include/units paths, only for Delphi. } |
| 108 |
EnginePathsDelphi: array [0..2] of String = ( |
| 109 |
'delphi', |
| 110 |
'compatibility/delphi-only', |
| 111 |
'compatibility/delphi-only/fcl-json' |
| 112 |
); |
| 113 |
|
| 114 |
{ Paths for library (object) files. |
| 115 |
For FPC these are passed using -Fl. } |
| 116 |
EngineLibraryPaths: array [0..1] of String = ( |
| 117 |
'vampyre_imaginglib/src/Extensions/J2KObjects', |
| 118 |
'vampyre_imaginglib/src/Extensions/LibTiff/Compiled' |
| 119 |
); |
| 120 |
|
| 121 |
type |
| 122 |
{ CGE project dependencies. } |
| 123 |
TDependency = (depFreetype, depZlib, depPng, depSound, depOggVorbis, depHttps); |
| 124 |
TDependencies = set of TDependency; |
| 125 |
|
| 126 |
{ Platforms where we make effort to copy extra files alongside the EXE |
| 127 |
("deploy"). |
| 128 |
This is done when building a CGE project |
| 129 |
using build tool ("castle-engine compile") |
| 130 |
or in Delphi IDE (with our CastleInternalDelphiDesign feature). |
| 131 |
|
| 132 |
TODO: In the future, we could use TOS and TCPU and make it general. |
| 133 |
For now, this is practically only used by Windows platforms that need |
| 134 |
some DLLs, on other platforms we're fine with just using system libraries. } |
| 135 |
TDeployFilesPlatform = (dpWin32, dpWin64); |
| 136 |
|
| 137 |
{ Managing project dependencies: reading from manifest, guessing based on data etc. } |
| 138 |
TProjectDependencies = class |
| 139 |
strict private |
| 140 |
FDependencies: TDependencies; |
| 141 |
procedure AddDependencyFromFoundDataFile( |
| 142 |
const FileInfo: TFileInfo; var StopSearch: Boolean); |
| 143 |
public |
| 144 |
property Dependencies: TDependencies read FDependencies write FDependencies; |
| 145 |
|
| 146 |
{ Add to Dependencies values from CastleEngineManifest.xml file. } |
| 147 |
procedure ReadFromManifest(const Doc: TXmlDocument); |
| 148 |
|
| 149 |
{ Add to Dependencies values that are implied by other values. |
| 150 |
E.g. using libpng requires using also zlib. } |
| 151 |
procedure CloseDependencies; |
| 152 |
|
| 153 |
{ Add to Dependencies values implied by some data files. |
| 154 |
E.g. if data has .png file, then we add depPng. } |
| 155 |
procedure GuessDependencies(const ProjectDataPath: String); |
| 156 |
|
| 157 |
{ Add deploy files (.dll on Windows) to Files, based on given platform and project |
| 158 |
dependencies. The resulting filenames are relative to build tool's data. } |
| 159 |
procedure DeployFiles(const DeployPlatform: TDeployFilesPlatform; |
| 160 |
const Files: TStrings); |
| 161 |
end; |
| 162 |
|
| 163 |
function DependencyToString(const D: TDependency): String; |
| 164 |
function StringToDependency(const S: String): TDependency; |
| 165 |
|
| 166 |
{ API reference (online or offline). |
| 167 |
EnginePath is the path to the engine root directory, |
| 168 |
or '' if not known. } |
| 169 |
function ApiReferenceUrlCore(const EnginePath: String): String; |
| 170 |
|
| 171 |
{ Make correct CGE project qualified name from any ProjectName. } |
| 172 |
function MakeQualifiedName(ProjectName: String): String; |
| 173 |
|
| 174 |
{ Make correct CGE project Pascal name from any ProjectName. } |
| 175 |
function MakeProjectPascalName(ProjectName: String): String; |
| 176 |
|
| 177 |
type |
| 178 |
{ Options for project creation. } |
| 179 |
TProjectCreationOptions = record |
| 180 |
{ Where to place the new project. |
| 181 |
A subdirectory ProjectName will be created there. } |
| 182 |
ParentDir: String; |
| 183 |
|
| 184 |
{ Template name to use, must be one of the allowed templates |
| 185 |
in project_templates. } |
| 186 |
TemplateName: String; |
| 187 |
|
| 188 |
{ New project parameters. } |
| 189 |
ProjectName: String; |
| 190 |
ProjectCaption: String; |
| 191 |
MainView: String; |
| 192 |
end; |
| 193 |
|
| 194 |
{ Create directory with new CGE project, instantiating given template. } |
| 195 |
procedure ProjectCreateFromTemplate(const EnginePath: String; |
| 196 |
const Options: TProjectCreationOptions; |
| 197 |
out ProjectDirUrl: String); |
| 198 |
|
| 199 |
implementation |
| 200 |
|
| 201 |
uses SysUtils, StrUtils, |
| 202 |
CastleUriUtils, CastleXmlUtils, CastleStringUtils, CastleLog, CastleUtils, |
| 203 |
CastleFilesUtils; |
| 204 |
|
| 205 |
function ApiReferenceUrlCore(const EnginePath: String): String; |
| 206 |
var |
| 207 |
LocalDocsPath: String; |
| 208 |
begin |
| 209 |
if EnginePath <> '' then |
| 210 |
begin |
| 211 |
LocalDocsPath := EnginePath + 'doc' + PathDelim + 'reference' + PathDelim; |
| 212 |
if DirectoryExists(LocalDocsPath) then |
| 213 |
Exit(FilenameToUriSafe(LocalDocsPath)); |
| 214 |
end; |
| 215 |
|
| 216 |
Result := 'https://castle-engine.io/apidoc/html/'; |
| 217 |
end; |
| 218 |
|
| 219 |
const |
| 220 |
DependencyNames: array [TDependency] of string = |
| 221 |
('Freetype', 'Zlib', 'Png', 'Sound', 'OggVorbis', 'Https'); |
| 222 |
|
| 223 |
function DependencyToString(const D: TDependency): String; |
| 224 |
begin |
| 225 |
Result := DependencyNames[D]; |
| 226 |
end; |
| 227 |
|
| 228 |
function StringToDependency(const S: String): TDependency; |
| 229 |
begin |
| 230 |
for Result := Low(TDependency) to High(TDependency) do |
| 231 |
if AnsiSameText(DependencyNames[Result], S) then |
| 232 |
Exit; |
| 233 |
raise Exception.CreateFmt('Invalid dependency name "%s"', [S]); |
| 234 |
end; |
| 235 |
|
| 236 |
{ TProjectDependencies ------------------------------------------------------- } |
| 237 |
|
| 238 |
procedure TProjectDependencies.ReadFromManifest(const Doc: TXmlDocument); |
| 239 |
var |
| 240 |
Element, ChildElement: TDOMElement; |
| 241 |
ChildElements: TXMLElementIterator; |
| 242 |
begin |
| 243 |
Element := Doc.DocumentElement.ChildElement('dependencies', false); |
| 244 |
if Element <> nil then |
| 245 |
begin |
| 246 |
ChildElements := Element.ChildrenIterator('dependency'); |
| 247 |
try |
| 248 |
while ChildElements.GetNext do |
| 249 |
begin |
| 250 |
ChildElement := ChildElements.Current; |
| 251 |
Include(FDependencies, |
| 252 |
StringToDependency(ChildElement.AttributeString('name'))); |
| 253 |
end; |
| 254 |
finally FreeAndNil(ChildElements) end; |
| 255 |
end; |
| 256 |
end; |
| 257 |
|
| 258 |
procedure TProjectDependencies.CloseDependencies; |
| 259 |
|
| 260 |
procedure DependenciesClosure(const Dep, DepRequirement: TDependency); |
| 261 |
begin |
| 262 |
if (Dep in Dependencies) and not (DepRequirement in Dependencies) then |
| 263 |
begin |
| 264 |
WritelnLog('Automatically adding "' + DependencyToString(DepRequirement) + |
| 265 |
'" to dependencies because it is a prerequisite of existing dependency "' |
| 266 |
+ DependencyToString(Dep) + '"'); |
| 267 |
Include(FDependencies, DepRequirement); |
| 268 |
end; |
| 269 |
end; |
| 270 |
|
| 271 |
begin |
| 272 |
DependenciesClosure(depPng, depZlib); |
| 273 |
DependenciesClosure(depFreetype, depZlib); |
| 274 |
DependenciesClosure(depOggVorbis, depSound); |
| 275 |
end; |
| 276 |
|
| 277 |
procedure TProjectDependencies.AddDependencyFromFoundDataFile( |
| 278 |
const FileInfo: TFileInfo; var StopSearch: Boolean); |
| 279 |
|
| 280 |
procedure AddDependency(const Dependency: TDependency; const FileInfo: TFileInfo); |
| 281 |
begin |
| 282 |
if not (Dependency in Dependencies) then |
| 283 |
begin |
| 284 |
WritelnLog('Automatically adding "' + DependencyToString(Dependency) + |
| 285 |
'" to dependencies because data contains file: ' + FileInfo.Url); |
| 286 |
Include(FDependencies, Dependency); |
| 287 |
end; |
| 288 |
end; |
| 289 |
|
| 290 |
const |
| 291 |
{ Ignore case on all platforms, to e.g. add freetype DLL when file FOO.TTF |
| 292 |
is present in data, even on case-sensitive filesystems. } |
| 293 |
IgnoreCase = true; |
| 294 |
begin |
| 295 |
if IsWild(FileInfo.Name, '*.ttf', IgnoreCase) or |
| 296 |
IsWild(FileInfo.Name, '*.otf', IgnoreCase) then |
| 297 |
AddDependency(depFreetype, FileInfo); |
| 298 |
if IsWild(FileInfo.Name, '*.gz' , IgnoreCase) then |
| 299 |
AddDependency(depZlib, FileInfo); |
| 300 |
if IsWild(FileInfo.Name, '*.png', IgnoreCase) then |
| 301 |
AddDependency(depPng, FileInfo); |
| 302 |
if IsWild(FileInfo.Name, '*.wav', IgnoreCase) then |
| 303 |
AddDependency(depSound, FileInfo); |
| 304 |
if IsWild(FileInfo.Name, '*.ogg', IgnoreCase) then |
| 305 |
AddDependency(depOggVorbis, FileInfo); |
| 306 |
end; |
| 307 |
|
| 308 |
procedure TProjectDependencies.GuessDependencies(const ProjectDataPath: String); |
| 309 |
begin |
| 310 |
{ Note: Instead of one FindFiles call, this could also be implemented by a series |
| 311 |
of FindFirstFileIgnoreCase calls, like |
| 312 |
|
| 313 |
if FindFirstFileIgnoreCase(DataPath, '*.ttf' , false, [ffRecursive], FileInfo) or |
| 314 |
if FindFirstFileIgnoreCase(DataPath, '*.otf' , false, [ffRecursive], FileInfo) then |
| 315 |
AddDependency(depFreetype, FileInfo); |
| 316 |
if FindFirstFileIgnoreCase(DataPath, '*.gz' , false, [ffRecursive], FileInfo) then |
| 317 |
AddDependency(depZlib, FileInfo); |
| 318 |
|
| 319 |
But this would be inefficient. Each FindFirstFileIgnoreCase effectively again |
| 320 |
enumerates all files in data. } |
| 321 |
|
| 322 |
FindFiles(ProjectDataPath, '*', false, |
| 323 |
{$ifdef FPC}@{$endif} AddDependencyFromFoundDataFile, [ffRecursive]); |
| 324 |
end; |
| 325 |
|
| 326 |
procedure TProjectDependencies.DeployFiles(const DeployPlatform: TDeployFilesPlatform; |
| 327 |
const Files: TStrings); |
| 328 |
var |
| 329 |
Prefix: String; |
| 330 |
begin |
| 331 |
case DeployPlatform of |
| 332 |
dpWin32: |
| 333 |
begin |
| 334 |
{ Matches CPUToString(CPU) + '-' + OSToString(OS) for win32. } |
| 335 |
Prefix := 'external_libraries/i386-win32/'; |
| 336 |
|
| 337 |
if depFreetype in Dependencies then |
| 338 |
begin |
| 339 |
Files.Add(Prefix + 'freetype.dll'); |
| 340 |
Files.Add(Prefix + 'vcruntime140.dll'); |
| 341 |
end; |
| 342 |
if depZlib in Dependencies then |
| 343 |
Files.Add(Prefix + 'zlib1.dll'); |
| 344 |
if depPng in Dependencies then |
| 345 |
Files.Add(Prefix + 'libpng12.dll'); |
| 346 |
if depSound in Dependencies then |
| 347 |
begin |
| 348 |
Files.Add(Prefix + 'OpenAL32.dll'); |
| 349 |
Files.Add(Prefix + 'wrap_oal.dll'); |
| 350 |
end; |
| 351 |
if depOggVorbis in Dependencies then |
| 352 |
begin |
| 353 |
Files.Add(Prefix + 'ogg.dll'); |
| 354 |
Files.Add(Prefix + 'vorbis.dll'); |
| 355 |
Files.Add(Prefix + 'vorbisenc.dll'); |
| 356 |
Files.Add(Prefix + 'vorbisfile.dll'); |
| 357 |
Files.Add(Prefix + 'msvcr120.dll'); |
| 358 |
end; |
| 359 |
if depHttps in Dependencies then |
| 360 |
begin |
| 361 |
Files.Add(Prefix + 'openssl/libeay32.dll'); |
| 362 |
Files.Add(Prefix + 'openssl/ssleay32.dll'); |
| 363 |
end; |
| 364 |
end; |
| 365 |
|
| 366 |
dpWin64: |
| 367 |
begin |
| 368 |
{ Matches CPUToString(CPU) + '-' + OSToString(OS) for win32. } |
| 369 |
Prefix := 'external_libraries/x86_64-win64/'; |
| 370 |
|
| 371 |
if depFreetype in Dependencies then |
| 372 |
begin |
| 373 |
Files.Add(Prefix + 'freetype.dll'); |
| 374 |
Files.Add(Prefix + 'vcruntime140.dll'); |
| 375 |
end; |
| 376 |
if depZlib in Dependencies then |
| 377 |
Files.Add(Prefix + 'zlib1.dll'); |
| 378 |
if depPng in Dependencies then |
| 379 |
Files.Add(Prefix + 'libpng14-14.dll'); |
| 380 |
if depSound in Dependencies then |
| 381 |
begin |
| 382 |
Files.Add(Prefix + 'OpenAL32.dll'); |
| 383 |
Files.Add(Prefix + 'wrap_oal.dll'); |
| 384 |
end; |
| 385 |
if depOggVorbis in Dependencies then |
| 386 |
begin |
| 387 |
Files.Add(Prefix + 'libogg.dll'); |
| 388 |
Files.Add(Prefix + 'libvorbis.dll'); |
| 389 |
{ Files.Add(Prefix + 'vorbisenc.dll'); not present? } |
| 390 |
Files.Add(Prefix + 'vorbisfile.dll'); |
| 391 |
Files.Add(Prefix + 'msvcr120.dll'); |
| 392 |
end; |
| 393 |
if depHttps in Dependencies then |
| 394 |
begin |
| 395 |
Files.Add(Prefix + 'openssl/libeay32.dll'); |
| 396 |
Files.Add(Prefix + 'openssl/ssleay32.dll'); |
| 397 |
end; |
| 398 |
end; |
| 399 |
end; |
| 400 |
end; |
| 401 |
|
| 402 |
{ TTemplateCopyProcess ------------------------------------------------------------ } |
| 403 |
|
| 404 |
type |
| 405 |
TTemplateCopyProcess = class |
| 406 |
TemplateUrl: String; |
| 407 |
ProjectDirUrl: String; |
| 408 |
MainView: String; |
| 409 |
Macros: TStringStringMap; |
| 410 |
procedure FoundFile(const FileInfo: TFileInfo; var StopSearch: Boolean); |
| 411 |
end; |
| 412 |
|
| 413 |
procedure TTemplateCopyProcess.FoundFile(const FileInfo: TFileInfo; var StopSearch: Boolean); |
| 414 |
var |
| 415 |
Contents, RelativeUrl, TargetUrl, TargetFileName, Mime: String; |
| 416 |
begin |
| 417 |
{ Ignore case at IsPrefix / PrefixRemove calls, |
| 418 |
in case it's not case-sensitive file-system, then the case in theory |
| 419 |
can differ. } |
| 420 |
if not IsPrefix(TemplateUrl, FileInfo.URL, true) then |
| 421 |
raise Exception.CreateFmt('Unexpected: %s is not a prefix of %s, report a bug', |
| 422 |
[TemplateUrl, FileInfo.URL]); |
| 423 |
RelativeUrl := PrefixRemove(TemplateUrl, FileInfo.URL, true); |
| 424 |
TargetUrl := CombineUri(ProjectDirUrl, RelativeUrl); |
| 425 |
{ Rename target files that depend on MainView. } |
| 426 |
if ExtractUriName(TargetUrl) = 'gameviewmain.pas' then |
| 427 |
TargetUrl := ExtractUriPath(TargetUrl) + 'gameview' + LowerCase(MainView) + '.pas'; |
| 428 |
if ExtractUriName(TargetUrl) = 'gameviewmain.castle-user-interface' then |
| 429 |
TargetUrl := ExtractUriPath(TargetUrl) + 'gameview' + LowerCase(MainView) + '.castle-user-interface'; |
| 430 |
TargetFileName := UriToFilenameSafe(TargetUrl); |
| 431 |
|
| 432 |
if FileInfo.Directory then |
| 433 |
begin |
| 434 |
// create directory |
| 435 |
if not ForceDirectories(TargetFileName) then |
| 436 |
raise Exception.CreateFmt('Cannot create directory "%s"', [TargetFileName]); |
| 437 |
end else |
| 438 |
begin |
| 439 |
Mime := UriMimeType(FileInfo.URL); |
| 440 |
if (Mime = 'application/xml') or |
| 441 |
(Mime = 'text/plain') then |
| 442 |
begin |
| 443 |
// copy text file, replacing macros |
| 444 |
Contents := FileToString(FileInfo.URL); |
| 445 |
Contents := SReplacePatterns(Contents, Macros, false); |
| 446 |
StringToFile(TargetFileName, Contents); |
| 447 |
end else |
| 448 |
begin |
| 449 |
// simply copy other file types (e.g. sample png images in project templates) |
| 450 |
CheckCopyFile(UriToFilenameSafe(FileInfo.URL), TargetFileName); |
| 451 |
end; |
| 452 |
end; |
| 453 |
end; |
| 454 |
|
| 455 |
procedure ProjectCreateFromTemplate(const EnginePath: String; |
| 456 |
const Options: TProjectCreationOptions; |
| 457 |
out ProjectDirUrl: String); |
| 458 |
|
| 459 |
procedure AddMacroXmlQuote(const Macros: TStringStringMap; const MacroName: String); |
| 460 |
|
| 461 |
function XmlQuote(const S: String): String; |
| 462 |
begin |
| 463 |
Result := SReplacePatterns(S, |
| 464 |
['&', '<', '>', '"'], |
| 465 |
['&', '<', '>', '"'], |
| 466 |
false { IgnoreCase; can be false, it doesn't matter, as our patterns are not letters } |
| 467 |
); |
| 468 |
end; |
| 469 |
|
| 470 |
begin |
| 471 |
Macros.Add('${XmlQuote(' + MacroName + ')}', XmlQuote(Macros['${' + MacroName + '}'])); |
| 472 |
end; |
| 473 |
|
| 474 |
var |
| 475 |
ProjectDir, TemplateUrl, ProjectQualifiedName, ProjectPascalName: String; |
| 476 |
CopyProcess: TTemplateCopyProcess; |
| 477 |
Macros: TStringStringMap; |
| 478 |
begin |
| 479 |
Assert(Options.ProjectName <> ''); |
| 480 |
|
| 481 |
// Calculate and check TemplateUrl |
| 482 |
// (do it early, to fail early if template does not exist, without creating project dir) |
| 483 |
TemplateUrl := FilenameToUriSafe(InclPathDelim(EnginePath) + |
| 484 |
'tools/castle-editor/data/project_templates/' + Options.TemplateName + '/files/'); |
| 485 |
if UriExists(TemplateUrl) <> ueDirectory then |
| 486 |
raise Exception.CreateFmt('Cannot find template directory %s. Make sure template name is valid and engine path is valid.', |
| 487 |
[TemplateUrl]); |
| 488 |
|
| 489 |
// Create project dir |
| 490 |
ProjectDir := InclPathDelim(Options.ParentDir) + Options.ProjectName; |
| 491 |
ProjectDirUrl := FilenameToUriSafe(InclPathDelim(ProjectDir)); |
| 492 |
if DirectoryExists(ProjectDir) then |
| 493 |
raise Exception.CreateFmt('Directory "%s" already exists. Choose a different project name.', [ProjectDir]); |
| 494 |
if not ForceDirectories(ProjectDir) then |
| 495 |
raise Exception.CreateFmt('Cannot create directory "%s".', [ProjectDir]); |
| 496 |
|
| 497 |
ProjectQualifiedName := MakeQualifiedName(Options.ProjectName); |
| 498 |
ProjectPascalName := MakeProjectPascalName(Options.ProjectName); |
| 499 |
|
| 500 |
Macros := TStringStringMap.Create; |
| 501 |
try |
| 502 |
Macros.Add('${PROJECT_NAME}', Options.ProjectName); |
| 503 |
Macros.Add('${PROJECT_QUALIFIED_NAME}', ProjectQualifiedName); |
| 504 |
Macros.Add('${PROJECT_PASCAL_NAME}', ProjectPascalName); |
| 505 |
Macros.Add('${PROJECT_CAPTION}', Options.ProjectCaption); |
| 506 |
Macros.Add('${MAIN_VIEW}', Options.MainView); |
| 507 |
Macros.Add('${MAIN_VIEW_LOWERCASE}', LowerCase(Options.MainView)); |
| 508 |
|
| 509 |
{ Generate versions of some macros with xml_quote function. } |
| 510 |
AddMacroXmlQuote(Macros, 'PROJECT_NAME'); |
| 511 |
AddMacroXmlQuote(Macros, 'PROJECT_QUALIFIED_NAME'); |
| 512 |
AddMacroXmlQuote(Macros, 'PROJECT_PASCAL_NAME'); |
| 513 |
AddMacroXmlQuote(Macros, 'PROJECT_CAPTION'); |
| 514 |
|
| 515 |
CopyProcess := TTemplateCopyProcess.Create; |
| 516 |
try |
| 517 |
CopyProcess.TemplateUrl := TemplateUrl; |
| 518 |
CopyProcess.ProjectDirUrl := ProjectDirUrl; |
| 519 |
CopyProcess.Macros := Macros; |
| 520 |
CopyProcess.MainView := Options.MainView; |
| 521 |
FindFiles(TemplateUrl, '*', true, {$ifdef FPC}@{$endif} CopyProcess.FoundFile, [ffRecursive]); |
| 522 |
finally FreeAndNil(CopyProcess) end; |
| 523 |
finally FreeAndNil(Macros) end; |
| 524 |
end; |
| 525 |
|
| 526 |
const |
| 527 |
AlphaNum = ['a'..'z', 'A'..'Z', '0'..'9']; |
| 528 |
|
| 529 |
function MakeQualifiedName(ProjectName: String): String; |
| 530 |
const |
| 531 |
{ See ToolProject constant in CGE build tool. } |
| 532 |
QualifiedNameAllowedChars = AlphaNum + ['.']; |
| 533 |
QualifiedNameAllowedCharsFirst = QualifiedNameAllowedChars - ['.', '0'..'9']; |
| 534 |
begin |
| 535 |
ProjectName := SDeleteChars(ProjectName, AllChars - QualifiedNameAllowedChars); |
| 536 |
if (ProjectName <> '') and not (ProjectName[1] in QualifiedNameAllowedCharsFirst) then |
| 537 |
ProjectName := 'project' + ProjectName; |
| 538 |
if ProjectName = '' then |
| 539 |
ProjectName := 'project'; // if ProjectName is left empty after above deletions, set it to anything |
| 540 |
Result := 'com.mycompany.' + ProjectName; |
| 541 |
end; |
| 542 |
|
| 543 |
function MakeProjectPascalName(ProjectName: String): String; |
| 544 |
const |
| 545 |
ValidProjectPascalNameChars = AlphaNum + ['_']; |
| 546 |
ValidProjectPascalNameCharsFirst = ValidProjectPascalNameChars - ['0'..'9']; |
| 547 |
begin |
| 548 |
ProjectName := SReplaceChars(ProjectName, AllChars - ValidProjectPascalNameChars, '_'); |
| 549 |
if (ProjectName <> '') and not (ProjectName[1] in ValidProjectPascalNameCharsFirst) then |
| 550 |
ProjectName := 'project' + ProjectName; |
| 551 |
if ProjectName = '' then |
| 552 |
ProjectName := 'project'; // if ProjectName is left empty after above deletions, set it to anything |
| 553 |
Result := ProjectName; |
| 554 |
end; |
| 555 |
|
| 556 |
end. |