Skip to content

RamanSharma100/pubspec_runtime

Repository files navigation

Pubspec Runtime

A Dart package that provides a simple way to manuplate the pubspec.yaml file at runtime.

Features

  • Add dependencies to the pubspec.yaml file.
  • Add dev dependencies to the pubspec.yaml file.
  • Add key value pairs to the pubspec.yaml file.
  • Get the value of a key from the pubspec.yaml file.
  • Remove key value pairs from the pubspec.yaml file.
  • Save changes to the pubspec.yaml file.
  • Run pub get command.
  • Parse the pubspec.yaml file 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

  • PubspecEditor class is the main class that provides the functionality to manipulate the pubspec.yaml file.
    • dependencies property is an instance of the DependencyManager class that provides the functionality to manipulate the dependencies in the pubspec.yaml file.
    • devDependencies property is an instance of the DependencyManager class that provides the functionality to manipulate the dev dependencies in the pubspec.yaml file.
    • parser property is an instance of the Parser class that provides the functionality to parse the pubspec.yaml file.
    • add function is used to add a key value pair to the pubspec.yaml file.
    • get function is used to get the value of a key from the pubspec.yaml file.
    • remove function is used to remove a key value pair from the pubspec.yaml file.
    • save function is used to save the changes to the pubspec.yaml file.
    • runPubGet function is used to run the pub get command.
    • exists function is used to check if a key exists in the pubspec.yaml file.
  • Dependency class is a simple class that represents a dependency in the pubspec.yaml file.
    • name property is the name of the dependency.
    • version property is the version of the dependency.
    • isDev property is a boolean value that indicates whether the dependency is a dev dependency or not.
  • DependencyManager class is a class that provides the functionality to manage dependencies as objects.
    • add function is used to add a dependency to the pubspec.yaml file.
    • remove function is used to remove a dependency from the pubspec.yaml file.
    • list property is used to get the list of dependencies in the pubspec.yaml file.
  • Parser class is a class that provides the functionality to parse the pubspec.yaml file.
    • parseFile function is used to parse the pubspec.yaml file and return the content as a map.
    • parse function is used to parse the content of the pubspec.yaml file and return it as a map.
    • mapToYamlString function is used to convert a map to a YAML string.

License

MIT License

Author

Raman Sharma

Contributors

Raman Sharma

  • 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! 😊