|
| 1 | +import argparse |
| 2 | +import re |
| 3 | +import subprocess |
| 4 | + |
| 5 | +# 更新対象のpodspecファイル |
| 6 | +PODSPEC_FILE = "Sora.podspec" |
| 7 | + |
| 8 | +# 更新対象のPackageInfoファイル |
| 9 | +PACKAGEINFO_FILE = "Sora/PackageInfo.swift" |
| 10 | + |
| 11 | + |
| 12 | +def update_sdk_version(podspec_content): |
| 13 | + """ |
| 14 | + Sora.podspecファイルの内容からバージョンを更新する |
| 15 | +
|
| 16 | + Args: |
| 17 | + podspec_content (list): podspecファイルの各行を要素とするリスト |
| 18 | +
|
| 19 | + Returns: |
| 20 | + tuple: (更新後のファイル内容のリスト, 新しいバージョン文字列) |
| 21 | +
|
| 22 | + Raises: |
| 23 | + ValueError: バージョン指定が見つからない場合 |
| 24 | + """ |
| 25 | + updated_content = [] |
| 26 | + sdk_version_updated = False |
| 27 | + new_version = None |
| 28 | + |
| 29 | + for line in podspec_content: |
| 30 | + line = line.rstrip() # 末尾の改行のみを削除 |
| 31 | + if "s.version" in line: |
| 32 | + # バージョン行のパターンマッチング |
| 33 | + # 例: s.version = "1.0.0" や s.version = "1.0.0-canary.1" にマッチ |
| 34 | + version_match = re.match( |
| 35 | + r'\s*s\.version\s*=\s*[\'"](\d+\.\d+\.\d+)(-canary\.(\d+))?[\'"]', line |
| 36 | + ) |
| 37 | + if version_match: |
| 38 | + major_minor_patch = version_match.group(1) # 基本バージョン (例: 1.0.0) |
| 39 | + canary_suffix = version_match.group(2) # canaryサフィックス部分 |
| 40 | + |
| 41 | + # canaryサフィックスが無い場合は.0から開始、ある場合は番号をインクリメント |
| 42 | + if canary_suffix is None: |
| 43 | + new_version = f"{major_minor_patch}-canary.0" |
| 44 | + else: |
| 45 | + canary_number = int(version_match.group(3)) |
| 46 | + new_version = f"{major_minor_patch}-canary.{canary_number + 1}" |
| 47 | + |
| 48 | + # podspecのバージョン行を更新 |
| 49 | + updated_content.append(f' s.version = "{new_version}"') |
| 50 | + sdk_version_updated = True |
| 51 | + else: |
| 52 | + updated_content.append(line) |
| 53 | + else: |
| 54 | + updated_content.append(line) |
| 55 | + |
| 56 | + if not sdk_version_updated: |
| 57 | + raise ValueError("Version specification not found in Sora.podspec file.") |
| 58 | + |
| 59 | + return updated_content, new_version |
| 60 | + |
| 61 | + |
| 62 | +def update_packageinfo_version(packageinfo_content): |
| 63 | + """ |
| 64 | + PackageInfo.swiftファイルの内容からバージョンを更新する |
| 65 | +
|
| 66 | + Args: |
| 67 | + packageinfo_content (list): PackageInfo.swiftファイルの各行を要素とするリスト |
| 68 | +
|
| 69 | + Returns: |
| 70 | + tuple: (更新後のファイル内容のリスト, 新しいバージョン文字列) |
| 71 | +
|
| 72 | + Raises: |
| 73 | + ValueError: バージョン指定が見つからない場合 |
| 74 | + """ |
| 75 | + updated_content = [] |
| 76 | + sdk_version_updated = False |
| 77 | + new_version = None |
| 78 | + |
| 79 | + for line in packageinfo_content: |
| 80 | + line = line.rstrip() # 末尾の改行のみを削除 |
| 81 | + if "public static let version" in line: |
| 82 | + # バージョン行のパターンマッチング |
| 83 | + version_match = re.match( |
| 84 | + r'\s*public\s+static\s+let\s+version\s*=\s*[\'"](\d+\.\d+\.\d+)(-canary\.(\d+))?[\'"]', |
| 85 | + line, |
| 86 | + ) |
| 87 | + if version_match: |
| 88 | + major_minor_patch = version_match.group(1) # 基本バージョン (例: 1.0.0) |
| 89 | + canary_suffix = version_match.group(2) # canaryサフィックス部分 |
| 90 | + |
| 91 | + # canaryサフィックスが無い場合は.0から開始、ある場合は番号をインクリメント |
| 92 | + if canary_suffix is None: |
| 93 | + new_version = f"{major_minor_patch}-canary.0" |
| 94 | + else: |
| 95 | + canary_number = int(version_match.group(3)) |
| 96 | + new_version = f"{major_minor_patch}-canary.{canary_number + 1}" |
| 97 | + |
| 98 | + # PackageInfoのバージョン行を更新 |
| 99 | + updated_content.append( |
| 100 | + f' public static let version = "{new_version}"' |
| 101 | + ) |
| 102 | + sdk_version_updated = True |
| 103 | + else: |
| 104 | + updated_content.append(line) |
| 105 | + else: |
| 106 | + updated_content.append(line) |
| 107 | + |
| 108 | + if not sdk_version_updated: |
| 109 | + raise ValueError("Version specification not found in PackageInfo.swift file.") |
| 110 | + |
| 111 | + return updated_content, new_version |
| 112 | + |
| 113 | + |
| 114 | +def write_file(filename, updated_content, dry_run): |
| 115 | + """ |
| 116 | + 更新後の内容をファイルに書き込む |
| 117 | +
|
| 118 | + Args: |
| 119 | + filename (str): 書き込み対象のファイル名 |
| 120 | + updated_content (list): 更新後のファイル内容 |
| 121 | + dry_run (bool): True の場合は実際の書き込みを行わない |
| 122 | + """ |
| 123 | + if dry_run: |
| 124 | + print(f"Dry run: The following changes would be written to {filename}:") |
| 125 | + print("\n".join(updated_content)) |
| 126 | + else: |
| 127 | + with open(filename, "w") as file: |
| 128 | + file.write("\n".join(updated_content) + "\n") |
| 129 | + print(f"{filename} updated.") |
| 130 | + |
| 131 | + |
| 132 | +def git_operations(new_version, dry_run): |
| 133 | + """ |
| 134 | + Git操作(コミット、タグ付け、プッシュ)を実行 |
| 135 | +
|
| 136 | + Args: |
| 137 | + new_version (str): 新しいバージョン文字列(タグとして使用) |
| 138 | + dry_run (bool): True の場合は実際のGit操作を行わない |
| 139 | + """ |
| 140 | + commit_message = ( |
| 141 | + f"[canary] Update Sora.podspec and PackageInfo.swift version to {new_version}" |
| 142 | + ) |
| 143 | + |
| 144 | + if dry_run: |
| 145 | + # dry-run時は実行されるコマンドを表示のみ |
| 146 | + print(f"Dry run: Would execute git add {PODSPEC_FILE} {PACKAGEINFO_FILE}") |
| 147 | + print(f"Dry run: Would execute git commit -m '{commit_message}'") |
| 148 | + print(f"Dry run: Would execute git tag {new_version}") |
| 149 | + print(f"Dry run: Would execute git push origin develop") |
| 150 | + print(f"Dry run: Would execute git push origin {new_version}") |
| 151 | + else: |
| 152 | + # ファイルをステージング |
| 153 | + print(f"Executing: git add {PODSPEC_FILE} {PACKAGEINFO_FILE}") |
| 154 | + subprocess.run(["git", "add", PODSPEC_FILE, PACKAGEINFO_FILE], check=True) |
| 155 | + |
| 156 | + # 変更をコミット |
| 157 | + print(f"Executing: git commit -m '{commit_message}'") |
| 158 | + subprocess.run(["git", "commit", "-m", commit_message], check=True) |
| 159 | + |
| 160 | + # バージョンタグを作成 |
| 161 | + print(f"Executing: git tag {new_version}") |
| 162 | + subprocess.run(["git", "tag", new_version], check=True) |
| 163 | + |
| 164 | + # developブランチをプッシュ |
| 165 | + print("Executing: git push origin develop") |
| 166 | + subprocess.run(["git", "push", "origin", "develop"], check=True) |
| 167 | + |
| 168 | + # タグをプッシュ |
| 169 | + print(f"Executing: git push origin {new_version}") |
| 170 | + subprocess.run(["git", "push", "origin", new_version], check=True) |
| 171 | + |
| 172 | + |
| 173 | +def main(): |
| 174 | + """ |
| 175 | + メイン処理: |
| 176 | + 1. コマンドライン引数の解析 |
| 177 | + 2. Sora.podspec ファイルの読み込みと更新 |
| 178 | + 3. PackageInfo.swiftファイルの読み込みと更新 |
| 179 | + 4. Git操作の実行 |
| 180 | + """ |
| 181 | + parser = argparse.ArgumentParser( |
| 182 | + description="Update Sora.podspec & PackageInfo.swift version and push changes with git." |
| 183 | + ) |
| 184 | + parser.add_argument( |
| 185 | + "--dry-run", |
| 186 | + action="store_true", |
| 187 | + help="Perform a dry run without making any changes.", |
| 188 | + ) |
| 189 | + args = parser.parse_args() |
| 190 | + |
| 191 | + # podspecファイルを読み込んでバージョンを更新 |
| 192 | + with open(PODSPEC_FILE, "r") as file: |
| 193 | + podspec_content = file.readlines() |
| 194 | + updated_podspec_content, new_version = update_sdk_version(podspec_content) |
| 195 | + write_file(PODSPEC_FILE, updated_podspec_content, args.dry_run) |
| 196 | + |
| 197 | + # PackageInfoファイルを読み込んでバージョンを更新 |
| 198 | + with open(PACKAGEINFO_FILE, "r") as file: |
| 199 | + packageinfo_content = file.readlines() |
| 200 | + updated_packageinfo_content, _ = update_packageinfo_version(packageinfo_content) |
| 201 | + write_file(PACKAGEINFO_FILE, updated_packageinfo_content, args.dry_run) |
| 202 | + |
| 203 | + # Git操作の実行 |
| 204 | + git_operations(new_version, args.dry_run) |
| 205 | + |
| 206 | + |
| 207 | +if __name__ == "__main__": |
| 208 | + main() |
0 commit comments