Pubspec Runtime
A Dart package that provides a simple way to manuplate the pubspec.yaml file at runtime.
Features
- Add dependencies to the
pubspec.yamlfile. - Add dev dependencies to the
pubspec.yamlfile. - Add key value pairs to the
pubspec.yamlfile. - Get the value of a key from the
pubspec.yamlfile. - Remove key value pairs from the
pubspec.yamlfile. - Save changes to the
pubspec.yamlfile. - Run
pub getcommand. - Parse the
pubspec.yamlfile using Parser
Usage
A simple usage example:
import 'package:pubspec_runtime/pubspec_runtime.dart';
void main() async {
final pubspecEditor = PubspecEditor();
print("Dependencies:");
print(pubspecEditor.dependencies.list);
print("Dev Dependencies:");
print(pubspecEditor.devDependencies.list);
print("Adding a dependency...");
pubspecEditor.dependencies
.add(Dependency(name: 'http', version: '^0.13.3', isDev: false));
print("Adding a dev dependency...");
pubspecEditor.devDependencies
.add(Dependency(name: 'http', version: '^0.13.3', isDev: true));
print("Add a key value pair to the `pubspec.yaml` file.");
pubspecEditor.add("author", "John Doe");
print("Get the value of a key from the `pubspec.yaml` file.");
print(pubspecEditor.get("author"));
print("Remove a key value pair from the `pubspec.yaml` file.");
pubspecEditor.remove("author");
print("Updated Dependencies:");
print(pubspecEditor.dependencies.list);
print("Saving changes...");
pubspecEditor.save();
print("Changes saved successfully!");
print("Running pub get...");
runPubGet()
.then((value) => print(
"Pub get executed successfully with exit code: ${value.exitCode}"))
.catchError(
(error) => print("Error occurred while executing pub get: $error"));
}
Simple usage of the parser:
import "package:pubspec_runtime/pubspec_runtime.dart";
void main() {
final Parser parser = Parser();
final String filePath =
"/media/raman/Projects/dart_libs/runtime_yaml/pubspec.yaml";
final Map<String, dynamic> content = parser.parseFile(filePath);
print(content);
}
Usable Functions and Classes
PubspecEditorclass is the main class that provides the functionality to manipulate thepubspec.yamlfile.dependenciesproperty is an instance of theDependencyManagerclass that provides the functionality to manipulate the dependencies in thepubspec.yamlfile.devDependenciesproperty is an instance of theDependencyManagerclass that provides the functionality to manipulate the dev dependencies in thepubspec.yamlfile.parserproperty is an instance of theParserclass that provides the functionality to parse thepubspec.yamlfile.addfunction is used to add a key value pair to thepubspec.yamlfile.getfunction is used to get the value of a key from thepubspec.yamlfile.removefunction is used to remove a key value pair from thepubspec.yamlfile.savefunction is used to save the changes to thepubspec.yamlfile.runPubGetfunction is used to run thepub getcommand.existsfunction is used to check if a key exists in thepubspec.yamlfile.
Dependencyclass is a simple class that represents a dependency in thepubspec.yamlfile.nameproperty is the name of the dependency.versionproperty is the version of the dependency.isDevproperty is a boolean value that indicates whether the dependency is a dev dependency or not.
DependencyManagerclass is a class that provides the functionality to manage dependencies as objects.addfunction is used to add a dependency to thepubspec.yamlfile.removefunction is used to remove a dependency from thepubspec.yamlfile.listproperty is used to get the list of dependencies in thepubspec.yamlfile.
Parserclass is a class that provides the functionality to parse thepubspec.yamlfile.parseFilefunction is used to parse thepubspec.yamlfile and return the content as a map.parsefunction is used to parse the content of thepubspec.yamlfile and return it as a map.mapToYamlStringfunction is used to convert a map to a YAML string.
License
MIT License
Author
Contributors
- Feel free to contribute to this project by creating a pull request or raising an issue.
Issues
- If you find any issues with the package, please raise an issue on the GitHub repository.
Enjoy Coding! 😊
Libraries
- pubspec_runtime
- Support for runtime manipulation of YAML files. Developed by: @ramansharma100