Fastlaneで作ったスクリーンショットをfastlane内で修正するコマンドを作ったときのメモ

     
⌛️ < 1 min.

Fastlaneを使って、自動でスクリーンショットが撮れるのは非常に便利なのですが、AdMobを使っていると、スクリーンショットの広告部分を修正したくなります。

この修正も自動でできると非常に手間が減るので、やってみることにしました。

目標は以下です。

  • fastlaneの1つの処理として、自動で、生成されたスクリーンショット画像の一部に変更を加える(これがやりたいこと)
  • 画像によって、変更すべき箇所が異なる(iPhoneの機種違いやiPadなので、対応必須)
  • 画像変更は、ImageMagickを使う
  • ある程度、プロジェクト固有の設定が発生するのはしょうがない(最初に一度調整したら、あとは、放置できると嬉しい)

fastlaneのaction

自分でactionを作ろうとするとRubyで書く必要があります。

ここに詳細があります。

といっても、数行ですが・・・・

調べてみると、shellを実行するアクションがあったので、自分の好きなPythonでactionを書くことに

action sh

いろいろなアクションがすでにありますが、Miscを読んでいくと、便利そうな”sh”というactionがあることがわかります。

すこし動作を調べてみました。

pwdはどこ?

Pwdコマンドを実行するアクションを作成して実行したところ、

pwd実行結果


[13:01:17]: -----------------
[13:01:17]: --- Step: pwd ---
[13:01:17]: -----------------
[13:01:17]: $ pwd
[13:01:17]: ▸ /........../ExecDir/fastlane

となりました。特に指定しなければ、fastlaneディレクトリで実行されるようです。

python動く?

HelloWorld.py


#!/usr/bin/python
print("Hello world")

上記のコードを、HelloWorld.pyとしてfastlaneディレクトリに配置して、”./HelloWorld.py”をshアクションで実行するようにしたところ、

Python実行結果


[13:01:17]: ▸ /Users/tyagishi/dev/SDSProduct/WeightClip2/fastlane
[13:01:17]: -----------------------------
[13:01:17]: --- Step: ./HelloWorld.py ---
[13:01:17]: -----------------------------
[13:01:17]: $ ./HelloWorld.py
[13:01:17]: ▸ Hello world

ということで、普通に(?)、Pythonが動きそうなので、Pythonでscreenshotsディレクトリ中のファイルをimageMagickを使いアップデートしていくスクリプトを作ります。

画像変更スクリプト

HideAd.jpgという広告部分を隠すための画像を用意して、合成する位置を確認したものが、以下のスクリプト

コード


#!/usr/bin/python

import os
import shutil

TargetDevices = [
    "iPhone 6s Plus",
    "iPhone Xs Max",
    "iPad Pro (12.9-inch) (2nd generation)",
    "iPad Pro (12.9-inch) (3rd generation)"
]
TargetDirs = [
    "screenshots/en-US",
    "screenshots/ja-JP"
]

BackupDirName = "backup/"

def checkDeviceTypeFromFileName(filename):
    for deviceName in TargetDevices:
        if filename.find(deviceName) != -1:
            return deviceName
    return "" # "" means no need to process

def HideAd():
    baseDir = "./"
    for targetDir in TargetDirs:
        # if targetDir has backup, remove it
        targetDirFullPath = baseDir + targetDir
        backupDirectory = targetDirFullPath + "/" + BackupDirName
        if os.path.exists(backupDirectory):
            shutil.rmtree(backupDirectory)
        os.makedirs(backupDirectory)

        for file in os.listdir(baseDir + targetDir) :
            deviceType = checkDeviceTypeFromFileName(file)
            if deviceType == "":
                print("Skipped   : " + file)
                continue

            print("Hiding ads: "+ file)
            fullFilePath = targetDirFullPath + "/" + file

            os.system("mv \"" + fullFilePath + "\" "+ backupDirectory)

            backupedFile = backupDirectory + file
            resultFile = targetDirFullPath + "/" + file
            if deviceType == TargetDevices[0]: # iPhone6sPlus
                ExecImageMagicWithArg("-geometry +0+60", backupedFile, resultFile)
            if deviceType == TargetDevices[1]: # iPhoneXsMax
                ExecImageMagicWithArg("-geometry +0+130", backupedFile, resultFile)
            if deviceType == TargetDevices[2]: # iPadPro Gen2
                ExecImageMagicWithArg("-geometry +0+40", backupedFile, resultFile)
            if deviceType == TargetDevices[3]: # iPadPro Gen3
                ExecImageMagicWithArg("-geometry +0+40", backupedFile, resultFile)
    return

def ExecImageMagicWithArg(position, origfile, targetfile):
    command = "composite -gravity north " + position + " -compose over HideAd.jpg \"" + origfile + "\" \"" + targetfile + "\""
    os.system(command)
    #print(command)

    return

if __name__ == '__main__':
    #print("executed from fastlane")
    HideAd()

改善した方が良いかもしれない点:
 現在は、オリジナルのファイルはbackupディレクトリにコピーしてますが、fastlaneでframeitを使っていると、backupないのファイルにもframeをつけてしまいます。
ファイル名を変えておくと、frame対象から外せるので、処理時間が減ります。

スクリーンショットは、iTunesConnectにD&DするだけでOKになりました。

最終確認してから登録したいので、iTunesConnectへのアップロードまでの自動化はやめてます。

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です