If you are like me, you will manually go to pubspec.yaml to modify the version when the project is packaged. If it’s a one-man project, this kind of change is acceptable. But if you are a team and the project is packaged through automation, it is painful to change version every time for the packaged branch. If you forget to do so, TestFlight’s package upload will fail and you’ll have to change the version number and start the build all over again. If the team is large, the chance of forgetting to change the version is greater. Therefore, it is necessary to write a program to automatically increase the build number.

Implementation approach

So how does this program work?

Asking the right questions is often half the solution. -- HeisenbergCopy the code

Question: How do I automatically increment the build number of the version in pubspec.yaml when committing code?

  • 1. Use Git hooks to listen for commit timing and trigger our custom script.
  • 2. This script is the version that replaces the pubspec.yaml text
  • 3. Finally, submit the changes to PubSpec. yaml.

Implementation steps

1. Install git_hooks globally:

pub global activate git_hooks
Copy the code

2. Add dependencies to existing projects:

dev_dependencies:
  Git hook
  process_run: 0.112.+ 8 
  git_hooks: ^ 0.1.0 from 
Copy the code
  • Process_run: Used to execute shell commands
  • Git_hooks: Write git hooks scripts using DART

3. Create the hooks file and dart hook file (from project root) :

git_hooks create git_hooks.dart
Copy the code

If the output

create files...                        
All files wrote successful!
0.2s
Copy the code

That means we’ve created it.

Check whether the file is generated successfully:

  • See if there are multiple files in the.git/hooks file such as pre-commit, pre-push, etc.

  • Check to see if the git_links.dart file has been generated in the root directory.

4. Open git_links.dart and write our own script.

import 'package:git_hooks/git_hooks.dart';
import 'package:process_run/shell.dart';

void main(List arguments) {
  Map<Git, UserBackFun> params = {
    Git.commitMsg: commitMsg,
    Git.preCommit: preCommit
  };
  GitHooks.call(arguments, params);
}


Future<bool> commitMsg() async {
  String commitMsg = Utils.getCommitEditMsg();
  // Normalize the commit log
  if (commitMsg.startsWith('Feat:') ||
      commitMsg.startsWith('Fix:') ||
      commitMsg.startsWith('Refactor:') ||
      commitMsg.startsWith('Style:') ||
      commitMsg.startsWith('Docs:') ||
      commitMsg.startsWith('Test:') ||
      commitMsg.startsWith('Chore:') ||
      commitMsg.startsWith('Merge')) {
    return true;
  } else {
    print('Please prefix the submitted copy');
    return false; }}/// Execute before git commit is complete
Future<bool> preCommit() async {
  final shell = Shell();
  // Submit a copy
  String commitMsg = Utils.getCommitEditMsg();
  
  // Get the current branch name
  var branchRes = await shell.run('git branch --show-current');
  String branch = branchRes.first.stdout;

  bool increase_build_num = false;

  // Automatically increase build number timing
  if (commitMsg.startsWith("Merge")) {
    increase_build_num = false;
  } else {
    if (branch.startsWith('v')) {

      increase_build_num = true;
    } else {
      if (commitMsg.startsWith("Feat") || commitMsg.startsWith("Fix")) {
        increase_build_num = true;
      } else {
        increase_build_num = false; }}}if (increase_build_num) {
    try {
      /// Add the build number
      var result = await shell.run('sh increase_build_num.sh');
      print('$result');
      return true;
    } catch (e) {
      return false; }}else {
    return true; }}Copy the code

5. Increase_build_num. Sh Script that automatically adds build numbers:

perl -i -pe 's/^(version:\s+\d+\.\d+\.) (\d+)(\+)(\d+)$/$1.$2.$3.($4+1)/e' pubspec.yaml
git add pubspec.yaml
Copy the code

instructions

Through the above five steps, we have achieved the desired functionality. Here’s a simple explanation.

Git Hooks

Git Hooks are scripts that trigger to run after Git executes specific events such as commit, push, receive, and so on.

Using it, you can

  • Executing unit tests
  • Check the code
  • Perform code formatting
  • Code is automatically deployed after submission
  • Other operations required for software engineering
  • .

If you want to know more, read the official documentation

git_hooks

This is a Dart command line plug-in and a Dart plug-in.

For more information, please refer to the official documentation

process_run

Dart is also a Dart plugin that facilitates Dart calls. Please browse the documentation

git_hooks.dart

This is our hook script. We hook two events: git.mitmsg and git.precommit

On commitMsg, we normalize the committed logs. If the prefix is not one of Feat: Fix: Refactor: Style: Docs: Test: Chore: Merge, the commit cannot be completed.

In preCommit, we first get the branch name and commit the log. Then control whether the build number needs to be added through the following process

increase_build_num.sh

Since the build can be quickly implemented by shell commands, these functions are separated into shell scripts.

shell.run(r"perl -i -pe 's/^(version:\s+\d+\.\d+\.) (\d+)(\+)(\d+)$/$1.$2.$3.($4+1)/e' pubspec.yaml")
Copy the code

Failed to execute.

The end of this article, if you have any questions, please follow the official wechat account OldBirds