Skip to content

Commit

Permalink
fix: Run examples from own directory (#834)
Browse files Browse the repository at this point in the history
<!--
  Thanks for contributing!

Provide a description of your changes below and a general summary in the
title

Please look at the following checklist to ensure that your PR can be
accepted quickly:
-->

## Description

Due to dart-lang/pub#4486 examples must run
`exec` from their own directories (not sure why this wasn't the case
from the beginning).

## Type of Change

<!--- Put an `x` in all the boxes that apply: -->

- [ ] ✨ `feat` -- New feature (non-breaking change which adds
functionality)
- [x] 🛠️ `fix` -- Bug fix (non-breaking change which fixes an issue)
- [ ] ❌ `!` -- Breaking change (fix or feature that would cause existing
functionality to change)
- [ ] 🧹 `refactor` -- Code refactor
- [ ] ✅ `ci` -- Build configuration change
- [ ] 📝 `docs` -- Documentation
- [ ] 🗑️ `chore` -- Chore
  • Loading branch information
spydon authored Jan 10, 2025
1 parent ade758c commit e4ade76
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 39 deletions.
23 changes: 12 additions & 11 deletions docs/environment-variables.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,18 @@ The path of the package that's currently executing a script (when using

### `MELOS_PARENT_PACKAGE_NAME`

The name of the parent package of the current package that's currently executing
a script (when using `melos exec`).
The name of the parent package of the example that's currently being executed
in a script (when using `melos exec`).

### `MELOS_PARENT_PACKAGE_VERSION`

The version of the parent package of the current package that's currently
executing a script (when using `melos exec`).
The version of the parent package of the example that's currently being executed
in a script (when using `melos exec`).

### `MELOS_PARENT_PACKAGE_PATH`

The path of the parent package of the current package that's currently executing
a script (when using `melos exec`).
The path of the parent package of the example that's currently being executed
in a script (when using `melos exec`).

### `MELOS_PUBLISH_DRY_RUN`

Expand All @@ -55,13 +55,14 @@ Whether the current publish is a dry run or not. This is `true` when running

#### What is a 'parent package'

If a package exists in a directory that is also a child of another package, then
its parent is the 'parent package'. For example; the package `firebase_auth` has
a `example` directory that is also a package, when running a `melos exec` script
in the `example` package then the parent package would be `firebase_auth`.
If an example package exists in a directory that is a child of another package,
then its parent is the 'parent package'. For example; the package
`firebase_auth` has an `example` directory that is also a package, when running
a `melos exec` script in the `example` package then the parent package would be
`firebase_auth`.

Note, this 'parent package' functionality only currently works when the 'child
package' name ends with `example`
package' name ends with `example`.

## User Defined

Expand Down
2 changes: 1 addition & 1 deletion packages/melos/lib/src/commands/exec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ mixin _ExecMixin on _Melos {
logger: logger,
environment: environment,
workingDirectory: package.path,
prefix: prefixLogs ? packagePrefix : null,
logPrefix: prefixLogs ? packagePrefix : null,
// The parent env is injected manually above
includeParentEnvironment: false,
);
Expand Down
2 changes: 1 addition & 1 deletion packages/melos/lib/src/commands/format.dart
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ mixin _FormatMixin on _Melos {
logger: logger,
environment: environment,
workingDirectory: package.path,
prefix: prefixLogs ? packagePrefix : null,
logPrefix: prefixLogs ? packagePrefix : null,
);
}
}
6 changes: 3 additions & 3 deletions packages/melos/lib/src/common/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ List<int> get runningPids => UnmodifiableListView(_runningPids);

Future<int> startCommand(
List<String> command, {
String? prefix,
String? logPrefix,
Map<String, String> environment = const {},
String? workingDirectory,
bool onlyOutputOnError = false,
Expand All @@ -466,14 +466,14 @@ Future<int> startCommand(
var stdoutStream = process.stdout;
var stderrStream = process.stderr;

if (prefix != null && prefix.isNotEmpty) {
if (logPrefix != null && logPrefix.isNotEmpty) {
final pluginPrefixTransformer =
StreamTransformer<String, String>.fromHandlers(
handleData: (data, sink) {
const lineSplitter = LineSplitter();
var lines = lineSplitter.convert(data);
lines = lines
.map((line) => '$prefix$line${line.contains('\n') ? '' : '\n'}')
.map((line) => '$logPrefix$line${line.contains('\n') ? '' : '\n'}')
.toList();
sink.add(lines.join());
},
Expand Down
25 changes: 2 additions & 23 deletions packages/melos/lib/src/package.dart
Original file line number Diff line number Diff line change
Expand Up @@ -987,29 +987,8 @@ class Package {
return pubClient.fetchPackage(name);
}

/// The example [Package] contained within this package, if any.
///
/// A package is considered to be an example if it is located in the `example`
/// directory of the [enclosingPackage].
late final Package? examplePackage = () {
final examplePath = p.join(path, 'example');
return _packageMap.values
.firstWhereOrNull((package) => p.equals(package.path, examplePath));
}();

/// The [Package] that encloses this package, if any.
///
/// A package is considered to be the enclosing package if this package is
/// located in a direct child directory of the enclosing package.
late final Package? enclosingPackage = () {
final enclosingPackagePath = p.dirname(path);
return _packageMap.values.firstWhereOrNull(
(package) => p.equals(package.path, enclosingPackagePath),
);
}();

/// Whether this package is an example package as defined by [examplePackage].
bool get isExample => enclosingPackage?.examplePackage == this;
/// Whether this package is an example package.
bool get isExample => p.dirname(path).endsWith('example');

/// Returns whether this package is a Flutter app.
///
Expand Down

0 comments on commit e4ade76

Please sign in to comment.