Flutter 通过命令行增加多平台权限

73 min read
function add_permission_label() {
    cd $FLUTTER_APP_FOLDER/scripts
    echo ""
    echo "Add permission labels to iOS."
    echo ""
    python3 add-line.py -i ../ios/Runner/Info.plist -s '<key>UILaunchStoryboardName</key>' -t '	<key>NSCameraUsageDescription</key>'
    python3 add-line.py -i ../ios/Runner/Info.plist -s '<key>UILaunchStoryboardName</key>' -t '	<string>$(PRODUCT_NAME) Camera Usage!</string>'
    python3 add-line.py -i ../ios/Runner/Info.plist -s '<key>UILaunchStoryboardName</key>' -t '	<key>NSMicrophoneUsageDescription</key>'
    python3 add-line.py -i ../ios/Runner/Info.plist -s '<key>UILaunchStoryboardName</key>' -t '	<string>$(PRODUCT_NAME) Microphone Usage!</string>'
    python3 add-line.py -i ../ios/Podfile -s "# platform :ios, '9.0'" -t "platform :ios, '10.0'" -r
    echo ""
    echo "Add permission labels to AndroidManifest.xml."
    echo ""
    python3 add-line.py -i ../android/app/build.gradle -s 'minSdkVersion 16' -t 'minSdkVersion 21' -r
    python3 add-line.py -i ../android/app/src/main/AndroidManifest.xml -s "<application" -t '    <uses-permission android:name="android.permission.CAMERA" />'
    python3 add-line.py -i ../android/app/src/main/AndroidManifest.xml -s "<application" -t '    <uses-permission android:name="android.permission.RECORD_AUDIO" />'
    python3 add-line.py -i ../android/app/src/main/AndroidManifest.xml -s "<application" -t '    <uses-permission android:name="android.permission.WAKE_LOCK" />'
    python3 add-line.py -i ../android/app/src/main/AndroidManifest.xml -s "<application" -t '    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />'
    python3 add-line.py -i ../android/app/src/main/AndroidManifest.xml -s "<application" -t '    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />'
    python3 add-line.py -i ../android/app/src/main/AndroidManifest.xml -s "<application" -t '    <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />'
    python3 add-line.py -i ../android/app/src/main/AndroidManifest.xml -s "<application" -t '    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />'
    python3 add-line.py -i ../android/app/src/main/AndroidManifest.xml -s "<application" -t '    <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />'
    echo ""
    echo "Add permission labels to macOS."
    echo ""
    python3 add-line.py -i ../macos/Runner/Info.plist -s '<key>CFBundleShortVersionString</key>' -t '	<key>NSCameraUsageDescription</key>'
    python3 add-line.py -i ../macos/Runner/Info.plist -s '<key>CFBundleShortVersionString</key>' -t '	<string>$(PRODUCT_NAME) Camera Usage!</string>'
    python3 add-line.py -i ../macos/Runner/Info.plist -s '<key>CFBundleShortVersionString</key>' -t '	<key>NSMicrophoneUsageDescription</key>'
    python3 add-line.py -i ../macos/Runner/Info.plist -s '<key>CFBundleShortVersionString</key>' -t '	<string>$(PRODUCT_NAME) Microphone Usage!</string>'

    python3 add-line.py -i ../macos/Runner/DebugProfile.entitlements -s '</dict>' -t '       <key>com.apple.security.device.camera</key>'
    python3 add-line.py -i ../macos/Runner/DebugProfile.entitlements -s '</dict>' -t '       <true/>'
    python3 add-line.py -i ../macos/Runner/DebugProfile.entitlements -s '</dict>' -t '       <key>com.apple.security.device.microphone</key>'
    python3 add-line.py -i ../macos/Runner/DebugProfile.entitlements -s '</dict>' -t '       <true/>'
    python3 add-line.py -i ../macos/Runner/DebugProfile.entitlements -s '</dict>' -t '       <key>com.apple.security.network.client</key>'
    python3 add-line.py -i ../macos/Runner/DebugProfile.entitlements -s '</dict>' -t '       <true/>'

    python3 add-line.py -i ../macos/Runner/Release.entitlements -s '</dict>' -t '       <key>com.apple.security.device.camera</key>'
    python3 add-line.py -i ../macos/Runner/Release.entitlements -s '</dict>' -t '       <true/>'
    python3 add-line.py -i ../macos/Runner/Release.entitlements -s '</dict>' -t '       <key>com.apple.security.device.microphone</key>'
    python3 add-line.py -i ../macos/Runner/Release.entitlements -s '</dict>' -t '       <true/>'
    python3 add-line.py -i ../macos/Runner/Release.entitlements -s '</dict>' -t '       <key>com.apple.security.network.client</key>'
    python3 add-line.py -i ../macos/Runner/Release.entitlements -s '</dict>' -t '       <true/>'
}
#!/usr/bin/python
# -*- coding: UTF-8 -*-

import sys
import getopt
import re


def findLine(pattern, fp):
    line = fp.readline()
    line_number = 1
    while line:
        #print("Line {}: {}".format(line_number, line.strip()))
        if pattern in line:
            return line_number
        line = fp.readline()
        line_number += 1
    return -1

def insertBefore(filename, pattern, text):
    with open(filename, 'r+') as fp:
        line_number = findLine(pattern, fp)
        if(line_number > 0):
            print('Insert', text,'to line', line_number)
            fp.seek(0)
            lines = fp.readlines()
            fp.seek(0)
            lines.insert(line_number - 1, text + '\n')
            fp.writelines(lines)
            return
        print('pattern',text,'not found!')

def replaceText(filename, pattern, text):
    with open(filename, 'r') as fp:
        lines = fp.read()
        fp.close()
        lines = (re.sub(pattern, text, lines))
        print('Replace', pattern ,'to', text)
        fp = open(filename, 'w')
        fp.write(lines)
        fp.close()

def main(argv):
    inputfile = ''
    string = ''
    text = ''
    replace = False
    try:
        opts, args = getopt.getopt(argv, "hi:s:t:r")
    except getopt.GetoptError:
        print('add-line.py -i <inputfile> -s <string> -t <text>')
        sys.exit(2)
    for opt, arg in opts:
        if opt == '-h':
            print('add-line.py -i <inputfile> -s <string> -t <text>')
            sys.exit()
        elif opt in ("-i"):
            inputfile = arg
        elif opt in ("-s"):
            string = arg
        elif opt in ("-t"):
            text = arg
        elif opt in ("-r"):
            replace = True
    if(replace):
        replaceText(inputfile, string, text)
    else:
        insertBefore(inputfile, string, text)

if __name__ == "__main__":
    main(sys.argv[1:])