p-hatan/ mirror

pub repository

mirror.p-hatan.ir

scratch_space

1.0.1

A tool to manage running external executables within package:build

Download archive

Readme

Pub Package

A ScratchSpace is a thin wrapper around a temporary directory. The constructor takes zero arguments, so making one is as simple as doing ScratchSpace().

In general, you should wrap a ScratchSpace in a Resource, so that you can re-use the scratch space across build steps in an individual build. This is safe to do since you are not allowed to overwrite files within a build.

This should look something like the following:

final myScratchSpaceResource =
    Resource(() => ScratchSpace(), dispose: (old) => old.delete());

And then you can get access to it through the BuildStep#fetchResource api:

class MyBuilder extends Builder {
  Future<void> build(BuildStep buildStep) async {
    var scratchSpace = await buildStep.fetchResource(myScratchSpaceResource);
  }
}

Adding assets to a ScratchSpace

To add assets to the ScratchSpace, you use the ensureAssets method, which takes an Iterable<AssetId> and an AssetReader (for which you should generally pass your BuildStep which implements that interface).

You must always call this method with all assets that your external executable might need in order to set up the proper dependencies and ensure hermetic builds.

Note: It is important to note that the ScratchSpace does not guarantee that the version of a file within it is the most updated version, only that some version of it exists. For this reason you should create a new ScratchSpace for each build using the Resource class as recommended above.

Deleting a ScratchSpace

When you are done with a ScratchSpace you should call delete on it to make sure it gets cleaned up, otherwise you can end up filling up the users tmp directory.

Note: You cannot delete individual assets from a ScratchSpace today, you can only delete the entire thing. If you have a use case for deleting individual files you can file an issue.

Getting the actual File objects for a ScratchSpace

When invoking an external binary, you probably need to tell it where to look for files. There are a few fields/methods to help you do this:

  • String get packagesDir: The packages directory in the ScratchSpace.
  • String get tmpDir: The root temp directory for the ScratchSpace.
  • File fileFor(AssetId id): The File object for id.

Copying back outputs from the temp directory

After running your executable, you most likely have some outputs that you need to notify the build system about. To do this you can use the copyOutput method, which takes an AssetId and AssetWriter (for which you should generally pass your BuildStep which implements that interface).

This will copy the asset referenced by the AssetId from the temp directory back to your actual output directory.

Feature requests and bugs

Please file feature requests and bugs at the issue tracker.

Changelog

1.0.1

  • Drop package:pedantic dependency and replace it with package:lints.

1.0.0

  • Migrate to null-safety
  • Update to build 2.x.

0.0.4+3

  • Change returns from Future<dynamic> to Future<void>.
  • Increase min sdk constraint to >=2.7.0.
  • Allow the null safe pre-releases of all migrated deps.

0.0.4+2

  • Fix a race condition bug where ensureAssets would complete before all pending writes were completed. If the next build was scheduled before these writes finished then they would get the old result.

0.0.4+1

  • Fix ScratchSpace.fileFor on windows to normalize the paths so they dont contain mixed separators.

0.0.4

  • Add requireContent argument to copyOutput to allow asserting that a file produced in a scratch space is not empty.

0.0.3+2

  • Declare support for package:build version 1.x.x.

0.0.3+1

  • Increased the upper bound for the sdk to <3.0.0.

0.0.3

  • Use digests to improve ensureAssets performance.
    • Scratch spaces can now be used across builds without cleaning them up, and will check digests and update assets as needed.

0.0.2+1

  • Fix a bug where failing to read an asset in serve mode could get the build stuck.

0.0.2

  • Allow creating files at the root of the scratch space.

0.0.1+3

  • Allow package:build 0.12.0

0.0.1+2

  • Allow package:build 0.11.0

0.0.1+1

  • Fix a deadlock issue around the file descriptor pool, only take control of a resource right before actually touching disk instead of also encapsulating the readAsBytes call from the wrapped AssetReader.

0.0.1

  • Initial release, adds the ScratchSpace class.

Installing

export PUB_HOSTED_URL=https://pub.mirror.p-hatan.ir
flutter pub add scratch_space
pubspec.yaml
name: scratch_space
version: 1.0.1
description: A tool to manage running external executables within package:build
repository: https://github.com/dart-lang/build/tree/master/scratch_space

environment:
  sdk: ">=2.14.0 <3.0.0"

dependencies:
  build: ^2.0.0
  crypto: ^3.0.0
  path: ^1.8.0
  pool: ^1.5.0

dev_dependencies:
  build_runner: ^2.0.0
  build_test: ^2.0.0
  lints: ^1.0.0
  test: ^1.16.0

Version history

VersionPublishedArchiveSHA-256
0.0.12017-09-21T22:01:50.421639ZDownloadb6a16d5a1a3441b9f16fc8c9918fc5ea0670c7a84766b054032aa988ac5f4e69
0.0.1+12017-10-04T14:28:30.570541ZDownload74af59597c6122924580d041db91cd91e734a24e7b0b6df971afc4f7d800040a
0.0.1+22017-11-02T23:58:33.418034ZDownload6dee239c51342744a62bd746e0946e0ef7a198f3cc30564f6dbf50c895e40d18
0.0.1+32018-01-18T22:20:57.513143ZDownloadf7434b33cb336c9ee4effdef0a956d5e9d26891d93d3f94cc28d81f59ded4d1f
0.0.22018-03-19T21:04:59.476483ZDownload8a2dfbd8618687610aa5cf420275e522059585979fae0f898ec031e96a3f1a6e
0.0.2+12018-04-21T03:01:57.370616ZDownload77d391e4324f3d5f57ecbacde3ebe149e8da6d5f49e1111e4b757677214a63eb
0.0.32018-06-22T18:09:54.469588ZDownload9bf32903655f8d5ada379d79072d7d68a2a3fe0284aefdf4c89d5defaf61f904
0.0.3+12018-07-16T21:35:48.973437ZDownload39300b29507600be6871b9fde69567c775e499dff98f16751dc6127e35f48ab4
0.0.3+22018-09-25T21:26:40.941501ZDownloade47f5e239d55047281c652be897bb62994c481056c32430a679f8bf2bec8ef12
0.0.42019-07-30T23:07:56.500173ZDownloada78131ffe281344fe61c240334d6756856607ece3f58da39d94bbfc0d2ee77d8
0.0.4+12019-10-08T20:35:56.667512ZDownload38547b3f3704cad164ba287469998e4066b4e6d1a0274f9c88ce8d3a6cee870c
0.0.4+22020-01-06T20:03:55.238735ZDownload6c0b147674612d505ef80d9a2f1d75ef28ca7cdb69f41518563ce3ecb32ce672
0.0.4+32021-01-07T16:58:31.613073ZDownloaddb2e494122d81e280fc2a322f9378bb758519bfc122f70b2c12bafa3f4dd3e92
1.0.02021-03-12T21:18:50.921093ZDownloadb4905a1f78a61c2306b2a2587a0cf30ab9487ea3eff6cd6fefc3a9873c3e9e6e
1.0.12021-10-01T14:47:09.962569ZDownloada469a9642a4d7ee406d6224a85446eb8baa9dd6d81e2f0b76770deae7bd32aab
1.0.22023-05-12T15:18:21.681714ZDownload8510fbff458d733a58fc427057d1ac86303b376d609d6e1bc43f240aad9aa445
1.1.02025-06-25T08:32:57.995630ZDownload816989dd0a1f92cd5f0db012ed330035571034956dc3593fba66aaa6ee6a7e43
1.1.12025-09-02T11:30:32.677472ZDownloadefe807ddf27b148b500465957ceee4255deb4a8558575530b6398840436300ce
1.2.02025-11-04T22:08:19.982741ZDownload3417e014d20b12cebc5bfb1c0b1f63806054177158596cc31cc4d9aaca767a60
1.2.12026-05-18T18:16:01.960596ZDownload564e4d0cef547454171ce97b1ea3ec70fa9bc5b3b9db896a620dd5930dc5e8b8
1.2.22026-06-16T10:01:39.335043ZDownload55f141cf286eca871f739518b689af61cb994d671f3fd8efabd63aba7578c309

Dependencies

build "^2.0.0"

crypto "^3.0.0"

path "^1.8.0"

pool "^1.5.0"