Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Run examples from own directory #834

Merged
merged 14 commits into from
Jan 10, 2025
Merged
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
Loading