Skip to content

Instantly share code, notes, and snippets.

@ccfiel
Created November 9, 2020 05:32
Show Gist options
  • Save ccfiel/ed6805d40a5105626fbb39dd8a758317 to your computer and use it in GitHub Desktop.
Save ccfiel/ed6805d40a5105626fbb39dd8a758317 to your computer and use it in GitHub Desktop.
Change permission FreshchatSDK.h in iOS
import uuid
import os
#Tags
FILE_REF_SECTION = "PBXFileReference section"
PACKAGE_HEADER_REF_SECTION = "PBXNativeTarget section"
PACKAGE_HEADER_DEFINITION = "PBXHeadersBuildPhase section"
PUBLIC_HEADER_DEFINITION = "PBXBuildFile section */"
#Constants
PBXFILE = "Pods/Pods.xcodeproj/project.pbxproj"
PACKAGENAME = "flutter_freshchat"
HEADERFILE = "FreshchatSDK.h"
# id = uuid.uuid4().hex.upper()
def extract_section(content, tag, tagend):
start = content.index(tag) + len(tag)
end = content.index(tagend, start)
return content[start:end]
#Extract File ref
def get_file_ref(content):
if content is None:
return
#Extract usable section from the file
section = extract_section(content, FILE_REF_SECTION, FILE_REF_SECTION)
#From the section, extract the ref
line = [i for i in section.splitlines() if HEADERFILE in i][0]
res = line[:(line.index("/*"))].strip()
return res
#Extract package header ref
def get_package_header_ref(content):
if content is None:
return
#Extract usable section from the file
section = extract_section(content, PACKAGE_HEADER_REF_SECTION, PACKAGE_HEADER_REF_SECTION)
#From the section, package sub section
section = extract_section(section, PACKAGENAME , ");")
line = [i for i in section.splitlines() if "Headers" in i][0]
res = line[:(line.index("/*"))].strip()
return res
#Extract File Header ref
#This section is of two states. 1. If the header definition exists within package's header def (Not Likely) Or Not
def get_file_header_ref(content, package_header_ref):
if content is None:
return
#Extract usable section from the file
section = extract_section(content, PACKAGE_HEADER_DEFINITION, PACKAGE_HEADER_DEFINITION)
#From the section, package sub section
section = extract_section(section, package_header_ref , ");")
#From the section, extract the ref
line = [i for i in section.splitlines() if HEADERFILE in i]
if len(line) == 0:
return
line = line[0]
res = line[:(line.index("/*"))].strip()
return res
#This section attempts to add header to the package header definition
def add_file_to_headers(content, package_header_ref):
#find the PBXHeadersBuildPhase section
pre = content.index(PACKAGE_HEADER_DEFINITION)
pre2 = content.index("{0} /* Headers */ =".format(package_header_ref), pre)
start = content.index("files = (", pre2) + len("files = (")
ref = uuid.uuid4().hex.upper()
#C4DD7552E76E975403D2B2E193DAC088 /* FlutterFreshchatPlugin.h in Headers */,
header = "\n\t\t\t\t{0} /* {1} in Headers */,".format(ref, HEADERFILE )
#add to content
return ref, content[:start] + header + content[start:]
#Three scenarios. 1, header exists and is public (in that case, skip)
#2, Header exists but isn't public (Fix)
#3, Header doesn't exist, so just add it
def add_public_header(content, file_header_ref, file_ref):
if content is None:
return
try: #First 2 scenarios
start = content.index(file_header_ref, 1, content.index("End {0}".format(PUBLIC_HEADER_DEFINITION) )) -3
end = content.index("\n", start+3)
except ValueError:
start = content.index(PUBLIC_HEADER_DEFINITION) + len(PUBLIC_HEADER_DEFINITION)
end = start
header = '\n\t\t'+file_header_ref+' /* '+HEADERFILE+' in Headers */ = {isa = PBXBuildFile; fileRef = '+file_ref+' /* '+HEADERFILE+' */; settings = {ATTRIBUTES = (Public, ); }; };'
return content[:start] + header + content[end:]
def perform_action():
#Check if the Pod Project File exists
if not os.path.isfile(PBXFILE):
print("{0} does not exist, make sure you've run the pod install command ".format(PBXFILE))
return
#read file content
content = ""
with open(PBXFILE, 'r') as file:
content = file.read()
file.close()
#Was packaged installed in the Pods?
if PACKAGENAME not in content:
print("Unable to find {0} in the pbx file, please reinstall pod".format(PACKAGENAME))
return
#Is headerfile reference declared in the pbx file?
if HEADERFILE not in content:
print("Unable to find {0} reference in the pbx file, please reinstall pod".format(HEADERFILE))
return
#Variables
file_ref=""
package_header_ref=""
file_header_ref=""
file_ref = get_file_ref(content)
if file_ref is None:
print("Unable to extract {0} reference from the pbx file".format(HEADERFILE))
package_header_ref = get_package_header_ref(content)
if package_header_ref is None:
print("Unable to extract {0} header reference from the pbx file".format(PACKAGENAME))
file_header_ref = get_file_header_ref(content, package_header_ref)
#If the file was not declared as header yet (Very likely), Add it
if file_header_ref is None:
file_header_ref, content = add_file_to_headers(content, package_header_ref)
if file_header_ref is None:
print("Couldn't find and unable to add {0} to package header def".format(HEADERFILE))
return
#Last Stage, Add file to public header def and add attribute public
content = add_public_header(content, file_header_ref, file_ref)
with open(PBXFILE, 'w') as file:
file.write(content)
file.close()
print("The change was done successfully")
perform_action()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment