|
| 1 | +--- |
| 2 | +title: Pack |
| 3 | +parent: ProjPacker |
| 4 | +permalink: /tB/Packages/Contributed/tbProjPacker/ProjPacker/Pack |
| 5 | +has_toc: false |
| 6 | +--- |
| 7 | + |
| 8 | +# Pack |
| 9 | +{: .no_toc } |
| 10 | + |
| 11 | +Packs a folder, and everything in it, into a project file. |
| 12 | + |
| 13 | +Syntax: *object*.**Pack** ( *inputDir*, *outputFile* [, *overwrite* [, *logFile* ]] ) **As Boolean** |
| 14 | + |
| 15 | +*object* |
| 16 | +: *required* An object expression that evaluates to a **ProjPacker**. |
| 17 | + |
| 18 | +*inputDir* |
| 19 | +: *required* A **String**, the folder to pack. It must exist, and it must hold a file named `Settings`. |
| 20 | + |
| 21 | +*outputFile* |
| 22 | +: *required* A **String**, the project file to write. The name is used as given: **Pack** adds no extension and checks none. |
| 23 | + |
| 24 | +*overwrite* |
| 25 | +: *optional* A **Boolean**. **True** replaces *outputFile* when it exists. The default is **False**, and the call then fails when anything exists at that path. |
| 26 | + |
| 27 | +*logFile* |
| 28 | +: *optional* A **String**, the path of a [log file](.#the-log-file) for the lines the call prints. The default is an empty string, which writes no log. |
| 29 | + |
| 30 | +Returns **True** when *outputFile* was written, and **False** when it was not. The method raises no error; [Results and errors](.#results-and-errors) describes how the reason is reported. |
| 31 | + |
| 32 | +## What is packed |
| 33 | + |
| 34 | +The root of the project file is named after the last folder of *inputDir*. Everything in *inputDir* is stored, in all its subfolders: hidden files, empty folders and a `.git` folder included. Keep the project's files in a folder of their own. The top folder of a Git repository would put the repository's own data into the project file. |
| 35 | + |
| 36 | +Version 1.0.5.0 stores each file exactly as it is on disk. It does not convert line endings. The IDE stores `.twin`, `.bas` and `.cls` files with CR LF line endings, so a checkout that has changed them to LF is packed with LF. |
| 37 | + |
| 38 | +Every project that the IDE exports has the folders `Resources`, `Sources`, `ImportedTypeLibraries`, `Miscellaneous` and `Packages`, even when they are empty. Git does not store an empty folder, so a folder checked out from a repository can lack some of them. **Pack** does not add the missing folders. It prints two lines after the summary line, and still returns **True**: |
| 39 | + |
| 40 | +``` |
| 41 | + note: expected folders not present on disk: Resources, Sources, ImportedTypeLibraries, Miscellaneous, Packages. |
| 42 | + twinBASIC should rebuild them on Open Project. |
| 43 | +``` |
| 44 | + |
| 45 | +## Refusals |
| 46 | + |
| 47 | +**Pack** returns **False** and writes nothing in these cases: |
| 48 | + |
| 49 | +- *inputDir* is not a folder. The line is `Pack failed: Not a directory: ` and the path. |
| 50 | +- *outputFile* exists and *overwrite* is **False**. The call makes this check before it reads the folder. |
| 51 | +- *inputDir* has no `Settings` file directly in it. The file holds the project's name, references, version and compile options, and a project file packed without it would open without them. A folder named `Settings` does not count, and the file name must be spelled exactly `Settings`. The line begins `Pack failed: Refusing to pack: no 'Settings' file at the top level of`. |
| 52 | + |
| 53 | +The line for the second case reads `Output already exists: ` and the path, then `Refusing to overwrite it. Use the overwrite option (CLI: --force) to replace it.` **Pack** has no `--force`: the option the message means is the *overwrite* argument. |
| 54 | + |
| 55 | +## Replacing a project file |
| 56 | + |
| 57 | +With *overwrite* **True**, **Pack** deletes the existing file just before it writes the new one, after the folder has been read. Do not put *outputFile* inside *inputDir*. A project file left there by an earlier call is packed into the new one, and the file grows with each call. |
| 58 | + |
| 59 | +## Output |
| 60 | + |
| 61 | +On success **Pack** prints one summary line: |
| 62 | + |
| 63 | +``` |
| 64 | +Exported "MyApp-src" -> C:\Temp\MyApp.twinproj (170594 bytes, 18 files, 6 directories) |
| 65 | +``` |
| 66 | + |
| 67 | +The name is the root's name, the last folder of *inputDir*. The byte count is the size of the new file, and the file and folder counts do not include the root. |
| 68 | + |
| 69 | +> [!NOTE] |
| 70 | +> The summary line of **Pack** begins `Exported`, and that of [**Unpack**](Unpack) begins `Imported`. The Import/export tool names the two directions the other way round: its `import` command packs a folder and its `export` command unpacks a project. |
| 71 | +
|
| 72 | +## Example |
| 73 | + |
| 74 | +This example packs a folder into a project file in the TEMP folder and replaces the file if it exists. |
| 75 | + |
| 76 | +```tb check_build |
| 77 | +Dim pp As New ProjPacker |
| 78 | +Dim src As String, outFile As String |
| 79 | +
|
| 80 | +src = Environ$("TEMP") & "\MyApp-src" |
| 81 | +outFile = Environ$("TEMP") & "\MyApp.twinproj" |
| 82 | +
|
| 83 | +If pp.Pack(src, outFile, True) Then |
| 84 | + Debug.Print "Packed " & outFile |
| 85 | +Else |
| 86 | + Debug.Print "Nothing was packed" |
| 87 | +End If |
| 88 | +``` |
| 89 | + |
| 90 | +Without *overwrite*, a second call with the same *outFile* fails and prints: |
| 91 | + |
| 92 | +``` |
| 93 | +Pack failed: Output already exists: "C:\Temp\MyApp.twinproj". Refusing to overwrite it. Use the overwrite option (CLI: --force) to replace it. |
| 94 | +``` |
| 95 | + |
| 96 | +## See Also |
| 97 | + |
| 98 | +- [Unpack](Unpack) method -- unpacks a project file into a folder |
| 99 | +- [Test](Test) method -- runs the package's tests |
| 100 | +- [ProjPacker](./) class -- overview, the log file and the paths |
| 101 | +- [Import/export tool](../../../../../Features/Packages/Import-Export-Tool) -- the command-line programs that do the same |
0 commit comments