Reputation: 2575
Suppose global_bin, a package's /bin file that is activated via
pub global activate global_bin
I need that global_bin searches in the package located in the directory that executed it a file that contains a class definition and then instantiates it.
i.e. :
// $HOME/package/lib/class.dart
class Clazz{
int number = 42;
}
Then in the command line:
cd $HOME/package
global_bin --echo number
# 42
Upvotes: 0
Views: 311
Reputation: 486
You cannot load an arbitrary library in the same isolate. Two things to explore:
1) Spawn a new isolate using https://api.dartlang.org/stable/1.23.0/dart-isolate/Isolate/spawnUri.html -- you give spawnUri a new entrypoint, which can be in any file, and it loads new dart code.
2) It sounds like you're looking to load code that is not ready to run in an isolate -- does not have its own "main" method. So you will have to generate code. In your example, you will need to generate code that imports Clazz, imports the engine that is going to look up symbols by name, then runs it:
import 'package:foo/class.dart';
import 'package:global_bin/runner.dart' as runner;
void main() {
runner.run();
}
Now your runner.dart can use mirrors to find class.dart and inspect it.
Finally, for actually finding the files -- you can just use dart's standard file functions, i.e. dart:io.
Upvotes: 3