0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

はじめに

この記事はどすこい塾 Advent Calendar 2025 21日目の記事です。

こんにちは、どすこいです。
今回はDartでスクリプトを書いていきます。

Flutter開発をしていたり、Dartで何かしらを作っているときにちょろっとスクリプトを書きたくなる場面があると思います。僕はあります。

シェルスクリプトや別の言語を使わずにDartだけでスクリプトも書けちゃうんです。今日はそれを紹介していきたいと思います。

環境

  • Dart 3.10.7

書いていく

めっちゃ簡単です。
ファイルは以下の2つで十分です。

pubspec.yaml
name: script_example

environment:
    sdk: ^3.10.7
bin/script_example.dart
void main() {
    print('Hello, World!');
}

はい、これで終わりです。
あとはプロジェクトのルートで

dart run bin/script_example.dart

を実行すればOKです。

なんでbin?

正直スクリプトだけであれば、binディレクトリにある必要はなくてlibでも任意のディレクトリで大丈夫です。
後述しますが、binディレクトリにあるといろいろ便利なのです。

補足

dart runで直接dartファイルを実行する際はJITコンパイルで実行されます。
まぁスクリプトなのでめちゃくちゃ早く起動したい!ネイティブバイナリで実行したい!みたいなケースはそう多くないと思います。そういう場合は素直にdart compile exeしてネイティブバイナリにしてください。

Executable

pubspec.yamlでの明示的な指定

実行可能ファイルはpubspec.yamlのexecutablesセクションで明示的に指定することもできます。

pubspec.yaml
name: script_example
...

executable:
    script_example: script_example # bin/以下のファイル名(拡張子なし)

この設定をすると、dart run :script_exampleのようにコロン記法で実行できます。

別名をつけることも可能です。

pubspec.yaml
executable:
    hello: script_example

こうするとdart run :helloというように実行できます。

binディレクトリの自動認識

実はexecutableを明示的に指定しなくても、bin/ディレクトリに置いたDartファイルは自動的にexecutableとして認識されます。
例えばbin/script_example.dartというファイルがあれば、pubspec.yamlに何も書かなくても
dart run :script_example
で実行できます。Dartがbinディレクトリを走査して、自動的にexecutableとして登録してくれるわけです。

具体例

pubspec.yamlに書いてあるバージョンをインクリメントするようなスクリプトを書きましょう

bump_version.dart
import 'dart:io';

/// pubspec.yamlのバージョンをインクリメントするスクリプト
///
/// 使い方:
///   dart run :bump_version [major|minor|patch]
///
/// 例:
///   dart run :bump_version patch  # 1.0.0 -> 1.0.1
///   dart run :bump_version minor  # 1.0.0 -> 1.1.0
///   dart run :bump_version major  # 1.0.0 -> 2.0.0
void main(List<String> arguments) {
  final bumpType = arguments.isNotEmpty ? arguments.first : 'patch';

  if (!['major', 'minor', 'patch'].contains(bumpType)) {
    print('Error: 引数は major, minor, patch のいずれかを指定してください');
    exit(1);
  }

  final pubspecFile = File('pubspec.yaml');
  if (!pubspecFile.existsSync()) {
    print('Error: pubspec.yaml が見つかりません');
    exit(1);
  }

  final content = pubspecFile.readAsStringSync();
  final versionRegex = RegExp(r'version:\s*(\d+)\.(\d+)\.(\d+)');
  final match = versionRegex.firstMatch(content);

  if (match == null) {
    print('Error: pubspec.yaml に version が見つかりません');
    exit(1);
  }

  var major = int.parse(match.group(1)!);
  var minor = int.parse(match.group(2)!);
  var patch = int.parse(match.group(3)!);

  final oldVersion = '$major.$minor.$patch';

  switch (bumpType) {
    case 'major':
      major++;
      minor = 0;
      patch = 0;
    case 'minor':
      minor++;
      patch = 0;
    case 'patch':
      patch++;
  }

  final newVersion = '$major.$minor.$patch';
  final newContent = content.replaceFirst(versionRegex, 'version: $newVersion');

  pubspecFile.writeAsStringSync(newContent);
  print('✨ バージョンを更新しました: $oldVersion -> $newVersion');
}

あとはdart run :bump_version major, dart run :bump_version minor, dart run :bump_version patchを実行すれば良いですね

最後に

良いDartスクリプトライフを!

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?