|
| 1 | +// Copyright 2013 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import 'package:meta/meta.dart'; |
| 6 | +import 'package:path/path.dart' as p; |
| 7 | + |
| 8 | +import 'repository_package.dart'; |
| 9 | + |
| 10 | +/// The state of a package on disk relative to git state. |
| 11 | +@immutable |
| 12 | +class PackageChangeState { |
| 13 | + /// Creates a new immutable state instance. |
| 14 | + const PackageChangeState({ |
| 15 | + required this.hasChanges, |
| 16 | + required this.hasChangelogChange, |
| 17 | + required this.needsVersionChange, |
| 18 | + }); |
| 19 | + |
| 20 | + /// True if there are any changes to files in the package. |
| 21 | + final bool hasChanges; |
| 22 | + |
| 23 | + /// True if the package's CHANGELOG.md has been changed. |
| 24 | + final bool hasChangelogChange; |
| 25 | + |
| 26 | + /// True if any changes in the package require a version change according |
| 27 | + /// to repository policy. |
| 28 | + final bool needsVersionChange; |
| 29 | +} |
| 30 | + |
| 31 | +/// Checks [package] against [changedPaths] to determine what changes it has |
| 32 | +/// and how those changes relate to repository policy about CHANGELOG and |
| 33 | +/// version updates. |
| 34 | +/// |
| 35 | +/// [changedPaths] should be a list of POSIX-style paths from a common root, |
| 36 | +/// and [relativePackagePath] should be the path to [package] from that same |
| 37 | +/// root. Commonly these will come from `gitVersionFinder.getChangedFiles()` |
| 38 | +/// and `getRelativePoixPath(package.directory, gitDir.path)` respectively; |
| 39 | +/// they are arguments mainly to allow for caching the changed paths for an |
| 40 | +/// entire command run. |
| 41 | +PackageChangeState checkPackageChangeState( |
| 42 | + RepositoryPackage package, { |
| 43 | + required List<String> changedPaths, |
| 44 | + required String relativePackagePath, |
| 45 | +}) { |
| 46 | + final String packagePrefix = relativePackagePath.endsWith('/') |
| 47 | + ? relativePackagePath |
| 48 | + : '$relativePackagePath/'; |
| 49 | + |
| 50 | + bool hasChanges = false; |
| 51 | + bool hasChangelogChange = false; |
| 52 | + bool needsVersionChange = false; |
| 53 | + for (final String path in changedPaths) { |
| 54 | + // Only consider files within the package. |
| 55 | + if (!path.startsWith(packagePrefix)) { |
| 56 | + continue; |
| 57 | + } |
| 58 | + final String packageRelativePath = path.substring(packagePrefix.length); |
| 59 | + hasChanges = true; |
| 60 | + |
| 61 | + final List<String> components = p.posix.split(packageRelativePath); |
| 62 | + if (components.isEmpty) { |
| 63 | + continue; |
| 64 | + } |
| 65 | + final bool isChangelog = components.first == 'CHANGELOG.md'; |
| 66 | + if (isChangelog) { |
| 67 | + hasChangelogChange = true; |
| 68 | + } |
| 69 | + |
| 70 | + if (!needsVersionChange && |
| 71 | + !isChangelog && |
| 72 | + // One of a few special files example will be shown on pub.dev, but for |
| 73 | + // anything else in the example publishing has no purpose. |
| 74 | + !(components.first == 'example' && |
| 75 | + !<String>{'main.dart', 'readme.md', 'example.md'} |
| 76 | + .contains(components.last.toLowerCase())) && |
| 77 | + // Changes to tests don't need to be published. |
| 78 | + !components.contains('test') && |
| 79 | + !components.contains('androidTest') && |
| 80 | + !components.contains('RunnerTests') && |
| 81 | + !components.contains('RunnerUITests') && |
| 82 | + // The top-level "tool" directory is for non-client-facing utility code, |
| 83 | + // so doesn't need to be published. |
| 84 | + components.first != 'tool' && |
| 85 | + // Ignoring lints doesn't affect clients. |
| 86 | + !components.contains('lint-baseline.xml')) { |
| 87 | + needsVersionChange = true; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + return PackageChangeState( |
| 92 | + hasChanges: hasChanges, |
| 93 | + hasChangelogChange: hasChangelogChange, |
| 94 | + needsVersionChange: needsVersionChange); |
| 95 | +} |
0 commit comments